blob: b1cb61fc09bf7b79b6c29b49fb8c81aa74146ae2 [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020022 * this library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020035 * When the job ends the text is put in a buffer. Redrawing then happens from
36 * that buffer, attributes come from the scrollback buffer tl_scrollback.
37 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020038 * TODO:
Bram Moolenaar423802d2017-07-30 16:52:24 +020039 * - add option values to the command:
Bram Moolenaar82b9ca02017-08-08 23:06:46 +020040 * :term ++24x80 ++close vim notes.txt
Bram Moolenaardd693ce2017-08-10 23:15:19 +020041 * - When using term_finish "open" have a way to specify how the window is to
42 * be opened. E.g. term_opencmd "10split buffer %d".
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020043 * - support different cursor shapes, colors and attributes
44 * - make term_getcursor() return type (none/block/bar/underline) and
45 * attributes (color, blink, etc.)
Bram Moolenaardd693ce2017-08-10 23:15:19 +020046 * - Make argument list work on MS-Windows. #1954
Bram Moolenaar292d5692017-08-08 21:52:22 +020047 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaard85f2712017-07-28 21:51:57 +020048 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020049 * For the GUI fill termios with default values, perhaps like pangoterm:
50 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
51 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020052 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020053 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020054 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020055 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020056 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020057 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
58 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020059 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020060 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020061 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020062 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020063 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020064 * terminal. Allow:
65 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
66 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
67 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
68 * Check that something is connected to the terminal.
69 * Test: "cat" reading from a file or buffer
70 * "ls" writing stdout to a file or buffer
71 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020072 * - support ":term NONE" to open a terminal with a pty but not running a job
73 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020074 * - if the job in the terminal does not support the mouse, we can use the
75 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020076 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
77 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020078 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020079 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020080 * - Copy text in the vterm to the Vim buffer once in a while, so that
81 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082 */
83
84#include "vim.h"
85
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020086#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020087
Bram Moolenaard5310982017-08-05 15:16:32 +020088#ifndef MIN
89# define MIN(x,y) ((x) < (y) ? (x) : (y))
90#endif
91#ifndef MAX
92# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020093#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020094
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020095#include "libvterm/include/vterm.h"
96
Bram Moolenaar33a43be2017-08-06 21:36:22 +020097/* This is VTermScreenCell without the characters, thus much smaller. */
98typedef struct {
99 VTermScreenCellAttrs attrs;
100 char width;
101 VTermColor fg, bg;
102} cellattr_T;
103
Bram Moolenaard85f2712017-07-28 21:51:57 +0200104typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200105 int sb_cols; /* can differ per line */
106 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200107} sb_line_T;
108
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200109/* typedef term_T in structs.h */
110struct terminal_S {
111 term_T *tl_next;
112
Bram Moolenaar423802d2017-07-30 16:52:24 +0200113 VTerm *tl_vterm;
114 job_T *tl_job;
115 buf_T *tl_buffer;
116
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200117 /* used when tl_job is NULL and only a pty was created */
118 int tl_tty_fd;
119 char_u *tl_tty_name;
120
Bram Moolenaar6d819742017-08-06 14:57:49 +0200121 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200122 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200123 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200124
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200125#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200126 void *tl_winpty_config;
127 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200128#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200129
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200130 /* last known vterm size */
131 int tl_rows;
132 int tl_cols;
133 /* vterm size does not follow window size */
134 int tl_rows_fixed;
135 int tl_cols_fixed;
136
Bram Moolenaar21554412017-07-24 21:44:43 +0200137 char_u *tl_title; /* NULL or allocated */
138 char_u *tl_status_text; /* NULL or allocated */
139
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200140 /* Range of screen rows to update. Zero based. */
141 int tl_dirty_row_start; /* -1 if nothing dirty */
142 int tl_dirty_row_end; /* row below last one to update */
143
Bram Moolenaard85f2712017-07-28 21:51:57 +0200144 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200145 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200146
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200147 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200148 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200149};
150
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200151#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
152#define TMODE_LOOP 2 /* CTRL-W N used */
153
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200154/*
155 * List of all active terminals.
156 */
157static term_T *first_term = NULL;
158
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200159
160#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
161#define KEY_BUF_LEN 200
162
163/*
164 * Functions with separate implementation for MS-Windows and Unix-like systems.
165 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200166static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200167static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200168static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200169
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200170/**************************************
171 * 1. Generic code for all systems.
172 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200173
174/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200175 * Determine the terminal size from 'termsize' and the current window.
176 * Assumes term->tl_rows and term->tl_cols are zero.
177 */
178 static void
179set_term_and_win_size(term_T *term)
180{
181 if (*curwin->w_p_tms != NUL)
182 {
183 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
184
185 term->tl_rows = atoi((char *)curwin->w_p_tms);
186 term->tl_cols = atoi((char *)p);
187 }
188 if (term->tl_rows == 0)
189 term->tl_rows = curwin->w_height;
190 else
191 {
192 win_setheight_win(term->tl_rows, curwin);
193 term->tl_rows_fixed = TRUE;
194 }
195 if (term->tl_cols == 0)
196 term->tl_cols = curwin->w_width;
197 else
198 {
199 win_setwidth_win(term->tl_cols, curwin);
200 term->tl_cols_fixed = TRUE;
201 }
202}
203
204/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200205 * Initialize job options for a terminal job.
206 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200207 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200208 static void
209init_job_options(jobopt_T *opt)
210{
211 clear_job_options(opt);
212
213 opt->jo_mode = MODE_RAW;
214 opt->jo_out_mode = MODE_RAW;
215 opt->jo_err_mode = MODE_RAW;
216 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
217
218 opt->jo_io[PART_OUT] = JIO_BUFFER;
219 opt->jo_io[PART_ERR] = JIO_BUFFER;
220 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
221
222 opt->jo_modifiable[PART_OUT] = 0;
223 opt->jo_modifiable[PART_ERR] = 0;
224 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
225
226 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
227}
228
229/*
230 * Set job options mandatory for a terminal job.
231 */
232 static void
233setup_job_options(jobopt_T *opt, int rows, int cols)
234{
235 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
236 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
237 opt->jo_pty = TRUE;
238 opt->jo_term_rows = rows;
239 opt->jo_term_cols = cols;
240}
241
242 static void
243term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200244{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200245 exarg_T split_ea;
246 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248
249 if (check_restricted() || check_secure())
250 return;
251
252 term = (term_T *)alloc_clear(sizeof(term_T));
253 if (term == NULL)
254 return;
255 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200256 term->tl_cursor_visible = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200257 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200258 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200259
260 /* Open a new window or tab. */
261 vim_memset(&split_ea, 0, sizeof(split_ea));
262 split_ea.cmdidx = CMD_new;
263 split_ea.cmd = (char_u *)"new";
264 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200265 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
266 {
267 split_ea.line2 = opt->jo_term_rows;
268 split_ea.addr_count = 1;
269 }
270 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
271 {
272 split_ea.line2 = opt->jo_term_cols;
273 split_ea.addr_count = 1;
274 }
275
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200276 ex_splitview(&split_ea);
277 if (curwin == old_curwin)
278 {
279 /* split failed */
280 vim_free(term);
281 return;
282 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200283 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200284 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200285
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200286 /* only one size was taken care of with :new, do the other one */
287 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
288 win_setheight(opt->jo_term_rows);
289 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
290 win_setwidth(opt->jo_term_cols);
291
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200292 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200293 term->tl_next = first_term;
294 first_term = term;
295
Bram Moolenaar293424c2017-07-26 23:11:01 +0200296 if (cmd == NULL || *cmd == NUL)
297 cmd = p_sh;
298
Bram Moolenaar78712a72017-08-05 14:50:12 +0200299 if (opt->jo_term_name != NULL)
300 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
301 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200302 {
303 int i;
304 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200305 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200306
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200307 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200308 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200309 /* Prepend a ! to the command name to avoid the buffer name equals
310 * the executable, otherwise ":w!" would overwrite it. */
311 if (i == 0)
312 vim_snprintf((char *)p, len, "!%s", cmd);
313 else
314 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200315 if (buflist_findname(p) == NULL)
316 {
317 curbuf->b_ffname = p;
318 break;
319 }
320 }
321 }
322 curbuf->b_fname = curbuf->b_ffname;
323
Bram Moolenaareb44a682017-08-03 22:44:55 +0200324 set_string_option_direct((char_u *)"buftype", -1,
325 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
326
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200327 /* Mark the buffer as not modifiable. It can only be made modifiable after
328 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200329 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200330
331 /* Set 'bufhidden' to "hide": allow closing the window. */
332 set_string_option_direct((char_u *)"bufhidden", -1,
333 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200334
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200335 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200336 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200337
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200338 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200339 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200340 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200341 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200342 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200343 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200344 }
345 else
346 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200347 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200348
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200349 /* Wiping out the buffer will also close the window and call
350 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200351 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200352 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200353}
354
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200355/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200356 * ":terminal": open a terminal window and execute a job in it.
357 */
358 void
359ex_terminal(exarg_T *eap)
360{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200361 jobopt_T opt;
362 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200363
364 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200365
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200366 cmd = eap->arg;
367 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
368 {
369 char_u *p;
370
371 cmd += 2;
372 p = skiptowhite(cmd);
373 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
374 opt.jo_term_finish = 'c';
375 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
376 opt.jo_term_finish = 'o';
377 else
378 {
379 if (*p)
380 *p = NUL;
381 EMSG2(_("E181: Invalid attribute: %s"), cmd);
382 return;
383 }
384 cmd = skipwhite(p);
385 }
386
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200387 if (eap->addr_count == 2)
388 {
389 opt.jo_term_rows = eap->line1;
390 opt.jo_term_cols = eap->line2;
391 }
392 else if (eap->addr_count == 1)
393 {
394 if (cmdmod.split & WSP_VERT)
395 opt.jo_term_cols = eap->line2;
396 else
397 opt.jo_term_rows = eap->line2;
398 }
399 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200400
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200401 term_start(cmd, &opt);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200402}
403
404/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200405 * Free the scrollback buffer for "term".
406 */
407 static void
408free_scrollback(term_T *term)
409{
410 int i;
411
412 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
413 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
414 ga_clear(&term->tl_scrollback);
415}
416
417/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200418 * Free a terminal and everything it refers to.
419 * Kills the job if there is one.
420 * Called when wiping out a buffer.
421 */
422 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200423free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200424{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200425 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200426 term_T *tp;
427
428 if (term == NULL)
429 return;
430 if (first_term == term)
431 first_term = term->tl_next;
432 else
433 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
434 if (tp->tl_next == term)
435 {
436 tp->tl_next = term->tl_next;
437 break;
438 }
439
440 if (term->tl_job != NULL)
441 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200442 if (term->tl_job->jv_status != JOB_ENDED
443 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200444 job_stop(term->tl_job, NULL, "kill");
445 job_unref(term->tl_job);
446 }
447
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200448 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200449
450 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200451 vim_free(term->tl_title);
452 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200453 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200454 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200455}
456
457/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200458 * Write job output "msg[len]" to the vterm.
459 */
460 static void
461term_write_job_output(term_T *term, char_u *msg, size_t len)
462{
463 VTerm *vterm = term->tl_vterm;
464 char_u *p;
465 size_t done;
466 size_t len_now;
467
468 for (done = 0; done < len; done += len_now)
469 {
470 for (p = msg + done; p < msg + len; )
471 {
472 if (*p == NL)
473 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200474 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200475 }
476 len_now = p - msg - done;
477 vterm_input_write(vterm, (char *)msg + done, len_now);
478 if (p < msg + len && *p == NL)
479 {
480 /* Convert NL to CR-NL, that appears to work best. */
481 vterm_input_write(vterm, "\r\n", 2);
482 ++len_now;
483 }
484 }
485
486 /* this invokes the damage callbacks */
487 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
488}
489
Bram Moolenaar1c844932017-07-24 23:36:41 +0200490 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200491update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200492{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200493 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200494 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200495 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200496 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200497 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200498 if (term->tl_cursor_visible)
499 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200500 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200501#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200502 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200503 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200504#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200505 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200506}
507
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200508/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200509 * Invoked when "msg" output from a job was received. Write it to the terminal
510 * of "buffer".
511 */
512 void
513write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
514{
515 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200516 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200517
Bram Moolenaard85f2712017-07-28 21:51:57 +0200518 if (term->tl_vterm == NULL)
519 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200520 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200521 return;
522 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200523 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200524 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200525
Bram Moolenaar6d819742017-08-06 14:57:49 +0200526 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200527 {
528 /* TODO: only update once in a while. */
529 update_screen(0);
530 update_cursor(term, TRUE);
531 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200532}
533
534/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200535 * Send a mouse position and click to the vterm
536 */
537 static int
538term_send_mouse(VTerm *vterm, int button, int pressed)
539{
540 VTermModifier mod = VTERM_MOD_NONE;
541
542 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
543 mouse_col - W_WINCOL(curwin), mod);
544 vterm_mouse_button(vterm, button, pressed, mod);
545 return TRUE;
546}
547
548/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200549 * Convert typed key "c" into bytes to send to the job.
550 * Return the number of bytes in "buf".
551 */
552 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200553term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200554{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200555 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200556 VTermKey key = VTERM_KEY_NONE;
557 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200558 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200559
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200560 switch (c)
561 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200562 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200563 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200564 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
565 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200566 case K_DEL: key = VTERM_KEY_DEL; break;
567 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200568 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
569 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200570 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200571 case K_S_END: mod = VTERM_MOD_SHIFT;
572 key = VTERM_KEY_END; break;
573 case K_C_END: mod = VTERM_MOD_CTRL;
574 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200575 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
576 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
577 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
578 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
579 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
580 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
581 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
582 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
583 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
584 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
585 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
586 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
587 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200588 case K_S_HOME: mod = VTERM_MOD_SHIFT;
589 key = VTERM_KEY_HOME; break;
590 case K_C_HOME: mod = VTERM_MOD_CTRL;
591 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200592 case K_INS: key = VTERM_KEY_INS; break;
593 case K_K0: key = VTERM_KEY_KP_0; break;
594 case K_K1: key = VTERM_KEY_KP_1; break;
595 case K_K2: key = VTERM_KEY_KP_2; break;
596 case K_K3: key = VTERM_KEY_KP_3; break;
597 case K_K4: key = VTERM_KEY_KP_4; break;
598 case K_K5: key = VTERM_KEY_KP_5; break;
599 case K_K6: key = VTERM_KEY_KP_6; break;
600 case K_K7: key = VTERM_KEY_KP_7; break;
601 case K_K8: key = VTERM_KEY_KP_8; break;
602 case K_K9: key = VTERM_KEY_KP_9; break;
603 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
604 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
605 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
606 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
607 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
608 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
609 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
610 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
611 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
612 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
613 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
614 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
615 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200616 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
617 key = VTERM_KEY_LEFT; break;
618 case K_C_LEFT: mod = VTERM_MOD_CTRL;
619 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200620 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
621 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
622 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200623 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
624 key = VTERM_KEY_RIGHT; break;
625 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
626 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200627 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200628 case K_S_UP: mod = VTERM_MOD_SHIFT;
629 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200630 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200631
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200632 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
633 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
634 case K_MOUSELEFT: /* TODO */ return 0;
635 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200636
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200637 case K_LEFTMOUSE:
638 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
639 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
640 case K_LEFTRELEASE:
641 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
642 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
643 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
644 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
645 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
646 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
647 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
648 case K_X1MOUSE: /* TODO */ return 0;
649 case K_X1DRAG: /* TODO */ return 0;
650 case K_X1RELEASE: /* TODO */ return 0;
651 case K_X2MOUSE: /* TODO */ return 0;
652 case K_X2DRAG: /* TODO */ return 0;
653 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200654
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200655 case K_IGNORE: return 0;
656 case K_NOP: return 0;
657 case K_UNDO: return 0;
658 case K_HELP: return 0;
659 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
660 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
661 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
662 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
663 case K_SELECT: return 0;
664#ifdef FEAT_GUI
665 case K_VER_SCROLLBAR: return 0;
666 case K_HOR_SCROLLBAR: return 0;
667#endif
668#ifdef FEAT_GUI_TABLINE
669 case K_TABLINE: return 0;
670 case K_TABMENU: return 0;
671#endif
672#ifdef FEAT_NETBEANS_INTG
673 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
674#endif
675#ifdef FEAT_DND
676 case K_DROP: return 0;
677#endif
678#ifdef FEAT_AUTOCMD
679 case K_CURSORHOLD: return 0;
680#endif
681 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
682 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200683 }
684
685 /*
686 * Convert special keys to vterm keys:
687 * - Write keys to vterm: vterm_keyboard_key()
688 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200689 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200690 */
691 if (key != VTERM_KEY_NONE)
692 /* Special key, let vterm convert it. */
693 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200694 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200695 /* Normal character, let vterm convert it. */
696 vterm_keyboard_unichar(vterm, c, mod);
697
698 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200699 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200700}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200701
Bram Moolenaar938783d2017-07-16 20:13:26 +0200702/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200703 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200704 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200705 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200706term_job_running(term_T *term)
707{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200708 /* Also consider the job finished when the channel is closed, to avoid a
709 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200710 return term != NULL
711 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200712 && term->tl_job->jv_status == JOB_STARTED
713 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200714}
715
716/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200717 * Add the last line of the scrollback buffer to the buffer in the window.
718 */
719 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200720add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200721{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200722 buf_T *buf = term->tl_buffer;
723 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
724 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200725
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200726 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200727 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200728 {
729 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200730 curbuf = buf;
731 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200732 curbuf = curwin->w_buffer;
733 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200734}
735
736/*
737 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200738 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200739 */
740 static void
741move_terminal_to_buffer(term_T *term)
742{
743 win_T *wp;
744 int len;
745 int lines_skipped = 0;
746 VTermPos pos;
747 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200748 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200749 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200750
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200751 if (term->tl_vterm == NULL)
752 return;
753 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200754 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
755 {
756 len = 0;
757 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
758 if (vterm_screen_get_cell(screen, pos, &cell) != 0
759 && cell.chars[0] != NUL)
760 len = pos.col + 1;
761
762 if (len == 0)
763 ++lines_skipped;
764 else
765 {
766 while (lines_skipped > 0)
767 {
768 /* Line was skipped, add an empty line. */
769 --lines_skipped;
770 if (ga_grow(&term->tl_scrollback, 1) == OK)
771 {
772 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
773 + term->tl_scrollback.ga_len;
774
775 line->sb_cols = 0;
776 line->sb_cells = NULL;
777 ++term->tl_scrollback.ga_len;
778
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200779 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200780 }
781 }
782
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200783 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200784 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
785 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200786 garray_T ga;
787 int width;
788 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200789 + term->tl_scrollback.ga_len;
790
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200791 ga_init2(&ga, 1, 100);
792 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200793 {
794 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200795 {
796 width = 1;
797 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
798 if (ga_grow(&ga, 1) == OK)
799 ga.ga_len += mb_char2bytes(' ',
800 (char_u *)ga.ga_data + ga.ga_len);
801 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200802 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200803 {
804 width = cell.width;
805
806 p[pos.col].width = cell.width;
807 p[pos.col].attrs = cell.attrs;
808 p[pos.col].fg = cell.fg;
809 p[pos.col].bg = cell.bg;
810
811 if (ga_grow(&ga, MB_MAXBYTES) == OK)
812 {
813 int i;
814 int c;
815
816 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
817 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
818 (char_u *)ga.ga_data + ga.ga_len);
819 }
820 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200821 }
822 line->sb_cols = len;
823 line->sb_cells = p;
824 ++term->tl_scrollback.ga_len;
825
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200826 if (ga_grow(&ga, 1) == FAIL)
827 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
828 else
829 {
830 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
831 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
832 }
833 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200834 }
835 else
836 vim_free(p);
837 }
838 }
839
840 FOR_ALL_WINDOWS(wp)
841 {
842 if (wp->w_buffer == term->tl_buffer)
843 {
844 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
845 wp->w_cursor.col = 0;
846 wp->w_valid = 0;
847 redraw_win_later(wp, NOT_VALID);
848 }
849 }
850}
851
852 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200853set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200854{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200855 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200856 vim_free(term->tl_status_text);
857 term->tl_status_text = NULL;
858 if (term->tl_buffer == curbuf)
859 maketitle();
860}
861
862/*
863 * Called after the job if finished and Terminal mode is not active:
864 * Move the vterm contents into the scrollback buffer and free the vterm.
865 */
866 static void
867cleanup_vterm(term_T *term)
868{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200869 if (term->tl_finish == 0)
870 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200871 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200872 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200873}
874
875/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200876 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200877 * Suspends updating the terminal window.
878 */
879 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200880term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200881{
882 term_T *term = curbuf->b_term;
883
884 /* Append the current terminal contents to the buffer. */
885 move_terminal_to_buffer(term);
886
Bram Moolenaar6d819742017-08-06 14:57:49 +0200887 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200888
Bram Moolenaar6d819742017-08-06 14:57:49 +0200889 /* Move the window cursor to the position of the cursor in the
890 * terminal. */
891 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
892 + term->tl_cursor_pos.row + 1;
893 check_cursor();
894 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200895
Bram Moolenaar6d819742017-08-06 14:57:49 +0200896 /* Display the same lines as in the terminal. */
897 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200898}
899
900/*
901 * Returns TRUE if the current window contains a terminal and we are in
902 * Terminal-Normal mode.
903 */
904 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200905term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200906{
907 term_T *term = curbuf->b_term;
908
Bram Moolenaar6d819742017-08-06 14:57:49 +0200909 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200910}
911
912/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200913 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200914 * Restores updating the terminal window.
915 */
916 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200917term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200918{
919 term_T *term = curbuf->b_term;
920 sb_line_T *line;
921 garray_T *gap;
922
923 /* Remove the terminal contents from the scrollback and the buffer. */
924 gap = &term->tl_scrollback;
925 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
926 {
927 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
928 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
929 vim_free(line->sb_cells);
930 --gap->ga_len;
931 if (gap->ga_len == 0)
932 break;
933 }
934 check_cursor();
935
Bram Moolenaar6d819742017-08-06 14:57:49 +0200936 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200937
938 if (term->tl_channel_closed)
939 cleanup_vterm(term);
940 redraw_buf_and_status_later(curbuf, NOT_VALID);
941}
942
943/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200944 * Get a key from the user without mapping.
945 * TODO: use terminal mode mappings.
946 */
947 static int
948term_vgetc()
949{
950 int c;
951
952 ++no_mapping;
953 ++allow_keys;
954 got_int = FALSE;
955 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200956 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200957 --no_mapping;
958 --allow_keys;
959 return c;
960}
961
962/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200963 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200964 * Return FAIL when the key needs to be handled in Normal mode.
965 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200966 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200967 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200968send_keys_to_term(term_T *term, int c, int typed)
969{
970 char msg[KEY_BUF_LEN];
971 size_t len;
972 static int mouse_was_outside = FALSE;
973 int dragging_outside = FALSE;
974
975 /* Catch keys that need to be handled as in Normal mode. */
976 switch (c)
977 {
978 case NUL:
979 case K_ZERO:
980 if (typed)
981 stuffcharReadbuff(c);
982 return FAIL;
983
984 case K_IGNORE:
985 return FAIL;
986
987 case K_LEFTDRAG:
988 case K_MIDDLEDRAG:
989 case K_RIGHTDRAG:
990 case K_X1DRAG:
991 case K_X2DRAG:
992 dragging_outside = mouse_was_outside;
993 /* FALLTHROUGH */
994 case K_LEFTMOUSE:
995 case K_LEFTMOUSE_NM:
996 case K_LEFTRELEASE:
997 case K_LEFTRELEASE_NM:
998 case K_MIDDLEMOUSE:
999 case K_MIDDLERELEASE:
1000 case K_RIGHTMOUSE:
1001 case K_RIGHTRELEASE:
1002 case K_X1MOUSE:
1003 case K_X1RELEASE:
1004 case K_X2MOUSE:
1005 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001006
1007 case K_MOUSEUP:
1008 case K_MOUSEDOWN:
1009 case K_MOUSELEFT:
1010 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001011 if (mouse_row < W_WINROW(curwin)
1012 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1013 || mouse_col < W_WINCOL(curwin)
1014 || mouse_col >= W_ENDCOL(curwin)
1015 || dragging_outside)
1016 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001017 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001018 if (typed)
1019 {
1020 stuffcharReadbuff(c);
1021 mouse_was_outside = TRUE;
1022 }
1023 return FAIL;
1024 }
1025 }
1026 if (typed)
1027 mouse_was_outside = FALSE;
1028
1029 /* Convert the typed key to a sequence of bytes for the job. */
1030 len = term_convert_key(term, c, msg);
1031 if (len > 0)
1032 /* TODO: if FAIL is returned, stop? */
1033 channel_send(term->tl_job->jv_channel, PART_IN,
1034 (char_u *)msg, (int)len, NULL);
1035
1036 return OK;
1037}
1038
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001039 static void
1040position_cursor(win_T *wp, VTermPos *pos)
1041{
1042 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1043 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1044 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1045}
1046
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001047/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001048 * Handle CTRL-W "": send register contents to the job.
1049 */
1050 static void
1051term_paste_register(int prev_c UNUSED)
1052{
1053 int c;
1054 list_T *l;
1055 listitem_T *item;
1056 long reglen = 0;
1057 int type;
1058
1059#ifdef FEAT_CMDL_INFO
1060 if (add_to_showcmd(prev_c))
1061 if (add_to_showcmd('"'))
1062 out_flush();
1063#endif
1064 c = term_vgetc();
1065#ifdef FEAT_CMDL_INFO
1066 clear_showcmd();
1067#endif
1068
1069 /* CTRL-W "= prompt for expression to evaluate. */
1070 if (c == '=' && get_expr_register() != '=')
1071 return;
1072
1073 l = (list_T *)get_reg_contents(c, GREG_LIST);
1074 if (l != NULL)
1075 {
1076 type = get_reg_type(c, &reglen);
1077 for (item = l->lv_first; item != NULL; item = item->li_next)
1078 {
1079 char_u *s = get_tv_string(&item->li_tv);
1080
1081 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1082 s, STRLEN(s), NULL);
1083 if (item->li_next != NULL || type == MLINE)
1084 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1085 (char_u *)"\r", 1, NULL);
1086 }
1087 list_free(l);
1088 }
1089}
1090
1091/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001092 * Returns TRUE if the current window contains a terminal and we are sending
1093 * keys to the job.
1094 */
1095 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001096term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001097{
1098 term_T *term = curbuf->b_term;
1099
1100 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001101 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001102 && term->tl_vterm != NULL
1103 && term_job_running(term);
1104}
1105
1106/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001107 * Wait for input and send it to the job.
1108 * Return when the start of a CTRL-W command is typed or anything else that
1109 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001110 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1111 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001112 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001113 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001114terminal_loop(void)
1115{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001116 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001117 int termkey = 0;
1118
1119 if (*curwin->w_p_tk != NUL)
1120 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001121 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001122
1123 for (;;)
1124 {
1125 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001126 /* Repeat redrawing in case a message is received while redrawing. */
1127 while (curwin->w_redr_type != 0)
1128 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001129 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001130
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001131 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001132 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001133 /* job finished while waiting for a character */
1134 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001135
Bram Moolenaarfae42832017-08-01 22:24:26 +02001136#ifdef UNIX
1137 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1138#endif
1139#ifdef WIN3264
1140 if (c == Ctrl_C)
1141 /* We don't know if the job can handle CTRL-C itself or not, this
1142 * may kill the shell instead of killing the command running in the
1143 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001144 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001145#endif
1146
Bram Moolenaar69198192017-08-05 14:10:48 +02001147 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001148 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001149 int prev_c = c;
1150
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001151#ifdef FEAT_CMDL_INFO
1152 if (add_to_showcmd(c))
1153 out_flush();
1154#endif
1155 c = term_vgetc();
1156#ifdef FEAT_CMDL_INFO
1157 clear_showcmd();
1158#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001159 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001160 /* job finished while waiting for a character */
1161 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001162
Bram Moolenaar69198192017-08-05 14:10:48 +02001163 if (prev_c == Ctrl_BSL)
1164 {
1165 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001166 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001167 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1168 term_enter_normal_mode();
1169 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001170 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001171 /* Send both keys to the terminal. */
1172 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1173 }
1174 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001175 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001176 /* "CTRL-W .": send CTRL-W to the job */
1177 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001178 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001179 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001180 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001181 /* CTRL-W N : go to Terminal-Normal mode. */
1182 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001183 return FAIL;
1184 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001185 else if (c == '"')
1186 {
1187 term_paste_register(prev_c);
1188 continue;
1189 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001190 else if (termkey == 0 || c != termkey)
1191 {
1192 stuffcharReadbuff(Ctrl_W);
1193 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001194 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001195 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001196 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001197 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1198 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001199 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001200 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001201}
1202
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001203/*
1204 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001205 * This updates the title and status, but does not close the vter, because
1206 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001207 */
1208 void
1209term_job_ended(job_T *job)
1210{
1211 term_T *term;
1212 int did_one = FALSE;
1213
1214 for (term = first_term; term != NULL; term = term->tl_next)
1215 if (term->tl_job == job)
1216 {
1217 vim_free(term->tl_title);
1218 term->tl_title = NULL;
1219 vim_free(term->tl_status_text);
1220 term->tl_status_text = NULL;
1221 redraw_buf_and_status_later(term->tl_buffer, VALID);
1222 did_one = TRUE;
1223 }
1224 if (did_one)
1225 redraw_statuslines();
1226 if (curbuf->b_term != NULL)
1227 {
1228 if (curbuf->b_term->tl_job == job)
1229 maketitle();
1230 update_cursor(curbuf->b_term, TRUE);
1231 }
1232}
1233
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001234 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001235may_toggle_cursor(term_T *term)
1236{
1237 if (curbuf == term->tl_buffer)
1238 {
1239 if (term->tl_cursor_visible)
1240 cursor_on();
1241 else
1242 cursor_off();
1243 }
1244}
1245
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001246 static int
1247handle_damage(VTermRect rect, void *user)
1248{
1249 term_T *term = (term_T *)user;
1250
1251 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1252 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1253 redraw_buf_later(term->tl_buffer, NOT_VALID);
1254 return 1;
1255}
1256
1257 static int
1258handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1259{
1260 term_T *term = (term_T *)user;
1261
1262 /* TODO */
1263 redraw_buf_later(term->tl_buffer, NOT_VALID);
1264 return 1;
1265}
1266
1267 static int
1268handle_movecursor(
1269 VTermPos pos,
1270 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001271 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001272 void *user)
1273{
1274 term_T *term = (term_T *)user;
1275 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001276
1277 term->tl_cursor_pos = pos;
1278 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001279
1280 FOR_ALL_WINDOWS(wp)
1281 {
1282 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001283 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001284 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001285 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001286 {
1287 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001288 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001289 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001290
1291 return 1;
1292}
1293
Bram Moolenaar21554412017-07-24 21:44:43 +02001294 static int
1295handle_settermprop(
1296 VTermProp prop,
1297 VTermValue *value,
1298 void *user)
1299{
1300 term_T *term = (term_T *)user;
1301
1302 switch (prop)
1303 {
1304 case VTERM_PROP_TITLE:
1305 vim_free(term->tl_title);
1306 term->tl_title = vim_strsave((char_u *)value->string);
1307 vim_free(term->tl_status_text);
1308 term->tl_status_text = NULL;
1309 if (term == curbuf->b_term)
1310 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001311 break;
1312
1313 case VTERM_PROP_CURSORVISIBLE:
1314 term->tl_cursor_visible = value->boolean;
1315 may_toggle_cursor(term);
1316 out_flush();
1317 break;
1318
Bram Moolenaar21554412017-07-24 21:44:43 +02001319 default:
1320 break;
1321 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001322 /* Always return 1, otherwise vterm doesn't store the value internally. */
1323 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001324}
1325
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001326/*
1327 * The job running in the terminal resized the terminal.
1328 */
1329 static int
1330handle_resize(int rows, int cols, void *user)
1331{
1332 term_T *term = (term_T *)user;
1333 win_T *wp;
1334
1335 term->tl_rows = rows;
1336 term->tl_cols = cols;
1337 FOR_ALL_WINDOWS(wp)
1338 {
1339 if (wp->w_buffer == term->tl_buffer)
1340 {
1341 win_setheight_win(rows, wp);
1342 win_setwidth_win(cols, wp);
1343 }
1344 }
1345
1346 redraw_buf_later(term->tl_buffer, NOT_VALID);
1347 return 1;
1348}
1349
Bram Moolenaard85f2712017-07-28 21:51:57 +02001350/*
1351 * Handle a line that is pushed off the top of the screen.
1352 */
1353 static int
1354handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1355{
1356 term_T *term = (term_T *)user;
1357
1358 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001359 if (ga_grow(&term->tl_scrollback, 1) == OK)
1360 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001361 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001362 int len = 0;
1363 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001364 int c;
1365 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001366 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001367 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001368
1369 /* do not store empty cells at the end */
1370 for (i = 0; i < cols; ++i)
1371 if (cells[i].chars[0] != 0)
1372 len = i + 1;
1373
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001374 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001375 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001376 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001377 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001378 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001379 for (col = 0; col < len; col += cells[col].width)
1380 {
1381 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1382 {
1383 ga.ga_len = 0;
1384 break;
1385 }
1386 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1387 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1388 (char_u *)ga.ga_data + ga.ga_len);
1389 p[col].width = cells[col].width;
1390 p[col].attrs = cells[col].attrs;
1391 p[col].fg = cells[col].fg;
1392 p[col].bg = cells[col].bg;
1393 }
1394 }
1395 if (ga_grow(&ga, 1) == FAIL)
1396 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1397 else
1398 {
1399 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1400 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1401 }
1402 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001403
1404 line = (sb_line_T *)term->tl_scrollback.ga_data
1405 + term->tl_scrollback.ga_len;
1406 line->sb_cols = len;
1407 line->sb_cells = p;
1408 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001409 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001410 }
1411 return 0; /* ignored */
1412}
1413
Bram Moolenaar21554412017-07-24 21:44:43 +02001414static VTermScreenCallbacks screen_callbacks = {
1415 handle_damage, /* damage */
1416 handle_moverect, /* moverect */
1417 handle_movecursor, /* movecursor */
1418 handle_settermprop, /* settermprop */
1419 NULL, /* bell */
1420 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001421 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001422 NULL /* sb_popline */
1423};
1424
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001425/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001426 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001427 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001428 */
1429 void
1430term_channel_closed(channel_T *ch)
1431{
1432 term_T *term;
1433 int did_one = FALSE;
1434
1435 for (term = first_term; term != NULL; term = term->tl_next)
1436 if (term->tl_job == ch->ch_job)
1437 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001438 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001439 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001440
Bram Moolenaard85f2712017-07-28 21:51:57 +02001441 vim_free(term->tl_title);
1442 term->tl_title = NULL;
1443 vim_free(term->tl_status_text);
1444 term->tl_status_text = NULL;
1445
Bram Moolenaar423802d2017-07-30 16:52:24 +02001446 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001447 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001448 {
1449 int fnum = term->tl_buffer->b_fnum;
1450
Bram Moolenaar423802d2017-07-30 16:52:24 +02001451 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001452
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001453 if (term->tl_finish == 'c')
1454 {
1455 /* ++close or term_finish == "close" */
1456 curbuf = term->tl_buffer;
1457 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1458 break;
1459 }
1460 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1461 {
1462 char buf[50];
1463
1464 /* TODO: use term_opencmd */
1465 vim_snprintf(buf, sizeof(buf), "botright sbuf %d", fnum);
1466 do_cmdline_cmd((char_u *)buf);
1467 }
1468 }
1469
Bram Moolenaard85f2712017-07-28 21:51:57 +02001470 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001471 }
1472 if (did_one)
1473 {
1474 redraw_statuslines();
1475
1476 /* Need to break out of vgetc(). */
1477 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001478 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001479
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001480 term = curbuf->b_term;
1481 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001482 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001483 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001484 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001485 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001486 }
1487 }
1488}
1489
1490/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001491 * Reverse engineer the RGB value into a cterm color index.
1492 * First color is 1. Return 0 if no match found.
1493 */
1494 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001495color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001496{
1497 int red = color->red;
1498 int blue = color->blue;
1499 int green = color->green;
1500
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001501 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001502 if (red == 0)
1503 {
1504 if (green == 0)
1505 {
1506 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001507 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001508 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001509 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001510 }
1511 else if (green == 224)
1512 {
1513 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001514 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001515 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001516 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001517 }
1518 }
1519 else if (red == 224)
1520 {
1521 if (green == 0)
1522 {
1523 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001524 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001525 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001526 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001527 }
1528 else if (green == 224)
1529 {
1530 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001531 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001532 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001533 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001534 }
1535 }
1536 else if (red == 128)
1537 {
1538 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001539 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001540 }
1541 else if (red == 255)
1542 {
1543 if (green == 64)
1544 {
1545 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001546 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001547 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001548 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001549 }
1550 else if (green == 255)
1551 {
1552 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001553 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001554 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001555 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001556 }
1557 }
1558 else if (red == 64)
1559 {
1560 if (green == 64)
1561 {
1562 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001563 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001564 }
1565 else if (green == 255)
1566 {
1567 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001568 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001569 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001570 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001571 }
1572 }
1573 if (t_colors >= 256)
1574 {
1575 if (red == blue && red == green)
1576 {
1577 /* 24-color greyscale */
1578 static int cutoff[23] = {
1579 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1580 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1581 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1582 int i;
1583
1584 for (i = 0; i < 23; ++i)
1585 if (red < cutoff[i])
1586 return i + 233;
1587 return 256;
1588 }
1589
1590 /* 216-color cube */
1591 return 17 + ((red + 25) / 0x33) * 36
1592 + ((green + 25) / 0x33) * 6
1593 + (blue + 25) / 0x33;
1594 }
1595 return 0;
1596}
1597
1598/*
1599 * Convert the attributes of a vterm cell into an attribute index.
1600 */
1601 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001602cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001603{
1604 int attr = 0;
1605
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001606 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001607 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001608 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001609 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001610 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001611 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001612 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001613 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001614 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001615 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001616
1617#ifdef FEAT_GUI
1618 if (gui.in_use)
1619 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001620 guicolor_T fg, bg;
1621
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001622 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1623 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001624 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001625 }
1626 else
1627#endif
1628#ifdef FEAT_TERMGUICOLORS
1629 if (p_tgc)
1630 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001631 guicolor_T fg, bg;
1632
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001633 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1634 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001635
1636 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001637 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001638 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001639#endif
1640 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001641 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001642 int fg = color2index(&cellfg, TRUE, &bold);
1643 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001644
1645 /* with 8 colors set the bold attribute to get a bright foreground */
1646 if (bold == TRUE)
1647 attr |= HL_BOLD;
1648 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001649 }
1650 return 0;
1651}
1652
1653/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001654 * Called to update a window that contains an active terminal.
1655 * Returns FAIL when there is no terminal running in this window or in
1656 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001657 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001658 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001659term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001660{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001661 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001662 VTerm *vterm;
1663 VTermScreen *screen;
1664 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001665 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001666
Bram Moolenaar6d819742017-08-06 14:57:49 +02001667 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001668 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001669
Bram Moolenaard85f2712017-07-28 21:51:57 +02001670 vterm = term->tl_vterm;
1671 screen = vterm_obtain_screen(vterm);
1672 state = vterm_obtain_state(vterm);
1673
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001674 /*
1675 * If the window was resized a redraw will be triggered and we get here.
1676 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1677 */
1678 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1679 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001680 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001681 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1682 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1683 win_T *twp;
1684
1685 FOR_ALL_WINDOWS(twp)
1686 {
1687 /* When more than one window shows the same terminal, use the
1688 * smallest size. */
1689 if (twp->w_buffer == term->tl_buffer)
1690 {
1691 if (!term->tl_rows_fixed && rows > twp->w_height)
1692 rows = twp->w_height;
1693 if (!term->tl_cols_fixed && cols > twp->w_width)
1694 cols = twp->w_width;
1695 }
1696 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001697
1698 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001699 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001700 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001701 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001702 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001703
1704 /* The cursor may have been moved when resizing. */
1705 vterm_state_get_cursorpos(state, &pos);
1706 position_cursor(wp, &pos);
1707
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001708 /* TODO: Only redraw what changed. */
1709 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001710 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001711 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001712 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001713
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001714 if (pos.row < term->tl_rows)
1715 {
1716 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001717 {
1718 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001719 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001720
Bram Moolenaareeac6772017-07-23 15:48:37 +02001721 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1722 vim_memset(&cell, 0, sizeof(cell));
1723
1724 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001725 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001726 if (c == NUL)
1727 {
1728 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001729#if defined(FEAT_MBYTE)
1730 if (enc_utf8)
1731 ScreenLinesUC[off] = NUL;
1732#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001733 }
1734 else
1735 {
1736#if defined(FEAT_MBYTE)
1737 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001738 {
1739 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001740 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001741 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001742 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001743 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001744 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001745 if (enc_utf8)
1746 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001747 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001748#else
1749 ScreenLines[off] = c;
1750#endif
1751 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001752 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001753
1754 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001755 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001756 if (cell.width == 2)
1757 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001758 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001759#if defined(FEAT_MBYTE)
1760 if (enc_utf8)
1761 ScreenLinesUC[off] = NUL;
1762#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001763 ++pos.col;
1764 ++off;
1765 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001766 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001767 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001768 else
1769 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001770
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001771 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1772 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001773 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001774
1775 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001776}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001777
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001778/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001779 * Return TRUE if "wp" is a terminal window where the job has finished.
1780 */
1781 int
1782term_is_finished(buf_T *buf)
1783{
1784 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1785}
1786
1787/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001788 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001789 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001790 */
1791 int
1792term_show_buffer(buf_T *buf)
1793{
1794 term_T *term = buf->b_term;
1795
Bram Moolenaar6d819742017-08-06 14:57:49 +02001796 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001797}
1798
1799/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001800 * The current buffer is going to be changed. If there is terminal
1801 * highlighting remove it now.
1802 */
1803 void
1804term_change_in_curbuf(void)
1805{
1806 term_T *term = curbuf->b_term;
1807
1808 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1809 {
1810 free_scrollback(term);
1811 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001812
1813 /* The buffer is now like a normal buffer, it cannot be easily
1814 * abandoned when changed. */
1815 set_string_option_direct((char_u *)"buftype", -1,
1816 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001817 }
1818}
1819
1820/*
1821 * Get the screen attribute for a position in the buffer.
1822 */
1823 int
1824term_get_attr(buf_T *buf, linenr_T lnum, int col)
1825{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001826 term_T *term = buf->b_term;
1827 sb_line_T *line;
1828 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001829
Bram Moolenaar70229f92017-07-29 16:01:53 +02001830 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001831 return 0;
1832 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1833 if (col >= line->sb_cols)
1834 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001835 cellattr = line->sb_cells + col;
1836 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001837}
1838
1839/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001840 * Create a new vterm and initialize it.
1841 */
1842 static void
1843create_vterm(term_T *term, int rows, int cols)
1844{
1845 VTerm *vterm;
1846 VTermScreen *screen;
1847
1848 vterm = vterm_new(rows, cols);
1849 term->tl_vterm = vterm;
1850 screen = vterm_obtain_screen(vterm);
1851 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1852 /* TODO: depends on 'encoding'. */
1853 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001854
1855 /* Vterm uses a default black background. Set it to white when
1856 * 'background' is "light". */
1857 if (*p_bg == 'l')
1858 {
1859 VTermColor fg, bg;
1860
1861 fg.red = fg.green = fg.blue = 0;
1862 bg.red = bg.green = bg.blue = 255;
1863 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1864 }
1865
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001866 /* Required to initialize most things. */
1867 vterm_screen_reset(screen, 1 /* hard */);
1868}
1869
Bram Moolenaar21554412017-07-24 21:44:43 +02001870/*
1871 * Return the text to show for the buffer name and status.
1872 */
1873 char_u *
1874term_get_status_text(term_T *term)
1875{
1876 if (term->tl_status_text == NULL)
1877 {
1878 char_u *txt;
1879 size_t len;
1880
Bram Moolenaar6d819742017-08-06 14:57:49 +02001881 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001882 {
1883 if (term_job_running(term))
1884 txt = (char_u *)_("Terminal");
1885 else
1886 txt = (char_u *)_("Terminal-finished");
1887 }
1888 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001889 txt = term->tl_title;
1890 else if (term_job_running(term))
1891 txt = (char_u *)_("running");
1892 else
1893 txt = (char_u *)_("finished");
1894 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001895 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001896 if (term->tl_status_text != NULL)
1897 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1898 term->tl_buffer->b_fname, txt);
1899 }
1900 return term->tl_status_text;
1901}
1902
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001903/*
1904 * Mark references in jobs of terminals.
1905 */
1906 int
1907set_ref_in_term(int copyID)
1908{
1909 int abort = FALSE;
1910 term_T *term;
1911 typval_T tv;
1912
1913 for (term = first_term; term != NULL; term = term->tl_next)
1914 if (term->tl_job != NULL)
1915 {
1916 tv.v_type = VAR_JOB;
1917 tv.vval.v_job = term->tl_job;
1918 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1919 }
1920 return abort;
1921}
1922
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001923/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001924 * Get the buffer from the first argument in "argvars".
1925 * Returns NULL when the buffer is not for a terminal window.
1926 */
1927 static buf_T *
1928term_get_buf(typval_T *argvars)
1929{
1930 buf_T *buf;
1931
1932 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1933 ++emsg_off;
1934 buf = get_buf_tv(&argvars[0], FALSE);
1935 --emsg_off;
1936 if (buf == NULL || buf->b_term == NULL)
1937 return NULL;
1938 return buf;
1939}
1940
1941/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001942 * "term_getattr(attr, name)" function
1943 */
1944 void
1945f_term_getattr(typval_T *argvars, typval_T *rettv)
1946{
1947 int attr;
1948 size_t i;
1949 char_u *name;
1950
1951 static struct {
1952 char *name;
1953 int attr;
1954 } attrs[] = {
1955 {"bold", HL_BOLD},
1956 {"italic", HL_ITALIC},
1957 {"underline", HL_UNDERLINE},
1958 {"strike", HL_STANDOUT},
1959 {"reverse", HL_INVERSE},
1960 };
1961
1962 attr = get_tv_number(&argvars[0]);
1963 name = get_tv_string_chk(&argvars[1]);
1964 if (name == NULL)
1965 return;
1966
1967 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1968 if (STRCMP(name, attrs[i].name) == 0)
1969 {
1970 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1971 break;
1972 }
1973}
1974
1975/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001976 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001977 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001978 void
1979f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001980{
Bram Moolenaar97870002017-07-30 18:28:38 +02001981 buf_T *buf = term_get_buf(argvars);
1982 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001983
Bram Moolenaar97870002017-07-30 18:28:38 +02001984 if (rettv_list_alloc(rettv) == FAIL)
1985 return;
1986 if (buf == NULL)
1987 return;
1988
1989 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001990 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1991 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001992 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001993}
1994
1995/*
1996 * "term_getjob(buf)" function
1997 */
1998 void
1999f_term_getjob(typval_T *argvars, typval_T *rettv)
2000{
2001 buf_T *buf = term_get_buf(argvars);
2002
2003 rettv->v_type = VAR_JOB;
2004 rettv->vval.v_job = NULL;
2005 if (buf == NULL)
2006 return;
2007
2008 rettv->vval.v_job = buf->b_term->tl_job;
2009 if (rettv->vval.v_job != NULL)
2010 ++rettv->vval.v_job->jv_refcount;
2011}
2012
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002013 static int
2014get_row_number(typval_T *tv, term_T *term)
2015{
2016 if (tv->v_type == VAR_STRING
2017 && tv->vval.v_string != NULL
2018 && STRCMP(tv->vval.v_string, ".") == 0)
2019 return term->tl_cursor_pos.row;
2020 return (int)get_tv_number(tv) - 1;
2021}
2022
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002023/*
2024 * "term_getline(buf, row)" function
2025 */
2026 void
2027f_term_getline(typval_T *argvars, typval_T *rettv)
2028{
2029 buf_T *buf = term_get_buf(argvars);
2030 term_T *term;
2031 int row;
2032
2033 rettv->v_type = VAR_STRING;
2034 if (buf == NULL)
2035 return;
2036 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002037 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002038
2039 if (term->tl_vterm == NULL)
2040 {
2041 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2042
2043 /* vterm is finished, get the text from the buffer */
2044 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2045 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2046 }
2047 else
2048 {
2049 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2050 VTermRect rect;
2051 int len;
2052 char_u *p;
2053
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002054 if (row < 0 || row >= term->tl_rows)
2055 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002056 len = term->tl_cols * MB_MAXBYTES + 1;
2057 p = alloc(len);
2058 if (p == NULL)
2059 return;
2060 rettv->vval.v_string = p;
2061
2062 rect.start_col = 0;
2063 rect.end_col = term->tl_cols;
2064 rect.start_row = row;
2065 rect.end_row = row + 1;
2066 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2067 }
2068}
2069
2070/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002071 * "term_getscrolled(buf)" function
2072 */
2073 void
2074f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2075{
2076 buf_T *buf = term_get_buf(argvars);
2077
2078 if (buf == NULL)
2079 return;
2080 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2081}
2082
2083/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002084 * "term_getsize(buf)" function
2085 */
2086 void
2087f_term_getsize(typval_T *argvars, typval_T *rettv)
2088{
2089 buf_T *buf = term_get_buf(argvars);
2090 list_T *l;
2091
2092 if (rettv_list_alloc(rettv) == FAIL)
2093 return;
2094 if (buf == NULL)
2095 return;
2096
2097 l = rettv->vval.v_list;
2098 list_append_number(l, buf->b_term->tl_rows);
2099 list_append_number(l, buf->b_term->tl_cols);
2100}
2101
2102/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002103 * "term_getstatus(buf)" function
2104 */
2105 void
2106f_term_getstatus(typval_T *argvars, typval_T *rettv)
2107{
2108 buf_T *buf = term_get_buf(argvars);
2109 term_T *term;
2110 char_u val[100];
2111
2112 rettv->v_type = VAR_STRING;
2113 if (buf == NULL)
2114 return;
2115 term = buf->b_term;
2116
2117 if (term_job_running(term))
2118 STRCPY(val, "running");
2119 else
2120 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002121 if (term->tl_normal_mode)
2122 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002123 rettv->vval.v_string = vim_strsave(val);
2124}
2125
2126/*
2127 * "term_gettitle(buf)" function
2128 */
2129 void
2130f_term_gettitle(typval_T *argvars, typval_T *rettv)
2131{
2132 buf_T *buf = term_get_buf(argvars);
2133
2134 rettv->v_type = VAR_STRING;
2135 if (buf == NULL)
2136 return;
2137
2138 if (buf->b_term->tl_title != NULL)
2139 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2140}
2141
2142/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002143 * "term_gettty(buf)" function
2144 */
2145 void
2146f_term_gettty(typval_T *argvars, typval_T *rettv)
2147{
2148 buf_T *buf = term_get_buf(argvars);
2149 char_u *p;
2150
2151 rettv->v_type = VAR_STRING;
2152 if (buf == NULL)
2153 return;
2154 if (buf->b_term->tl_job != NULL)
2155 p = buf->b_term->tl_job->jv_tty_name;
2156 else
2157 p = buf->b_term->tl_tty_name;
2158 if (p != NULL)
2159 rettv->vval.v_string = vim_strsave(p);
2160}
2161
2162/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002163 * "term_list()" function
2164 */
2165 void
2166f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2167{
2168 term_T *tp;
2169 list_T *l;
2170
2171 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2172 return;
2173
2174 l = rettv->vval.v_list;
2175 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2176 if (tp != NULL && tp->tl_buffer != NULL)
2177 if (list_append_number(l,
2178 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2179 return;
2180}
2181
2182/*
2183 * "term_scrape(buf, row)" function
2184 */
2185 void
2186f_term_scrape(typval_T *argvars, typval_T *rettv)
2187{
2188 buf_T *buf = term_get_buf(argvars);
2189 VTermScreen *screen = NULL;
2190 VTermPos pos;
2191 list_T *l;
2192 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002193 char_u *p;
2194 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002195
2196 if (rettv_list_alloc(rettv) == FAIL)
2197 return;
2198 if (buf == NULL)
2199 return;
2200 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002201
2202 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002203 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002204
2205 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002206 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002207 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002208 p = NULL;
2209 line = NULL;
2210 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002211 else
2212 {
2213 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2214
2215 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2216 return;
2217 p = ml_get_buf(buf, lnum + 1, FALSE);
2218 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2219 }
2220
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002221 for (pos.col = 0; pos.col < term->tl_cols; )
2222 {
2223 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002224 int width;
2225 VTermScreenCellAttrs attrs;
2226 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002227 char_u rgb[8];
2228 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2229 int off = 0;
2230 int i;
2231
2232 if (screen == NULL)
2233 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002234 cellattr_T *cellattr;
2235 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002236
2237 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002238 if (pos.col >= line->sb_cols)
2239 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002240 cellattr = line->sb_cells + pos.col;
2241 width = cellattr->width;
2242 attrs = cellattr->attrs;
2243 fg = cellattr->fg;
2244 bg = cellattr->bg;
2245 len = MB_PTR2LEN(p);
2246 mch_memmove(mbs, p, len);
2247 mbs[len] = NUL;
2248 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002249 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002250 else
2251 {
2252 VTermScreenCell cell;
2253 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2254 break;
2255 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2256 {
2257 if (cell.chars[i] == 0)
2258 break;
2259 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2260 }
2261 mbs[off] = NUL;
2262 width = cell.width;
2263 attrs = cell.attrs;
2264 fg = cell.fg;
2265 bg = cell.bg;
2266 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002267 dcell = dict_alloc();
2268 list_append_dict(l, dcell);
2269
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002270 dict_add_nr_str(dcell, "chars", 0, mbs);
2271
2272 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002273 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002274 dict_add_nr_str(dcell, "fg", 0, rgb);
2275 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002276 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002277 dict_add_nr_str(dcell, "bg", 0, rgb);
2278
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002279 dict_add_nr_str(dcell, "attr",
2280 cell2attr(attrs, fg, bg), NULL);
2281 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002282
2283 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002284 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002285 ++pos.col;
2286 }
2287}
2288
2289/*
2290 * "term_sendkeys(buf, keys)" function
2291 */
2292 void
2293f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2294{
2295 buf_T *buf = term_get_buf(argvars);
2296 char_u *msg;
2297 term_T *term;
2298
2299 rettv->v_type = VAR_UNKNOWN;
2300 if (buf == NULL)
2301 return;
2302
2303 msg = get_tv_string_chk(&argvars[1]);
2304 if (msg == NULL)
2305 return;
2306 term = buf->b_term;
2307 if (term->tl_vterm == NULL)
2308 return;
2309
2310 while (*msg != NUL)
2311 {
2312 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2313 msg += MB_PTR2LEN(msg);
2314 }
2315
Bram Moolenaar6d819742017-08-06 14:57:49 +02002316 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002317 {
2318 /* TODO: only update once in a while. */
2319 update_screen(0);
2320 if (buf == curbuf)
2321 update_cursor(term, TRUE);
2322 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002323}
2324
2325/*
2326 * "term_start(command, options)" function
2327 */
2328 void
2329f_term_start(typval_T *argvars, typval_T *rettv)
2330{
2331 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002332 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002333
2334 if (cmd == NULL)
2335 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002336 init_job_options(&opt);
2337 /* TODO: allow more job options */
2338 if (argvars[1].v_type != VAR_UNKNOWN
2339 && get_job_options(&argvars[1], &opt,
2340 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002341 + JO_EXIT_CB + JO_CLOSE_CALLBACK
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002342 + JO2_TERM_NAME + JO2_TERM_FINISH) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002343 return;
2344
2345 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002346
2347 if (curbuf->b_term != NULL)
2348 rettv->vval.v_number = curbuf->b_fnum;
2349}
2350
2351/*
2352 * "term_wait" function
2353 */
2354 void
2355f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2356{
2357 buf_T *buf = term_get_buf(argvars);
2358
2359 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002360 {
2361 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002362 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002363 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002364 if (buf->b_term->tl_job == NULL)
2365 {
2366 ch_log(NULL, "term_wait(): no job to wait for");
2367 return;
2368 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002369
2370 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002371 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002372 {
2373 /* The job is dead, keep reading channel I/O until the channel is
2374 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002375 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002376 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2377 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002378 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002379 parse_queued_messages();
2380 ui_delay(10L, FALSE);
2381 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002382 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002383 parse_queued_messages();
2384 }
2385 else
2386 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002387 long wait = 10L;
2388
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002389 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002390 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002391
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002392 /* Wait for some time for any channel I/O. */
2393 if (argvars[1].v_type != VAR_UNKNOWN)
2394 wait = get_tv_number(&argvars[1]);
2395 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002396 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002397
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002398 /* Flushing messages on channels is hopefully sufficient.
2399 * TODO: is there a better way? */
2400 parse_queued_messages();
2401 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002402}
2403
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002404# ifdef WIN3264
2405
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002406/**************************************
2407 * 2. MS-Windows implementation.
2408 */
2409
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002410#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2411#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2412
Bram Moolenaar8a773062017-07-24 22:29:21 +02002413void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002414void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002415void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002416BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2417void (*winpty_config_set_initial_size)(void*, int, int);
2418LPCWSTR (*winpty_conin_name)(void*);
2419LPCWSTR (*winpty_conout_name)(void*);
2420LPCWSTR (*winpty_conerr_name)(void*);
2421void (*winpty_free)(void*);
2422void (*winpty_config_free)(void*);
2423void (*winpty_spawn_config_free)(void*);
2424void (*winpty_error_free)(void*);
2425LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002426BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002427HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002428
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002429#define WINPTY_DLL "winpty.dll"
2430
2431static HINSTANCE hWinPtyDLL = NULL;
2432
2433 int
2434dyn_winpty_init(void)
2435{
2436 int i;
2437 static struct
2438 {
2439 char *name;
2440 FARPROC *ptr;
2441 } winpty_entry[] =
2442 {
2443 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2444 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2445 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2446 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2447 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2448 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2449 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2450 {"winpty_free", (FARPROC*)&winpty_free},
2451 {"winpty_open", (FARPROC*)&winpty_open},
2452 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2453 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2454 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2455 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002456 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002457 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002458 {NULL, NULL}
2459 };
2460
2461 /* No need to initialize twice. */
2462 if (hWinPtyDLL)
2463 return 1;
2464 /* Load winpty.dll */
2465 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2466 if (!hWinPtyDLL)
2467 {
2468 EMSG2(_(e_loadlib), WINPTY_DLL);
2469 return 0;
2470 }
2471 for (i = 0; winpty_entry[i].name != NULL
2472 && winpty_entry[i].ptr != NULL; ++i)
2473 {
2474 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2475 winpty_entry[i].name)) == NULL)
2476 {
2477 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2478 return 0;
2479 }
2480 }
2481
2482 return 1;
2483}
2484
2485/*
2486 * Create a new terminal of "rows" by "cols" cells.
2487 * Store a reference in "term".
2488 * Return OK or FAIL.
2489 */
2490 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002491term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002492{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002493 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002494 channel_T *channel = NULL;
2495 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002496 DWORD error;
2497 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2498 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002499 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002500 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002501
2502 if (!dyn_winpty_init())
2503 return FAIL;
2504
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002505 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002506 if (p == NULL)
2507 return FAIL;
2508
2509 job = job_alloc();
2510 if (job == NULL)
2511 goto failed;
2512
2513 channel = add_channel();
2514 if (channel == NULL)
2515 goto failed;
2516
2517 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2518 if (term->tl_winpty_config == NULL)
2519 goto failed;
2520
2521 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2522 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2523 if (term->tl_winpty == NULL)
2524 goto failed;
2525
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002526 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002527 spawn_config = winpty_spawn_config_new(
2528 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2529 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2530 NULL,
2531 p,
2532 NULL,
2533 NULL,
2534 &winpty_err);
2535 if (spawn_config == NULL)
2536 goto failed;
2537
2538 channel = add_channel();
2539 if (channel == NULL)
2540 goto failed;
2541
2542 job = job_alloc();
2543 if (job == NULL)
2544 goto failed;
2545
2546 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2547 &child_thread_handle, &error, &winpty_err))
2548 goto failed;
2549
2550 channel_set_pipes(channel,
2551 (sock_T) CreateFileW(
2552 winpty_conin_name(term->tl_winpty),
2553 GENERIC_WRITE, 0, NULL,
2554 OPEN_EXISTING, 0, NULL),
2555 (sock_T) CreateFileW(
2556 winpty_conout_name(term->tl_winpty),
2557 GENERIC_READ, 0, NULL,
2558 OPEN_EXISTING, 0, NULL),
2559 (sock_T) CreateFileW(
2560 winpty_conerr_name(term->tl_winpty),
2561 GENERIC_READ, 0, NULL,
2562 OPEN_EXISTING, 0, NULL));
2563
2564 jo = CreateJobObject(NULL, NULL);
2565 if (jo == NULL)
2566 goto failed;
2567
2568 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002569 {
2570 /* Failed, switch the way to terminate process with TerminateProcess. */
2571 CloseHandle(jo);
2572 jo = NULL;
2573 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002574
2575 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002576 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002577
2578 create_vterm(term, rows, cols);
2579
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002580 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002581 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002582
2583 job->jv_channel = channel;
2584 job->jv_proc_info.hProcess = child_process_handle;
2585 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2586 job->jv_job_object = jo;
2587 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002588 sprintf(buf, "winpty://%lu",
2589 GetProcessId(winpty_agent_process(term->tl_winpty)));
2590 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002591 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002592 term->tl_job = job;
2593
2594 return OK;
2595
2596failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002597 if (spawn_config != NULL)
2598 winpty_spawn_config_free(spawn_config);
2599 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002600 if (channel != NULL)
2601 channel_clear(channel);
2602 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002603 {
2604 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002605 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002606 }
2607 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002608 if (jo != NULL)
2609 CloseHandle(jo);
2610 if (term->tl_winpty != NULL)
2611 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002612 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002613 if (term->tl_winpty_config != NULL)
2614 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002615 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002616 if (winpty_err != NULL)
2617 {
2618 char_u *msg = utf16_to_enc(
2619 (short_u *)winpty_error_msg(winpty_err), NULL);
2620
2621 EMSG(msg);
2622 winpty_error_free(winpty_err);
2623 }
2624 return FAIL;
2625}
2626
2627/*
2628 * Free the terminal emulator part of "term".
2629 */
2630 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002631term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002632{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002633 if (term->tl_winpty != NULL)
2634 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002635 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002636 if (term->tl_winpty_config != NULL)
2637 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002638 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002639 if (term->tl_vterm != NULL)
2640 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002641 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002642}
2643
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002644/*
2645 * Request size to terminal.
2646 */
2647 static void
2648term_report_winsize(term_T *term, int rows, int cols)
2649{
2650 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2651}
2652
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002653# else
2654
2655/**************************************
2656 * 3. Unix-like implementation.
2657 */
2658
2659/*
2660 * Create a new terminal of "rows" by "cols" cells.
2661 * Start job for "cmd".
2662 * Store the pointers in "term".
2663 * Return OK or FAIL.
2664 */
2665 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002666term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002667{
2668 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002669
2670 create_vterm(term, rows, cols);
2671
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002672 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002673 argvars[0].v_type = VAR_STRING;
2674 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002675
2676 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002677 if (term->tl_job != NULL)
2678 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002679
Bram Moolenaar61a66052017-07-22 18:39:00 +02002680 return term->tl_job != NULL
2681 && term->tl_job->jv_channel != NULL
2682 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002683}
2684
2685/*
2686 * Free the terminal emulator part of "term".
2687 */
2688 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002689term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002690{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002691 if (term->tl_vterm != NULL)
2692 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002693 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002694}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002695
2696/*
2697 * Request size to terminal.
2698 */
2699 static void
2700term_report_winsize(term_T *term, int rows, int cols)
2701{
2702 /* Use an ioctl() to report the new window size to the job. */
2703 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2704 {
2705 int fd = -1;
2706 int part;
2707
2708 for (part = PART_OUT; part < PART_COUNT; ++part)
2709 {
2710 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2711 if (isatty(fd))
2712 break;
2713 }
2714 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2715 mch_stop_job(term->tl_job, (char_u *)"winch");
2716 }
2717}
2718
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002719# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002720
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002721#endif /* FEAT_TERMINAL */