blob: 18fed65f0384b5d657e4382ade43c8744f6b3435 [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 Moolenaardd693ce2017-08-10 23:15:19 +020039 * - When using term_finish "open" have a way to specify how the window is to
40 * be opened. E.g. term_opencmd "10split buffer %d".
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020041 * - support different cursor shapes, colors and attributes
42 * - make term_getcursor() return type (none/block/bar/underline) and
43 * attributes (color, blink, etc.)
Bram Moolenaardd693ce2017-08-10 23:15:19 +020044 * - Make argument list work on MS-Windows. #1954
Bram Moolenaar292d5692017-08-08 21:52:22 +020045 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaard85f2712017-07-28 21:51:57 +020046 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020047 * For the GUI fill termios with default values, perhaps like pangoterm:
48 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
49 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020050 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020051 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020052 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020053 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020054 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020055 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
56 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020057 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020058 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020059 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020060 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020061 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020062 * terminal. Allow:
63 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
64 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
65 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
66 * Check that something is connected to the terminal.
67 * Test: "cat" reading from a file or buffer
68 * "ls" writing stdout to a file or buffer
69 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020070 * - support ":term NONE" to open a terminal with a pty but not running a job
71 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020072 * - if the job in the terminal does not support the mouse, we can use the
73 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020074 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
75 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020076 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020077 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020078 * - Copy text in the vterm to the Vim buffer once in a while, so that
79 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020080 */
81
82#include "vim.h"
83
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020084#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020085
Bram Moolenaard5310982017-08-05 15:16:32 +020086#ifndef MIN
87# define MIN(x,y) ((x) < (y) ? (x) : (y))
88#endif
89#ifndef MAX
90# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020091#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020093#include "libvterm/include/vterm.h"
94
Bram Moolenaar33a43be2017-08-06 21:36:22 +020095/* This is VTermScreenCell without the characters, thus much smaller. */
96typedef struct {
97 VTermScreenCellAttrs attrs;
98 char width;
99 VTermColor fg, bg;
100} cellattr_T;
101
Bram Moolenaard85f2712017-07-28 21:51:57 +0200102typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200103 int sb_cols; /* can differ per line */
104 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200105} sb_line_T;
106
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200107/* typedef term_T in structs.h */
108struct terminal_S {
109 term_T *tl_next;
110
Bram Moolenaar423802d2017-07-30 16:52:24 +0200111 VTerm *tl_vterm;
112 job_T *tl_job;
113 buf_T *tl_buffer;
114
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200115 /* used when tl_job is NULL and only a pty was created */
116 int tl_tty_fd;
117 char_u *tl_tty_name;
118
Bram Moolenaar6d819742017-08-06 14:57:49 +0200119 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200120 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200121 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200122
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200123#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200124 void *tl_winpty_config;
125 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200126#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200127
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200128 /* last known vterm size */
129 int tl_rows;
130 int tl_cols;
131 /* vterm size does not follow window size */
132 int tl_rows_fixed;
133 int tl_cols_fixed;
134
Bram Moolenaar21554412017-07-24 21:44:43 +0200135 char_u *tl_title; /* NULL or allocated */
136 char_u *tl_status_text; /* NULL or allocated */
137
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200138 /* Range of screen rows to update. Zero based. */
139 int tl_dirty_row_start; /* -1 if nothing dirty */
140 int tl_dirty_row_end; /* row below last one to update */
141
Bram Moolenaard85f2712017-07-28 21:51:57 +0200142 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200143 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200144
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200145 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200146 int tl_cursor_visible;
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200147
148 int tl_using_altscreen;
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;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200238 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
239 opt->jo_term_rows = rows;
240 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
241 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200242}
243
244 static void
Bram Moolenaarda43b612017-08-11 22:27:50 +0200245term_start(char_u *cmd, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247 exarg_T split_ea;
248 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200250 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251
252 if (check_restricted() || check_secure())
253 return;
254
255 term = (term_T *)alloc_clear(sizeof(term_T));
256 if (term == NULL)
257 return;
258 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200259 term->tl_cursor_visible = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200260 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200261 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200262
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200264 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200265 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200266 /* Create a new buffer in the current window. */
267 if (!can_abandon(curbuf, forceit))
268 {
269 EMSG(_(e_nowrtmsg));
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200270 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200271 return;
272 }
273 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
274 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200275 {
276 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200277 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200278 }
279 }
280 else if (opt->jo_hidden)
281 {
282 buf_T *buf;
283
284 /* Create a new buffer without a window. Make it the current buffer for
285 * a moment to be able to do the initialisations. */
286 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
287 BLN_NEW | BLN_LISTED);
288 if (buf == NULL || ml_open(buf) == FAIL)
289 {
290 vim_free(term);
291 return;
292 }
293 old_curbuf = curbuf;
294 --curbuf->b_nwindows;
295 curbuf = buf;
296 curwin->w_buffer = buf;
297 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200298 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200299 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200300 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200301 /* Open a new window or tab. */
302 split_ea.cmdidx = CMD_new;
303 split_ea.cmd = (char_u *)"new";
304 split_ea.arg = (char_u *)"";
305 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
306 {
307 split_ea.line2 = opt->jo_term_rows;
308 split_ea.addr_count = 1;
309 }
310 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
311 {
312 split_ea.line2 = opt->jo_term_cols;
313 split_ea.addr_count = 1;
314 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200315
Bram Moolenaarda43b612017-08-11 22:27:50 +0200316 ex_splitview(&split_ea);
317 if (curwin == old_curwin)
318 {
319 /* split failed */
320 vim_free(term);
321 return;
322 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200323 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200324 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200325 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200326
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200327 if (!opt->jo_hidden)
328 {
329 /* only one size was taken care of with :new, do the other one */
330 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
331 win_setheight(opt->jo_term_rows);
332 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
333 win_setwidth(opt->jo_term_cols);
334 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200335
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200336 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200337 term->tl_next = first_term;
338 first_term = term;
339
Bram Moolenaar293424c2017-07-26 23:11:01 +0200340 if (cmd == NULL || *cmd == NUL)
341 cmd = p_sh;
342
Bram Moolenaar78712a72017-08-05 14:50:12 +0200343 if (opt->jo_term_name != NULL)
344 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
345 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200346 {
347 int i;
348 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200349 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200350
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200351 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200352 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200353 /* Prepend a ! to the command name to avoid the buffer name equals
354 * the executable, otherwise ":w!" would overwrite it. */
355 if (i == 0)
356 vim_snprintf((char *)p, len, "!%s", cmd);
357 else
358 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200359 if (buflist_findname(p) == NULL)
360 {
361 curbuf->b_ffname = p;
362 break;
363 }
364 }
365 }
366 curbuf->b_fname = curbuf->b_ffname;
367
Bram Moolenaareb44a682017-08-03 22:44:55 +0200368 set_string_option_direct((char_u *)"buftype", -1,
369 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
370
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200371 /* Mark the buffer as not modifiable. It can only be made modifiable after
372 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200373 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200374
375 /* Set 'bufhidden' to "hide": allow closing the window. */
376 set_string_option_direct((char_u *)"bufhidden", -1,
377 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200378
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200379 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200380 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200381
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200382 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200383 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200384 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200385 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200386 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200387 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200388
389 if (old_curbuf != NULL)
390 {
391 --curbuf->b_nwindows;
392 curbuf = old_curbuf;
393 curwin->w_buffer = curbuf;
394 ++curbuf->b_nwindows;
395 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200396 }
397 else
398 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200399 buf_T *buf = curbuf;
400
Bram Moolenaard85f2712017-07-28 21:51:57 +0200401 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200402 if (old_curbuf != NULL)
403 {
404 --curbuf->b_nwindows;
405 curbuf = old_curbuf;
406 curwin->w_buffer = curbuf;
407 ++curbuf->b_nwindows;
408 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200409
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200410 /* Wiping out the buffer will also close the window and call
411 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200412 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200413 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200414}
415
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200416/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200417 * ":terminal": open a terminal window and execute a job in it.
418 */
419 void
420ex_terminal(exarg_T *eap)
421{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200422 jobopt_T opt;
423 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200424
425 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200426
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200427 cmd = eap->arg;
428 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
429 {
430 char_u *p;
431
432 cmd += 2;
433 p = skiptowhite(cmd);
434 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
435 opt.jo_term_finish = 'c';
436 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
437 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200438 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
439 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200440 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
441 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200442 else
443 {
444 if (*p)
445 *p = NUL;
446 EMSG2(_("E181: Invalid attribute: %s"), cmd);
447 return;
448 }
449 cmd = skipwhite(p);
450 }
451
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200452 if (eap->addr_count == 2)
453 {
454 opt.jo_term_rows = eap->line1;
455 opt.jo_term_cols = eap->line2;
456 }
457 else if (eap->addr_count == 1)
458 {
459 if (cmdmod.split & WSP_VERT)
460 opt.jo_term_cols = eap->line2;
461 else
462 opt.jo_term_rows = eap->line2;
463 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200464
Bram Moolenaarda43b612017-08-11 22:27:50 +0200465 term_start(cmd, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200466}
467
468/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200469 * Free the scrollback buffer for "term".
470 */
471 static void
472free_scrollback(term_T *term)
473{
474 int i;
475
476 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
477 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
478 ga_clear(&term->tl_scrollback);
479}
480
481/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200482 * Free a terminal and everything it refers to.
483 * Kills the job if there is one.
484 * Called when wiping out a buffer.
485 */
486 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200487free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200488{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200489 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200490 term_T *tp;
491
492 if (term == NULL)
493 return;
494 if (first_term == term)
495 first_term = term->tl_next;
496 else
497 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
498 if (tp->tl_next == term)
499 {
500 tp->tl_next = term->tl_next;
501 break;
502 }
503
504 if (term->tl_job != NULL)
505 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200506 if (term->tl_job->jv_status != JOB_ENDED
507 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200508 job_stop(term->tl_job, NULL, "kill");
509 job_unref(term->tl_job);
510 }
511
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200512 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200513
514 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200515 vim_free(term->tl_title);
516 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200517 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200518 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200519}
520
521/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200522 * Write job output "msg[len]" to the vterm.
523 */
524 static void
525term_write_job_output(term_T *term, char_u *msg, size_t len)
526{
527 VTerm *vterm = term->tl_vterm;
528 char_u *p;
529 size_t done;
530 size_t len_now;
531
532 for (done = 0; done < len; done += len_now)
533 {
534 for (p = msg + done; p < msg + len; )
535 {
536 if (*p == NL)
537 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200538 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200539 }
540 len_now = p - msg - done;
541 vterm_input_write(vterm, (char *)msg + done, len_now);
542 if (p < msg + len && *p == NL)
543 {
544 /* Convert NL to CR-NL, that appears to work best. */
545 vterm_input_write(vterm, "\r\n", 2);
546 ++len_now;
547 }
548 }
549
550 /* this invokes the damage callbacks */
551 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
552}
553
Bram Moolenaar1c844932017-07-24 23:36:41 +0200554 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200555update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200556{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200557 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200558 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200559 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200560 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200561 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200562 if (term->tl_cursor_visible)
563 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200564 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200565#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200566 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200567 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200568#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200569 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200570}
571
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200572/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200573 * Invoked when "msg" output from a job was received. Write it to the terminal
574 * of "buffer".
575 */
576 void
577write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
578{
579 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200580 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200581
Bram Moolenaard85f2712017-07-28 21:51:57 +0200582 if (term->tl_vterm == NULL)
583 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200584 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200585 return;
586 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200587 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200588 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200589
Bram Moolenaar6d819742017-08-06 14:57:49 +0200590 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200591 {
592 /* TODO: only update once in a while. */
593 update_screen(0);
594 update_cursor(term, TRUE);
595 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200596}
597
598/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200599 * Send a mouse position and click to the vterm
600 */
601 static int
602term_send_mouse(VTerm *vterm, int button, int pressed)
603{
604 VTermModifier mod = VTERM_MOD_NONE;
605
606 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
607 mouse_col - W_WINCOL(curwin), mod);
608 vterm_mouse_button(vterm, button, pressed, mod);
609 return TRUE;
610}
611
612/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200613 * Convert typed key "c" into bytes to send to the job.
614 * Return the number of bytes in "buf".
615 */
616 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200617term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200618{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200619 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200620 VTermKey key = VTERM_KEY_NONE;
621 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200622 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200623
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200624 switch (c)
625 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200626 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200627 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200628 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
629 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200630 case K_DEL: key = VTERM_KEY_DEL; break;
631 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200632 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
633 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200634 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200635 case K_S_END: mod = VTERM_MOD_SHIFT;
636 key = VTERM_KEY_END; break;
637 case K_C_END: mod = VTERM_MOD_CTRL;
638 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200639 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
640 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
641 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
642 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
643 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
644 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
645 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
646 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
647 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
648 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
649 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
650 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
651 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200652 case K_S_HOME: mod = VTERM_MOD_SHIFT;
653 key = VTERM_KEY_HOME; break;
654 case K_C_HOME: mod = VTERM_MOD_CTRL;
655 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200656 case K_INS: key = VTERM_KEY_INS; break;
657 case K_K0: key = VTERM_KEY_KP_0; break;
658 case K_K1: key = VTERM_KEY_KP_1; break;
659 case K_K2: key = VTERM_KEY_KP_2; break;
660 case K_K3: key = VTERM_KEY_KP_3; break;
661 case K_K4: key = VTERM_KEY_KP_4; break;
662 case K_K5: key = VTERM_KEY_KP_5; break;
663 case K_K6: key = VTERM_KEY_KP_6; break;
664 case K_K7: key = VTERM_KEY_KP_7; break;
665 case K_K8: key = VTERM_KEY_KP_8; break;
666 case K_K9: key = VTERM_KEY_KP_9; break;
667 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
668 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
669 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
670 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
671 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
672 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
673 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
674 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
675 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
676 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
677 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
678 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
679 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200680 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
681 key = VTERM_KEY_LEFT; break;
682 case K_C_LEFT: mod = VTERM_MOD_CTRL;
683 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200684 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
685 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
686 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200687 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
688 key = VTERM_KEY_RIGHT; break;
689 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
690 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200691 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200692 case K_S_UP: mod = VTERM_MOD_SHIFT;
693 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200694 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200695
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200696 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
697 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
698 case K_MOUSELEFT: /* TODO */ return 0;
699 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200700
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200701 case K_LEFTMOUSE:
702 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
703 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
704 case K_LEFTRELEASE:
705 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
706 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
707 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
708 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
709 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
710 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
711 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
712 case K_X1MOUSE: /* TODO */ return 0;
713 case K_X1DRAG: /* TODO */ return 0;
714 case K_X1RELEASE: /* TODO */ return 0;
715 case K_X2MOUSE: /* TODO */ return 0;
716 case K_X2DRAG: /* TODO */ return 0;
717 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200718
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200719 case K_IGNORE: return 0;
720 case K_NOP: return 0;
721 case K_UNDO: return 0;
722 case K_HELP: return 0;
723 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
724 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
725 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
726 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
727 case K_SELECT: return 0;
728#ifdef FEAT_GUI
729 case K_VER_SCROLLBAR: return 0;
730 case K_HOR_SCROLLBAR: return 0;
731#endif
732#ifdef FEAT_GUI_TABLINE
733 case K_TABLINE: return 0;
734 case K_TABMENU: return 0;
735#endif
736#ifdef FEAT_NETBEANS_INTG
737 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
738#endif
739#ifdef FEAT_DND
740 case K_DROP: return 0;
741#endif
742#ifdef FEAT_AUTOCMD
743 case K_CURSORHOLD: return 0;
744#endif
745 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
746 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200747 }
748
749 /*
750 * Convert special keys to vterm keys:
751 * - Write keys to vterm: vterm_keyboard_key()
752 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200753 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200754 */
755 if (key != VTERM_KEY_NONE)
756 /* Special key, let vterm convert it. */
757 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200758 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200759 /* Normal character, let vterm convert it. */
760 vterm_keyboard_unichar(vterm, c, mod);
761
762 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200763 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200764}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200765
Bram Moolenaar938783d2017-07-16 20:13:26 +0200766/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200767 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200768 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200769 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200770term_job_running(term_T *term)
771{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200772 /* Also consider the job finished when the channel is closed, to avoid a
773 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200774 return term != NULL
775 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200776 && term->tl_job->jv_status == JOB_STARTED
777 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200778}
779
780/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200781 * Add the last line of the scrollback buffer to the buffer in the window.
782 */
783 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200784add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200785{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200786 buf_T *buf = term->tl_buffer;
787 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
788 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200789
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200790 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200791 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200792 {
793 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200794 curbuf = buf;
795 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200796 curbuf = curwin->w_buffer;
797 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200798}
799
800/*
801 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200802 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200803 */
804 static void
805move_terminal_to_buffer(term_T *term)
806{
807 win_T *wp;
808 int len;
809 int lines_skipped = 0;
810 VTermPos pos;
811 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200812 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200813 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200814
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200815 if (term->tl_vterm == NULL)
816 return;
817 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200818 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
819 {
820 len = 0;
821 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
822 if (vterm_screen_get_cell(screen, pos, &cell) != 0
823 && cell.chars[0] != NUL)
824 len = pos.col + 1;
825
826 if (len == 0)
827 ++lines_skipped;
828 else
829 {
830 while (lines_skipped > 0)
831 {
832 /* Line was skipped, add an empty line. */
833 --lines_skipped;
834 if (ga_grow(&term->tl_scrollback, 1) == OK)
835 {
836 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
837 + term->tl_scrollback.ga_len;
838
839 line->sb_cols = 0;
840 line->sb_cells = NULL;
841 ++term->tl_scrollback.ga_len;
842
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200843 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200844 }
845 }
846
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200847 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200848 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
849 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200850 garray_T ga;
851 int width;
852 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200853 + term->tl_scrollback.ga_len;
854
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200855 ga_init2(&ga, 1, 100);
856 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200857 {
858 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200859 {
860 width = 1;
861 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
862 if (ga_grow(&ga, 1) == OK)
863 ga.ga_len += mb_char2bytes(' ',
864 (char_u *)ga.ga_data + ga.ga_len);
865 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200866 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200867 {
868 width = cell.width;
869
870 p[pos.col].width = cell.width;
871 p[pos.col].attrs = cell.attrs;
872 p[pos.col].fg = cell.fg;
873 p[pos.col].bg = cell.bg;
874
875 if (ga_grow(&ga, MB_MAXBYTES) == OK)
876 {
877 int i;
878 int c;
879
880 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
881 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
882 (char_u *)ga.ga_data + ga.ga_len);
883 }
884 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200885 }
886 line->sb_cols = len;
887 line->sb_cells = p;
888 ++term->tl_scrollback.ga_len;
889
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200890 if (ga_grow(&ga, 1) == FAIL)
891 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
892 else
893 {
894 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
895 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
896 }
897 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200898 }
899 else
900 vim_free(p);
901 }
902 }
903
904 FOR_ALL_WINDOWS(wp)
905 {
906 if (wp->w_buffer == term->tl_buffer)
907 {
908 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
909 wp->w_cursor.col = 0;
910 wp->w_valid = 0;
911 redraw_win_later(wp, NOT_VALID);
912 }
913 }
914}
915
916 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200917set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200918{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200919 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200920 vim_free(term->tl_status_text);
921 term->tl_status_text = NULL;
922 if (term->tl_buffer == curbuf)
923 maketitle();
924}
925
926/*
927 * Called after the job if finished and Terminal mode is not active:
928 * Move the vterm contents into the scrollback buffer and free the vterm.
929 */
930 static void
931cleanup_vterm(term_T *term)
932{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200933 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200934 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200935 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200936 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200937}
938
939/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200940 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200941 * Suspends updating the terminal window.
942 */
943 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200944term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200945{
946 term_T *term = curbuf->b_term;
947
948 /* Append the current terminal contents to the buffer. */
949 move_terminal_to_buffer(term);
950
Bram Moolenaar6d819742017-08-06 14:57:49 +0200951 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200952
Bram Moolenaar6d819742017-08-06 14:57:49 +0200953 /* Move the window cursor to the position of the cursor in the
954 * terminal. */
955 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
956 + term->tl_cursor_pos.row + 1;
957 check_cursor();
958 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200959
Bram Moolenaar6d819742017-08-06 14:57:49 +0200960 /* Display the same lines as in the terminal. */
961 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200962}
963
964/*
965 * Returns TRUE if the current window contains a terminal and we are in
966 * Terminal-Normal mode.
967 */
968 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200969term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200970{
971 term_T *term = curbuf->b_term;
972
Bram Moolenaar6d819742017-08-06 14:57:49 +0200973 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200974}
975
976/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200977 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200978 * Restores updating the terminal window.
979 */
980 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200981term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200982{
983 term_T *term = curbuf->b_term;
984 sb_line_T *line;
985 garray_T *gap;
986
987 /* Remove the terminal contents from the scrollback and the buffer. */
988 gap = &term->tl_scrollback;
989 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
990 {
991 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
992 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
993 vim_free(line->sb_cells);
994 --gap->ga_len;
995 if (gap->ga_len == 0)
996 break;
997 }
998 check_cursor();
999
Bram Moolenaar6d819742017-08-06 14:57:49 +02001000 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001001
1002 if (term->tl_channel_closed)
1003 cleanup_vterm(term);
1004 redraw_buf_and_status_later(curbuf, NOT_VALID);
1005}
1006
1007/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001008 * Get a key from the user without mapping.
1009 * TODO: use terminal mode mappings.
1010 */
1011 static int
1012term_vgetc()
1013{
1014 int c;
1015
1016 ++no_mapping;
1017 ++allow_keys;
1018 got_int = FALSE;
1019 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001020 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001021 --no_mapping;
1022 --allow_keys;
1023 return c;
1024}
1025
1026/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001027 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001028 * Return FAIL when the key needs to be handled in Normal mode.
1029 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001030 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001031 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001032send_keys_to_term(term_T *term, int c, int typed)
1033{
1034 char msg[KEY_BUF_LEN];
1035 size_t len;
1036 static int mouse_was_outside = FALSE;
1037 int dragging_outside = FALSE;
1038
1039 /* Catch keys that need to be handled as in Normal mode. */
1040 switch (c)
1041 {
1042 case NUL:
1043 case K_ZERO:
1044 if (typed)
1045 stuffcharReadbuff(c);
1046 return FAIL;
1047
1048 case K_IGNORE:
1049 return FAIL;
1050
1051 case K_LEFTDRAG:
1052 case K_MIDDLEDRAG:
1053 case K_RIGHTDRAG:
1054 case K_X1DRAG:
1055 case K_X2DRAG:
1056 dragging_outside = mouse_was_outside;
1057 /* FALLTHROUGH */
1058 case K_LEFTMOUSE:
1059 case K_LEFTMOUSE_NM:
1060 case K_LEFTRELEASE:
1061 case K_LEFTRELEASE_NM:
1062 case K_MIDDLEMOUSE:
1063 case K_MIDDLERELEASE:
1064 case K_RIGHTMOUSE:
1065 case K_RIGHTRELEASE:
1066 case K_X1MOUSE:
1067 case K_X1RELEASE:
1068 case K_X2MOUSE:
1069 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001070
1071 case K_MOUSEUP:
1072 case K_MOUSEDOWN:
1073 case K_MOUSELEFT:
1074 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001075 if (mouse_row < W_WINROW(curwin)
1076 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1077 || mouse_col < W_WINCOL(curwin)
1078 || mouse_col >= W_ENDCOL(curwin)
1079 || dragging_outside)
1080 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001081 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001082 if (typed)
1083 {
1084 stuffcharReadbuff(c);
1085 mouse_was_outside = TRUE;
1086 }
1087 return FAIL;
1088 }
1089 }
1090 if (typed)
1091 mouse_was_outside = FALSE;
1092
1093 /* Convert the typed key to a sequence of bytes for the job. */
1094 len = term_convert_key(term, c, msg);
1095 if (len > 0)
1096 /* TODO: if FAIL is returned, stop? */
1097 channel_send(term->tl_job->jv_channel, PART_IN,
1098 (char_u *)msg, (int)len, NULL);
1099
1100 return OK;
1101}
1102
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001103 static void
1104position_cursor(win_T *wp, VTermPos *pos)
1105{
1106 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1107 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1108 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1109}
1110
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001111/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001112 * Handle CTRL-W "": send register contents to the job.
1113 */
1114 static void
1115term_paste_register(int prev_c UNUSED)
1116{
1117 int c;
1118 list_T *l;
1119 listitem_T *item;
1120 long reglen = 0;
1121 int type;
1122
1123#ifdef FEAT_CMDL_INFO
1124 if (add_to_showcmd(prev_c))
1125 if (add_to_showcmd('"'))
1126 out_flush();
1127#endif
1128 c = term_vgetc();
1129#ifdef FEAT_CMDL_INFO
1130 clear_showcmd();
1131#endif
1132
1133 /* CTRL-W "= prompt for expression to evaluate. */
1134 if (c == '=' && get_expr_register() != '=')
1135 return;
1136
1137 l = (list_T *)get_reg_contents(c, GREG_LIST);
1138 if (l != NULL)
1139 {
1140 type = get_reg_type(c, &reglen);
1141 for (item = l->lv_first; item != NULL; item = item->li_next)
1142 {
1143 char_u *s = get_tv_string(&item->li_tv);
1144
1145 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1146 s, STRLEN(s), NULL);
1147 if (item->li_next != NULL || type == MLINE)
1148 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1149 (char_u *)"\r", 1, NULL);
1150 }
1151 list_free(l);
1152 }
1153}
1154
1155/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001156 * Returns TRUE if the current window contains a terminal and we are sending
1157 * keys to the job.
1158 */
1159 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001160term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001161{
1162 term_T *term = curbuf->b_term;
1163
1164 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001165 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001166 && term->tl_vterm != NULL
1167 && term_job_running(term);
1168}
1169
1170/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001171 * Wait for input and send it to the job.
1172 * Return when the start of a CTRL-W command is typed or anything else that
1173 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001174 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1175 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001176 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001177 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001178terminal_loop(void)
1179{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001180 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001181 int termkey = 0;
1182
1183 if (*curwin->w_p_tk != NUL)
1184 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001185 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001186
1187 for (;;)
1188 {
1189 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001190 /* Repeat redrawing in case a message is received while redrawing. */
1191 while (curwin->w_redr_type != 0)
1192 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001193 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001194
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001195 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001196 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001197 /* job finished while waiting for a character */
1198 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001199
Bram Moolenaarfae42832017-08-01 22:24:26 +02001200#ifdef UNIX
1201 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1202#endif
1203#ifdef WIN3264
1204 if (c == Ctrl_C)
1205 /* We don't know if the job can handle CTRL-C itself or not, this
1206 * may kill the shell instead of killing the command running in the
1207 * shell. */
Bram Moolenaar2d33e902017-08-11 16:31:54 +02001208 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001209#endif
1210
Bram Moolenaar69198192017-08-05 14:10:48 +02001211 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001212 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001213 int prev_c = c;
1214
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001215#ifdef FEAT_CMDL_INFO
1216 if (add_to_showcmd(c))
1217 out_flush();
1218#endif
1219 c = term_vgetc();
1220#ifdef FEAT_CMDL_INFO
1221 clear_showcmd();
1222#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001223 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001224 /* job finished while waiting for a character */
1225 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001226
Bram Moolenaar69198192017-08-05 14:10:48 +02001227 if (prev_c == Ctrl_BSL)
1228 {
1229 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001230 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001231 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1232 term_enter_normal_mode();
1233 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001234 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001235 /* Send both keys to the terminal. */
1236 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1237 }
1238 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001239 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001240 /* "CTRL-W .": send CTRL-W to the job */
1241 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001242 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001243 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001244 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001245 /* CTRL-W N : go to Terminal-Normal mode. */
1246 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001247 return FAIL;
1248 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001249 else if (c == '"')
1250 {
1251 term_paste_register(prev_c);
1252 continue;
1253 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001254 else if (termkey == 0 || c != termkey)
1255 {
1256 stuffcharReadbuff(Ctrl_W);
1257 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001258 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001259 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001260 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001261 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1262 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001263 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001264 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001265}
1266
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001267/*
1268 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001269 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001270 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001271 */
1272 void
1273term_job_ended(job_T *job)
1274{
1275 term_T *term;
1276 int did_one = FALSE;
1277
1278 for (term = first_term; term != NULL; term = term->tl_next)
1279 if (term->tl_job == job)
1280 {
1281 vim_free(term->tl_title);
1282 term->tl_title = NULL;
1283 vim_free(term->tl_status_text);
1284 term->tl_status_text = NULL;
1285 redraw_buf_and_status_later(term->tl_buffer, VALID);
1286 did_one = TRUE;
1287 }
1288 if (did_one)
1289 redraw_statuslines();
1290 if (curbuf->b_term != NULL)
1291 {
1292 if (curbuf->b_term->tl_job == job)
1293 maketitle();
1294 update_cursor(curbuf->b_term, TRUE);
1295 }
1296}
1297
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001298 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001299may_toggle_cursor(term_T *term)
1300{
1301 if (curbuf == term->tl_buffer)
1302 {
1303 if (term->tl_cursor_visible)
1304 cursor_on();
1305 else
1306 cursor_off();
1307 }
1308}
1309
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001310 static int
1311handle_damage(VTermRect rect, void *user)
1312{
1313 term_T *term = (term_T *)user;
1314
1315 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1316 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1317 redraw_buf_later(term->tl_buffer, NOT_VALID);
1318 return 1;
1319}
1320
1321 static int
1322handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1323{
1324 term_T *term = (term_T *)user;
1325
1326 /* TODO */
1327 redraw_buf_later(term->tl_buffer, NOT_VALID);
1328 return 1;
1329}
1330
1331 static int
1332handle_movecursor(
1333 VTermPos pos,
1334 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001335 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001336 void *user)
1337{
1338 term_T *term = (term_T *)user;
1339 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001340
1341 term->tl_cursor_pos = pos;
1342 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001343
1344 FOR_ALL_WINDOWS(wp)
1345 {
1346 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001347 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001348 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001349 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001350 {
1351 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001352 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001353 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001354
1355 return 1;
1356}
1357
Bram Moolenaar21554412017-07-24 21:44:43 +02001358 static int
1359handle_settermprop(
1360 VTermProp prop,
1361 VTermValue *value,
1362 void *user)
1363{
1364 term_T *term = (term_T *)user;
1365
1366 switch (prop)
1367 {
1368 case VTERM_PROP_TITLE:
1369 vim_free(term->tl_title);
1370 term->tl_title = vim_strsave((char_u *)value->string);
1371 vim_free(term->tl_status_text);
1372 term->tl_status_text = NULL;
1373 if (term == curbuf->b_term)
1374 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001375 break;
1376
1377 case VTERM_PROP_CURSORVISIBLE:
1378 term->tl_cursor_visible = value->boolean;
1379 may_toggle_cursor(term);
1380 out_flush();
1381 break;
1382
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001383 case VTERM_PROP_ALTSCREEN:
1384 /* TODO: do anything else? */
1385 term->tl_using_altscreen = value->boolean;
1386 break;
1387
Bram Moolenaar21554412017-07-24 21:44:43 +02001388 default:
1389 break;
1390 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001391 /* Always return 1, otherwise vterm doesn't store the value internally. */
1392 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001393}
1394
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001395/*
1396 * The job running in the terminal resized the terminal.
1397 */
1398 static int
1399handle_resize(int rows, int cols, void *user)
1400{
1401 term_T *term = (term_T *)user;
1402 win_T *wp;
1403
1404 term->tl_rows = rows;
1405 term->tl_cols = cols;
1406 FOR_ALL_WINDOWS(wp)
1407 {
1408 if (wp->w_buffer == term->tl_buffer)
1409 {
1410 win_setheight_win(rows, wp);
1411 win_setwidth_win(cols, wp);
1412 }
1413 }
1414
1415 redraw_buf_later(term->tl_buffer, NOT_VALID);
1416 return 1;
1417}
1418
Bram Moolenaard85f2712017-07-28 21:51:57 +02001419/*
1420 * Handle a line that is pushed off the top of the screen.
1421 */
1422 static int
1423handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1424{
1425 term_T *term = (term_T *)user;
1426
1427 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001428 if (ga_grow(&term->tl_scrollback, 1) == OK)
1429 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001430 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001431 int len = 0;
1432 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001433 int c;
1434 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001435 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001436 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001437
1438 /* do not store empty cells at the end */
1439 for (i = 0; i < cols; ++i)
1440 if (cells[i].chars[0] != 0)
1441 len = i + 1;
1442
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001443 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001444 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001445 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001446 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001447 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001448 for (col = 0; col < len; col += cells[col].width)
1449 {
1450 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1451 {
1452 ga.ga_len = 0;
1453 break;
1454 }
1455 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1456 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1457 (char_u *)ga.ga_data + ga.ga_len);
1458 p[col].width = cells[col].width;
1459 p[col].attrs = cells[col].attrs;
1460 p[col].fg = cells[col].fg;
1461 p[col].bg = cells[col].bg;
1462 }
1463 }
1464 if (ga_grow(&ga, 1) == FAIL)
1465 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1466 else
1467 {
1468 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1469 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1470 }
1471 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001472
1473 line = (sb_line_T *)term->tl_scrollback.ga_data
1474 + term->tl_scrollback.ga_len;
1475 line->sb_cols = len;
1476 line->sb_cells = p;
1477 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001478 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001479 }
1480 return 0; /* ignored */
1481}
1482
Bram Moolenaar21554412017-07-24 21:44:43 +02001483static VTermScreenCallbacks screen_callbacks = {
1484 handle_damage, /* damage */
1485 handle_moverect, /* moverect */
1486 handle_movecursor, /* movecursor */
1487 handle_settermprop, /* settermprop */
1488 NULL, /* bell */
1489 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001490 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001491 NULL /* sb_popline */
1492};
1493
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001494/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001495 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001496 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001497 */
1498 void
1499term_channel_closed(channel_T *ch)
1500{
1501 term_T *term;
1502 int did_one = FALSE;
1503
1504 for (term = first_term; term != NULL; term = term->tl_next)
1505 if (term->tl_job == ch->ch_job)
1506 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001507 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001508 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001509
Bram Moolenaard85f2712017-07-28 21:51:57 +02001510 vim_free(term->tl_title);
1511 term->tl_title = NULL;
1512 vim_free(term->tl_status_text);
1513 term->tl_status_text = NULL;
1514
Bram Moolenaar423802d2017-07-30 16:52:24 +02001515 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001516 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001517 {
1518 int fnum = term->tl_buffer->b_fnum;
1519
Bram Moolenaar423802d2017-07-30 16:52:24 +02001520 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001521
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001522 if (term->tl_finish == 'c')
1523 {
1524 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001525 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001526 curbuf = term->tl_buffer;
1527 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1528 break;
1529 }
1530 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1531 {
1532 char buf[50];
1533
1534 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001535 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001536 vim_snprintf(buf, sizeof(buf), "botright sbuf %d", fnum);
1537 do_cmdline_cmd((char_u *)buf);
1538 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001539 else
1540 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001541 }
1542
Bram Moolenaard85f2712017-07-28 21:51:57 +02001543 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001544 }
1545 if (did_one)
1546 {
1547 redraw_statuslines();
1548
1549 /* Need to break out of vgetc(). */
1550 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001551 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001552
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001553 term = curbuf->b_term;
1554 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001555 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001556 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001557 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001558 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001559 }
1560 }
1561}
1562
1563/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001564 * Reverse engineer the RGB value into a cterm color index.
1565 * First color is 1. Return 0 if no match found.
1566 */
1567 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001568color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001569{
1570 int red = color->red;
1571 int blue = color->blue;
1572 int green = color->green;
1573
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001574 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001575 if (red == 0)
1576 {
1577 if (green == 0)
1578 {
1579 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001580 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001581 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001582 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001583 }
1584 else if (green == 224)
1585 {
1586 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001587 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001588 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001589 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001590 }
1591 }
1592 else if (red == 224)
1593 {
1594 if (green == 0)
1595 {
1596 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001597 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001598 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001599 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001600 }
1601 else if (green == 224)
1602 {
1603 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001604 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001605 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001606 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001607 }
1608 }
1609 else if (red == 128)
1610 {
1611 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001612 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001613 }
1614 else if (red == 255)
1615 {
1616 if (green == 64)
1617 {
1618 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001619 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001620 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001621 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001622 }
1623 else if (green == 255)
1624 {
1625 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001626 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001627 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001628 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001629 }
1630 }
1631 else if (red == 64)
1632 {
1633 if (green == 64)
1634 {
1635 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001636 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001637 }
1638 else if (green == 255)
1639 {
1640 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001641 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001642 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001643 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001644 }
1645 }
1646 if (t_colors >= 256)
1647 {
1648 if (red == blue && red == green)
1649 {
1650 /* 24-color greyscale */
1651 static int cutoff[23] = {
1652 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1653 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1654 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1655 int i;
1656
1657 for (i = 0; i < 23; ++i)
1658 if (red < cutoff[i])
1659 return i + 233;
1660 return 256;
1661 }
1662
1663 /* 216-color cube */
1664 return 17 + ((red + 25) / 0x33) * 36
1665 + ((green + 25) / 0x33) * 6
1666 + (blue + 25) / 0x33;
1667 }
1668 return 0;
1669}
1670
1671/*
1672 * Convert the attributes of a vterm cell into an attribute index.
1673 */
1674 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001675cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001676{
1677 int attr = 0;
1678
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001679 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001680 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001681 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001682 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001683 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001684 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001685 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001686 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001687 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001688 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001689
1690#ifdef FEAT_GUI
1691 if (gui.in_use)
1692 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001693 guicolor_T fg, bg;
1694
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001695 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1696 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001697 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001698 }
1699 else
1700#endif
1701#ifdef FEAT_TERMGUICOLORS
1702 if (p_tgc)
1703 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001704 guicolor_T fg, bg;
1705
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001706 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1707 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001708
1709 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001710 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001711 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001712#endif
1713 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001714 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001715 int fg = color2index(&cellfg, TRUE, &bold);
1716 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001717
1718 /* with 8 colors set the bold attribute to get a bright foreground */
1719 if (bold == TRUE)
1720 attr |= HL_BOLD;
1721 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001722 }
1723 return 0;
1724}
1725
1726/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001727 * Called to update a window that contains an active terminal.
1728 * Returns FAIL when there is no terminal running in this window or in
1729 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001730 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001731 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001732term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001733{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001734 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001735 VTerm *vterm;
1736 VTermScreen *screen;
1737 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001738 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001739
Bram Moolenaar6d819742017-08-06 14:57:49 +02001740 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001741 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001742
Bram Moolenaard85f2712017-07-28 21:51:57 +02001743 vterm = term->tl_vterm;
1744 screen = vterm_obtain_screen(vterm);
1745 state = vterm_obtain_state(vterm);
1746
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001747 /*
1748 * If the window was resized a redraw will be triggered and we get here.
1749 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1750 */
1751 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1752 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001753 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001754 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1755 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1756 win_T *twp;
1757
1758 FOR_ALL_WINDOWS(twp)
1759 {
1760 /* When more than one window shows the same terminal, use the
1761 * smallest size. */
1762 if (twp->w_buffer == term->tl_buffer)
1763 {
1764 if (!term->tl_rows_fixed && rows > twp->w_height)
1765 rows = twp->w_height;
1766 if (!term->tl_cols_fixed && cols > twp->w_width)
1767 cols = twp->w_width;
1768 }
1769 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001770
1771 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001772 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001773 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001774 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001775 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001776
1777 /* The cursor may have been moved when resizing. */
1778 vterm_state_get_cursorpos(state, &pos);
1779 position_cursor(wp, &pos);
1780
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001781 /* TODO: Only redraw what changed. */
1782 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001783 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001784 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001785 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001786
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001787 if (pos.row < term->tl_rows)
1788 {
1789 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001790 {
1791 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001792 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001793
Bram Moolenaareeac6772017-07-23 15:48:37 +02001794 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1795 vim_memset(&cell, 0, sizeof(cell));
1796
1797 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001798 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001799 if (c == NUL)
1800 {
1801 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001802#if defined(FEAT_MBYTE)
1803 if (enc_utf8)
1804 ScreenLinesUC[off] = NUL;
1805#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001806 }
1807 else
1808 {
1809#if defined(FEAT_MBYTE)
1810 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001811 {
1812 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001813 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001814 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001815 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001816 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001817 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001818 if (enc_utf8)
1819 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001820 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001821#else
1822 ScreenLines[off] = c;
1823#endif
1824 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001825 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001826
1827 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001828 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001829 if (cell.width == 2)
1830 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001831 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001832#if defined(FEAT_MBYTE)
1833 if (enc_utf8)
1834 ScreenLinesUC[off] = NUL;
1835#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001836 ++pos.col;
1837 ++off;
1838 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001839 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001840 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001841 else
1842 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001843
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001844 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1845 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001846 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001847
1848 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001849}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001850
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001851/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001852 * Return TRUE if "wp" is a terminal window where the job has finished.
1853 */
1854 int
1855term_is_finished(buf_T *buf)
1856{
1857 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1858}
1859
1860/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001861 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001862 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001863 */
1864 int
1865term_show_buffer(buf_T *buf)
1866{
1867 term_T *term = buf->b_term;
1868
Bram Moolenaar6d819742017-08-06 14:57:49 +02001869 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001870}
1871
1872/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001873 * The current buffer is going to be changed. If there is terminal
1874 * highlighting remove it now.
1875 */
1876 void
1877term_change_in_curbuf(void)
1878{
1879 term_T *term = curbuf->b_term;
1880
1881 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1882 {
1883 free_scrollback(term);
1884 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001885
1886 /* The buffer is now like a normal buffer, it cannot be easily
1887 * abandoned when changed. */
1888 set_string_option_direct((char_u *)"buftype", -1,
1889 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001890 }
1891}
1892
1893/*
1894 * Get the screen attribute for a position in the buffer.
1895 */
1896 int
1897term_get_attr(buf_T *buf, linenr_T lnum, int col)
1898{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001899 term_T *term = buf->b_term;
1900 sb_line_T *line;
1901 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001902
Bram Moolenaar70229f92017-07-29 16:01:53 +02001903 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001904 return 0;
1905 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1906 if (col >= line->sb_cols)
1907 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001908 cellattr = line->sb_cells + col;
1909 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001910}
1911
1912/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001913 * Create a new vterm and initialize it.
1914 */
1915 static void
1916create_vterm(term_T *term, int rows, int cols)
1917{
1918 VTerm *vterm;
1919 VTermScreen *screen;
1920
1921 vterm = vterm_new(rows, cols);
1922 term->tl_vterm = vterm;
1923 screen = vterm_obtain_screen(vterm);
1924 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1925 /* TODO: depends on 'encoding'. */
1926 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001927
1928 /* Vterm uses a default black background. Set it to white when
1929 * 'background' is "light". */
1930 if (*p_bg == 'l')
1931 {
1932 VTermColor fg, bg;
1933
1934 fg.red = fg.green = fg.blue = 0;
1935 bg.red = bg.green = bg.blue = 255;
1936 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1937 }
1938
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001939 /* Required to initialize most things. */
1940 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001941
1942 /* Allow using alternate screen. */
1943 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001944}
1945
Bram Moolenaar21554412017-07-24 21:44:43 +02001946/*
1947 * Return the text to show for the buffer name and status.
1948 */
1949 char_u *
1950term_get_status_text(term_T *term)
1951{
1952 if (term->tl_status_text == NULL)
1953 {
1954 char_u *txt;
1955 size_t len;
1956
Bram Moolenaar6d819742017-08-06 14:57:49 +02001957 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001958 {
1959 if (term_job_running(term))
1960 txt = (char_u *)_("Terminal");
1961 else
1962 txt = (char_u *)_("Terminal-finished");
1963 }
1964 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001965 txt = term->tl_title;
1966 else if (term_job_running(term))
1967 txt = (char_u *)_("running");
1968 else
1969 txt = (char_u *)_("finished");
1970 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001971 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001972 if (term->tl_status_text != NULL)
1973 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1974 term->tl_buffer->b_fname, txt);
1975 }
1976 return term->tl_status_text;
1977}
1978
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001979/*
1980 * Mark references in jobs of terminals.
1981 */
1982 int
1983set_ref_in_term(int copyID)
1984{
1985 int abort = FALSE;
1986 term_T *term;
1987 typval_T tv;
1988
1989 for (term = first_term; term != NULL; term = term->tl_next)
1990 if (term->tl_job != NULL)
1991 {
1992 tv.v_type = VAR_JOB;
1993 tv.vval.v_job = term->tl_job;
1994 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1995 }
1996 return abort;
1997}
1998
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001999/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002000 * Get the buffer from the first argument in "argvars".
2001 * Returns NULL when the buffer is not for a terminal window.
2002 */
2003 static buf_T *
2004term_get_buf(typval_T *argvars)
2005{
2006 buf_T *buf;
2007
2008 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2009 ++emsg_off;
2010 buf = get_buf_tv(&argvars[0], FALSE);
2011 --emsg_off;
2012 if (buf == NULL || buf->b_term == NULL)
2013 return NULL;
2014 return buf;
2015}
2016
2017/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002018 * "term_getaltscreen(buf)" function
2019 */
2020 void
2021f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2022{
2023 buf_T *buf = term_get_buf(argvars);
2024
2025 if (buf == NULL)
2026 return;
2027 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2028}
2029
2030/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002031 * "term_getattr(attr, name)" function
2032 */
2033 void
2034f_term_getattr(typval_T *argvars, typval_T *rettv)
2035{
2036 int attr;
2037 size_t i;
2038 char_u *name;
2039
2040 static struct {
2041 char *name;
2042 int attr;
2043 } attrs[] = {
2044 {"bold", HL_BOLD},
2045 {"italic", HL_ITALIC},
2046 {"underline", HL_UNDERLINE},
2047 {"strike", HL_STANDOUT},
2048 {"reverse", HL_INVERSE},
2049 };
2050
2051 attr = get_tv_number(&argvars[0]);
2052 name = get_tv_string_chk(&argvars[1]);
2053 if (name == NULL)
2054 return;
2055
2056 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2057 if (STRCMP(name, attrs[i].name) == 0)
2058 {
2059 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2060 break;
2061 }
2062}
2063
2064/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002065 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002066 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002067 void
2068f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002069{
Bram Moolenaar97870002017-07-30 18:28:38 +02002070 buf_T *buf = term_get_buf(argvars);
2071 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002072
Bram Moolenaar97870002017-07-30 18:28:38 +02002073 if (rettv_list_alloc(rettv) == FAIL)
2074 return;
2075 if (buf == NULL)
2076 return;
2077
2078 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002079 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
2080 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02002081 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002082}
2083
2084/*
2085 * "term_getjob(buf)" function
2086 */
2087 void
2088f_term_getjob(typval_T *argvars, typval_T *rettv)
2089{
2090 buf_T *buf = term_get_buf(argvars);
2091
2092 rettv->v_type = VAR_JOB;
2093 rettv->vval.v_job = NULL;
2094 if (buf == NULL)
2095 return;
2096
2097 rettv->vval.v_job = buf->b_term->tl_job;
2098 if (rettv->vval.v_job != NULL)
2099 ++rettv->vval.v_job->jv_refcount;
2100}
2101
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002102 static int
2103get_row_number(typval_T *tv, term_T *term)
2104{
2105 if (tv->v_type == VAR_STRING
2106 && tv->vval.v_string != NULL
2107 && STRCMP(tv->vval.v_string, ".") == 0)
2108 return term->tl_cursor_pos.row;
2109 return (int)get_tv_number(tv) - 1;
2110}
2111
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002112/*
2113 * "term_getline(buf, row)" function
2114 */
2115 void
2116f_term_getline(typval_T *argvars, typval_T *rettv)
2117{
2118 buf_T *buf = term_get_buf(argvars);
2119 term_T *term;
2120 int row;
2121
2122 rettv->v_type = VAR_STRING;
2123 if (buf == NULL)
2124 return;
2125 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002126 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002127
2128 if (term->tl_vterm == NULL)
2129 {
2130 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2131
2132 /* vterm is finished, get the text from the buffer */
2133 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2134 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2135 }
2136 else
2137 {
2138 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2139 VTermRect rect;
2140 int len;
2141 char_u *p;
2142
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002143 if (row < 0 || row >= term->tl_rows)
2144 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002145 len = term->tl_cols * MB_MAXBYTES + 1;
2146 p = alloc(len);
2147 if (p == NULL)
2148 return;
2149 rettv->vval.v_string = p;
2150
2151 rect.start_col = 0;
2152 rect.end_col = term->tl_cols;
2153 rect.start_row = row;
2154 rect.end_row = row + 1;
2155 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2156 }
2157}
2158
2159/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002160 * "term_getscrolled(buf)" function
2161 */
2162 void
2163f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2164{
2165 buf_T *buf = term_get_buf(argvars);
2166
2167 if (buf == NULL)
2168 return;
2169 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2170}
2171
2172/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002173 * "term_getsize(buf)" function
2174 */
2175 void
2176f_term_getsize(typval_T *argvars, typval_T *rettv)
2177{
2178 buf_T *buf = term_get_buf(argvars);
2179 list_T *l;
2180
2181 if (rettv_list_alloc(rettv) == FAIL)
2182 return;
2183 if (buf == NULL)
2184 return;
2185
2186 l = rettv->vval.v_list;
2187 list_append_number(l, buf->b_term->tl_rows);
2188 list_append_number(l, buf->b_term->tl_cols);
2189}
2190
2191/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002192 * "term_getstatus(buf)" function
2193 */
2194 void
2195f_term_getstatus(typval_T *argvars, typval_T *rettv)
2196{
2197 buf_T *buf = term_get_buf(argvars);
2198 term_T *term;
2199 char_u val[100];
2200
2201 rettv->v_type = VAR_STRING;
2202 if (buf == NULL)
2203 return;
2204 term = buf->b_term;
2205
2206 if (term_job_running(term))
2207 STRCPY(val, "running");
2208 else
2209 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002210 if (term->tl_normal_mode)
2211 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002212 rettv->vval.v_string = vim_strsave(val);
2213}
2214
2215/*
2216 * "term_gettitle(buf)" function
2217 */
2218 void
2219f_term_gettitle(typval_T *argvars, typval_T *rettv)
2220{
2221 buf_T *buf = term_get_buf(argvars);
2222
2223 rettv->v_type = VAR_STRING;
2224 if (buf == NULL)
2225 return;
2226
2227 if (buf->b_term->tl_title != NULL)
2228 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2229}
2230
2231/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002232 * "term_gettty(buf)" function
2233 */
2234 void
2235f_term_gettty(typval_T *argvars, typval_T *rettv)
2236{
2237 buf_T *buf = term_get_buf(argvars);
2238 char_u *p;
2239
2240 rettv->v_type = VAR_STRING;
2241 if (buf == NULL)
2242 return;
2243 if (buf->b_term->tl_job != NULL)
2244 p = buf->b_term->tl_job->jv_tty_name;
2245 else
2246 p = buf->b_term->tl_tty_name;
2247 if (p != NULL)
2248 rettv->vval.v_string = vim_strsave(p);
2249}
2250
2251/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002252 * "term_list()" function
2253 */
2254 void
2255f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2256{
2257 term_T *tp;
2258 list_T *l;
2259
2260 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2261 return;
2262
2263 l = rettv->vval.v_list;
2264 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2265 if (tp != NULL && tp->tl_buffer != NULL)
2266 if (list_append_number(l,
2267 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2268 return;
2269}
2270
2271/*
2272 * "term_scrape(buf, row)" function
2273 */
2274 void
2275f_term_scrape(typval_T *argvars, typval_T *rettv)
2276{
2277 buf_T *buf = term_get_buf(argvars);
2278 VTermScreen *screen = NULL;
2279 VTermPos pos;
2280 list_T *l;
2281 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002282 char_u *p;
2283 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002284
2285 if (rettv_list_alloc(rettv) == FAIL)
2286 return;
2287 if (buf == NULL)
2288 return;
2289 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002290
2291 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002292 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002293
2294 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002295 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002296 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002297 p = NULL;
2298 line = NULL;
2299 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002300 else
2301 {
2302 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2303
2304 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2305 return;
2306 p = ml_get_buf(buf, lnum + 1, FALSE);
2307 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2308 }
2309
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002310 for (pos.col = 0; pos.col < term->tl_cols; )
2311 {
2312 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002313 int width;
2314 VTermScreenCellAttrs attrs;
2315 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002316 char_u rgb[8];
2317 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2318 int off = 0;
2319 int i;
2320
2321 if (screen == NULL)
2322 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002323 cellattr_T *cellattr;
2324 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002325
2326 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002327 if (pos.col >= line->sb_cols)
2328 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002329 cellattr = line->sb_cells + pos.col;
2330 width = cellattr->width;
2331 attrs = cellattr->attrs;
2332 fg = cellattr->fg;
2333 bg = cellattr->bg;
2334 len = MB_PTR2LEN(p);
2335 mch_memmove(mbs, p, len);
2336 mbs[len] = NUL;
2337 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002338 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002339 else
2340 {
2341 VTermScreenCell cell;
2342 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2343 break;
2344 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2345 {
2346 if (cell.chars[i] == 0)
2347 break;
2348 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2349 }
2350 mbs[off] = NUL;
2351 width = cell.width;
2352 attrs = cell.attrs;
2353 fg = cell.fg;
2354 bg = cell.bg;
2355 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002356 dcell = dict_alloc();
2357 list_append_dict(l, dcell);
2358
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002359 dict_add_nr_str(dcell, "chars", 0, mbs);
2360
2361 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002362 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002363 dict_add_nr_str(dcell, "fg", 0, rgb);
2364 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002365 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002366 dict_add_nr_str(dcell, "bg", 0, rgb);
2367
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002368 dict_add_nr_str(dcell, "attr",
2369 cell2attr(attrs, fg, bg), NULL);
2370 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002371
2372 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002373 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002374 ++pos.col;
2375 }
2376}
2377
2378/*
2379 * "term_sendkeys(buf, keys)" function
2380 */
2381 void
2382f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2383{
2384 buf_T *buf = term_get_buf(argvars);
2385 char_u *msg;
2386 term_T *term;
2387
2388 rettv->v_type = VAR_UNKNOWN;
2389 if (buf == NULL)
2390 return;
2391
2392 msg = get_tv_string_chk(&argvars[1]);
2393 if (msg == NULL)
2394 return;
2395 term = buf->b_term;
2396 if (term->tl_vterm == NULL)
2397 return;
2398
2399 while (*msg != NUL)
2400 {
2401 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2402 msg += MB_PTR2LEN(msg);
2403 }
2404
Bram Moolenaar6d819742017-08-06 14:57:49 +02002405 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002406 {
2407 /* TODO: only update once in a while. */
2408 update_screen(0);
2409 if (buf == curbuf)
2410 update_cursor(term, TRUE);
2411 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002412}
2413
2414/*
2415 * "term_start(command, options)" function
2416 */
2417 void
2418f_term_start(typval_T *argvars, typval_T *rettv)
2419{
2420 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002421 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002422
2423 if (cmd == NULL)
2424 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002425 init_job_options(&opt);
2426 /* TODO: allow more job options */
2427 if (argvars[1].v_type != VAR_UNKNOWN
2428 && get_job_options(&argvars[1], &opt,
2429 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002430 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002431 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN
Bram Moolenaarda43b612017-08-11 22:27:50 +02002432 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002433 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002434 return;
2435
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002436 if (opt.jo_vertical)
2437 cmdmod.split = WSP_VERT;
Bram Moolenaarda43b612017-08-11 22:27:50 +02002438 term_start(cmd, &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002439
2440 if (curbuf->b_term != NULL)
2441 rettv->vval.v_number = curbuf->b_fnum;
2442}
2443
2444/*
2445 * "term_wait" function
2446 */
2447 void
2448f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2449{
2450 buf_T *buf = term_get_buf(argvars);
2451
2452 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002453 {
2454 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002455 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002456 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002457 if (buf->b_term->tl_job == NULL)
2458 {
2459 ch_log(NULL, "term_wait(): no job to wait for");
2460 return;
2461 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002462
2463 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002464 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002465 {
2466 /* The job is dead, keep reading channel I/O until the channel is
2467 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002468 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002469 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2470 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002471 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002472 parse_queued_messages();
2473 ui_delay(10L, FALSE);
2474 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002475 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002476 parse_queued_messages();
2477 }
2478 else
2479 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002480 long wait = 10L;
2481
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002482 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002483 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002484
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002485 /* Wait for some time for any channel I/O. */
2486 if (argvars[1].v_type != VAR_UNKNOWN)
2487 wait = get_tv_number(&argvars[1]);
2488 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002489 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002490
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002491 /* Flushing messages on channels is hopefully sufficient.
2492 * TODO: is there a better way? */
2493 parse_queued_messages();
2494 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002495}
2496
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002497# ifdef WIN3264
2498
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002499/**************************************
2500 * 2. MS-Windows implementation.
2501 */
2502
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002503#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2504#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2505
Bram Moolenaar8a773062017-07-24 22:29:21 +02002506void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002507void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002508void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002509BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2510void (*winpty_config_set_initial_size)(void*, int, int);
2511LPCWSTR (*winpty_conin_name)(void*);
2512LPCWSTR (*winpty_conout_name)(void*);
2513LPCWSTR (*winpty_conerr_name)(void*);
2514void (*winpty_free)(void*);
2515void (*winpty_config_free)(void*);
2516void (*winpty_spawn_config_free)(void*);
2517void (*winpty_error_free)(void*);
2518LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002519BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002520HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002521
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002522#define WINPTY_DLL "winpty.dll"
2523
2524static HINSTANCE hWinPtyDLL = NULL;
2525
2526 int
2527dyn_winpty_init(void)
2528{
2529 int i;
2530 static struct
2531 {
2532 char *name;
2533 FARPROC *ptr;
2534 } winpty_entry[] =
2535 {
2536 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2537 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2538 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2539 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2540 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2541 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2542 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2543 {"winpty_free", (FARPROC*)&winpty_free},
2544 {"winpty_open", (FARPROC*)&winpty_open},
2545 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2546 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2547 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2548 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002549 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002550 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002551 {NULL, NULL}
2552 };
2553
2554 /* No need to initialize twice. */
2555 if (hWinPtyDLL)
2556 return 1;
2557 /* Load winpty.dll */
2558 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2559 if (!hWinPtyDLL)
2560 {
2561 EMSG2(_(e_loadlib), WINPTY_DLL);
2562 return 0;
2563 }
2564 for (i = 0; winpty_entry[i].name != NULL
2565 && winpty_entry[i].ptr != NULL; ++i)
2566 {
2567 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2568 winpty_entry[i].name)) == NULL)
2569 {
2570 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2571 return 0;
2572 }
2573 }
2574
2575 return 1;
2576}
2577
2578/*
2579 * Create a new terminal of "rows" by "cols" cells.
2580 * Store a reference in "term".
2581 * Return OK or FAIL.
2582 */
2583 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002584term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002585{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002586 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002587 channel_T *channel = NULL;
2588 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002589 DWORD error;
2590 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2591 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002592 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002593 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002594
2595 if (!dyn_winpty_init())
2596 return FAIL;
2597
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002598 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002599 if (p == NULL)
2600 return FAIL;
2601
2602 job = job_alloc();
2603 if (job == NULL)
2604 goto failed;
2605
2606 channel = add_channel();
2607 if (channel == NULL)
2608 goto failed;
2609
2610 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2611 if (term->tl_winpty_config == NULL)
2612 goto failed;
2613
2614 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2615 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2616 if (term->tl_winpty == NULL)
2617 goto failed;
2618
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002619 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002620 spawn_config = winpty_spawn_config_new(
2621 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2622 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2623 NULL,
2624 p,
2625 NULL,
2626 NULL,
2627 &winpty_err);
2628 if (spawn_config == NULL)
2629 goto failed;
2630
2631 channel = add_channel();
2632 if (channel == NULL)
2633 goto failed;
2634
2635 job = job_alloc();
2636 if (job == NULL)
2637 goto failed;
2638
2639 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2640 &child_thread_handle, &error, &winpty_err))
2641 goto failed;
2642
2643 channel_set_pipes(channel,
2644 (sock_T) CreateFileW(
2645 winpty_conin_name(term->tl_winpty),
2646 GENERIC_WRITE, 0, NULL,
2647 OPEN_EXISTING, 0, NULL),
2648 (sock_T) CreateFileW(
2649 winpty_conout_name(term->tl_winpty),
2650 GENERIC_READ, 0, NULL,
2651 OPEN_EXISTING, 0, NULL),
2652 (sock_T) CreateFileW(
2653 winpty_conerr_name(term->tl_winpty),
2654 GENERIC_READ, 0, NULL,
2655 OPEN_EXISTING, 0, NULL));
2656
2657 jo = CreateJobObject(NULL, NULL);
2658 if (jo == NULL)
2659 goto failed;
2660
2661 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002662 {
2663 /* Failed, switch the way to terminate process with TerminateProcess. */
2664 CloseHandle(jo);
2665 jo = NULL;
2666 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002667
2668 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002669 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002670
2671 create_vterm(term, rows, cols);
2672
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002673 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002674 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002675
2676 job->jv_channel = channel;
2677 job->jv_proc_info.hProcess = child_process_handle;
2678 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2679 job->jv_job_object = jo;
2680 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002681 sprintf(buf, "winpty://%lu",
2682 GetProcessId(winpty_agent_process(term->tl_winpty)));
2683 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002684 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002685 term->tl_job = job;
2686
2687 return OK;
2688
2689failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002690 if (spawn_config != NULL)
2691 winpty_spawn_config_free(spawn_config);
2692 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002693 if (channel != NULL)
2694 channel_clear(channel);
2695 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002696 {
2697 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002698 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002699 }
2700 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002701 if (jo != NULL)
2702 CloseHandle(jo);
2703 if (term->tl_winpty != NULL)
2704 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002705 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002706 if (term->tl_winpty_config != NULL)
2707 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002708 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002709 if (winpty_err != NULL)
2710 {
2711 char_u *msg = utf16_to_enc(
2712 (short_u *)winpty_error_msg(winpty_err), NULL);
2713
2714 EMSG(msg);
2715 winpty_error_free(winpty_err);
2716 }
2717 return FAIL;
2718}
2719
2720/*
2721 * Free the terminal emulator part of "term".
2722 */
2723 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002724term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002725{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002726 if (term->tl_winpty != NULL)
2727 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002728 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002729 if (term->tl_winpty_config != NULL)
2730 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002731 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002732 if (term->tl_vterm != NULL)
2733 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002734 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002735}
2736
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002737/*
2738 * Request size to terminal.
2739 */
2740 static void
2741term_report_winsize(term_T *term, int rows, int cols)
2742{
2743 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2744}
2745
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002746# else
2747
2748/**************************************
2749 * 3. Unix-like implementation.
2750 */
2751
2752/*
2753 * Create a new terminal of "rows" by "cols" cells.
2754 * Start job for "cmd".
2755 * Store the pointers in "term".
2756 * Return OK or FAIL.
2757 */
2758 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002759term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002760{
2761 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002762
2763 create_vterm(term, rows, cols);
2764
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002765 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002766 argvars[0].v_type = VAR_STRING;
2767 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002768
2769 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002770 if (term->tl_job != NULL)
2771 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002772
Bram Moolenaar61a66052017-07-22 18:39:00 +02002773 return term->tl_job != NULL
2774 && term->tl_job->jv_channel != NULL
2775 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002776}
2777
2778/*
2779 * Free the terminal emulator part of "term".
2780 */
2781 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002782term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002783{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002784 if (term->tl_vterm != NULL)
2785 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002786 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002787}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002788
2789/*
2790 * Request size to terminal.
2791 */
2792 static void
2793term_report_winsize(term_T *term, int rows, int cols)
2794{
2795 /* Use an ioctl() to report the new window size to the job. */
2796 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2797 {
2798 int fd = -1;
2799 int part;
2800
2801 for (part = PART_OUT; part < PART_COUNT; ++part)
2802 {
2803 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2804 if (isatty(fd))
2805 break;
2806 }
2807 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002808 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002809 }
2810}
2811
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002812# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002813
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002814#endif /* FEAT_TERMINAL */