blob: 0f5382bacd2e892b131e47277475eaed0dc70564 [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 Moolenaard8dc1792017-08-03 11:55:21 +020039 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar423802d2017-07-30 16:52:24 +020040 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020041 * - add argument to term_wait() for waiting time.
Bram Moolenaard85f2712017-07-28 21:51:57 +020042 * - For the scrollback buffer store lines in the buffer, only attributes in
43 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020044 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020045 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020046 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
47 * job finishes).
48 * - add option values to the command:
49 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020050 * - support different cursor shapes, colors and attributes
51 * - make term_getcursor() return type (none/block/bar/underline) and
52 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020053 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020054 * For the GUI fill termios with default values, perhaps like pangoterm:
55 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
56 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020057 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020058 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020059 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020060 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020061 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020062 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
63 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020064 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020066 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020067 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020068 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020069 * terminal. Allow:
70 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
71 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
72 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
73 * Check that something is connected to the terminal.
74 * Test: "cat" reading from a file or buffer
75 * "ls" writing stdout to a file or buffer
76 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020077 * - support ":term NONE" to open a terminal with a pty but not running a job
78 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020079 * - if the job in the terminal does not support the mouse, we can use the
80 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020081 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
82 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020083 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020084 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020085 * - Copy text in the vterm to the Vim buffer once in a while, so that
86 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020087 */
88
89#include "vim.h"
90
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020091#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092
Bram Moolenaard5310982017-08-05 15:16:32 +020093#ifndef MIN
94# define MIN(x,y) ((x) < (y) ? (x) : (y))
95#endif
96#ifndef MAX
97# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020098#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020099
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200100#include "libvterm/include/vterm.h"
101
Bram Moolenaard85f2712017-07-28 21:51:57 +0200102typedef struct sb_line_S {
103 int sb_cols; /* can differ per line */
104 VTermScreenCell *sb_cells; /* allocated */
105} 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 Moolenaar423802d2017-07-30 16:52:24 +0200119 int tl_terminal_mode;
120 int tl_channel_closed;
121
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200122#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200123 void *tl_winpty_config;
124 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200125#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200127 /* last known vterm size */
128 int tl_rows;
129 int tl_cols;
130 /* vterm size does not follow window size */
131 int tl_rows_fixed;
132 int tl_cols_fixed;
133
Bram Moolenaar21554412017-07-24 21:44:43 +0200134 char_u *tl_title; /* NULL or allocated */
135 char_u *tl_status_text; /* NULL or allocated */
136
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200137 /* Range of screen rows to update. Zero based. */
138 int tl_dirty_row_start; /* -1 if nothing dirty */
139 int tl_dirty_row_end; /* row below last one to update */
140
Bram Moolenaard85f2712017-07-28 21:51:57 +0200141 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200142 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200143
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200144 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200145 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200146};
147
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200148/*
149 * List of all active terminals.
150 */
151static term_T *first_term = NULL;
152
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200153
154#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
155#define KEY_BUF_LEN 200
156
157/*
158 * Functions with separate implementation for MS-Windows and Unix-like systems.
159 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200160static 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 +0200161static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200162static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200163
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200164/**************************************
165 * 1. Generic code for all systems.
166 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200167
168/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200169 * Determine the terminal size from 'termsize' and the current window.
170 * Assumes term->tl_rows and term->tl_cols are zero.
171 */
172 static void
173set_term_and_win_size(term_T *term)
174{
175 if (*curwin->w_p_tms != NUL)
176 {
177 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
178
179 term->tl_rows = atoi((char *)curwin->w_p_tms);
180 term->tl_cols = atoi((char *)p);
181 }
182 if (term->tl_rows == 0)
183 term->tl_rows = curwin->w_height;
184 else
185 {
186 win_setheight_win(term->tl_rows, curwin);
187 term->tl_rows_fixed = TRUE;
188 }
189 if (term->tl_cols == 0)
190 term->tl_cols = curwin->w_width;
191 else
192 {
193 win_setwidth_win(term->tl_cols, curwin);
194 term->tl_cols_fixed = TRUE;
195 }
196}
197
198/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200199 * Initialize job options for a terminal job.
200 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200201 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200202 static void
203init_job_options(jobopt_T *opt)
204{
205 clear_job_options(opt);
206
207 opt->jo_mode = MODE_RAW;
208 opt->jo_out_mode = MODE_RAW;
209 opt->jo_err_mode = MODE_RAW;
210 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
211
212 opt->jo_io[PART_OUT] = JIO_BUFFER;
213 opt->jo_io[PART_ERR] = JIO_BUFFER;
214 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
215
216 opt->jo_modifiable[PART_OUT] = 0;
217 opt->jo_modifiable[PART_ERR] = 0;
218 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
219
220 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
221}
222
223/*
224 * Set job options mandatory for a terminal job.
225 */
226 static void
227setup_job_options(jobopt_T *opt, int rows, int cols)
228{
229 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
230 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
231 opt->jo_pty = TRUE;
232 opt->jo_term_rows = rows;
233 opt->jo_term_cols = cols;
234}
235
236 static void
237term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200238{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200239 exarg_T split_ea;
240 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200241 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200242
243 if (check_restricted() || check_secure())
244 return;
245
246 term = (term_T *)alloc_clear(sizeof(term_T));
247 if (term == NULL)
248 return;
249 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200250 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200251 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252
253 /* Open a new window or tab. */
254 vim_memset(&split_ea, 0, sizeof(split_ea));
255 split_ea.cmdidx = CMD_new;
256 split_ea.cmd = (char_u *)"new";
257 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200258 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
259 {
260 split_ea.line2 = opt->jo_term_rows;
261 split_ea.addr_count = 1;
262 }
263 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
264 {
265 split_ea.line2 = opt->jo_term_cols;
266 split_ea.addr_count = 1;
267 }
268
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200269 ex_splitview(&split_ea);
270 if (curwin == old_curwin)
271 {
272 /* split failed */
273 vim_free(term);
274 return;
275 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200276 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200277 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200278
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200279 /* only one size was taken care of with :new, do the other one */
280 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
281 win_setheight(opt->jo_term_rows);
282 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
283 win_setwidth(opt->jo_term_cols);
284
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200285 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200286 term->tl_next = first_term;
287 first_term = term;
288
Bram Moolenaar293424c2017-07-26 23:11:01 +0200289 if (cmd == NULL || *cmd == NUL)
290 cmd = p_sh;
291
Bram Moolenaar78712a72017-08-05 14:50:12 +0200292 if (opt->jo_term_name != NULL)
293 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
294 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200295 {
296 int i;
297 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200298 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200299
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200300 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200301 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200302 /* Prepend a ! to the command name to avoid the buffer name equals
303 * the executable, otherwise ":w!" would overwrite it. */
304 if (i == 0)
305 vim_snprintf((char *)p, len, "!%s", cmd);
306 else
307 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200308 if (buflist_findname(p) == NULL)
309 {
310 curbuf->b_ffname = p;
311 break;
312 }
313 }
314 }
315 curbuf->b_fname = curbuf->b_ffname;
316
Bram Moolenaareb44a682017-08-03 22:44:55 +0200317 set_string_option_direct((char_u *)"buftype", -1,
318 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
319
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200320 /* Mark the buffer as not modifiable. It can only be made modifiable after
321 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200322 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200323
324 /* Set 'bufhidden' to "hide": allow closing the window. */
325 set_string_option_direct((char_u *)"bufhidden", -1,
326 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200327
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200328 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200329 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200330
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200331 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200332 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200333 {
334 /* store the size we ended up with */
335 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200336 }
337 else
338 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200339 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200340
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200341 /* Wiping out the buffer will also close the window and call
342 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200343 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200344 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200345}
346
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200347/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200348 * ":terminal": open a terminal window and execute a job in it.
349 */
350 void
351ex_terminal(exarg_T *eap)
352{
353 jobopt_T opt;
354
355 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200356
357 if (eap->addr_count == 2)
358 {
359 opt.jo_term_rows = eap->line1;
360 opt.jo_term_cols = eap->line2;
361 }
362 else if (eap->addr_count == 1)
363 {
364 if (cmdmod.split & WSP_VERT)
365 opt.jo_term_cols = eap->line2;
366 else
367 opt.jo_term_rows = eap->line2;
368 }
369 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200370
371 term_start(eap->arg, &opt);
372}
373
374/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200375 * Free the scrollback buffer for "term".
376 */
377 static void
378free_scrollback(term_T *term)
379{
380 int i;
381
382 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
383 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
384 ga_clear(&term->tl_scrollback);
385}
386
387/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200388 * Free a terminal and everything it refers to.
389 * Kills the job if there is one.
390 * Called when wiping out a buffer.
391 */
392 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200393free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200394{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200395 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200396 term_T *tp;
397
398 if (term == NULL)
399 return;
400 if (first_term == term)
401 first_term = term->tl_next;
402 else
403 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
404 if (tp->tl_next == term)
405 {
406 tp->tl_next = term->tl_next;
407 break;
408 }
409
410 if (term->tl_job != NULL)
411 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200412 if (term->tl_job->jv_status != JOB_ENDED
413 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200414 job_stop(term->tl_job, NULL, "kill");
415 job_unref(term->tl_job);
416 }
417
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200418 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200419
420 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200421 vim_free(term->tl_title);
422 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200423 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200424 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200425}
426
427/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200428 * Write job output "msg[len]" to the vterm.
429 */
430 static void
431term_write_job_output(term_T *term, char_u *msg, size_t len)
432{
433 VTerm *vterm = term->tl_vterm;
434 char_u *p;
435 size_t done;
436 size_t len_now;
437
438 for (done = 0; done < len; done += len_now)
439 {
440 for (p = msg + done; p < msg + len; )
441 {
442 if (*p == NL)
443 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200444 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200445 }
446 len_now = p - msg - done;
447 vterm_input_write(vterm, (char *)msg + done, len_now);
448 if (p < msg + len && *p == NL)
449 {
450 /* Convert NL to CR-NL, that appears to work best. */
451 vterm_input_write(vterm, "\r\n", 2);
452 ++len_now;
453 }
454 }
455
456 /* this invokes the damage callbacks */
457 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
458}
459
Bram Moolenaar1c844932017-07-24 23:36:41 +0200460 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200461update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200462{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200463 if (term->tl_terminal_mode)
464 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200465 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200466 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200467 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200468 if (term->tl_cursor_visible)
469 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200470 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200471#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200472 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200473 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200474#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200475 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200476}
477
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200478/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200479 * Invoked when "msg" output from a job was received. Write it to the terminal
480 * of "buffer".
481 */
482 void
483write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
484{
485 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200486 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200487
Bram Moolenaard85f2712017-07-28 21:51:57 +0200488 if (term->tl_vterm == NULL)
489 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200490 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200491 return;
492 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200493 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200494 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200495
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200496 if (!term->tl_terminal_mode)
497 {
498 /* TODO: only update once in a while. */
499 update_screen(0);
500 update_cursor(term, TRUE);
501 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200502}
503
504/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200505 * Send a mouse position and click to the vterm
506 */
507 static int
508term_send_mouse(VTerm *vterm, int button, int pressed)
509{
510 VTermModifier mod = VTERM_MOD_NONE;
511
512 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
513 mouse_col - W_WINCOL(curwin), mod);
514 vterm_mouse_button(vterm, button, pressed, mod);
515 return TRUE;
516}
517
518/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200519 * Convert typed key "c" into bytes to send to the job.
520 * Return the number of bytes in "buf".
521 */
522 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200523term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200524{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200525 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200526 VTermKey key = VTERM_KEY_NONE;
527 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200528 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200529
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200530 switch (c)
531 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200532 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200533 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200534 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
535 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200536 case K_DEL: key = VTERM_KEY_DEL; break;
537 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200538 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
539 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200540 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200541 case K_S_END: mod = VTERM_MOD_SHIFT;
542 key = VTERM_KEY_END; break;
543 case K_C_END: mod = VTERM_MOD_CTRL;
544 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200545 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
546 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
547 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
548 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
549 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
550 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
551 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
552 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
553 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
554 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
555 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
556 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
557 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200558 case K_S_HOME: mod = VTERM_MOD_SHIFT;
559 key = VTERM_KEY_HOME; break;
560 case K_C_HOME: mod = VTERM_MOD_CTRL;
561 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200562 case K_INS: key = VTERM_KEY_INS; break;
563 case K_K0: key = VTERM_KEY_KP_0; break;
564 case K_K1: key = VTERM_KEY_KP_1; break;
565 case K_K2: key = VTERM_KEY_KP_2; break;
566 case K_K3: key = VTERM_KEY_KP_3; break;
567 case K_K4: key = VTERM_KEY_KP_4; break;
568 case K_K5: key = VTERM_KEY_KP_5; break;
569 case K_K6: key = VTERM_KEY_KP_6; break;
570 case K_K7: key = VTERM_KEY_KP_7; break;
571 case K_K8: key = VTERM_KEY_KP_8; break;
572 case K_K9: key = VTERM_KEY_KP_9; break;
573 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
574 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
575 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
576 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
577 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
578 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
579 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
580 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
581 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
582 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
583 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
584 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
585 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200586 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
587 key = VTERM_KEY_LEFT; break;
588 case K_C_LEFT: mod = VTERM_MOD_CTRL;
589 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200590 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
591 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
592 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200593 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
594 key = VTERM_KEY_RIGHT; break;
595 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
596 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200597 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200598 case K_S_UP: mod = VTERM_MOD_SHIFT;
599 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200600 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200601
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200602 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
603 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
604 case K_MOUSELEFT: /* TODO */ return 0;
605 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200606
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200607 case K_LEFTMOUSE:
608 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
609 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
610 case K_LEFTRELEASE:
611 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
612 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
613 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
614 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
615 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
616 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
617 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
618 case K_X1MOUSE: /* TODO */ return 0;
619 case K_X1DRAG: /* TODO */ return 0;
620 case K_X1RELEASE: /* TODO */ return 0;
621 case K_X2MOUSE: /* TODO */ return 0;
622 case K_X2DRAG: /* TODO */ return 0;
623 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200624
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200625 case K_IGNORE: return 0;
626 case K_NOP: return 0;
627 case K_UNDO: return 0;
628 case K_HELP: return 0;
629 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
630 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
631 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
632 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
633 case K_SELECT: return 0;
634#ifdef FEAT_GUI
635 case K_VER_SCROLLBAR: return 0;
636 case K_HOR_SCROLLBAR: return 0;
637#endif
638#ifdef FEAT_GUI_TABLINE
639 case K_TABLINE: return 0;
640 case K_TABMENU: return 0;
641#endif
642#ifdef FEAT_NETBEANS_INTG
643 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
644#endif
645#ifdef FEAT_DND
646 case K_DROP: return 0;
647#endif
648#ifdef FEAT_AUTOCMD
649 case K_CURSORHOLD: return 0;
650#endif
651 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
652 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200653 }
654
655 /*
656 * Convert special keys to vterm keys:
657 * - Write keys to vterm: vterm_keyboard_key()
658 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200659 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200660 */
661 if (key != VTERM_KEY_NONE)
662 /* Special key, let vterm convert it. */
663 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200664 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200665 /* Normal character, let vterm convert it. */
666 vterm_keyboard_unichar(vterm, c, mod);
667
668 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200669 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200670}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200671
Bram Moolenaar938783d2017-07-16 20:13:26 +0200672/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200673 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200674 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200675 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200676term_job_running(term_T *term)
677{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200678 /* Also consider the job finished when the channel is closed, to avoid a
679 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200680 return term != NULL
681 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200682 && term->tl_job->jv_status == JOB_STARTED
683 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200684}
685
686/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200687 * Add the last line of the scrollback buffer to the buffer in the window.
688 */
689 static void
690add_scrollback_line_to_buffer(term_T *term)
691{
692 linenr_T lnum = term->tl_scrollback.ga_len - 1;
693 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
694 garray_T ga;
695 int c;
696 int col;
697 int i;
698
699 ga_init2(&ga, 1, 100);
700 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
701 {
702 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
703 goto failed;
704 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
705 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
706 (char_u *)ga.ga_data + ga.ga_len);
707 }
708 if (ga_grow(&ga, 1) == FAIL)
709 goto failed;
710 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
711 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
712
713 if (lnum == 0)
714 {
715 /* Delete the empty line that was in the empty buffer. */
716 curbuf = term->tl_buffer;
717 ml_delete(2, FALSE);
718 curbuf = curwin->w_buffer;
719 }
720
721failed:
722 ga_clear(&ga);
723}
724
725/*
726 * Add the current lines of the terminal to scrollback and to the buffer.
727 * Called after the job has ended and when switching to Terminal mode.
728 */
729 static void
730move_terminal_to_buffer(term_T *term)
731{
732 win_T *wp;
733 int len;
734 int lines_skipped = 0;
735 VTermPos pos;
736 VTermScreenCell cell;
737 VTermScreenCell *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200738 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200739
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200740 if (term->tl_vterm == NULL)
741 return;
742 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200743 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
744 {
745 len = 0;
746 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
747 if (vterm_screen_get_cell(screen, pos, &cell) != 0
748 && cell.chars[0] != NUL)
749 len = pos.col + 1;
750
751 if (len == 0)
752 ++lines_skipped;
753 else
754 {
755 while (lines_skipped > 0)
756 {
757 /* Line was skipped, add an empty line. */
758 --lines_skipped;
759 if (ga_grow(&term->tl_scrollback, 1) == OK)
760 {
761 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
762 + term->tl_scrollback.ga_len;
763
764 line->sb_cols = 0;
765 line->sb_cells = NULL;
766 ++term->tl_scrollback.ga_len;
767
768 add_scrollback_line_to_buffer(term);
769 }
770 }
771
772 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
773 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
774 {
775 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
776 + term->tl_scrollback.ga_len;
777
778 for (pos.col = 0; pos.col < len; ++pos.col)
779 {
780 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
781 vim_memset(p + pos.col, 0, sizeof(cell));
782 else
783 p[pos.col] = cell;
784 }
785 line->sb_cols = len;
786 line->sb_cells = p;
787 ++term->tl_scrollback.ga_len;
788
789 add_scrollback_line_to_buffer(term);
790 }
791 else
792 vim_free(p);
793 }
794 }
795
796 FOR_ALL_WINDOWS(wp)
797 {
798 if (wp->w_buffer == term->tl_buffer)
799 {
800 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
801 wp->w_cursor.col = 0;
802 wp->w_valid = 0;
803 redraw_win_later(wp, NOT_VALID);
804 }
805 }
806}
807
808 static void
809set_terminal_mode(term_T *term, int on)
810{
811 term->tl_terminal_mode = on;
812 vim_free(term->tl_status_text);
813 term->tl_status_text = NULL;
814 if (term->tl_buffer == curbuf)
815 maketitle();
816}
817
818/*
819 * Called after the job if finished and Terminal mode is not active:
820 * Move the vterm contents into the scrollback buffer and free the vterm.
821 */
822 static void
823cleanup_vterm(term_T *term)
824{
825 move_terminal_to_buffer(term);
826 term_free_vterm(term);
827 set_terminal_mode(term, FALSE);
828}
829
830/*
831 * Switch from sending keys to the job to Terminal-Normal mode.
832 * Suspends updating the terminal window.
833 */
834 static void
835term_enter_terminal_mode()
836{
837 term_T *term = curbuf->b_term;
838
839 /* Append the current terminal contents to the buffer. */
840 move_terminal_to_buffer(term);
841
842 set_terminal_mode(term, TRUE);
843}
844
845/*
846 * Returns TRUE if the current window contains a terminal and we are in
847 * Terminal-Normal mode.
848 */
849 int
850term_in_terminal_mode()
851{
852 term_T *term = curbuf->b_term;
853
854 return term != NULL && term->tl_terminal_mode;
855}
856
857/*
858 * Switch from Terminal-Normal mode to sending keys to the job.
859 * Restores updating the terminal window.
860 */
861 void
862term_leave_terminal_mode()
863{
864 term_T *term = curbuf->b_term;
865 sb_line_T *line;
866 garray_T *gap;
867
868 /* Remove the terminal contents from the scrollback and the buffer. */
869 gap = &term->tl_scrollback;
870 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
871 {
872 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
873 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
874 vim_free(line->sb_cells);
875 --gap->ga_len;
876 if (gap->ga_len == 0)
877 break;
878 }
879 check_cursor();
880
881 set_terminal_mode(term, FALSE);
882
883 if (term->tl_channel_closed)
884 cleanup_vterm(term);
885 redraw_buf_and_status_later(curbuf, NOT_VALID);
886}
887
888/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200889 * Get a key from the user without mapping.
890 * TODO: use terminal mode mappings.
891 */
892 static int
893term_vgetc()
894{
895 int c;
896
897 ++no_mapping;
898 ++allow_keys;
899 got_int = FALSE;
900 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200901 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200902 --no_mapping;
903 --allow_keys;
904 return c;
905}
906
907/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200908 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200909 * Return FAIL when the key needs to be handled in Normal mode.
910 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200911 */
912 static int
913send_keys_to_term(term_T *term, int c, int typed)
914{
915 char msg[KEY_BUF_LEN];
916 size_t len;
917 static int mouse_was_outside = FALSE;
918 int dragging_outside = FALSE;
919
920 /* Catch keys that need to be handled as in Normal mode. */
921 switch (c)
922 {
923 case NUL:
924 case K_ZERO:
925 if (typed)
926 stuffcharReadbuff(c);
927 return FAIL;
928
929 case K_IGNORE:
930 return FAIL;
931
932 case K_LEFTDRAG:
933 case K_MIDDLEDRAG:
934 case K_RIGHTDRAG:
935 case K_X1DRAG:
936 case K_X2DRAG:
937 dragging_outside = mouse_was_outside;
938 /* FALLTHROUGH */
939 case K_LEFTMOUSE:
940 case K_LEFTMOUSE_NM:
941 case K_LEFTRELEASE:
942 case K_LEFTRELEASE_NM:
943 case K_MIDDLEMOUSE:
944 case K_MIDDLERELEASE:
945 case K_RIGHTMOUSE:
946 case K_RIGHTRELEASE:
947 case K_X1MOUSE:
948 case K_X1RELEASE:
949 case K_X2MOUSE:
950 case K_X2RELEASE:
951 if (mouse_row < W_WINROW(curwin)
952 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
953 || mouse_col < W_WINCOL(curwin)
954 || mouse_col >= W_ENDCOL(curwin)
955 || dragging_outside)
956 {
957 /* click outside the current window */
958 if (typed)
959 {
960 stuffcharReadbuff(c);
961 mouse_was_outside = TRUE;
962 }
963 return FAIL;
964 }
965 }
966 if (typed)
967 mouse_was_outside = FALSE;
968
969 /* Convert the typed key to a sequence of bytes for the job. */
970 len = term_convert_key(term, c, msg);
971 if (len > 0)
972 /* TODO: if FAIL is returned, stop? */
973 channel_send(term->tl_job->jv_channel, PART_IN,
974 (char_u *)msg, (int)len, NULL);
975
976 return OK;
977}
978
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200979 static void
980position_cursor(win_T *wp, VTermPos *pos)
981{
982 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
983 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
984 wp->w_valid |= (VALID_WCOL|VALID_WROW);
985}
986
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200987/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200988 * Handle CTRL-W "": send register contents to the job.
989 */
990 static void
991term_paste_register(int prev_c UNUSED)
992{
993 int c;
994 list_T *l;
995 listitem_T *item;
996 long reglen = 0;
997 int type;
998
999#ifdef FEAT_CMDL_INFO
1000 if (add_to_showcmd(prev_c))
1001 if (add_to_showcmd('"'))
1002 out_flush();
1003#endif
1004 c = term_vgetc();
1005#ifdef FEAT_CMDL_INFO
1006 clear_showcmd();
1007#endif
1008
1009 /* CTRL-W "= prompt for expression to evaluate. */
1010 if (c == '=' && get_expr_register() != '=')
1011 return;
1012
1013 l = (list_T *)get_reg_contents(c, GREG_LIST);
1014 if (l != NULL)
1015 {
1016 type = get_reg_type(c, &reglen);
1017 for (item = l->lv_first; item != NULL; item = item->li_next)
1018 {
1019 char_u *s = get_tv_string(&item->li_tv);
1020
1021 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1022 s, STRLEN(s), NULL);
1023 if (item->li_next != NULL || type == MLINE)
1024 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1025 (char_u *)"\r", 1, NULL);
1026 }
1027 list_free(l);
1028 }
1029}
1030
1031/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001032 * Returns TRUE if the current window contains a terminal and we are sending
1033 * keys to the job.
1034 */
1035 int
1036term_use_loop()
1037{
1038 term_T *term = curbuf->b_term;
1039
1040 return term != NULL
1041 && !term->tl_terminal_mode
1042 && term->tl_vterm != NULL
1043 && term_job_running(term);
1044}
1045
1046/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001047 * Wait for input and send it to the job.
1048 * Return when the start of a CTRL-W command is typed or anything else that
1049 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001050 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1051 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001052 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001053 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001054terminal_loop(void)
1055{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001056 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001057 int termkey = 0;
1058
1059 if (*curwin->w_p_tk != NUL)
1060 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001061 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001062
1063 for (;;)
1064 {
1065 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001066 /* Repeat redrawing in case a message is received while redrawing. */
1067 while (curwin->w_redr_type != 0)
1068 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001069 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001070
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001071 c = term_vgetc();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001072 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001073 /* job finished while waiting for a character */
1074 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001075
Bram Moolenaarfae42832017-08-01 22:24:26 +02001076#ifdef UNIX
1077 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1078#endif
1079#ifdef WIN3264
1080 if (c == Ctrl_C)
1081 /* We don't know if the job can handle CTRL-C itself or not, this
1082 * may kill the shell instead of killing the command running in the
1083 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001084 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001085#endif
1086
Bram Moolenaar69198192017-08-05 14:10:48 +02001087 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001088 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001089 int prev_c = c;
1090
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001091#ifdef FEAT_CMDL_INFO
1092 if (add_to_showcmd(c))
1093 out_flush();
1094#endif
1095 c = term_vgetc();
1096#ifdef FEAT_CMDL_INFO
1097 clear_showcmd();
1098#endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001099 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001100 /* job finished while waiting for a character */
1101 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001102
Bram Moolenaar69198192017-08-05 14:10:48 +02001103 if (prev_c == Ctrl_BSL)
1104 {
1105 if (c == Ctrl_N)
1106 /* CTRL-\ CTRL-N : execute one Normal mode command. */
1107 return OK;
1108 /* Send both keys to the terminal. */
1109 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1110 }
1111 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001112 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001113 /* "CTRL-W .": send CTRL-W to the job */
1114 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001115 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001116 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001117 {
1118 term_enter_terminal_mode();
1119 return FAIL;
1120 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001121 else if (c == '"')
1122 {
1123 term_paste_register(prev_c);
1124 continue;
1125 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001126 else if (termkey == 0 || c != termkey)
1127 {
1128 stuffcharReadbuff(Ctrl_W);
1129 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001130 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001131 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001132 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001133 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1134 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001135 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001136 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001137}
1138
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001139/*
1140 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001141 * This updates the title and status, but does not close the vter, because
1142 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001143 */
1144 void
1145term_job_ended(job_T *job)
1146{
1147 term_T *term;
1148 int did_one = FALSE;
1149
1150 for (term = first_term; term != NULL; term = term->tl_next)
1151 if (term->tl_job == job)
1152 {
1153 vim_free(term->tl_title);
1154 term->tl_title = NULL;
1155 vim_free(term->tl_status_text);
1156 term->tl_status_text = NULL;
1157 redraw_buf_and_status_later(term->tl_buffer, VALID);
1158 did_one = TRUE;
1159 }
1160 if (did_one)
1161 redraw_statuslines();
1162 if (curbuf->b_term != NULL)
1163 {
1164 if (curbuf->b_term->tl_job == job)
1165 maketitle();
1166 update_cursor(curbuf->b_term, TRUE);
1167 }
1168}
1169
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001170 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001171may_toggle_cursor(term_T *term)
1172{
1173 if (curbuf == term->tl_buffer)
1174 {
1175 if (term->tl_cursor_visible)
1176 cursor_on();
1177 else
1178 cursor_off();
1179 }
1180}
1181
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001182 static int
1183handle_damage(VTermRect rect, void *user)
1184{
1185 term_T *term = (term_T *)user;
1186
1187 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1188 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1189 redraw_buf_later(term->tl_buffer, NOT_VALID);
1190 return 1;
1191}
1192
1193 static int
1194handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1195{
1196 term_T *term = (term_T *)user;
1197
1198 /* TODO */
1199 redraw_buf_later(term->tl_buffer, NOT_VALID);
1200 return 1;
1201}
1202
1203 static int
1204handle_movecursor(
1205 VTermPos pos,
1206 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001207 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001208 void *user)
1209{
1210 term_T *term = (term_T *)user;
1211 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001212
1213 term->tl_cursor_pos = pos;
1214 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001215
1216 FOR_ALL_WINDOWS(wp)
1217 {
1218 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001219 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001220 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001221 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001222 {
1223 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001224 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001225 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001226
1227 return 1;
1228}
1229
Bram Moolenaar21554412017-07-24 21:44:43 +02001230 static int
1231handle_settermprop(
1232 VTermProp prop,
1233 VTermValue *value,
1234 void *user)
1235{
1236 term_T *term = (term_T *)user;
1237
1238 switch (prop)
1239 {
1240 case VTERM_PROP_TITLE:
1241 vim_free(term->tl_title);
1242 term->tl_title = vim_strsave((char_u *)value->string);
1243 vim_free(term->tl_status_text);
1244 term->tl_status_text = NULL;
1245 if (term == curbuf->b_term)
1246 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001247 break;
1248
1249 case VTERM_PROP_CURSORVISIBLE:
1250 term->tl_cursor_visible = value->boolean;
1251 may_toggle_cursor(term);
1252 out_flush();
1253 break;
1254
Bram Moolenaar21554412017-07-24 21:44:43 +02001255 default:
1256 break;
1257 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001258 /* Always return 1, otherwise vterm doesn't store the value internally. */
1259 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001260}
1261
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001262/*
1263 * The job running in the terminal resized the terminal.
1264 */
1265 static int
1266handle_resize(int rows, int cols, void *user)
1267{
1268 term_T *term = (term_T *)user;
1269 win_T *wp;
1270
1271 term->tl_rows = rows;
1272 term->tl_cols = cols;
1273 FOR_ALL_WINDOWS(wp)
1274 {
1275 if (wp->w_buffer == term->tl_buffer)
1276 {
1277 win_setheight_win(rows, wp);
1278 win_setwidth_win(cols, wp);
1279 }
1280 }
1281
1282 redraw_buf_later(term->tl_buffer, NOT_VALID);
1283 return 1;
1284}
1285
Bram Moolenaard85f2712017-07-28 21:51:57 +02001286/*
1287 * Handle a line that is pushed off the top of the screen.
1288 */
1289 static int
1290handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1291{
1292 term_T *term = (term_T *)user;
1293
1294 /* TODO: Limit the number of lines that are stored. */
1295 /* TODO: put the text in the buffer. */
1296 if (ga_grow(&term->tl_scrollback, 1) == OK)
1297 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001298 VTermScreenCell *p = NULL;
1299 int len = 0;
1300 int i;
1301 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001302
1303 /* do not store empty cells at the end */
1304 for (i = 0; i < cols; ++i)
1305 if (cells[i].chars[0] != 0)
1306 len = i + 1;
1307
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001308 if (len > 0)
1309 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001310 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001311 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001312
1313 line = (sb_line_T *)term->tl_scrollback.ga_data
1314 + term->tl_scrollback.ga_len;
1315 line->sb_cols = len;
1316 line->sb_cells = p;
1317 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001318 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001319
1320 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001321 }
1322 return 0; /* ignored */
1323}
1324
Bram Moolenaar21554412017-07-24 21:44:43 +02001325static VTermScreenCallbacks screen_callbacks = {
1326 handle_damage, /* damage */
1327 handle_moverect, /* moverect */
1328 handle_movecursor, /* movecursor */
1329 handle_settermprop, /* settermprop */
1330 NULL, /* bell */
1331 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001332 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001333 NULL /* sb_popline */
1334};
1335
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001336/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001337 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001338 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001339 */
1340 void
1341term_channel_closed(channel_T *ch)
1342{
1343 term_T *term;
1344 int did_one = FALSE;
1345
1346 for (term = first_term; term != NULL; term = term->tl_next)
1347 if (term->tl_job == ch->ch_job)
1348 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001349 term->tl_channel_closed = TRUE;
1350
Bram Moolenaard85f2712017-07-28 21:51:57 +02001351 vim_free(term->tl_title);
1352 term->tl_title = NULL;
1353 vim_free(term->tl_status_text);
1354 term->tl_status_text = NULL;
1355
Bram Moolenaar423802d2017-07-30 16:52:24 +02001356 /* Unless in Terminal-Normal mode: clear the vterm. */
1357 if (!term->tl_terminal_mode)
1358 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001359
1360 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1361 did_one = TRUE;
1362 }
1363 if (did_one)
1364 {
1365 redraw_statuslines();
1366
1367 /* Need to break out of vgetc(). */
1368 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001369 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001370
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001371 term = curbuf->b_term;
1372 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001373 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001374 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001375 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001376 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001377 }
1378 }
1379}
1380
1381/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001382 * Reverse engineer the RGB value into a cterm color index.
1383 * First color is 1. Return 0 if no match found.
1384 */
1385 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001386color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001387{
1388 int red = color->red;
1389 int blue = color->blue;
1390 int green = color->green;
1391
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001392 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001393 if (red == 0)
1394 {
1395 if (green == 0)
1396 {
1397 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001398 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001399 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001400 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001401 }
1402 else if (green == 224)
1403 {
1404 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001405 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001406 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001407 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001408 }
1409 }
1410 else if (red == 224)
1411 {
1412 if (green == 0)
1413 {
1414 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001415 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001416 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001417 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001418 }
1419 else if (green == 224)
1420 {
1421 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001422 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001423 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001424 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001425 }
1426 }
1427 else if (red == 128)
1428 {
1429 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001430 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001431 }
1432 else if (red == 255)
1433 {
1434 if (green == 64)
1435 {
1436 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001437 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001438 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001439 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001440 }
1441 else if (green == 255)
1442 {
1443 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001444 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001445 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001446 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001447 }
1448 }
1449 else if (red == 64)
1450 {
1451 if (green == 64)
1452 {
1453 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001454 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001455 }
1456 else if (green == 255)
1457 {
1458 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001459 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001460 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001461 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001462 }
1463 }
1464 if (t_colors >= 256)
1465 {
1466 if (red == blue && red == green)
1467 {
1468 /* 24-color greyscale */
1469 static int cutoff[23] = {
1470 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1471 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1472 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1473 int i;
1474
1475 for (i = 0; i < 23; ++i)
1476 if (red < cutoff[i])
1477 return i + 233;
1478 return 256;
1479 }
1480
1481 /* 216-color cube */
1482 return 17 + ((red + 25) / 0x33) * 36
1483 + ((green + 25) / 0x33) * 6
1484 + (blue + 25) / 0x33;
1485 }
1486 return 0;
1487}
1488
1489/*
1490 * Convert the attributes of a vterm cell into an attribute index.
1491 */
1492 static int
1493cell2attr(VTermScreenCell *cell)
1494{
1495 int attr = 0;
1496
1497 if (cell->attrs.bold)
1498 attr |= HL_BOLD;
1499 if (cell->attrs.underline)
1500 attr |= HL_UNDERLINE;
1501 if (cell->attrs.italic)
1502 attr |= HL_ITALIC;
1503 if (cell->attrs.strike)
1504 attr |= HL_STANDOUT;
1505 if (cell->attrs.reverse)
1506 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001507
1508#ifdef FEAT_GUI
1509 if (gui.in_use)
1510 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001511 guicolor_T fg, bg;
1512
1513 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1514 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1515 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001516 }
1517 else
1518#endif
1519#ifdef FEAT_TERMGUICOLORS
1520 if (p_tgc)
1521 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001522 guicolor_T fg, bg;
1523
1524 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1525 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1526
1527 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001528 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001529 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001530#endif
1531 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001532 int bold = MAYBE;
1533 int fg = color2index(&cell->fg, TRUE, &bold);
1534 int bg = color2index(&cell->bg, FALSE, &bold);
1535
1536 /* with 8 colors set the bold attribute to get a bright foreground */
1537 if (bold == TRUE)
1538 attr |= HL_BOLD;
1539 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001540 }
1541 return 0;
1542}
1543
1544/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001545 * Called to update the window that contains a terminal.
1546 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001547 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001548 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001549term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001550{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001551 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001552 VTerm *vterm;
1553 VTermScreen *screen;
1554 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001555 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001556
Bram Moolenaar423802d2017-07-30 16:52:24 +02001557 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001558 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001559
Bram Moolenaard85f2712017-07-28 21:51:57 +02001560 vterm = term->tl_vterm;
1561 screen = vterm_obtain_screen(vterm);
1562 state = vterm_obtain_state(vterm);
1563
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001564 /*
1565 * If the window was resized a redraw will be triggered and we get here.
1566 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1567 */
1568 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1569 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001570 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001571 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1572 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1573 win_T *twp;
1574
1575 FOR_ALL_WINDOWS(twp)
1576 {
1577 /* When more than one window shows the same terminal, use the
1578 * smallest size. */
1579 if (twp->w_buffer == term->tl_buffer)
1580 {
1581 if (!term->tl_rows_fixed && rows > twp->w_height)
1582 rows = twp->w_height;
1583 if (!term->tl_cols_fixed && cols > twp->w_width)
1584 cols = twp->w_width;
1585 }
1586 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001587
1588 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001589 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001590 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001591 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001592 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001593
1594 /* The cursor may have been moved when resizing. */
1595 vterm_state_get_cursorpos(state, &pos);
1596 position_cursor(wp, &pos);
1597
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001598 /* TODO: Only redraw what changed. */
1599 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001600 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001601 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001602 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001603
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001604 if (pos.row < term->tl_rows)
1605 {
1606 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001607 {
1608 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001609 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001610
Bram Moolenaareeac6772017-07-23 15:48:37 +02001611 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1612 vim_memset(&cell, 0, sizeof(cell));
1613
1614 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001615 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001616 if (c == NUL)
1617 {
1618 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001619#if defined(FEAT_MBYTE)
1620 if (enc_utf8)
1621 ScreenLinesUC[off] = NUL;
1622#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001623 }
1624 else
1625 {
1626#if defined(FEAT_MBYTE)
1627 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001628 {
1629 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001630 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001631 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001632 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001633 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001634 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001635 if (enc_utf8)
1636 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001637 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001638#else
1639 ScreenLines[off] = c;
1640#endif
1641 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001642 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001643
1644 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001645 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001646 if (cell.width == 2)
1647 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001648 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001649#if defined(FEAT_MBYTE)
1650 if (enc_utf8)
1651 ScreenLinesUC[off] = NUL;
1652#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001653 ++pos.col;
1654 ++off;
1655 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001656 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001657 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001658 else
1659 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001660
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001661 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1662 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001663 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001664
1665 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001666}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001667
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001668/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001669 * Return TRUE if "wp" is a terminal window where the job has finished.
1670 */
1671 int
1672term_is_finished(buf_T *buf)
1673{
1674 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1675}
1676
1677/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001678 * Return TRUE if "wp" is a terminal window where the job has finished or we
1679 * are in Terminal-Normal mode.
1680 */
1681 int
1682term_show_buffer(buf_T *buf)
1683{
1684 term_T *term = buf->b_term;
1685
1686 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1687}
1688
1689/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001690 * The current buffer is going to be changed. If there is terminal
1691 * highlighting remove it now.
1692 */
1693 void
1694term_change_in_curbuf(void)
1695{
1696 term_T *term = curbuf->b_term;
1697
1698 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1699 {
1700 free_scrollback(term);
1701 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001702
1703 /* The buffer is now like a normal buffer, it cannot be easily
1704 * abandoned when changed. */
1705 set_string_option_direct((char_u *)"buftype", -1,
1706 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001707 }
1708}
1709
1710/*
1711 * Get the screen attribute for a position in the buffer.
1712 */
1713 int
1714term_get_attr(buf_T *buf, linenr_T lnum, int col)
1715{
1716 term_T *term = buf->b_term;
1717 sb_line_T *line;
1718
Bram Moolenaar70229f92017-07-29 16:01:53 +02001719 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001720 return 0;
1721 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1722 if (col >= line->sb_cols)
1723 return 0;
1724 return cell2attr(line->sb_cells + col);
1725}
1726
1727/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001728 * Create a new vterm and initialize it.
1729 */
1730 static void
1731create_vterm(term_T *term, int rows, int cols)
1732{
1733 VTerm *vterm;
1734 VTermScreen *screen;
1735
1736 vterm = vterm_new(rows, cols);
1737 term->tl_vterm = vterm;
1738 screen = vterm_obtain_screen(vterm);
1739 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1740 /* TODO: depends on 'encoding'. */
1741 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001742
1743 /* Vterm uses a default black background. Set it to white when
1744 * 'background' is "light". */
1745 if (*p_bg == 'l')
1746 {
1747 VTermColor fg, bg;
1748
1749 fg.red = fg.green = fg.blue = 0;
1750 bg.red = bg.green = bg.blue = 255;
1751 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1752 }
1753
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001754 /* Required to initialize most things. */
1755 vterm_screen_reset(screen, 1 /* hard */);
1756}
1757
Bram Moolenaar21554412017-07-24 21:44:43 +02001758/*
1759 * Return the text to show for the buffer name and status.
1760 */
1761 char_u *
1762term_get_status_text(term_T *term)
1763{
1764 if (term->tl_status_text == NULL)
1765 {
1766 char_u *txt;
1767 size_t len;
1768
Bram Moolenaar423802d2017-07-30 16:52:24 +02001769 if (term->tl_terminal_mode)
1770 {
1771 if (term_job_running(term))
1772 txt = (char_u *)_("Terminal");
1773 else
1774 txt = (char_u *)_("Terminal-finished");
1775 }
1776 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001777 txt = term->tl_title;
1778 else if (term_job_running(term))
1779 txt = (char_u *)_("running");
1780 else
1781 txt = (char_u *)_("finished");
1782 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001783 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001784 if (term->tl_status_text != NULL)
1785 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1786 term->tl_buffer->b_fname, txt);
1787 }
1788 return term->tl_status_text;
1789}
1790
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001791/*
1792 * Mark references in jobs of terminals.
1793 */
1794 int
1795set_ref_in_term(int copyID)
1796{
1797 int abort = FALSE;
1798 term_T *term;
1799 typval_T tv;
1800
1801 for (term = first_term; term != NULL; term = term->tl_next)
1802 if (term->tl_job != NULL)
1803 {
1804 tv.v_type = VAR_JOB;
1805 tv.vval.v_job = term->tl_job;
1806 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1807 }
1808 return abort;
1809}
1810
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001811/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001812 * Get the buffer from the first argument in "argvars".
1813 * Returns NULL when the buffer is not for a terminal window.
1814 */
1815 static buf_T *
1816term_get_buf(typval_T *argvars)
1817{
1818 buf_T *buf;
1819
1820 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1821 ++emsg_off;
1822 buf = get_buf_tv(&argvars[0], FALSE);
1823 --emsg_off;
1824 if (buf == NULL || buf->b_term == NULL)
1825 return NULL;
1826 return buf;
1827}
1828
1829/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001830 * "term_getattr(attr, name)" function
1831 */
1832 void
1833f_term_getattr(typval_T *argvars, typval_T *rettv)
1834{
1835 int attr;
1836 size_t i;
1837 char_u *name;
1838
1839 static struct {
1840 char *name;
1841 int attr;
1842 } attrs[] = {
1843 {"bold", HL_BOLD},
1844 {"italic", HL_ITALIC},
1845 {"underline", HL_UNDERLINE},
1846 {"strike", HL_STANDOUT},
1847 {"reverse", HL_INVERSE},
1848 };
1849
1850 attr = get_tv_number(&argvars[0]);
1851 name = get_tv_string_chk(&argvars[1]);
1852 if (name == NULL)
1853 return;
1854
1855 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1856 if (STRCMP(name, attrs[i].name) == 0)
1857 {
1858 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1859 break;
1860 }
1861}
1862
1863/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001864 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001865 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001866 void
1867f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001868{
Bram Moolenaar97870002017-07-30 18:28:38 +02001869 buf_T *buf = term_get_buf(argvars);
1870 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001871
Bram Moolenaar97870002017-07-30 18:28:38 +02001872 if (rettv_list_alloc(rettv) == FAIL)
1873 return;
1874 if (buf == NULL)
1875 return;
1876
1877 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001878 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1879 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001880 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001881}
1882
1883/*
1884 * "term_getjob(buf)" function
1885 */
1886 void
1887f_term_getjob(typval_T *argvars, typval_T *rettv)
1888{
1889 buf_T *buf = term_get_buf(argvars);
1890
1891 rettv->v_type = VAR_JOB;
1892 rettv->vval.v_job = NULL;
1893 if (buf == NULL)
1894 return;
1895
1896 rettv->vval.v_job = buf->b_term->tl_job;
1897 if (rettv->vval.v_job != NULL)
1898 ++rettv->vval.v_job->jv_refcount;
1899}
1900
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001901 static int
1902get_row_number(typval_T *tv, term_T *term)
1903{
1904 if (tv->v_type == VAR_STRING
1905 && tv->vval.v_string != NULL
1906 && STRCMP(tv->vval.v_string, ".") == 0)
1907 return term->tl_cursor_pos.row;
1908 return (int)get_tv_number(tv) - 1;
1909}
1910
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001911/*
1912 * "term_getline(buf, row)" function
1913 */
1914 void
1915f_term_getline(typval_T *argvars, typval_T *rettv)
1916{
1917 buf_T *buf = term_get_buf(argvars);
1918 term_T *term;
1919 int row;
1920
1921 rettv->v_type = VAR_STRING;
1922 if (buf == NULL)
1923 return;
1924 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001925 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001926
1927 if (term->tl_vterm == NULL)
1928 {
1929 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1930
1931 /* vterm is finished, get the text from the buffer */
1932 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1933 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1934 }
1935 else
1936 {
1937 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1938 VTermRect rect;
1939 int len;
1940 char_u *p;
1941
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001942 if (row < 0 || row >= term->tl_rows)
1943 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001944 len = term->tl_cols * MB_MAXBYTES + 1;
1945 p = alloc(len);
1946 if (p == NULL)
1947 return;
1948 rettv->vval.v_string = p;
1949
1950 rect.start_col = 0;
1951 rect.end_col = term->tl_cols;
1952 rect.start_row = row;
1953 rect.end_row = row + 1;
1954 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1955 }
1956}
1957
1958/*
1959 * "term_getsize(buf)" function
1960 */
1961 void
1962f_term_getsize(typval_T *argvars, typval_T *rettv)
1963{
1964 buf_T *buf = term_get_buf(argvars);
1965 list_T *l;
1966
1967 if (rettv_list_alloc(rettv) == FAIL)
1968 return;
1969 if (buf == NULL)
1970 return;
1971
1972 l = rettv->vval.v_list;
1973 list_append_number(l, buf->b_term->tl_rows);
1974 list_append_number(l, buf->b_term->tl_cols);
1975}
1976
1977/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001978 * "term_getstatus(buf)" function
1979 */
1980 void
1981f_term_getstatus(typval_T *argvars, typval_T *rettv)
1982{
1983 buf_T *buf = term_get_buf(argvars);
1984 term_T *term;
1985 char_u val[100];
1986
1987 rettv->v_type = VAR_STRING;
1988 if (buf == NULL)
1989 return;
1990 term = buf->b_term;
1991
1992 if (term_job_running(term))
1993 STRCPY(val, "running");
1994 else
1995 STRCPY(val, "finished");
1996 if (term->tl_terminal_mode)
1997 STRCAT(val, ",terminal");
1998 rettv->vval.v_string = vim_strsave(val);
1999}
2000
2001/*
2002 * "term_gettitle(buf)" function
2003 */
2004 void
2005f_term_gettitle(typval_T *argvars, typval_T *rettv)
2006{
2007 buf_T *buf = term_get_buf(argvars);
2008
2009 rettv->v_type = VAR_STRING;
2010 if (buf == NULL)
2011 return;
2012
2013 if (buf->b_term->tl_title != NULL)
2014 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2015}
2016
2017/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002018 * "term_gettty(buf)" function
2019 */
2020 void
2021f_term_gettty(typval_T *argvars, typval_T *rettv)
2022{
2023 buf_T *buf = term_get_buf(argvars);
2024 char_u *p;
2025
2026 rettv->v_type = VAR_STRING;
2027 if (buf == NULL)
2028 return;
2029 if (buf->b_term->tl_job != NULL)
2030 p = buf->b_term->tl_job->jv_tty_name;
2031 else
2032 p = buf->b_term->tl_tty_name;
2033 if (p != NULL)
2034 rettv->vval.v_string = vim_strsave(p);
2035}
2036
2037/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002038 * "term_list()" function
2039 */
2040 void
2041f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2042{
2043 term_T *tp;
2044 list_T *l;
2045
2046 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2047 return;
2048
2049 l = rettv->vval.v_list;
2050 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2051 if (tp != NULL && tp->tl_buffer != NULL)
2052 if (list_append_number(l,
2053 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2054 return;
2055}
2056
2057/*
2058 * "term_scrape(buf, row)" function
2059 */
2060 void
2061f_term_scrape(typval_T *argvars, typval_T *rettv)
2062{
2063 buf_T *buf = term_get_buf(argvars);
2064 VTermScreen *screen = NULL;
2065 VTermPos pos;
2066 list_T *l;
2067 term_T *term;
2068
2069 if (rettv_list_alloc(rettv) == FAIL)
2070 return;
2071 if (buf == NULL)
2072 return;
2073 term = buf->b_term;
2074 if (term->tl_vterm != NULL)
2075 screen = vterm_obtain_screen(term->tl_vterm);
2076
2077 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002078 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002079 for (pos.col = 0; pos.col < term->tl_cols; )
2080 {
2081 dict_T *dcell;
2082 VTermScreenCell cell;
2083 char_u rgb[8];
2084 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2085 int off = 0;
2086 int i;
2087
2088 if (screen == NULL)
2089 {
2090 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2091 sb_line_T *line;
2092
2093 /* vterm has finished, get the cell from scrollback */
2094 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2095 break;
2096 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2097 if (pos.col >= line->sb_cols)
2098 break;
2099 cell = line->sb_cells[pos.col];
2100 }
2101 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2102 break;
2103 dcell = dict_alloc();
2104 list_append_dict(l, dcell);
2105
2106 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2107 {
2108 if (cell.chars[i] == 0)
2109 break;
2110 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2111 }
2112 mbs[off] = NUL;
2113 dict_add_nr_str(dcell, "chars", 0, mbs);
2114
2115 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2116 cell.fg.red, cell.fg.green, cell.fg.blue);
2117 dict_add_nr_str(dcell, "fg", 0, rgb);
2118 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2119 cell.bg.red, cell.bg.green, cell.bg.blue);
2120 dict_add_nr_str(dcell, "bg", 0, rgb);
2121
2122 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2123 dict_add_nr_str(dcell, "width", cell.width, NULL);
2124
2125 ++pos.col;
2126 if (cell.width == 2)
2127 ++pos.col;
2128 }
2129}
2130
2131/*
2132 * "term_sendkeys(buf, keys)" function
2133 */
2134 void
2135f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2136{
2137 buf_T *buf = term_get_buf(argvars);
2138 char_u *msg;
2139 term_T *term;
2140
2141 rettv->v_type = VAR_UNKNOWN;
2142 if (buf == NULL)
2143 return;
2144
2145 msg = get_tv_string_chk(&argvars[1]);
2146 if (msg == NULL)
2147 return;
2148 term = buf->b_term;
2149 if (term->tl_vterm == NULL)
2150 return;
2151
2152 while (*msg != NUL)
2153 {
2154 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2155 msg += MB_PTR2LEN(msg);
2156 }
2157
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002158 if (!term->tl_terminal_mode)
2159 {
2160 /* TODO: only update once in a while. */
2161 update_screen(0);
2162 if (buf == curbuf)
2163 update_cursor(term, TRUE);
2164 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002165}
2166
2167/*
2168 * "term_start(command, options)" function
2169 */
2170 void
2171f_term_start(typval_T *argvars, typval_T *rettv)
2172{
2173 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002174 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002175
2176 if (cmd == NULL)
2177 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002178 init_job_options(&opt);
2179 /* TODO: allow more job options */
2180 if (argvars[1].v_type != VAR_UNKNOWN
2181 && get_job_options(&argvars[1], &opt,
2182 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002183 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2184 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002185 return;
2186
2187 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002188
2189 if (curbuf->b_term != NULL)
2190 rettv->vval.v_number = curbuf->b_fnum;
2191}
2192
2193/*
2194 * "term_wait" function
2195 */
2196 void
2197f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2198{
2199 buf_T *buf = term_get_buf(argvars);
2200
2201 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002202 {
2203 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002204 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002205 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002206 if (buf->b_term->tl_job == NULL)
2207 {
2208 ch_log(NULL, "term_wait(): no job to wait for");
2209 return;
2210 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002211
2212 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002213 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002214 {
2215 /* The job is dead, keep reading channel I/O until the channel is
2216 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002217 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002218 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2219 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002220 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002221 parse_queued_messages();
2222 ui_delay(10L, FALSE);
2223 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002224 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002225 parse_queued_messages();
2226 }
2227 else
2228 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002229 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002230 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002231
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002232 /* Wait for 10 msec for any channel I/O. */
2233 /* TODO: use delay from optional argument */
2234 ui_delay(10L, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002235 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002236
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002237 /* Flushing messages on channels is hopefully sufficient.
2238 * TODO: is there a better way? */
2239 parse_queued_messages();
2240 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002241}
2242
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002243# ifdef WIN3264
2244
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002245/**************************************
2246 * 2. MS-Windows implementation.
2247 */
2248
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002249#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2250#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2251
Bram Moolenaar8a773062017-07-24 22:29:21 +02002252void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002253void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002254void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002255BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2256void (*winpty_config_set_initial_size)(void*, int, int);
2257LPCWSTR (*winpty_conin_name)(void*);
2258LPCWSTR (*winpty_conout_name)(void*);
2259LPCWSTR (*winpty_conerr_name)(void*);
2260void (*winpty_free)(void*);
2261void (*winpty_config_free)(void*);
2262void (*winpty_spawn_config_free)(void*);
2263void (*winpty_error_free)(void*);
2264LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002265BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002266HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002267
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002268#define WINPTY_DLL "winpty.dll"
2269
2270static HINSTANCE hWinPtyDLL = NULL;
2271
2272 int
2273dyn_winpty_init(void)
2274{
2275 int i;
2276 static struct
2277 {
2278 char *name;
2279 FARPROC *ptr;
2280 } winpty_entry[] =
2281 {
2282 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2283 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2284 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2285 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2286 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2287 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2288 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2289 {"winpty_free", (FARPROC*)&winpty_free},
2290 {"winpty_open", (FARPROC*)&winpty_open},
2291 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2292 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2293 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2294 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002295 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002296 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002297 {NULL, NULL}
2298 };
2299
2300 /* No need to initialize twice. */
2301 if (hWinPtyDLL)
2302 return 1;
2303 /* Load winpty.dll */
2304 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2305 if (!hWinPtyDLL)
2306 {
2307 EMSG2(_(e_loadlib), WINPTY_DLL);
2308 return 0;
2309 }
2310 for (i = 0; winpty_entry[i].name != NULL
2311 && winpty_entry[i].ptr != NULL; ++i)
2312 {
2313 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2314 winpty_entry[i].name)) == NULL)
2315 {
2316 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2317 return 0;
2318 }
2319 }
2320
2321 return 1;
2322}
2323
2324/*
2325 * Create a new terminal of "rows" by "cols" cells.
2326 * Store a reference in "term".
2327 * Return OK or FAIL.
2328 */
2329 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002330term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002331{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002332 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002333 channel_T *channel = NULL;
2334 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002335 DWORD error;
2336 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2337 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002338 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002339 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002340
2341 if (!dyn_winpty_init())
2342 return FAIL;
2343
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002344 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002345 if (p == NULL)
2346 return FAIL;
2347
2348 job = job_alloc();
2349 if (job == NULL)
2350 goto failed;
2351
2352 channel = add_channel();
2353 if (channel == NULL)
2354 goto failed;
2355
2356 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2357 if (term->tl_winpty_config == NULL)
2358 goto failed;
2359
2360 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2361 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2362 if (term->tl_winpty == NULL)
2363 goto failed;
2364
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002365 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002366 spawn_config = winpty_spawn_config_new(
2367 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2368 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2369 NULL,
2370 p,
2371 NULL,
2372 NULL,
2373 &winpty_err);
2374 if (spawn_config == NULL)
2375 goto failed;
2376
2377 channel = add_channel();
2378 if (channel == NULL)
2379 goto failed;
2380
2381 job = job_alloc();
2382 if (job == NULL)
2383 goto failed;
2384
2385 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2386 &child_thread_handle, &error, &winpty_err))
2387 goto failed;
2388
2389 channel_set_pipes(channel,
2390 (sock_T) CreateFileW(
2391 winpty_conin_name(term->tl_winpty),
2392 GENERIC_WRITE, 0, NULL,
2393 OPEN_EXISTING, 0, NULL),
2394 (sock_T) CreateFileW(
2395 winpty_conout_name(term->tl_winpty),
2396 GENERIC_READ, 0, NULL,
2397 OPEN_EXISTING, 0, NULL),
2398 (sock_T) CreateFileW(
2399 winpty_conerr_name(term->tl_winpty),
2400 GENERIC_READ, 0, NULL,
2401 OPEN_EXISTING, 0, NULL));
2402
2403 jo = CreateJobObject(NULL, NULL);
2404 if (jo == NULL)
2405 goto failed;
2406
2407 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002408 {
2409 /* Failed, switch the way to terminate process with TerminateProcess. */
2410 CloseHandle(jo);
2411 jo = NULL;
2412 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002413
2414 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002415 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002416
2417 create_vterm(term, rows, cols);
2418
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002419 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002420 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002421
2422 job->jv_channel = channel;
2423 job->jv_proc_info.hProcess = child_process_handle;
2424 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2425 job->jv_job_object = jo;
2426 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002427 sprintf(buf, "winpty://%lu",
2428 GetProcessId(winpty_agent_process(term->tl_winpty)));
2429 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002430 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002431 term->tl_job = job;
2432
2433 return OK;
2434
2435failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002436 if (spawn_config != NULL)
2437 winpty_spawn_config_free(spawn_config);
2438 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002439 if (channel != NULL)
2440 channel_clear(channel);
2441 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002442 {
2443 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002444 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002445 }
2446 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002447 if (jo != NULL)
2448 CloseHandle(jo);
2449 if (term->tl_winpty != NULL)
2450 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002451 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002452 if (term->tl_winpty_config != NULL)
2453 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002454 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002455 if (winpty_err != NULL)
2456 {
2457 char_u *msg = utf16_to_enc(
2458 (short_u *)winpty_error_msg(winpty_err), NULL);
2459
2460 EMSG(msg);
2461 winpty_error_free(winpty_err);
2462 }
2463 return FAIL;
2464}
2465
2466/*
2467 * Free the terminal emulator part of "term".
2468 */
2469 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002470term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002471{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002472 if (term->tl_winpty != NULL)
2473 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002474 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002475 if (term->tl_winpty_config != NULL)
2476 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002477 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002478 if (term->tl_vterm != NULL)
2479 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002480 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002481}
2482
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002483/*
2484 * Request size to terminal.
2485 */
2486 static void
2487term_report_winsize(term_T *term, int rows, int cols)
2488{
2489 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2490}
2491
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002492# else
2493
2494/**************************************
2495 * 3. Unix-like implementation.
2496 */
2497
2498/*
2499 * Create a new terminal of "rows" by "cols" cells.
2500 * Start job for "cmd".
2501 * Store the pointers in "term".
2502 * Return OK or FAIL.
2503 */
2504 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002505term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002506{
2507 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002508
2509 create_vterm(term, rows, cols);
2510
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002511 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002512 argvars[0].v_type = VAR_STRING;
2513 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002514
2515 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002516 if (term->tl_job != NULL)
2517 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002518
Bram Moolenaar61a66052017-07-22 18:39:00 +02002519 return term->tl_job != NULL
2520 && term->tl_job->jv_channel != NULL
2521 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002522}
2523
2524/*
2525 * Free the terminal emulator part of "term".
2526 */
2527 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002528term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002529{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002530 if (term->tl_vterm != NULL)
2531 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002532 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002533}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002534
2535/*
2536 * Request size to terminal.
2537 */
2538 static void
2539term_report_winsize(term_T *term, int rows, int cols)
2540{
2541 /* Use an ioctl() to report the new window size to the job. */
2542 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2543 {
2544 int fd = -1;
2545 int part;
2546
2547 for (part = PART_OUT; part < PART_COUNT; ++part)
2548 {
2549 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2550 if (isatty(fd))
2551 break;
2552 }
2553 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2554 mch_stop_job(term->tl_job, (char_u *)"winch");
2555 }
2556}
2557
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002558# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002559
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002560#endif /* FEAT_TERMINAL */