blob: f5cd2e0190aa16e82b7f95176dbb01d456dc3a43 [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 Moolenaar78712a72017-08-05 14:50:12 +020039 * - job_start('ls') sometimes does not work.
Bram Moolenaard8dc1792017-08-03 11:55:21 +020040 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar423802d2017-07-30 16:52:24 +020041 * - in bash mouse clicks are inserting characters.
42 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020043 * - add argument to term_wait() for waiting time.
Bram Moolenaard85f2712017-07-28 21:51:57 +020044 * - For the scrollback buffer store lines in the buffer, only attributes in
45 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020046 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020048 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
49 * job finishes).
50 * - add option values to the command:
51 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020052 * - support different cursor shapes, colors and attributes
53 * - make term_getcursor() return type (none/block/bar/underline) and
54 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020055 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020056 * For the GUI fill termios with default values, perhaps like pangoterm:
57 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
58 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020059 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020060 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020061 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020062 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020063 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020064 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
65 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020066 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020067 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020068 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020069 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020070 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020071 * terminal. Allow:
72 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
73 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
74 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
75 * Check that something is connected to the terminal.
76 * Test: "cat" reading from a file or buffer
77 * "ls" writing stdout to a file or buffer
78 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020079 * - support ":term NONE" to open a terminal with a pty but not running a job
80 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020081 * - if the job in the terminal does not support the mouse, we can use the
82 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020083 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
84 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020085 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020086 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020087 * - Copy text in the vterm to the Vim buffer once in a while, so that
88 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020089 */
90
91#include "vim.h"
92
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020093#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020094
Bram Moolenaard5310982017-08-05 15:16:32 +020095#ifndef MIN
96# define MIN(x,y) ((x) < (y) ? (x) : (y))
97#endif
98#ifndef MAX
99# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200100#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200101
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200102#include "libvterm/include/vterm.h"
103
Bram Moolenaard85f2712017-07-28 21:51:57 +0200104typedef struct sb_line_S {
105 int sb_cols; /* can differ per line */
106 VTermScreenCell *sb_cells; /* allocated */
107} sb_line_T;
108
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200109/* typedef term_T in structs.h */
110struct terminal_S {
111 term_T *tl_next;
112
Bram Moolenaar423802d2017-07-30 16:52:24 +0200113 VTerm *tl_vterm;
114 job_T *tl_job;
115 buf_T *tl_buffer;
116
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200117 /* used when tl_job is NULL and only a pty was created */
118 int tl_tty_fd;
119 char_u *tl_tty_name;
120
Bram Moolenaar423802d2017-07-30 16:52:24 +0200121 int tl_terminal_mode;
122 int tl_channel_closed;
123
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200124#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200125 void *tl_winpty_config;
126 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200127#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200128
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200129 /* last known vterm size */
130 int tl_rows;
131 int tl_cols;
132 /* vterm size does not follow window size */
133 int tl_rows_fixed;
134 int tl_cols_fixed;
135
Bram Moolenaar21554412017-07-24 21:44:43 +0200136 char_u *tl_title; /* NULL or allocated */
137 char_u *tl_status_text; /* NULL or allocated */
138
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200139 /* Range of screen rows to update. Zero based. */
140 int tl_dirty_row_start; /* -1 if nothing dirty */
141 int tl_dirty_row_end; /* row below last one to update */
142
Bram Moolenaard85f2712017-07-28 21:51:57 +0200143 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200144 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200145
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200146 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200147 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200148};
149
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200150/*
151 * List of all active terminals.
152 */
153static term_T *first_term = NULL;
154
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200155
156#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
157#define KEY_BUF_LEN 200
158
159/*
160 * Functions with separate implementation for MS-Windows and Unix-like systems.
161 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162static 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 +0200163static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200164static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200165
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200166/**************************************
167 * 1. Generic code for all systems.
168 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200169
170/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200171 * Determine the terminal size from 'termsize' and the current window.
172 * Assumes term->tl_rows and term->tl_cols are zero.
173 */
174 static void
175set_term_and_win_size(term_T *term)
176{
177 if (*curwin->w_p_tms != NUL)
178 {
179 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
180
181 term->tl_rows = atoi((char *)curwin->w_p_tms);
182 term->tl_cols = atoi((char *)p);
183 }
184 if (term->tl_rows == 0)
185 term->tl_rows = curwin->w_height;
186 else
187 {
188 win_setheight_win(term->tl_rows, curwin);
189 term->tl_rows_fixed = TRUE;
190 }
191 if (term->tl_cols == 0)
192 term->tl_cols = curwin->w_width;
193 else
194 {
195 win_setwidth_win(term->tl_cols, curwin);
196 term->tl_cols_fixed = TRUE;
197 }
198}
199
200/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200201 * Initialize job options for a terminal job.
202 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200203 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200204 static void
205init_job_options(jobopt_T *opt)
206{
207 clear_job_options(opt);
208
209 opt->jo_mode = MODE_RAW;
210 opt->jo_out_mode = MODE_RAW;
211 opt->jo_err_mode = MODE_RAW;
212 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
213
214 opt->jo_io[PART_OUT] = JIO_BUFFER;
215 opt->jo_io[PART_ERR] = JIO_BUFFER;
216 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
217
218 opt->jo_modifiable[PART_OUT] = 0;
219 opt->jo_modifiable[PART_ERR] = 0;
220 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
221
222 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
223}
224
225/*
226 * Set job options mandatory for a terminal job.
227 */
228 static void
229setup_job_options(jobopt_T *opt, int rows, int cols)
230{
231 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
232 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
233 opt->jo_pty = TRUE;
234 opt->jo_term_rows = rows;
235 opt->jo_term_cols = cols;
236}
237
238 static void
239term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200240{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200241 exarg_T split_ea;
242 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200243 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200244
245 if (check_restricted() || check_secure())
246 return;
247
248 term = (term_T *)alloc_clear(sizeof(term_T));
249 if (term == NULL)
250 return;
251 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200252 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200253 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254
255 /* Open a new window or tab. */
256 vim_memset(&split_ea, 0, sizeof(split_ea));
257 split_ea.cmdidx = CMD_new;
258 split_ea.cmd = (char_u *)"new";
259 split_ea.arg = (char_u *)"";
260 ex_splitview(&split_ea);
261 if (curwin == old_curwin)
262 {
263 /* split failed */
264 vim_free(term);
265 return;
266 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200267 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200268 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200269
270 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200271 term->tl_next = first_term;
272 first_term = term;
273
Bram Moolenaar293424c2017-07-26 23:11:01 +0200274 if (cmd == NULL || *cmd == NUL)
275 cmd = p_sh;
276
Bram Moolenaar78712a72017-08-05 14:50:12 +0200277 if (opt->jo_term_name != NULL)
278 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
279 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200280 {
281 int i;
282 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200283 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200284
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200285 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200286 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200287 /* Prepend a ! to the command name to avoid the buffer name equals
288 * the executable, otherwise ":w!" would overwrite it. */
289 if (i == 0)
290 vim_snprintf((char *)p, len, "!%s", cmd);
291 else
292 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200293 if (buflist_findname(p) == NULL)
294 {
295 curbuf->b_ffname = p;
296 break;
297 }
298 }
299 }
300 curbuf->b_fname = curbuf->b_ffname;
301
Bram Moolenaareb44a682017-08-03 22:44:55 +0200302 set_string_option_direct((char_u *)"buftype", -1,
303 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
304
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200305 /* Mark the buffer as not modifiable. It can only be made modifiable after
306 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200307 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200308
309 /* Set 'bufhidden' to "hide": allow closing the window. */
310 set_string_option_direct((char_u *)"bufhidden", -1,
311 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200312
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200313 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200314 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200315
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200316 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200317 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200318 {
319 /* store the size we ended up with */
320 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200321 }
322 else
323 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200324 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200325
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200326 /* Wiping out the buffer will also close the window and call
327 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200328 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200329 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200330}
331
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200332/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200333 * ":terminal": open a terminal window and execute a job in it.
334 */
335 void
336ex_terminal(exarg_T *eap)
337{
338 jobopt_T opt;
339
340 init_job_options(&opt);
341 /* TODO: get options from before the command */
342
343 term_start(eap->arg, &opt);
344}
345
346/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200347 * Free the scrollback buffer for "term".
348 */
349 static void
350free_scrollback(term_T *term)
351{
352 int i;
353
354 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
355 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
356 ga_clear(&term->tl_scrollback);
357}
358
359/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200360 * Free a terminal and everything it refers to.
361 * Kills the job if there is one.
362 * Called when wiping out a buffer.
363 */
364 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200365free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200366{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200367 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200368 term_T *tp;
369
370 if (term == NULL)
371 return;
372 if (first_term == term)
373 first_term = term->tl_next;
374 else
375 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
376 if (tp->tl_next == term)
377 {
378 tp->tl_next = term->tl_next;
379 break;
380 }
381
382 if (term->tl_job != NULL)
383 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200384 if (term->tl_job->jv_status != JOB_ENDED
385 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200386 job_stop(term->tl_job, NULL, "kill");
387 job_unref(term->tl_job);
388 }
389
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200390 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200391
392 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200393 vim_free(term->tl_title);
394 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200395 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200396 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200397}
398
399/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200400 * Write job output "msg[len]" to the vterm.
401 */
402 static void
403term_write_job_output(term_T *term, char_u *msg, size_t len)
404{
405 VTerm *vterm = term->tl_vterm;
406 char_u *p;
407 size_t done;
408 size_t len_now;
409
410 for (done = 0; done < len; done += len_now)
411 {
412 for (p = msg + done; p < msg + len; )
413 {
414 if (*p == NL)
415 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200416 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200417 }
418 len_now = p - msg - done;
419 vterm_input_write(vterm, (char *)msg + done, len_now);
420 if (p < msg + len && *p == NL)
421 {
422 /* Convert NL to CR-NL, that appears to work best. */
423 vterm_input_write(vterm, "\r\n", 2);
424 ++len_now;
425 }
426 }
427
428 /* this invokes the damage callbacks */
429 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
430}
431
Bram Moolenaar1c844932017-07-24 23:36:41 +0200432 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200433update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200434{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200435 if (term->tl_terminal_mode)
436 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200437 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200438 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200439 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200440 if (term->tl_cursor_visible)
441 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200442 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200443#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200444 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200445 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200446#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200447 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200448}
449
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200450/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200451 * Invoked when "msg" output from a job was received. Write it to the terminal
452 * of "buffer".
453 */
454 void
455write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
456{
457 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200458 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200459
Bram Moolenaard85f2712017-07-28 21:51:57 +0200460 if (term->tl_vterm == NULL)
461 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200462 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200463 return;
464 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200465 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200466 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200467
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200468 if (!term->tl_terminal_mode)
469 {
470 /* TODO: only update once in a while. */
471 update_screen(0);
472 update_cursor(term, TRUE);
473 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200474}
475
476/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200477 * Send a mouse position and click to the vterm
478 */
479 static int
480term_send_mouse(VTerm *vterm, int button, int pressed)
481{
482 VTermModifier mod = VTERM_MOD_NONE;
483
484 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
485 mouse_col - W_WINCOL(curwin), mod);
486 vterm_mouse_button(vterm, button, pressed, mod);
487 return TRUE;
488}
489
490/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200491 * Convert typed key "c" into bytes to send to the job.
492 * Return the number of bytes in "buf".
493 */
494 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200495term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200496{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200497 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200498 VTermKey key = VTERM_KEY_NONE;
499 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200500 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200501
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200502 switch (c)
503 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200504 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200505 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200506 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
507 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200508 case K_DEL: key = VTERM_KEY_DEL; break;
509 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200510 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
511 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200512 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200513 case K_S_END: mod = VTERM_MOD_SHIFT;
514 key = VTERM_KEY_END; break;
515 case K_C_END: mod = VTERM_MOD_CTRL;
516 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200517 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
518 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
519 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
520 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
521 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
522 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
523 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
524 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
525 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
526 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
527 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
528 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
529 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200530 case K_S_HOME: mod = VTERM_MOD_SHIFT;
531 key = VTERM_KEY_HOME; break;
532 case K_C_HOME: mod = VTERM_MOD_CTRL;
533 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200534 case K_INS: key = VTERM_KEY_INS; break;
535 case K_K0: key = VTERM_KEY_KP_0; break;
536 case K_K1: key = VTERM_KEY_KP_1; break;
537 case K_K2: key = VTERM_KEY_KP_2; break;
538 case K_K3: key = VTERM_KEY_KP_3; break;
539 case K_K4: key = VTERM_KEY_KP_4; break;
540 case K_K5: key = VTERM_KEY_KP_5; break;
541 case K_K6: key = VTERM_KEY_KP_6; break;
542 case K_K7: key = VTERM_KEY_KP_7; break;
543 case K_K8: key = VTERM_KEY_KP_8; break;
544 case K_K9: key = VTERM_KEY_KP_9; break;
545 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
546 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
547 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
548 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
549 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
550 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
551 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
552 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
553 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
554 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
555 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
556 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
557 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200558 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
559 key = VTERM_KEY_LEFT; break;
560 case K_C_LEFT: mod = VTERM_MOD_CTRL;
561 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200562 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
563 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
564 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200565 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
566 key = VTERM_KEY_RIGHT; break;
567 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
568 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200569 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200570 case K_S_UP: mod = VTERM_MOD_SHIFT;
571 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200572 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200573
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200574 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
575 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
576 case K_MOUSELEFT: /* TODO */ return 0;
577 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200578
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200579 case K_LEFTMOUSE:
580 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
581 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
582 case K_LEFTRELEASE:
583 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
584 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
585 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
586 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
587 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
588 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
589 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
590 case K_X1MOUSE: /* TODO */ return 0;
591 case K_X1DRAG: /* TODO */ return 0;
592 case K_X1RELEASE: /* TODO */ return 0;
593 case K_X2MOUSE: /* TODO */ return 0;
594 case K_X2DRAG: /* TODO */ return 0;
595 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200596
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200597 case K_IGNORE: return 0;
598 case K_NOP: return 0;
599 case K_UNDO: return 0;
600 case K_HELP: return 0;
601 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
602 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
603 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
604 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
605 case K_SELECT: return 0;
606#ifdef FEAT_GUI
607 case K_VER_SCROLLBAR: return 0;
608 case K_HOR_SCROLLBAR: return 0;
609#endif
610#ifdef FEAT_GUI_TABLINE
611 case K_TABLINE: return 0;
612 case K_TABMENU: return 0;
613#endif
614#ifdef FEAT_NETBEANS_INTG
615 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
616#endif
617#ifdef FEAT_DND
618 case K_DROP: return 0;
619#endif
620#ifdef FEAT_AUTOCMD
621 case K_CURSORHOLD: return 0;
622#endif
623 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
624 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200625 }
626
627 /*
628 * Convert special keys to vterm keys:
629 * - Write keys to vterm: vterm_keyboard_key()
630 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200631 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200632 */
633 if (key != VTERM_KEY_NONE)
634 /* Special key, let vterm convert it. */
635 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200636 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200637 /* Normal character, let vterm convert it. */
638 vterm_keyboard_unichar(vterm, c, mod);
639
640 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200641 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200642}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200643
Bram Moolenaar938783d2017-07-16 20:13:26 +0200644/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200645 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200646 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200647 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200648term_job_running(term_T *term)
649{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200650 /* Also consider the job finished when the channel is closed, to avoid a
651 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200652 return term != NULL
653 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200654 && term->tl_job->jv_status == JOB_STARTED
655 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200656}
657
658/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200659 * Add the last line of the scrollback buffer to the buffer in the window.
660 */
661 static void
662add_scrollback_line_to_buffer(term_T *term)
663{
664 linenr_T lnum = term->tl_scrollback.ga_len - 1;
665 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
666 garray_T ga;
667 int c;
668 int col;
669 int i;
670
671 ga_init2(&ga, 1, 100);
672 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
673 {
674 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
675 goto failed;
676 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
677 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
678 (char_u *)ga.ga_data + ga.ga_len);
679 }
680 if (ga_grow(&ga, 1) == FAIL)
681 goto failed;
682 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
683 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
684
685 if (lnum == 0)
686 {
687 /* Delete the empty line that was in the empty buffer. */
688 curbuf = term->tl_buffer;
689 ml_delete(2, FALSE);
690 curbuf = curwin->w_buffer;
691 }
692
693failed:
694 ga_clear(&ga);
695}
696
697/*
698 * Add the current lines of the terminal to scrollback and to the buffer.
699 * Called after the job has ended and when switching to Terminal mode.
700 */
701 static void
702move_terminal_to_buffer(term_T *term)
703{
704 win_T *wp;
705 int len;
706 int lines_skipped = 0;
707 VTermPos pos;
708 VTermScreenCell cell;
709 VTermScreenCell *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200710 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200711
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200712 if (term->tl_vterm == NULL)
713 return;
714 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200715 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
716 {
717 len = 0;
718 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
719 if (vterm_screen_get_cell(screen, pos, &cell) != 0
720 && cell.chars[0] != NUL)
721 len = pos.col + 1;
722
723 if (len == 0)
724 ++lines_skipped;
725 else
726 {
727 while (lines_skipped > 0)
728 {
729 /* Line was skipped, add an empty line. */
730 --lines_skipped;
731 if (ga_grow(&term->tl_scrollback, 1) == OK)
732 {
733 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
734 + term->tl_scrollback.ga_len;
735
736 line->sb_cols = 0;
737 line->sb_cells = NULL;
738 ++term->tl_scrollback.ga_len;
739
740 add_scrollback_line_to_buffer(term);
741 }
742 }
743
744 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
745 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
746 {
747 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
748 + term->tl_scrollback.ga_len;
749
750 for (pos.col = 0; pos.col < len; ++pos.col)
751 {
752 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
753 vim_memset(p + pos.col, 0, sizeof(cell));
754 else
755 p[pos.col] = cell;
756 }
757 line->sb_cols = len;
758 line->sb_cells = p;
759 ++term->tl_scrollback.ga_len;
760
761 add_scrollback_line_to_buffer(term);
762 }
763 else
764 vim_free(p);
765 }
766 }
767
768 FOR_ALL_WINDOWS(wp)
769 {
770 if (wp->w_buffer == term->tl_buffer)
771 {
772 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
773 wp->w_cursor.col = 0;
774 wp->w_valid = 0;
775 redraw_win_later(wp, NOT_VALID);
776 }
777 }
778}
779
780 static void
781set_terminal_mode(term_T *term, int on)
782{
783 term->tl_terminal_mode = on;
784 vim_free(term->tl_status_text);
785 term->tl_status_text = NULL;
786 if (term->tl_buffer == curbuf)
787 maketitle();
788}
789
790/*
791 * Called after the job if finished and Terminal mode is not active:
792 * Move the vterm contents into the scrollback buffer and free the vterm.
793 */
794 static void
795cleanup_vterm(term_T *term)
796{
797 move_terminal_to_buffer(term);
798 term_free_vterm(term);
799 set_terminal_mode(term, FALSE);
800}
801
802/*
803 * Switch from sending keys to the job to Terminal-Normal mode.
804 * Suspends updating the terminal window.
805 */
806 static void
807term_enter_terminal_mode()
808{
809 term_T *term = curbuf->b_term;
810
811 /* Append the current terminal contents to the buffer. */
812 move_terminal_to_buffer(term);
813
814 set_terminal_mode(term, TRUE);
815}
816
817/*
818 * Returns TRUE if the current window contains a terminal and we are in
819 * Terminal-Normal mode.
820 */
821 int
822term_in_terminal_mode()
823{
824 term_T *term = curbuf->b_term;
825
826 return term != NULL && term->tl_terminal_mode;
827}
828
829/*
830 * Switch from Terminal-Normal mode to sending keys to the job.
831 * Restores updating the terminal window.
832 */
833 void
834term_leave_terminal_mode()
835{
836 term_T *term = curbuf->b_term;
837 sb_line_T *line;
838 garray_T *gap;
839
840 /* Remove the terminal contents from the scrollback and the buffer. */
841 gap = &term->tl_scrollback;
842 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
843 {
844 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
845 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
846 vim_free(line->sb_cells);
847 --gap->ga_len;
848 if (gap->ga_len == 0)
849 break;
850 }
851 check_cursor();
852
853 set_terminal_mode(term, FALSE);
854
855 if (term->tl_channel_closed)
856 cleanup_vterm(term);
857 redraw_buf_and_status_later(curbuf, NOT_VALID);
858}
859
860/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200861 * Get a key from the user without mapping.
862 * TODO: use terminal mode mappings.
863 */
864 static int
865term_vgetc()
866{
867 int c;
868
869 ++no_mapping;
870 ++allow_keys;
871 got_int = FALSE;
872 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200873 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200874 --no_mapping;
875 --allow_keys;
876 return c;
877}
878
879/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200880 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200881 * Return FAIL when the key needs to be handled in Normal mode.
882 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200883 */
884 static int
885send_keys_to_term(term_T *term, int c, int typed)
886{
887 char msg[KEY_BUF_LEN];
888 size_t len;
889 static int mouse_was_outside = FALSE;
890 int dragging_outside = FALSE;
891
892 /* Catch keys that need to be handled as in Normal mode. */
893 switch (c)
894 {
895 case NUL:
896 case K_ZERO:
897 if (typed)
898 stuffcharReadbuff(c);
899 return FAIL;
900
901 case K_IGNORE:
902 return FAIL;
903
904 case K_LEFTDRAG:
905 case K_MIDDLEDRAG:
906 case K_RIGHTDRAG:
907 case K_X1DRAG:
908 case K_X2DRAG:
909 dragging_outside = mouse_was_outside;
910 /* FALLTHROUGH */
911 case K_LEFTMOUSE:
912 case K_LEFTMOUSE_NM:
913 case K_LEFTRELEASE:
914 case K_LEFTRELEASE_NM:
915 case K_MIDDLEMOUSE:
916 case K_MIDDLERELEASE:
917 case K_RIGHTMOUSE:
918 case K_RIGHTRELEASE:
919 case K_X1MOUSE:
920 case K_X1RELEASE:
921 case K_X2MOUSE:
922 case K_X2RELEASE:
923 if (mouse_row < W_WINROW(curwin)
924 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
925 || mouse_col < W_WINCOL(curwin)
926 || mouse_col >= W_ENDCOL(curwin)
927 || dragging_outside)
928 {
929 /* click outside the current window */
930 if (typed)
931 {
932 stuffcharReadbuff(c);
933 mouse_was_outside = TRUE;
934 }
935 return FAIL;
936 }
937 }
938 if (typed)
939 mouse_was_outside = FALSE;
940
941 /* Convert the typed key to a sequence of bytes for the job. */
942 len = term_convert_key(term, c, msg);
943 if (len > 0)
944 /* TODO: if FAIL is returned, stop? */
945 channel_send(term->tl_job->jv_channel, PART_IN,
946 (char_u *)msg, (int)len, NULL);
947
948 return OK;
949}
950
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200951 static void
952position_cursor(win_T *wp, VTermPos *pos)
953{
954 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
955 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
956 wp->w_valid |= (VALID_WCOL|VALID_WROW);
957}
958
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200959/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200960 * Handle CTRL-W "": send register contents to the job.
961 */
962 static void
963term_paste_register(int prev_c UNUSED)
964{
965 int c;
966 list_T *l;
967 listitem_T *item;
968 long reglen = 0;
969 int type;
970
971#ifdef FEAT_CMDL_INFO
972 if (add_to_showcmd(prev_c))
973 if (add_to_showcmd('"'))
974 out_flush();
975#endif
976 c = term_vgetc();
977#ifdef FEAT_CMDL_INFO
978 clear_showcmd();
979#endif
980
981 /* CTRL-W "= prompt for expression to evaluate. */
982 if (c == '=' && get_expr_register() != '=')
983 return;
984
985 l = (list_T *)get_reg_contents(c, GREG_LIST);
986 if (l != NULL)
987 {
988 type = get_reg_type(c, &reglen);
989 for (item = l->lv_first; item != NULL; item = item->li_next)
990 {
991 char_u *s = get_tv_string(&item->li_tv);
992
993 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
994 s, STRLEN(s), NULL);
995 if (item->li_next != NULL || type == MLINE)
996 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
997 (char_u *)"\r", 1, NULL);
998 }
999 list_free(l);
1000 }
1001}
1002
1003/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001004 * Returns TRUE if the current window contains a terminal and we are sending
1005 * keys to the job.
1006 */
1007 int
1008term_use_loop()
1009{
1010 term_T *term = curbuf->b_term;
1011
1012 return term != NULL
1013 && !term->tl_terminal_mode
1014 && term->tl_vterm != NULL
1015 && term_job_running(term);
1016}
1017
1018/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001019 * Wait for input and send it to the job.
1020 * Return when the start of a CTRL-W command is typed or anything else that
1021 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001022 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1023 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001024 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001025 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001026terminal_loop(void)
1027{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001028 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001029 int termkey = 0;
1030
1031 if (*curwin->w_p_tk != NUL)
1032 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001033 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001034
1035 for (;;)
1036 {
1037 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001038 /* Repeat redrawing in case a message is received while redrawing. */
1039 while (curwin->w_redr_type != 0)
1040 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001041 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001042
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001043 c = term_vgetc();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001044 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001045 /* job finished while waiting for a character */
1046 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001047
Bram Moolenaarfae42832017-08-01 22:24:26 +02001048#ifdef UNIX
1049 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1050#endif
1051#ifdef WIN3264
1052 if (c == Ctrl_C)
1053 /* We don't know if the job can handle CTRL-C itself or not, this
1054 * may kill the shell instead of killing the command running in the
1055 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001056 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001057#endif
1058
Bram Moolenaar69198192017-08-05 14:10:48 +02001059 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001060 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001061 int prev_c = c;
1062
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001063#ifdef FEAT_CMDL_INFO
1064 if (add_to_showcmd(c))
1065 out_flush();
1066#endif
1067 c = term_vgetc();
1068#ifdef FEAT_CMDL_INFO
1069 clear_showcmd();
1070#endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001071 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001072 /* job finished while waiting for a character */
1073 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001074
Bram Moolenaar69198192017-08-05 14:10:48 +02001075 if (prev_c == Ctrl_BSL)
1076 {
1077 if (c == Ctrl_N)
1078 /* CTRL-\ CTRL-N : execute one Normal mode command. */
1079 return OK;
1080 /* Send both keys to the terminal. */
1081 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1082 }
1083 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001084 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001085 /* "CTRL-W .": send CTRL-W to the job */
1086 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001087 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001088 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001089 {
1090 term_enter_terminal_mode();
1091 return FAIL;
1092 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001093 else if (c == '"')
1094 {
1095 term_paste_register(prev_c);
1096 continue;
1097 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001098 else if (termkey == 0 || c != termkey)
1099 {
1100 stuffcharReadbuff(Ctrl_W);
1101 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001102 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001103 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001104 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001105 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1106 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001107 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001108 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001109}
1110
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001111/*
1112 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001113 * This updates the title and status, but does not close the vter, because
1114 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001115 */
1116 void
1117term_job_ended(job_T *job)
1118{
1119 term_T *term;
1120 int did_one = FALSE;
1121
1122 for (term = first_term; term != NULL; term = term->tl_next)
1123 if (term->tl_job == job)
1124 {
1125 vim_free(term->tl_title);
1126 term->tl_title = NULL;
1127 vim_free(term->tl_status_text);
1128 term->tl_status_text = NULL;
1129 redraw_buf_and_status_later(term->tl_buffer, VALID);
1130 did_one = TRUE;
1131 }
1132 if (did_one)
1133 redraw_statuslines();
1134 if (curbuf->b_term != NULL)
1135 {
1136 if (curbuf->b_term->tl_job == job)
1137 maketitle();
1138 update_cursor(curbuf->b_term, TRUE);
1139 }
1140}
1141
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001142 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001143may_toggle_cursor(term_T *term)
1144{
1145 if (curbuf == term->tl_buffer)
1146 {
1147 if (term->tl_cursor_visible)
1148 cursor_on();
1149 else
1150 cursor_off();
1151 }
1152}
1153
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001154 static int
1155handle_damage(VTermRect rect, void *user)
1156{
1157 term_T *term = (term_T *)user;
1158
1159 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1160 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1161 redraw_buf_later(term->tl_buffer, NOT_VALID);
1162 return 1;
1163}
1164
1165 static int
1166handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1167{
1168 term_T *term = (term_T *)user;
1169
1170 /* TODO */
1171 redraw_buf_later(term->tl_buffer, NOT_VALID);
1172 return 1;
1173}
1174
1175 static int
1176handle_movecursor(
1177 VTermPos pos,
1178 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001179 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001180 void *user)
1181{
1182 term_T *term = (term_T *)user;
1183 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001184
1185 term->tl_cursor_pos = pos;
1186 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001187
1188 FOR_ALL_WINDOWS(wp)
1189 {
1190 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001191 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001192 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001193 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001194 {
1195 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001196 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001197 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001198
1199 return 1;
1200}
1201
Bram Moolenaar21554412017-07-24 21:44:43 +02001202 static int
1203handle_settermprop(
1204 VTermProp prop,
1205 VTermValue *value,
1206 void *user)
1207{
1208 term_T *term = (term_T *)user;
1209
1210 switch (prop)
1211 {
1212 case VTERM_PROP_TITLE:
1213 vim_free(term->tl_title);
1214 term->tl_title = vim_strsave((char_u *)value->string);
1215 vim_free(term->tl_status_text);
1216 term->tl_status_text = NULL;
1217 if (term == curbuf->b_term)
1218 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001219 break;
1220
1221 case VTERM_PROP_CURSORVISIBLE:
1222 term->tl_cursor_visible = value->boolean;
1223 may_toggle_cursor(term);
1224 out_flush();
1225 break;
1226
Bram Moolenaar21554412017-07-24 21:44:43 +02001227 default:
1228 break;
1229 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001230 /* Always return 1, otherwise vterm doesn't store the value internally. */
1231 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001232}
1233
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001234/*
1235 * The job running in the terminal resized the terminal.
1236 */
1237 static int
1238handle_resize(int rows, int cols, void *user)
1239{
1240 term_T *term = (term_T *)user;
1241 win_T *wp;
1242
1243 term->tl_rows = rows;
1244 term->tl_cols = cols;
1245 FOR_ALL_WINDOWS(wp)
1246 {
1247 if (wp->w_buffer == term->tl_buffer)
1248 {
1249 win_setheight_win(rows, wp);
1250 win_setwidth_win(cols, wp);
1251 }
1252 }
1253
1254 redraw_buf_later(term->tl_buffer, NOT_VALID);
1255 return 1;
1256}
1257
Bram Moolenaard85f2712017-07-28 21:51:57 +02001258/*
1259 * Handle a line that is pushed off the top of the screen.
1260 */
1261 static int
1262handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1263{
1264 term_T *term = (term_T *)user;
1265
1266 /* TODO: Limit the number of lines that are stored. */
1267 /* TODO: put the text in the buffer. */
1268 if (ga_grow(&term->tl_scrollback, 1) == OK)
1269 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001270 VTermScreenCell *p = NULL;
1271 int len = 0;
1272 int i;
1273 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001274
1275 /* do not store empty cells at the end */
1276 for (i = 0; i < cols; ++i)
1277 if (cells[i].chars[0] != 0)
1278 len = i + 1;
1279
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001280 if (len > 0)
1281 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001282 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001283 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001284
1285 line = (sb_line_T *)term->tl_scrollback.ga_data
1286 + term->tl_scrollback.ga_len;
1287 line->sb_cols = len;
1288 line->sb_cells = p;
1289 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001290 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001291
1292 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001293 }
1294 return 0; /* ignored */
1295}
1296
Bram Moolenaar21554412017-07-24 21:44:43 +02001297static VTermScreenCallbacks screen_callbacks = {
1298 handle_damage, /* damage */
1299 handle_moverect, /* moverect */
1300 handle_movecursor, /* movecursor */
1301 handle_settermprop, /* settermprop */
1302 NULL, /* bell */
1303 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001304 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001305 NULL /* sb_popline */
1306};
1307
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001308/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001309 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001310 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001311 */
1312 void
1313term_channel_closed(channel_T *ch)
1314{
1315 term_T *term;
1316 int did_one = FALSE;
1317
1318 for (term = first_term; term != NULL; term = term->tl_next)
1319 if (term->tl_job == ch->ch_job)
1320 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001321 term->tl_channel_closed = TRUE;
1322
Bram Moolenaard85f2712017-07-28 21:51:57 +02001323 vim_free(term->tl_title);
1324 term->tl_title = NULL;
1325 vim_free(term->tl_status_text);
1326 term->tl_status_text = NULL;
1327
Bram Moolenaar423802d2017-07-30 16:52:24 +02001328 /* Unless in Terminal-Normal mode: clear the vterm. */
1329 if (!term->tl_terminal_mode)
1330 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001331
1332 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1333 did_one = TRUE;
1334 }
1335 if (did_one)
1336 {
1337 redraw_statuslines();
1338
1339 /* Need to break out of vgetc(). */
1340 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001341 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001342
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001343 term = curbuf->b_term;
1344 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001345 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001346 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001347 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001348 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001349 }
1350 }
1351}
1352
1353/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001354 * Reverse engineer the RGB value into a cterm color index.
1355 * First color is 1. Return 0 if no match found.
1356 */
1357 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001358color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001359{
1360 int red = color->red;
1361 int blue = color->blue;
1362 int green = color->green;
1363
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001364 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001365 if (red == 0)
1366 {
1367 if (green == 0)
1368 {
1369 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001370 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001371 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001372 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001373 }
1374 else if (green == 224)
1375 {
1376 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001377 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001378 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001379 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001380 }
1381 }
1382 else if (red == 224)
1383 {
1384 if (green == 0)
1385 {
1386 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001387 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001388 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001389 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001390 }
1391 else if (green == 224)
1392 {
1393 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001394 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001395 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001396 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001397 }
1398 }
1399 else if (red == 128)
1400 {
1401 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001402 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001403 }
1404 else if (red == 255)
1405 {
1406 if (green == 64)
1407 {
1408 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001409 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001410 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001411 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001412 }
1413 else if (green == 255)
1414 {
1415 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001416 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001417 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001418 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001419 }
1420 }
1421 else if (red == 64)
1422 {
1423 if (green == 64)
1424 {
1425 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001426 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001427 }
1428 else if (green == 255)
1429 {
1430 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001431 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001432 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001433 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001434 }
1435 }
1436 if (t_colors >= 256)
1437 {
1438 if (red == blue && red == green)
1439 {
1440 /* 24-color greyscale */
1441 static int cutoff[23] = {
1442 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1443 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1444 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1445 int i;
1446
1447 for (i = 0; i < 23; ++i)
1448 if (red < cutoff[i])
1449 return i + 233;
1450 return 256;
1451 }
1452
1453 /* 216-color cube */
1454 return 17 + ((red + 25) / 0x33) * 36
1455 + ((green + 25) / 0x33) * 6
1456 + (blue + 25) / 0x33;
1457 }
1458 return 0;
1459}
1460
1461/*
1462 * Convert the attributes of a vterm cell into an attribute index.
1463 */
1464 static int
1465cell2attr(VTermScreenCell *cell)
1466{
1467 int attr = 0;
1468
1469 if (cell->attrs.bold)
1470 attr |= HL_BOLD;
1471 if (cell->attrs.underline)
1472 attr |= HL_UNDERLINE;
1473 if (cell->attrs.italic)
1474 attr |= HL_ITALIC;
1475 if (cell->attrs.strike)
1476 attr |= HL_STANDOUT;
1477 if (cell->attrs.reverse)
1478 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001479
1480#ifdef FEAT_GUI
1481 if (gui.in_use)
1482 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001483 guicolor_T fg, bg;
1484
1485 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1486 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1487 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001488 }
1489 else
1490#endif
1491#ifdef FEAT_TERMGUICOLORS
1492 if (p_tgc)
1493 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001494 guicolor_T fg, bg;
1495
1496 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1497 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1498
1499 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001500 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001501 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001502#endif
1503 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001504 int bold = MAYBE;
1505 int fg = color2index(&cell->fg, TRUE, &bold);
1506 int bg = color2index(&cell->bg, FALSE, &bold);
1507
1508 /* with 8 colors set the bold attribute to get a bright foreground */
1509 if (bold == TRUE)
1510 attr |= HL_BOLD;
1511 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001512 }
1513 return 0;
1514}
1515
1516/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001517 * Called to update the window that contains a terminal.
1518 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001519 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001520 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001521term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001522{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001523 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001524 VTerm *vterm;
1525 VTermScreen *screen;
1526 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001527 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001528
Bram Moolenaar423802d2017-07-30 16:52:24 +02001529 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001530 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001531
Bram Moolenaard85f2712017-07-28 21:51:57 +02001532 vterm = term->tl_vterm;
1533 screen = vterm_obtain_screen(vterm);
1534 state = vterm_obtain_state(vterm);
1535
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001536 /*
1537 * If the window was resized a redraw will be triggered and we get here.
1538 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1539 */
1540 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1541 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001542 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001543 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1544 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1545 win_T *twp;
1546
1547 FOR_ALL_WINDOWS(twp)
1548 {
1549 /* When more than one window shows the same terminal, use the
1550 * smallest size. */
1551 if (twp->w_buffer == term->tl_buffer)
1552 {
1553 if (!term->tl_rows_fixed && rows > twp->w_height)
1554 rows = twp->w_height;
1555 if (!term->tl_cols_fixed && cols > twp->w_width)
1556 cols = twp->w_width;
1557 }
1558 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001559
1560 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001561 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001562 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001563 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001564 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001565
1566 /* The cursor may have been moved when resizing. */
1567 vterm_state_get_cursorpos(state, &pos);
1568 position_cursor(wp, &pos);
1569
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001570 /* TODO: Only redraw what changed. */
1571 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001572 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001573 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001574 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001575
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001576 if (pos.row < term->tl_rows)
1577 {
1578 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001579 {
1580 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001581 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001582
Bram Moolenaareeac6772017-07-23 15:48:37 +02001583 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1584 vim_memset(&cell, 0, sizeof(cell));
1585
1586 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001587 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001588 if (c == NUL)
1589 {
1590 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001591#if defined(FEAT_MBYTE)
1592 if (enc_utf8)
1593 ScreenLinesUC[off] = NUL;
1594#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001595 }
1596 else
1597 {
1598#if defined(FEAT_MBYTE)
1599 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001600 {
1601 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001602 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001603 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001604 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001605 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001606 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001607 if (enc_utf8)
1608 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001609 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001610#else
1611 ScreenLines[off] = c;
1612#endif
1613 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001614 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001615
1616 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001617 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001618 if (cell.width == 2)
1619 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001620 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001621#if defined(FEAT_MBYTE)
1622 if (enc_utf8)
1623 ScreenLinesUC[off] = NUL;
1624#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001625 ++pos.col;
1626 ++off;
1627 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001628 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001629 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001630 else
1631 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001632
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001633 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1634 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001635 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001636
1637 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001638}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001639
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001640/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001641 * Return TRUE if "wp" is a terminal window where the job has finished.
1642 */
1643 int
1644term_is_finished(buf_T *buf)
1645{
1646 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1647}
1648
1649/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001650 * Return TRUE if "wp" is a terminal window where the job has finished or we
1651 * are in Terminal-Normal mode.
1652 */
1653 int
1654term_show_buffer(buf_T *buf)
1655{
1656 term_T *term = buf->b_term;
1657
1658 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1659}
1660
1661/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001662 * The current buffer is going to be changed. If there is terminal
1663 * highlighting remove it now.
1664 */
1665 void
1666term_change_in_curbuf(void)
1667{
1668 term_T *term = curbuf->b_term;
1669
1670 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1671 {
1672 free_scrollback(term);
1673 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001674
1675 /* The buffer is now like a normal buffer, it cannot be easily
1676 * abandoned when changed. */
1677 set_string_option_direct((char_u *)"buftype", -1,
1678 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001679 }
1680}
1681
1682/*
1683 * Get the screen attribute for a position in the buffer.
1684 */
1685 int
1686term_get_attr(buf_T *buf, linenr_T lnum, int col)
1687{
1688 term_T *term = buf->b_term;
1689 sb_line_T *line;
1690
Bram Moolenaar70229f92017-07-29 16:01:53 +02001691 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001692 return 0;
1693 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1694 if (col >= line->sb_cols)
1695 return 0;
1696 return cell2attr(line->sb_cells + col);
1697}
1698
1699/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001700 * Create a new vterm and initialize it.
1701 */
1702 static void
1703create_vterm(term_T *term, int rows, int cols)
1704{
1705 VTerm *vterm;
1706 VTermScreen *screen;
1707
1708 vterm = vterm_new(rows, cols);
1709 term->tl_vterm = vterm;
1710 screen = vterm_obtain_screen(vterm);
1711 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1712 /* TODO: depends on 'encoding'. */
1713 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001714
1715 /* Vterm uses a default black background. Set it to white when
1716 * 'background' is "light". */
1717 if (*p_bg == 'l')
1718 {
1719 VTermColor fg, bg;
1720
1721 fg.red = fg.green = fg.blue = 0;
1722 bg.red = bg.green = bg.blue = 255;
1723 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1724 }
1725
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001726 /* Required to initialize most things. */
1727 vterm_screen_reset(screen, 1 /* hard */);
1728}
1729
Bram Moolenaar21554412017-07-24 21:44:43 +02001730/*
1731 * Return the text to show for the buffer name and status.
1732 */
1733 char_u *
1734term_get_status_text(term_T *term)
1735{
1736 if (term->tl_status_text == NULL)
1737 {
1738 char_u *txt;
1739 size_t len;
1740
Bram Moolenaar423802d2017-07-30 16:52:24 +02001741 if (term->tl_terminal_mode)
1742 {
1743 if (term_job_running(term))
1744 txt = (char_u *)_("Terminal");
1745 else
1746 txt = (char_u *)_("Terminal-finished");
1747 }
1748 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001749 txt = term->tl_title;
1750 else if (term_job_running(term))
1751 txt = (char_u *)_("running");
1752 else
1753 txt = (char_u *)_("finished");
1754 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001755 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001756 if (term->tl_status_text != NULL)
1757 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1758 term->tl_buffer->b_fname, txt);
1759 }
1760 return term->tl_status_text;
1761}
1762
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001763/*
1764 * Mark references in jobs of terminals.
1765 */
1766 int
1767set_ref_in_term(int copyID)
1768{
1769 int abort = FALSE;
1770 term_T *term;
1771 typval_T tv;
1772
1773 for (term = first_term; term != NULL; term = term->tl_next)
1774 if (term->tl_job != NULL)
1775 {
1776 tv.v_type = VAR_JOB;
1777 tv.vval.v_job = term->tl_job;
1778 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1779 }
1780 return abort;
1781}
1782
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001783/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001784 * Get the buffer from the first argument in "argvars".
1785 * Returns NULL when the buffer is not for a terminal window.
1786 */
1787 static buf_T *
1788term_get_buf(typval_T *argvars)
1789{
1790 buf_T *buf;
1791
1792 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1793 ++emsg_off;
1794 buf = get_buf_tv(&argvars[0], FALSE);
1795 --emsg_off;
1796 if (buf == NULL || buf->b_term == NULL)
1797 return NULL;
1798 return buf;
1799}
1800
1801/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001802 * "term_getattr(attr, name)" function
1803 */
1804 void
1805f_term_getattr(typval_T *argvars, typval_T *rettv)
1806{
1807 int attr;
1808 size_t i;
1809 char_u *name;
1810
1811 static struct {
1812 char *name;
1813 int attr;
1814 } attrs[] = {
1815 {"bold", HL_BOLD},
1816 {"italic", HL_ITALIC},
1817 {"underline", HL_UNDERLINE},
1818 {"strike", HL_STANDOUT},
1819 {"reverse", HL_INVERSE},
1820 };
1821
1822 attr = get_tv_number(&argvars[0]);
1823 name = get_tv_string_chk(&argvars[1]);
1824 if (name == NULL)
1825 return;
1826
1827 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1828 if (STRCMP(name, attrs[i].name) == 0)
1829 {
1830 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1831 break;
1832 }
1833}
1834
1835/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001836 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001837 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001838 void
1839f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001840{
Bram Moolenaar97870002017-07-30 18:28:38 +02001841 buf_T *buf = term_get_buf(argvars);
1842 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001843
Bram Moolenaar97870002017-07-30 18:28:38 +02001844 if (rettv_list_alloc(rettv) == FAIL)
1845 return;
1846 if (buf == NULL)
1847 return;
1848
1849 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001850 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1851 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001852 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001853}
1854
1855/*
1856 * "term_getjob(buf)" function
1857 */
1858 void
1859f_term_getjob(typval_T *argvars, typval_T *rettv)
1860{
1861 buf_T *buf = term_get_buf(argvars);
1862
1863 rettv->v_type = VAR_JOB;
1864 rettv->vval.v_job = NULL;
1865 if (buf == NULL)
1866 return;
1867
1868 rettv->vval.v_job = buf->b_term->tl_job;
1869 if (rettv->vval.v_job != NULL)
1870 ++rettv->vval.v_job->jv_refcount;
1871}
1872
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001873 static int
1874get_row_number(typval_T *tv, term_T *term)
1875{
1876 if (tv->v_type == VAR_STRING
1877 && tv->vval.v_string != NULL
1878 && STRCMP(tv->vval.v_string, ".") == 0)
1879 return term->tl_cursor_pos.row;
1880 return (int)get_tv_number(tv) - 1;
1881}
1882
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001883/*
1884 * "term_getline(buf, row)" function
1885 */
1886 void
1887f_term_getline(typval_T *argvars, typval_T *rettv)
1888{
1889 buf_T *buf = term_get_buf(argvars);
1890 term_T *term;
1891 int row;
1892
1893 rettv->v_type = VAR_STRING;
1894 if (buf == NULL)
1895 return;
1896 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001897 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001898
1899 if (term->tl_vterm == NULL)
1900 {
1901 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1902
1903 /* vterm is finished, get the text from the buffer */
1904 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1905 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1906 }
1907 else
1908 {
1909 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1910 VTermRect rect;
1911 int len;
1912 char_u *p;
1913
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001914 if (row < 0 || row >= term->tl_rows)
1915 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001916 len = term->tl_cols * MB_MAXBYTES + 1;
1917 p = alloc(len);
1918 if (p == NULL)
1919 return;
1920 rettv->vval.v_string = p;
1921
1922 rect.start_col = 0;
1923 rect.end_col = term->tl_cols;
1924 rect.start_row = row;
1925 rect.end_row = row + 1;
1926 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1927 }
1928}
1929
1930/*
1931 * "term_getsize(buf)" function
1932 */
1933 void
1934f_term_getsize(typval_T *argvars, typval_T *rettv)
1935{
1936 buf_T *buf = term_get_buf(argvars);
1937 list_T *l;
1938
1939 if (rettv_list_alloc(rettv) == FAIL)
1940 return;
1941 if (buf == NULL)
1942 return;
1943
1944 l = rettv->vval.v_list;
1945 list_append_number(l, buf->b_term->tl_rows);
1946 list_append_number(l, buf->b_term->tl_cols);
1947}
1948
1949/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001950 * "term_getstatus(buf)" function
1951 */
1952 void
1953f_term_getstatus(typval_T *argvars, typval_T *rettv)
1954{
1955 buf_T *buf = term_get_buf(argvars);
1956 term_T *term;
1957 char_u val[100];
1958
1959 rettv->v_type = VAR_STRING;
1960 if (buf == NULL)
1961 return;
1962 term = buf->b_term;
1963
1964 if (term_job_running(term))
1965 STRCPY(val, "running");
1966 else
1967 STRCPY(val, "finished");
1968 if (term->tl_terminal_mode)
1969 STRCAT(val, ",terminal");
1970 rettv->vval.v_string = vim_strsave(val);
1971}
1972
1973/*
1974 * "term_gettitle(buf)" function
1975 */
1976 void
1977f_term_gettitle(typval_T *argvars, typval_T *rettv)
1978{
1979 buf_T *buf = term_get_buf(argvars);
1980
1981 rettv->v_type = VAR_STRING;
1982 if (buf == NULL)
1983 return;
1984
1985 if (buf->b_term->tl_title != NULL)
1986 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1987}
1988
1989/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001990 * "term_gettty(buf)" function
1991 */
1992 void
1993f_term_gettty(typval_T *argvars, typval_T *rettv)
1994{
1995 buf_T *buf = term_get_buf(argvars);
1996 char_u *p;
1997
1998 rettv->v_type = VAR_STRING;
1999 if (buf == NULL)
2000 return;
2001 if (buf->b_term->tl_job != NULL)
2002 p = buf->b_term->tl_job->jv_tty_name;
2003 else
2004 p = buf->b_term->tl_tty_name;
2005 if (p != NULL)
2006 rettv->vval.v_string = vim_strsave(p);
2007}
2008
2009/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002010 * "term_list()" function
2011 */
2012 void
2013f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2014{
2015 term_T *tp;
2016 list_T *l;
2017
2018 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2019 return;
2020
2021 l = rettv->vval.v_list;
2022 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2023 if (tp != NULL && tp->tl_buffer != NULL)
2024 if (list_append_number(l,
2025 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2026 return;
2027}
2028
2029/*
2030 * "term_scrape(buf, row)" function
2031 */
2032 void
2033f_term_scrape(typval_T *argvars, typval_T *rettv)
2034{
2035 buf_T *buf = term_get_buf(argvars);
2036 VTermScreen *screen = NULL;
2037 VTermPos pos;
2038 list_T *l;
2039 term_T *term;
2040
2041 if (rettv_list_alloc(rettv) == FAIL)
2042 return;
2043 if (buf == NULL)
2044 return;
2045 term = buf->b_term;
2046 if (term->tl_vterm != NULL)
2047 screen = vterm_obtain_screen(term->tl_vterm);
2048
2049 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002050 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002051 for (pos.col = 0; pos.col < term->tl_cols; )
2052 {
2053 dict_T *dcell;
2054 VTermScreenCell cell;
2055 char_u rgb[8];
2056 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2057 int off = 0;
2058 int i;
2059
2060 if (screen == NULL)
2061 {
2062 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2063 sb_line_T *line;
2064
2065 /* vterm has finished, get the cell from scrollback */
2066 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2067 break;
2068 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2069 if (pos.col >= line->sb_cols)
2070 break;
2071 cell = line->sb_cells[pos.col];
2072 }
2073 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2074 break;
2075 dcell = dict_alloc();
2076 list_append_dict(l, dcell);
2077
2078 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2079 {
2080 if (cell.chars[i] == 0)
2081 break;
2082 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2083 }
2084 mbs[off] = NUL;
2085 dict_add_nr_str(dcell, "chars", 0, mbs);
2086
2087 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2088 cell.fg.red, cell.fg.green, cell.fg.blue);
2089 dict_add_nr_str(dcell, "fg", 0, rgb);
2090 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2091 cell.bg.red, cell.bg.green, cell.bg.blue);
2092 dict_add_nr_str(dcell, "bg", 0, rgb);
2093
2094 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2095 dict_add_nr_str(dcell, "width", cell.width, NULL);
2096
2097 ++pos.col;
2098 if (cell.width == 2)
2099 ++pos.col;
2100 }
2101}
2102
2103/*
2104 * "term_sendkeys(buf, keys)" function
2105 */
2106 void
2107f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2108{
2109 buf_T *buf = term_get_buf(argvars);
2110 char_u *msg;
2111 term_T *term;
2112
2113 rettv->v_type = VAR_UNKNOWN;
2114 if (buf == NULL)
2115 return;
2116
2117 msg = get_tv_string_chk(&argvars[1]);
2118 if (msg == NULL)
2119 return;
2120 term = buf->b_term;
2121 if (term->tl_vterm == NULL)
2122 return;
2123
2124 while (*msg != NUL)
2125 {
2126 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2127 msg += MB_PTR2LEN(msg);
2128 }
2129
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002130 if (!term->tl_terminal_mode)
2131 {
2132 /* TODO: only update once in a while. */
2133 update_screen(0);
2134 if (buf == curbuf)
2135 update_cursor(term, TRUE);
2136 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002137}
2138
2139/*
2140 * "term_start(command, options)" function
2141 */
2142 void
2143f_term_start(typval_T *argvars, typval_T *rettv)
2144{
2145 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002146 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002147
2148 if (cmd == NULL)
2149 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002150 init_job_options(&opt);
2151 /* TODO: allow more job options */
2152 if (argvars[1].v_type != VAR_UNKNOWN
2153 && get_job_options(&argvars[1], &opt,
2154 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002155 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2156 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002157 return;
2158
2159 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002160
2161 if (curbuf->b_term != NULL)
2162 rettv->vval.v_number = curbuf->b_fnum;
2163}
2164
2165/*
2166 * "term_wait" function
2167 */
2168 void
2169f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2170{
2171 buf_T *buf = term_get_buf(argvars);
2172
2173 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002174 {
2175 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002176 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002177 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002178 if (buf->b_term->tl_job == NULL)
2179 {
2180 ch_log(NULL, "term_wait(): no job to wait for");
2181 return;
2182 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002183
2184 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002185 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002186 {
2187 /* The job is dead, keep reading channel I/O until the channel is
2188 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002189 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002190 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2191 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002192 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002193 parse_queued_messages();
2194 ui_delay(10L, FALSE);
2195 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002196 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002197 parse_queued_messages();
2198 }
2199 else
2200 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002201 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002202 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002203
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002204 /* Wait for 10 msec for any channel I/O. */
2205 /* TODO: use delay from optional argument */
2206 ui_delay(10L, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002207 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002208
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002209 /* Flushing messages on channels is hopefully sufficient.
2210 * TODO: is there a better way? */
2211 parse_queued_messages();
2212 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002213}
2214
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002215# ifdef WIN3264
2216
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002217/**************************************
2218 * 2. MS-Windows implementation.
2219 */
2220
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002221#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2222#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2223
Bram Moolenaar8a773062017-07-24 22:29:21 +02002224void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002225void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002226void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002227BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2228void (*winpty_config_set_initial_size)(void*, int, int);
2229LPCWSTR (*winpty_conin_name)(void*);
2230LPCWSTR (*winpty_conout_name)(void*);
2231LPCWSTR (*winpty_conerr_name)(void*);
2232void (*winpty_free)(void*);
2233void (*winpty_config_free)(void*);
2234void (*winpty_spawn_config_free)(void*);
2235void (*winpty_error_free)(void*);
2236LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002237BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002238HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002239
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002240#define WINPTY_DLL "winpty.dll"
2241
2242static HINSTANCE hWinPtyDLL = NULL;
2243
2244 int
2245dyn_winpty_init(void)
2246{
2247 int i;
2248 static struct
2249 {
2250 char *name;
2251 FARPROC *ptr;
2252 } winpty_entry[] =
2253 {
2254 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2255 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2256 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2257 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2258 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2259 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2260 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2261 {"winpty_free", (FARPROC*)&winpty_free},
2262 {"winpty_open", (FARPROC*)&winpty_open},
2263 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2264 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2265 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2266 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002267 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002268 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002269 {NULL, NULL}
2270 };
2271
2272 /* No need to initialize twice. */
2273 if (hWinPtyDLL)
2274 return 1;
2275 /* Load winpty.dll */
2276 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2277 if (!hWinPtyDLL)
2278 {
2279 EMSG2(_(e_loadlib), WINPTY_DLL);
2280 return 0;
2281 }
2282 for (i = 0; winpty_entry[i].name != NULL
2283 && winpty_entry[i].ptr != NULL; ++i)
2284 {
2285 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2286 winpty_entry[i].name)) == NULL)
2287 {
2288 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2289 return 0;
2290 }
2291 }
2292
2293 return 1;
2294}
2295
2296/*
2297 * Create a new terminal of "rows" by "cols" cells.
2298 * Store a reference in "term".
2299 * Return OK or FAIL.
2300 */
2301 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002302term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002303{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002304 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002305 channel_T *channel = NULL;
2306 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002307 DWORD error;
2308 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2309 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002310 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002311 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002312
2313 if (!dyn_winpty_init())
2314 return FAIL;
2315
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002316 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002317 if (p == NULL)
2318 return FAIL;
2319
2320 job = job_alloc();
2321 if (job == NULL)
2322 goto failed;
2323
2324 channel = add_channel();
2325 if (channel == NULL)
2326 goto failed;
2327
2328 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2329 if (term->tl_winpty_config == NULL)
2330 goto failed;
2331
2332 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2333 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2334 if (term->tl_winpty == NULL)
2335 goto failed;
2336
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002337 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002338 spawn_config = winpty_spawn_config_new(
2339 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2340 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2341 NULL,
2342 p,
2343 NULL,
2344 NULL,
2345 &winpty_err);
2346 if (spawn_config == NULL)
2347 goto failed;
2348
2349 channel = add_channel();
2350 if (channel == NULL)
2351 goto failed;
2352
2353 job = job_alloc();
2354 if (job == NULL)
2355 goto failed;
2356
2357 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2358 &child_thread_handle, &error, &winpty_err))
2359 goto failed;
2360
2361 channel_set_pipes(channel,
2362 (sock_T) CreateFileW(
2363 winpty_conin_name(term->tl_winpty),
2364 GENERIC_WRITE, 0, NULL,
2365 OPEN_EXISTING, 0, NULL),
2366 (sock_T) CreateFileW(
2367 winpty_conout_name(term->tl_winpty),
2368 GENERIC_READ, 0, NULL,
2369 OPEN_EXISTING, 0, NULL),
2370 (sock_T) CreateFileW(
2371 winpty_conerr_name(term->tl_winpty),
2372 GENERIC_READ, 0, NULL,
2373 OPEN_EXISTING, 0, NULL));
2374
2375 jo = CreateJobObject(NULL, NULL);
2376 if (jo == NULL)
2377 goto failed;
2378
2379 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002380 {
2381 /* Failed, switch the way to terminate process with TerminateProcess. */
2382 CloseHandle(jo);
2383 jo = NULL;
2384 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002385
2386 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002387 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002388
2389 create_vterm(term, rows, cols);
2390
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002391 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002392 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002393
2394 job->jv_channel = channel;
2395 job->jv_proc_info.hProcess = child_process_handle;
2396 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2397 job->jv_job_object = jo;
2398 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002399 sprintf(buf, "winpty://%lu",
2400 GetProcessId(winpty_agent_process(term->tl_winpty)));
2401 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002402 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002403 term->tl_job = job;
2404
2405 return OK;
2406
2407failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002408 if (spawn_config != NULL)
2409 winpty_spawn_config_free(spawn_config);
2410 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002411 if (channel != NULL)
2412 channel_clear(channel);
2413 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002414 {
2415 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002416 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002417 }
2418 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002419 if (jo != NULL)
2420 CloseHandle(jo);
2421 if (term->tl_winpty != NULL)
2422 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002423 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002424 if (term->tl_winpty_config != NULL)
2425 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002426 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002427 if (winpty_err != NULL)
2428 {
2429 char_u *msg = utf16_to_enc(
2430 (short_u *)winpty_error_msg(winpty_err), NULL);
2431
2432 EMSG(msg);
2433 winpty_error_free(winpty_err);
2434 }
2435 return FAIL;
2436}
2437
2438/*
2439 * Free the terminal emulator part of "term".
2440 */
2441 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002442term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002443{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002444 if (term->tl_winpty != NULL)
2445 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002446 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002447 if (term->tl_winpty_config != NULL)
2448 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002449 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002450 if (term->tl_vterm != NULL)
2451 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002452 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002453}
2454
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002455/*
2456 * Request size to terminal.
2457 */
2458 static void
2459term_report_winsize(term_T *term, int rows, int cols)
2460{
2461 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2462}
2463
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002464# else
2465
2466/**************************************
2467 * 3. Unix-like implementation.
2468 */
2469
2470/*
2471 * Create a new terminal of "rows" by "cols" cells.
2472 * Start job for "cmd".
2473 * Store the pointers in "term".
2474 * Return OK or FAIL.
2475 */
2476 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002477term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002478{
2479 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002480
2481 create_vterm(term, rows, cols);
2482
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002483 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002484 argvars[0].v_type = VAR_STRING;
2485 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002486
2487 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002488 if (term->tl_job != NULL)
2489 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002490
Bram Moolenaar61a66052017-07-22 18:39:00 +02002491 return term->tl_job != NULL
2492 && term->tl_job->jv_channel != NULL
2493 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002494}
2495
2496/*
2497 * Free the terminal emulator part of "term".
2498 */
2499 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002500term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002501{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002502 if (term->tl_vterm != NULL)
2503 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002504 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002505}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002506
2507/*
2508 * Request size to terminal.
2509 */
2510 static void
2511term_report_winsize(term_T *term, int rows, int cols)
2512{
2513 /* Use an ioctl() to report the new window size to the job. */
2514 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2515 {
2516 int fd = -1;
2517 int part;
2518
2519 for (part = PART_OUT; part < PART_COUNT; ++part)
2520 {
2521 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2522 if (isatty(fd))
2523 break;
2524 }
2525 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2526 mch_stop_job(term->tl_job, (char_u *)"winch");
2527 }
2528}
2529
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002530# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002531
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002532#endif /* FEAT_TERMINAL */