blob: cc73f4fb9c8d7cb8345e79127717cd9d56f8603d [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 Moolenaar8c0095c2017-07-18 22:53:21 +020095#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020096# define MIN(x,y) (x < y ? x : y)
97# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020098#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020099
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200100#include "libvterm/include/vterm.h"
101
Bram Moolenaard85f2712017-07-28 21:51:57 +0200102typedef struct sb_line_S {
103 int sb_cols; /* can differ per line */
104 VTermScreenCell *sb_cells; /* allocated */
105} sb_line_T;
106
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200107/* typedef term_T in structs.h */
108struct terminal_S {
109 term_T *tl_next;
110
Bram Moolenaar423802d2017-07-30 16:52:24 +0200111 VTerm *tl_vterm;
112 job_T *tl_job;
113 buf_T *tl_buffer;
114
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200115 /* used when tl_job is NULL and only a pty was created */
116 int tl_tty_fd;
117 char_u *tl_tty_name;
118
Bram Moolenaar423802d2017-07-30 16:52:24 +0200119 int tl_terminal_mode;
120 int tl_channel_closed;
121
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200122#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200123 void *tl_winpty_config;
124 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200125#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200127 /* last known vterm size */
128 int tl_rows;
129 int tl_cols;
130 /* vterm size does not follow window size */
131 int tl_rows_fixed;
132 int tl_cols_fixed;
133
Bram Moolenaar21554412017-07-24 21:44:43 +0200134 char_u *tl_title; /* NULL or allocated */
135 char_u *tl_status_text; /* NULL or allocated */
136
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200137 /* Range of screen rows to update. Zero based. */
138 int tl_dirty_row_start; /* -1 if nothing dirty */
139 int tl_dirty_row_end; /* row below last one to update */
140
Bram Moolenaard85f2712017-07-28 21:51:57 +0200141 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200142 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200143
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200144 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200145 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200146};
147
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200148/*
149 * List of all active terminals.
150 */
151static term_T *first_term = NULL;
152
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200153
154#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
155#define KEY_BUF_LEN 200
156
157/*
158 * Functions with separate implementation for MS-Windows and Unix-like systems.
159 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200160static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200161static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200162static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200163
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200164/**************************************
165 * 1. Generic code for all systems.
166 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200167
168/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200169 * Determine the terminal size from 'termsize' and the current window.
170 * Assumes term->tl_rows and term->tl_cols are zero.
171 */
172 static void
173set_term_and_win_size(term_T *term)
174{
175 if (*curwin->w_p_tms != NUL)
176 {
177 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
178
179 term->tl_rows = atoi((char *)curwin->w_p_tms);
180 term->tl_cols = atoi((char *)p);
181 }
182 if (term->tl_rows == 0)
183 term->tl_rows = curwin->w_height;
184 else
185 {
186 win_setheight_win(term->tl_rows, curwin);
187 term->tl_rows_fixed = TRUE;
188 }
189 if (term->tl_cols == 0)
190 term->tl_cols = curwin->w_width;
191 else
192 {
193 win_setwidth_win(term->tl_cols, curwin);
194 term->tl_cols_fixed = TRUE;
195 }
196}
197
198/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200199 * Initialize job options for a terminal job.
200 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200201 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200202 static void
203init_job_options(jobopt_T *opt)
204{
205 clear_job_options(opt);
206
207 opt->jo_mode = MODE_RAW;
208 opt->jo_out_mode = MODE_RAW;
209 opt->jo_err_mode = MODE_RAW;
210 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
211
212 opt->jo_io[PART_OUT] = JIO_BUFFER;
213 opt->jo_io[PART_ERR] = JIO_BUFFER;
214 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
215
216 opt->jo_modifiable[PART_OUT] = 0;
217 opt->jo_modifiable[PART_ERR] = 0;
218 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
219
220 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
221}
222
223/*
224 * Set job options mandatory for a terminal job.
225 */
226 static void
227setup_job_options(jobopt_T *opt, int rows, int cols)
228{
229 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
230 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
231 opt->jo_pty = TRUE;
232 opt->jo_term_rows = rows;
233 opt->jo_term_cols = cols;
234}
235
236 static void
237term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200238{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200239 exarg_T split_ea;
240 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200241 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200242
243 if (check_restricted() || check_secure())
244 return;
245
246 term = (term_T *)alloc_clear(sizeof(term_T));
247 if (term == NULL)
248 return;
249 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200250 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200251 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252
253 /* Open a new window or tab. */
254 vim_memset(&split_ea, 0, sizeof(split_ea));
255 split_ea.cmdidx = CMD_new;
256 split_ea.cmd = (char_u *)"new";
257 split_ea.arg = (char_u *)"";
258 ex_splitview(&split_ea);
259 if (curwin == old_curwin)
260 {
261 /* split failed */
262 vim_free(term);
263 return;
264 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200265 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200266 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200267
268 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200269 term->tl_next = first_term;
270 first_term = term;
271
Bram Moolenaar293424c2017-07-26 23:11:01 +0200272 if (cmd == NULL || *cmd == NUL)
273 cmd = p_sh;
274
Bram Moolenaar78712a72017-08-05 14:50:12 +0200275 if (opt->jo_term_name != NULL)
276 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
277 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200278 {
279 int i;
280 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200281 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200282
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200283 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200284 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200285 /* Prepend a ! to the command name to avoid the buffer name equals
286 * the executable, otherwise ":w!" would overwrite it. */
287 if (i == 0)
288 vim_snprintf((char *)p, len, "!%s", cmd);
289 else
290 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200291 if (buflist_findname(p) == NULL)
292 {
293 curbuf->b_ffname = p;
294 break;
295 }
296 }
297 }
298 curbuf->b_fname = curbuf->b_ffname;
299
Bram Moolenaareb44a682017-08-03 22:44:55 +0200300 set_string_option_direct((char_u *)"buftype", -1,
301 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
302
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200303 /* Mark the buffer as not modifiable. It can only be made modifiable after
304 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200305 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200306
307 /* Set 'bufhidden' to "hide": allow closing the window. */
308 set_string_option_direct((char_u *)"bufhidden", -1,
309 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200310
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200311 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200312 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200313
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200314 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200315 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200316 {
317 /* store the size we ended up with */
318 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200319 }
320 else
321 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200322 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200323
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200324 /* Wiping out the buffer will also close the window and call
325 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200326 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200327 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200328}
329
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200330/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200331 * ":terminal": open a terminal window and execute a job in it.
332 */
333 void
334ex_terminal(exarg_T *eap)
335{
336 jobopt_T opt;
337
338 init_job_options(&opt);
339 /* TODO: get options from before the command */
340
341 term_start(eap->arg, &opt);
342}
343
344/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200345 * Free the scrollback buffer for "term".
346 */
347 static void
348free_scrollback(term_T *term)
349{
350 int i;
351
352 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
353 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
354 ga_clear(&term->tl_scrollback);
355}
356
357/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200358 * Free a terminal and everything it refers to.
359 * Kills the job if there is one.
360 * Called when wiping out a buffer.
361 */
362 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200363free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200364{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200365 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200366 term_T *tp;
367
368 if (term == NULL)
369 return;
370 if (first_term == term)
371 first_term = term->tl_next;
372 else
373 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
374 if (tp->tl_next == term)
375 {
376 tp->tl_next = term->tl_next;
377 break;
378 }
379
380 if (term->tl_job != NULL)
381 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200382 if (term->tl_job->jv_status != JOB_ENDED
383 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200384 job_stop(term->tl_job, NULL, "kill");
385 job_unref(term->tl_job);
386 }
387
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200388 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200389
390 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200391 vim_free(term->tl_title);
392 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200393 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200394 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200395}
396
397/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200398 * Write job output "msg[len]" to the vterm.
399 */
400 static void
401term_write_job_output(term_T *term, char_u *msg, size_t len)
402{
403 VTerm *vterm = term->tl_vterm;
404 char_u *p;
405 size_t done;
406 size_t len_now;
407
408 for (done = 0; done < len; done += len_now)
409 {
410 for (p = msg + done; p < msg + len; )
411 {
412 if (*p == NL)
413 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200414 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200415 }
416 len_now = p - msg - done;
417 vterm_input_write(vterm, (char *)msg + done, len_now);
418 if (p < msg + len && *p == NL)
419 {
420 /* Convert NL to CR-NL, that appears to work best. */
421 vterm_input_write(vterm, "\r\n", 2);
422 ++len_now;
423 }
424 }
425
426 /* this invokes the damage callbacks */
427 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
428}
429
Bram Moolenaar1c844932017-07-24 23:36:41 +0200430 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200431update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200432{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200433 if (term->tl_terminal_mode)
434 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200435 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200436 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200437 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200438 if (term->tl_cursor_visible)
439 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200440 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200441#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200442 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200443 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200444#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200445 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200446}
447
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200448/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200449 * Invoked when "msg" output from a job was received. Write it to the terminal
450 * of "buffer".
451 */
452 void
453write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
454{
455 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200456 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200457
Bram Moolenaard85f2712017-07-28 21:51:57 +0200458 if (term->tl_vterm == NULL)
459 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200460 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200461 return;
462 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200463 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200464 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200465
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200466 if (!term->tl_terminal_mode)
467 {
468 /* TODO: only update once in a while. */
469 update_screen(0);
470 update_cursor(term, TRUE);
471 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200472}
473
474/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200475 * Send a mouse position and click to the vterm
476 */
477 static int
478term_send_mouse(VTerm *vterm, int button, int pressed)
479{
480 VTermModifier mod = VTERM_MOD_NONE;
481
482 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
483 mouse_col - W_WINCOL(curwin), mod);
484 vterm_mouse_button(vterm, button, pressed, mod);
485 return TRUE;
486}
487
488/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200489 * Convert typed key "c" into bytes to send to the job.
490 * Return the number of bytes in "buf".
491 */
492 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200493term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200494{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200495 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200496 VTermKey key = VTERM_KEY_NONE;
497 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200498 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200499
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200500 switch (c)
501 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200502 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200503 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200504 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
505 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200506 case K_DEL: key = VTERM_KEY_DEL; break;
507 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200508 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
509 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200510 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200511 case K_S_END: mod = VTERM_MOD_SHIFT;
512 key = VTERM_KEY_END; break;
513 case K_C_END: mod = VTERM_MOD_CTRL;
514 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200515 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
516 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
517 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
518 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
519 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
520 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
521 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
522 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
523 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
524 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
525 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
526 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
527 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200528 case K_S_HOME: mod = VTERM_MOD_SHIFT;
529 key = VTERM_KEY_HOME; break;
530 case K_C_HOME: mod = VTERM_MOD_CTRL;
531 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200532 case K_INS: key = VTERM_KEY_INS; break;
533 case K_K0: key = VTERM_KEY_KP_0; break;
534 case K_K1: key = VTERM_KEY_KP_1; break;
535 case K_K2: key = VTERM_KEY_KP_2; break;
536 case K_K3: key = VTERM_KEY_KP_3; break;
537 case K_K4: key = VTERM_KEY_KP_4; break;
538 case K_K5: key = VTERM_KEY_KP_5; break;
539 case K_K6: key = VTERM_KEY_KP_6; break;
540 case K_K7: key = VTERM_KEY_KP_7; break;
541 case K_K8: key = VTERM_KEY_KP_8; break;
542 case K_K9: key = VTERM_KEY_KP_9; break;
543 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
544 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
545 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
546 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
547 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
548 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
549 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
550 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
551 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
552 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
553 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
554 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
555 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200556 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
557 key = VTERM_KEY_LEFT; break;
558 case K_C_LEFT: mod = VTERM_MOD_CTRL;
559 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200560 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
561 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
562 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200563 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
564 key = VTERM_KEY_RIGHT; break;
565 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
566 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200567 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200568 case K_S_UP: mod = VTERM_MOD_SHIFT;
569 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200570 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200571
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200572 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
573 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
574 case K_MOUSELEFT: /* TODO */ return 0;
575 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200576
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200577 case K_LEFTMOUSE:
578 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
579 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
580 case K_LEFTRELEASE:
581 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
582 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
583 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
584 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
585 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
586 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
587 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
588 case K_X1MOUSE: /* TODO */ return 0;
589 case K_X1DRAG: /* TODO */ return 0;
590 case K_X1RELEASE: /* TODO */ return 0;
591 case K_X2MOUSE: /* TODO */ return 0;
592 case K_X2DRAG: /* TODO */ return 0;
593 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200594
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200595 case K_IGNORE: return 0;
596 case K_NOP: return 0;
597 case K_UNDO: return 0;
598 case K_HELP: return 0;
599 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
600 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
601 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
602 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
603 case K_SELECT: return 0;
604#ifdef FEAT_GUI
605 case K_VER_SCROLLBAR: return 0;
606 case K_HOR_SCROLLBAR: return 0;
607#endif
608#ifdef FEAT_GUI_TABLINE
609 case K_TABLINE: return 0;
610 case K_TABMENU: return 0;
611#endif
612#ifdef FEAT_NETBEANS_INTG
613 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
614#endif
615#ifdef FEAT_DND
616 case K_DROP: return 0;
617#endif
618#ifdef FEAT_AUTOCMD
619 case K_CURSORHOLD: return 0;
620#endif
621 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
622 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200623 }
624
625 /*
626 * Convert special keys to vterm keys:
627 * - Write keys to vterm: vterm_keyboard_key()
628 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200629 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200630 */
631 if (key != VTERM_KEY_NONE)
632 /* Special key, let vterm convert it. */
633 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200634 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200635 /* Normal character, let vterm convert it. */
636 vterm_keyboard_unichar(vterm, c, mod);
637
638 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200639 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200640}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200641
Bram Moolenaar938783d2017-07-16 20:13:26 +0200642/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200643 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200644 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200645 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200646term_job_running(term_T *term)
647{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200648 /* Also consider the job finished when the channel is closed, to avoid a
649 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200650 return term != NULL
651 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200652 && term->tl_job->jv_status == JOB_STARTED
653 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200654}
655
656/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200657 * Add the last line of the scrollback buffer to the buffer in the window.
658 */
659 static void
660add_scrollback_line_to_buffer(term_T *term)
661{
662 linenr_T lnum = term->tl_scrollback.ga_len - 1;
663 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
664 garray_T ga;
665 int c;
666 int col;
667 int i;
668
669 ga_init2(&ga, 1, 100);
670 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
671 {
672 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
673 goto failed;
674 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
675 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
676 (char_u *)ga.ga_data + ga.ga_len);
677 }
678 if (ga_grow(&ga, 1) == FAIL)
679 goto failed;
680 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
681 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
682
683 if (lnum == 0)
684 {
685 /* Delete the empty line that was in the empty buffer. */
686 curbuf = term->tl_buffer;
687 ml_delete(2, FALSE);
688 curbuf = curwin->w_buffer;
689 }
690
691failed:
692 ga_clear(&ga);
693}
694
695/*
696 * Add the current lines of the terminal to scrollback and to the buffer.
697 * Called after the job has ended and when switching to Terminal mode.
698 */
699 static void
700move_terminal_to_buffer(term_T *term)
701{
702 win_T *wp;
703 int len;
704 int lines_skipped = 0;
705 VTermPos pos;
706 VTermScreenCell cell;
707 VTermScreenCell *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200708 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200709
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200710 if (term->tl_vterm == NULL)
711 return;
712 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200713 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
714 {
715 len = 0;
716 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
717 if (vterm_screen_get_cell(screen, pos, &cell) != 0
718 && cell.chars[0] != NUL)
719 len = pos.col + 1;
720
721 if (len == 0)
722 ++lines_skipped;
723 else
724 {
725 while (lines_skipped > 0)
726 {
727 /* Line was skipped, add an empty line. */
728 --lines_skipped;
729 if (ga_grow(&term->tl_scrollback, 1) == OK)
730 {
731 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
732 + term->tl_scrollback.ga_len;
733
734 line->sb_cols = 0;
735 line->sb_cells = NULL;
736 ++term->tl_scrollback.ga_len;
737
738 add_scrollback_line_to_buffer(term);
739 }
740 }
741
742 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
743 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
744 {
745 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
746 + term->tl_scrollback.ga_len;
747
748 for (pos.col = 0; pos.col < len; ++pos.col)
749 {
750 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
751 vim_memset(p + pos.col, 0, sizeof(cell));
752 else
753 p[pos.col] = cell;
754 }
755 line->sb_cols = len;
756 line->sb_cells = p;
757 ++term->tl_scrollback.ga_len;
758
759 add_scrollback_line_to_buffer(term);
760 }
761 else
762 vim_free(p);
763 }
764 }
765
766 FOR_ALL_WINDOWS(wp)
767 {
768 if (wp->w_buffer == term->tl_buffer)
769 {
770 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
771 wp->w_cursor.col = 0;
772 wp->w_valid = 0;
773 redraw_win_later(wp, NOT_VALID);
774 }
775 }
776}
777
778 static void
779set_terminal_mode(term_T *term, int on)
780{
781 term->tl_terminal_mode = on;
782 vim_free(term->tl_status_text);
783 term->tl_status_text = NULL;
784 if (term->tl_buffer == curbuf)
785 maketitle();
786}
787
788/*
789 * Called after the job if finished and Terminal mode is not active:
790 * Move the vterm contents into the scrollback buffer and free the vterm.
791 */
792 static void
793cleanup_vterm(term_T *term)
794{
795 move_terminal_to_buffer(term);
796 term_free_vterm(term);
797 set_terminal_mode(term, FALSE);
798}
799
800/*
801 * Switch from sending keys to the job to Terminal-Normal mode.
802 * Suspends updating the terminal window.
803 */
804 static void
805term_enter_terminal_mode()
806{
807 term_T *term = curbuf->b_term;
808
809 /* Append the current terminal contents to the buffer. */
810 move_terminal_to_buffer(term);
811
812 set_terminal_mode(term, TRUE);
813}
814
815/*
816 * Returns TRUE if the current window contains a terminal and we are in
817 * Terminal-Normal mode.
818 */
819 int
820term_in_terminal_mode()
821{
822 term_T *term = curbuf->b_term;
823
824 return term != NULL && term->tl_terminal_mode;
825}
826
827/*
828 * Switch from Terminal-Normal mode to sending keys to the job.
829 * Restores updating the terminal window.
830 */
831 void
832term_leave_terminal_mode()
833{
834 term_T *term = curbuf->b_term;
835 sb_line_T *line;
836 garray_T *gap;
837
838 /* Remove the terminal contents from the scrollback and the buffer. */
839 gap = &term->tl_scrollback;
840 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
841 {
842 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
843 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
844 vim_free(line->sb_cells);
845 --gap->ga_len;
846 if (gap->ga_len == 0)
847 break;
848 }
849 check_cursor();
850
851 set_terminal_mode(term, FALSE);
852
853 if (term->tl_channel_closed)
854 cleanup_vterm(term);
855 redraw_buf_and_status_later(curbuf, NOT_VALID);
856}
857
858/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200859 * Get a key from the user without mapping.
860 * TODO: use terminal mode mappings.
861 */
862 static int
863term_vgetc()
864{
865 int c;
866
867 ++no_mapping;
868 ++allow_keys;
869 got_int = FALSE;
870 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200871 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200872 --no_mapping;
873 --allow_keys;
874 return c;
875}
876
877/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200878 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200879 * Return FAIL when the key needs to be handled in Normal mode.
880 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200881 */
882 static int
883send_keys_to_term(term_T *term, int c, int typed)
884{
885 char msg[KEY_BUF_LEN];
886 size_t len;
887 static int mouse_was_outside = FALSE;
888 int dragging_outside = FALSE;
889
890 /* Catch keys that need to be handled as in Normal mode. */
891 switch (c)
892 {
893 case NUL:
894 case K_ZERO:
895 if (typed)
896 stuffcharReadbuff(c);
897 return FAIL;
898
899 case K_IGNORE:
900 return FAIL;
901
902 case K_LEFTDRAG:
903 case K_MIDDLEDRAG:
904 case K_RIGHTDRAG:
905 case K_X1DRAG:
906 case K_X2DRAG:
907 dragging_outside = mouse_was_outside;
908 /* FALLTHROUGH */
909 case K_LEFTMOUSE:
910 case K_LEFTMOUSE_NM:
911 case K_LEFTRELEASE:
912 case K_LEFTRELEASE_NM:
913 case K_MIDDLEMOUSE:
914 case K_MIDDLERELEASE:
915 case K_RIGHTMOUSE:
916 case K_RIGHTRELEASE:
917 case K_X1MOUSE:
918 case K_X1RELEASE:
919 case K_X2MOUSE:
920 case K_X2RELEASE:
921 if (mouse_row < W_WINROW(curwin)
922 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
923 || mouse_col < W_WINCOL(curwin)
924 || mouse_col >= W_ENDCOL(curwin)
925 || dragging_outside)
926 {
927 /* click outside the current window */
928 if (typed)
929 {
930 stuffcharReadbuff(c);
931 mouse_was_outside = TRUE;
932 }
933 return FAIL;
934 }
935 }
936 if (typed)
937 mouse_was_outside = FALSE;
938
939 /* Convert the typed key to a sequence of bytes for the job. */
940 len = term_convert_key(term, c, msg);
941 if (len > 0)
942 /* TODO: if FAIL is returned, stop? */
943 channel_send(term->tl_job->jv_channel, PART_IN,
944 (char_u *)msg, (int)len, NULL);
945
946 return OK;
947}
948
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200949 static void
950position_cursor(win_T *wp, VTermPos *pos)
951{
952 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
953 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
954 wp->w_valid |= (VALID_WCOL|VALID_WROW);
955}
956
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200957/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200958 * Handle CTRL-W "": send register contents to the job.
959 */
960 static void
961term_paste_register(int prev_c UNUSED)
962{
963 int c;
964 list_T *l;
965 listitem_T *item;
966 long reglen = 0;
967 int type;
968
969#ifdef FEAT_CMDL_INFO
970 if (add_to_showcmd(prev_c))
971 if (add_to_showcmd('"'))
972 out_flush();
973#endif
974 c = term_vgetc();
975#ifdef FEAT_CMDL_INFO
976 clear_showcmd();
977#endif
978
979 /* CTRL-W "= prompt for expression to evaluate. */
980 if (c == '=' && get_expr_register() != '=')
981 return;
982
983 l = (list_T *)get_reg_contents(c, GREG_LIST);
984 if (l != NULL)
985 {
986 type = get_reg_type(c, &reglen);
987 for (item = l->lv_first; item != NULL; item = item->li_next)
988 {
989 char_u *s = get_tv_string(&item->li_tv);
990
991 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
992 s, STRLEN(s), NULL);
993 if (item->li_next != NULL || type == MLINE)
994 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
995 (char_u *)"\r", 1, NULL);
996 }
997 list_free(l);
998 }
999}
1000
1001/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001002 * Returns TRUE if the current window contains a terminal and we are sending
1003 * keys to the job.
1004 */
1005 int
1006term_use_loop()
1007{
1008 term_T *term = curbuf->b_term;
1009
1010 return term != NULL
1011 && !term->tl_terminal_mode
1012 && term->tl_vterm != NULL
1013 && term_job_running(term);
1014}
1015
1016/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001017 * Wait for input and send it to the job.
1018 * Return when the start of a CTRL-W command is typed or anything else that
1019 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001020 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1021 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001022 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001023 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001024terminal_loop(void)
1025{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001026 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001027 int termkey = 0;
1028
1029 if (*curwin->w_p_tk != NUL)
1030 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001031 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001032
1033 for (;;)
1034 {
1035 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001036 /* Repeat redrawing in case a message is received while redrawing. */
1037 while (curwin->w_redr_type != 0)
1038 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001039 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001040
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001041 c = term_vgetc();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001042 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001043 /* job finished while waiting for a character */
1044 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001045
Bram Moolenaarfae42832017-08-01 22:24:26 +02001046#ifdef UNIX
1047 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1048#endif
1049#ifdef WIN3264
1050 if (c == Ctrl_C)
1051 /* We don't know if the job can handle CTRL-C itself or not, this
1052 * may kill the shell instead of killing the command running in the
1053 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001054 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001055#endif
1056
Bram Moolenaar69198192017-08-05 14:10:48 +02001057 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001058 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001059 int prev_c = c;
1060
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001061#ifdef FEAT_CMDL_INFO
1062 if (add_to_showcmd(c))
1063 out_flush();
1064#endif
1065 c = term_vgetc();
1066#ifdef FEAT_CMDL_INFO
1067 clear_showcmd();
1068#endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001069 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001070 /* job finished while waiting for a character */
1071 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001072
Bram Moolenaar69198192017-08-05 14:10:48 +02001073 if (prev_c == Ctrl_BSL)
1074 {
1075 if (c == Ctrl_N)
1076 /* CTRL-\ CTRL-N : execute one Normal mode command. */
1077 return OK;
1078 /* Send both keys to the terminal. */
1079 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1080 }
1081 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001082 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001083 /* "CTRL-W .": send CTRL-W to the job */
1084 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001085 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001086 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001087 {
1088 term_enter_terminal_mode();
1089 return FAIL;
1090 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001091 else if (c == '"')
1092 {
1093 term_paste_register(prev_c);
1094 continue;
1095 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001096 else if (termkey == 0 || c != termkey)
1097 {
1098 stuffcharReadbuff(Ctrl_W);
1099 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001100 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001101 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001102 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001103 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1104 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001105 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001106 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001107}
1108
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001109/*
1110 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001111 * This updates the title and status, but does not close the vter, because
1112 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001113 */
1114 void
1115term_job_ended(job_T *job)
1116{
1117 term_T *term;
1118 int did_one = FALSE;
1119
1120 for (term = first_term; term != NULL; term = term->tl_next)
1121 if (term->tl_job == job)
1122 {
1123 vim_free(term->tl_title);
1124 term->tl_title = NULL;
1125 vim_free(term->tl_status_text);
1126 term->tl_status_text = NULL;
1127 redraw_buf_and_status_later(term->tl_buffer, VALID);
1128 did_one = TRUE;
1129 }
1130 if (did_one)
1131 redraw_statuslines();
1132 if (curbuf->b_term != NULL)
1133 {
1134 if (curbuf->b_term->tl_job == job)
1135 maketitle();
1136 update_cursor(curbuf->b_term, TRUE);
1137 }
1138}
1139
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001140 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001141may_toggle_cursor(term_T *term)
1142{
1143 if (curbuf == term->tl_buffer)
1144 {
1145 if (term->tl_cursor_visible)
1146 cursor_on();
1147 else
1148 cursor_off();
1149 }
1150}
1151
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001152 static int
1153handle_damage(VTermRect rect, void *user)
1154{
1155 term_T *term = (term_T *)user;
1156
1157 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1158 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1159 redraw_buf_later(term->tl_buffer, NOT_VALID);
1160 return 1;
1161}
1162
1163 static int
1164handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1165{
1166 term_T *term = (term_T *)user;
1167
1168 /* TODO */
1169 redraw_buf_later(term->tl_buffer, NOT_VALID);
1170 return 1;
1171}
1172
1173 static int
1174handle_movecursor(
1175 VTermPos pos,
1176 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001177 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001178 void *user)
1179{
1180 term_T *term = (term_T *)user;
1181 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001182
1183 term->tl_cursor_pos = pos;
1184 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001185
1186 FOR_ALL_WINDOWS(wp)
1187 {
1188 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001189 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001190 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001191 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001192 {
1193 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001194 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001195 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001196
1197 return 1;
1198}
1199
Bram Moolenaar21554412017-07-24 21:44:43 +02001200 static int
1201handle_settermprop(
1202 VTermProp prop,
1203 VTermValue *value,
1204 void *user)
1205{
1206 term_T *term = (term_T *)user;
1207
1208 switch (prop)
1209 {
1210 case VTERM_PROP_TITLE:
1211 vim_free(term->tl_title);
1212 term->tl_title = vim_strsave((char_u *)value->string);
1213 vim_free(term->tl_status_text);
1214 term->tl_status_text = NULL;
1215 if (term == curbuf->b_term)
1216 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001217 break;
1218
1219 case VTERM_PROP_CURSORVISIBLE:
1220 term->tl_cursor_visible = value->boolean;
1221 may_toggle_cursor(term);
1222 out_flush();
1223 break;
1224
Bram Moolenaar21554412017-07-24 21:44:43 +02001225 default:
1226 break;
1227 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001228 /* Always return 1, otherwise vterm doesn't store the value internally. */
1229 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001230}
1231
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001232/*
1233 * The job running in the terminal resized the terminal.
1234 */
1235 static int
1236handle_resize(int rows, int cols, void *user)
1237{
1238 term_T *term = (term_T *)user;
1239 win_T *wp;
1240
1241 term->tl_rows = rows;
1242 term->tl_cols = cols;
1243 FOR_ALL_WINDOWS(wp)
1244 {
1245 if (wp->w_buffer == term->tl_buffer)
1246 {
1247 win_setheight_win(rows, wp);
1248 win_setwidth_win(cols, wp);
1249 }
1250 }
1251
1252 redraw_buf_later(term->tl_buffer, NOT_VALID);
1253 return 1;
1254}
1255
Bram Moolenaard85f2712017-07-28 21:51:57 +02001256/*
1257 * Handle a line that is pushed off the top of the screen.
1258 */
1259 static int
1260handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1261{
1262 term_T *term = (term_T *)user;
1263
1264 /* TODO: Limit the number of lines that are stored. */
1265 /* TODO: put the text in the buffer. */
1266 if (ga_grow(&term->tl_scrollback, 1) == OK)
1267 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001268 VTermScreenCell *p = NULL;
1269 int len = 0;
1270 int i;
1271 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001272
1273 /* do not store empty cells at the end */
1274 for (i = 0; i < cols; ++i)
1275 if (cells[i].chars[0] != 0)
1276 len = i + 1;
1277
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001278 if (len > 0)
1279 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001280 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001281 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001282
1283 line = (sb_line_T *)term->tl_scrollback.ga_data
1284 + term->tl_scrollback.ga_len;
1285 line->sb_cols = len;
1286 line->sb_cells = p;
1287 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001288 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001289
1290 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001291 }
1292 return 0; /* ignored */
1293}
1294
Bram Moolenaar21554412017-07-24 21:44:43 +02001295static VTermScreenCallbacks screen_callbacks = {
1296 handle_damage, /* damage */
1297 handle_moverect, /* moverect */
1298 handle_movecursor, /* movecursor */
1299 handle_settermprop, /* settermprop */
1300 NULL, /* bell */
1301 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001302 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001303 NULL /* sb_popline */
1304};
1305
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001306/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001307 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001308 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001309 */
1310 void
1311term_channel_closed(channel_T *ch)
1312{
1313 term_T *term;
1314 int did_one = FALSE;
1315
1316 for (term = first_term; term != NULL; term = term->tl_next)
1317 if (term->tl_job == ch->ch_job)
1318 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001319 term->tl_channel_closed = TRUE;
1320
Bram Moolenaard85f2712017-07-28 21:51:57 +02001321 vim_free(term->tl_title);
1322 term->tl_title = NULL;
1323 vim_free(term->tl_status_text);
1324 term->tl_status_text = NULL;
1325
Bram Moolenaar423802d2017-07-30 16:52:24 +02001326 /* Unless in Terminal-Normal mode: clear the vterm. */
1327 if (!term->tl_terminal_mode)
1328 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001329
1330 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1331 did_one = TRUE;
1332 }
1333 if (did_one)
1334 {
1335 redraw_statuslines();
1336
1337 /* Need to break out of vgetc(). */
1338 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001339 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001340
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001341 term = curbuf->b_term;
1342 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001343 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001344 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001345 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001346 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001347 }
1348 }
1349}
1350
1351/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001352 * Reverse engineer the RGB value into a cterm color index.
1353 * First color is 1. Return 0 if no match found.
1354 */
1355 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001356color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001357{
1358 int red = color->red;
1359 int blue = color->blue;
1360 int green = color->green;
1361
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001362 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001363 if (red == 0)
1364 {
1365 if (green == 0)
1366 {
1367 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001368 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001369 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001370 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001371 }
1372 else if (green == 224)
1373 {
1374 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001375 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001376 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001377 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001378 }
1379 }
1380 else if (red == 224)
1381 {
1382 if (green == 0)
1383 {
1384 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001385 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001386 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001387 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001388 }
1389 else if (green == 224)
1390 {
1391 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001392 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001393 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001394 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001395 }
1396 }
1397 else if (red == 128)
1398 {
1399 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001400 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001401 }
1402 else if (red == 255)
1403 {
1404 if (green == 64)
1405 {
1406 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001407 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001408 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001409 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001410 }
1411 else if (green == 255)
1412 {
1413 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001414 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001415 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001416 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001417 }
1418 }
1419 else if (red == 64)
1420 {
1421 if (green == 64)
1422 {
1423 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001424 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001425 }
1426 else if (green == 255)
1427 {
1428 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001429 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001430 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001431 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001432 }
1433 }
1434 if (t_colors >= 256)
1435 {
1436 if (red == blue && red == green)
1437 {
1438 /* 24-color greyscale */
1439 static int cutoff[23] = {
1440 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1441 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1442 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1443 int i;
1444
1445 for (i = 0; i < 23; ++i)
1446 if (red < cutoff[i])
1447 return i + 233;
1448 return 256;
1449 }
1450
1451 /* 216-color cube */
1452 return 17 + ((red + 25) / 0x33) * 36
1453 + ((green + 25) / 0x33) * 6
1454 + (blue + 25) / 0x33;
1455 }
1456 return 0;
1457}
1458
1459/*
1460 * Convert the attributes of a vterm cell into an attribute index.
1461 */
1462 static int
1463cell2attr(VTermScreenCell *cell)
1464{
1465 int attr = 0;
1466
1467 if (cell->attrs.bold)
1468 attr |= HL_BOLD;
1469 if (cell->attrs.underline)
1470 attr |= HL_UNDERLINE;
1471 if (cell->attrs.italic)
1472 attr |= HL_ITALIC;
1473 if (cell->attrs.strike)
1474 attr |= HL_STANDOUT;
1475 if (cell->attrs.reverse)
1476 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001477
1478#ifdef FEAT_GUI
1479 if (gui.in_use)
1480 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001481 guicolor_T fg, bg;
1482
1483 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1484 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1485 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001486 }
1487 else
1488#endif
1489#ifdef FEAT_TERMGUICOLORS
1490 if (p_tgc)
1491 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001492 guicolor_T fg, bg;
1493
1494 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1495 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1496
1497 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001498 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001499 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001500#endif
1501 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001502 int bold = MAYBE;
1503 int fg = color2index(&cell->fg, TRUE, &bold);
1504 int bg = color2index(&cell->bg, FALSE, &bold);
1505
1506 /* with 8 colors set the bold attribute to get a bright foreground */
1507 if (bold == TRUE)
1508 attr |= HL_BOLD;
1509 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001510 }
1511 return 0;
1512}
1513
1514/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001515 * Called to update the window that contains a terminal.
1516 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001517 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001518 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001519term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001520{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001521 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001522 VTerm *vterm;
1523 VTermScreen *screen;
1524 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001525 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001526
Bram Moolenaar423802d2017-07-30 16:52:24 +02001527 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001528 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001529
Bram Moolenaard85f2712017-07-28 21:51:57 +02001530 vterm = term->tl_vterm;
1531 screen = vterm_obtain_screen(vterm);
1532 state = vterm_obtain_state(vterm);
1533
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001534 /*
1535 * If the window was resized a redraw will be triggered and we get here.
1536 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1537 */
1538 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1539 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001540 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001541 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1542 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1543 win_T *twp;
1544
1545 FOR_ALL_WINDOWS(twp)
1546 {
1547 /* When more than one window shows the same terminal, use the
1548 * smallest size. */
1549 if (twp->w_buffer == term->tl_buffer)
1550 {
1551 if (!term->tl_rows_fixed && rows > twp->w_height)
1552 rows = twp->w_height;
1553 if (!term->tl_cols_fixed && cols > twp->w_width)
1554 cols = twp->w_width;
1555 }
1556 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001557
1558 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001559 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001560 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001561 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001562 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001563
1564 /* The cursor may have been moved when resizing. */
1565 vterm_state_get_cursorpos(state, &pos);
1566 position_cursor(wp, &pos);
1567
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001568 /* TODO: Only redraw what changed. */
1569 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001570 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001571 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001572 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001573
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001574 if (pos.row < term->tl_rows)
1575 {
1576 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001577 {
1578 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001579 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001580
Bram Moolenaareeac6772017-07-23 15:48:37 +02001581 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1582 vim_memset(&cell, 0, sizeof(cell));
1583
1584 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001585 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001586 if (c == NUL)
1587 {
1588 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001589#if defined(FEAT_MBYTE)
1590 if (enc_utf8)
1591 ScreenLinesUC[off] = NUL;
1592#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001593 }
1594 else
1595 {
1596#if defined(FEAT_MBYTE)
1597 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001598 {
1599 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001600 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001601 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001602 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001603 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001604 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001605 if (enc_utf8)
1606 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001607 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001608#else
1609 ScreenLines[off] = c;
1610#endif
1611 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001612 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001613
1614 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001615 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001616 if (cell.width == 2)
1617 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001618 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001619#if defined(FEAT_MBYTE)
1620 if (enc_utf8)
1621 ScreenLinesUC[off] = NUL;
1622#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001623 ++pos.col;
1624 ++off;
1625 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001626 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001627 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001628 else
1629 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001630
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001631 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1632 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001633 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001634
1635 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001636}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001637
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001638/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001639 * Return TRUE if "wp" is a terminal window where the job has finished.
1640 */
1641 int
1642term_is_finished(buf_T *buf)
1643{
1644 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1645}
1646
1647/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001648 * Return TRUE if "wp" is a terminal window where the job has finished or we
1649 * are in Terminal-Normal mode.
1650 */
1651 int
1652term_show_buffer(buf_T *buf)
1653{
1654 term_T *term = buf->b_term;
1655
1656 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1657}
1658
1659/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001660 * The current buffer is going to be changed. If there is terminal
1661 * highlighting remove it now.
1662 */
1663 void
1664term_change_in_curbuf(void)
1665{
1666 term_T *term = curbuf->b_term;
1667
1668 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1669 {
1670 free_scrollback(term);
1671 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001672
1673 /* The buffer is now like a normal buffer, it cannot be easily
1674 * abandoned when changed. */
1675 set_string_option_direct((char_u *)"buftype", -1,
1676 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001677 }
1678}
1679
1680/*
1681 * Get the screen attribute for a position in the buffer.
1682 */
1683 int
1684term_get_attr(buf_T *buf, linenr_T lnum, int col)
1685{
1686 term_T *term = buf->b_term;
1687 sb_line_T *line;
1688
Bram Moolenaar70229f92017-07-29 16:01:53 +02001689 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001690 return 0;
1691 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1692 if (col >= line->sb_cols)
1693 return 0;
1694 return cell2attr(line->sb_cells + col);
1695}
1696
1697/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001698 * Create a new vterm and initialize it.
1699 */
1700 static void
1701create_vterm(term_T *term, int rows, int cols)
1702{
1703 VTerm *vterm;
1704 VTermScreen *screen;
1705
1706 vterm = vterm_new(rows, cols);
1707 term->tl_vterm = vterm;
1708 screen = vterm_obtain_screen(vterm);
1709 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1710 /* TODO: depends on 'encoding'. */
1711 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001712
1713 /* Vterm uses a default black background. Set it to white when
1714 * 'background' is "light". */
1715 if (*p_bg == 'l')
1716 {
1717 VTermColor fg, bg;
1718
1719 fg.red = fg.green = fg.blue = 0;
1720 bg.red = bg.green = bg.blue = 255;
1721 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1722 }
1723
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001724 /* Required to initialize most things. */
1725 vterm_screen_reset(screen, 1 /* hard */);
1726}
1727
Bram Moolenaar21554412017-07-24 21:44:43 +02001728/*
1729 * Return the text to show for the buffer name and status.
1730 */
1731 char_u *
1732term_get_status_text(term_T *term)
1733{
1734 if (term->tl_status_text == NULL)
1735 {
1736 char_u *txt;
1737 size_t len;
1738
Bram Moolenaar423802d2017-07-30 16:52:24 +02001739 if (term->tl_terminal_mode)
1740 {
1741 if (term_job_running(term))
1742 txt = (char_u *)_("Terminal");
1743 else
1744 txt = (char_u *)_("Terminal-finished");
1745 }
1746 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001747 txt = term->tl_title;
1748 else if (term_job_running(term))
1749 txt = (char_u *)_("running");
1750 else
1751 txt = (char_u *)_("finished");
1752 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001753 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001754 if (term->tl_status_text != NULL)
1755 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1756 term->tl_buffer->b_fname, txt);
1757 }
1758 return term->tl_status_text;
1759}
1760
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001761/*
1762 * Mark references in jobs of terminals.
1763 */
1764 int
1765set_ref_in_term(int copyID)
1766{
1767 int abort = FALSE;
1768 term_T *term;
1769 typval_T tv;
1770
1771 for (term = first_term; term != NULL; term = term->tl_next)
1772 if (term->tl_job != NULL)
1773 {
1774 tv.v_type = VAR_JOB;
1775 tv.vval.v_job = term->tl_job;
1776 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1777 }
1778 return abort;
1779}
1780
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001781/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001782 * Get the buffer from the first argument in "argvars".
1783 * Returns NULL when the buffer is not for a terminal window.
1784 */
1785 static buf_T *
1786term_get_buf(typval_T *argvars)
1787{
1788 buf_T *buf;
1789
1790 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1791 ++emsg_off;
1792 buf = get_buf_tv(&argvars[0], FALSE);
1793 --emsg_off;
1794 if (buf == NULL || buf->b_term == NULL)
1795 return NULL;
1796 return buf;
1797}
1798
1799/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001800 * "term_getattr(attr, name)" function
1801 */
1802 void
1803f_term_getattr(typval_T *argvars, typval_T *rettv)
1804{
1805 int attr;
1806 size_t i;
1807 char_u *name;
1808
1809 static struct {
1810 char *name;
1811 int attr;
1812 } attrs[] = {
1813 {"bold", HL_BOLD},
1814 {"italic", HL_ITALIC},
1815 {"underline", HL_UNDERLINE},
1816 {"strike", HL_STANDOUT},
1817 {"reverse", HL_INVERSE},
1818 };
1819
1820 attr = get_tv_number(&argvars[0]);
1821 name = get_tv_string_chk(&argvars[1]);
1822 if (name == NULL)
1823 return;
1824
1825 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1826 if (STRCMP(name, attrs[i].name) == 0)
1827 {
1828 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1829 break;
1830 }
1831}
1832
1833/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001834 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001835 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001836 void
1837f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001838{
Bram Moolenaar97870002017-07-30 18:28:38 +02001839 buf_T *buf = term_get_buf(argvars);
1840 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001841
Bram Moolenaar97870002017-07-30 18:28:38 +02001842 if (rettv_list_alloc(rettv) == FAIL)
1843 return;
1844 if (buf == NULL)
1845 return;
1846
1847 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001848 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1849 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001850 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001851}
1852
1853/*
1854 * "term_getjob(buf)" function
1855 */
1856 void
1857f_term_getjob(typval_T *argvars, typval_T *rettv)
1858{
1859 buf_T *buf = term_get_buf(argvars);
1860
1861 rettv->v_type = VAR_JOB;
1862 rettv->vval.v_job = NULL;
1863 if (buf == NULL)
1864 return;
1865
1866 rettv->vval.v_job = buf->b_term->tl_job;
1867 if (rettv->vval.v_job != NULL)
1868 ++rettv->vval.v_job->jv_refcount;
1869}
1870
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001871 static int
1872get_row_number(typval_T *tv, term_T *term)
1873{
1874 if (tv->v_type == VAR_STRING
1875 && tv->vval.v_string != NULL
1876 && STRCMP(tv->vval.v_string, ".") == 0)
1877 return term->tl_cursor_pos.row;
1878 return (int)get_tv_number(tv) - 1;
1879}
1880
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001881/*
1882 * "term_getline(buf, row)" function
1883 */
1884 void
1885f_term_getline(typval_T *argvars, typval_T *rettv)
1886{
1887 buf_T *buf = term_get_buf(argvars);
1888 term_T *term;
1889 int row;
1890
1891 rettv->v_type = VAR_STRING;
1892 if (buf == NULL)
1893 return;
1894 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001895 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001896
1897 if (term->tl_vterm == NULL)
1898 {
1899 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1900
1901 /* vterm is finished, get the text from the buffer */
1902 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1903 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1904 }
1905 else
1906 {
1907 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1908 VTermRect rect;
1909 int len;
1910 char_u *p;
1911
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001912 if (row < 0 || row >= term->tl_rows)
1913 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001914 len = term->tl_cols * MB_MAXBYTES + 1;
1915 p = alloc(len);
1916 if (p == NULL)
1917 return;
1918 rettv->vval.v_string = p;
1919
1920 rect.start_col = 0;
1921 rect.end_col = term->tl_cols;
1922 rect.start_row = row;
1923 rect.end_row = row + 1;
1924 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1925 }
1926}
1927
1928/*
1929 * "term_getsize(buf)" function
1930 */
1931 void
1932f_term_getsize(typval_T *argvars, typval_T *rettv)
1933{
1934 buf_T *buf = term_get_buf(argvars);
1935 list_T *l;
1936
1937 if (rettv_list_alloc(rettv) == FAIL)
1938 return;
1939 if (buf == NULL)
1940 return;
1941
1942 l = rettv->vval.v_list;
1943 list_append_number(l, buf->b_term->tl_rows);
1944 list_append_number(l, buf->b_term->tl_cols);
1945}
1946
1947/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001948 * "term_getstatus(buf)" function
1949 */
1950 void
1951f_term_getstatus(typval_T *argvars, typval_T *rettv)
1952{
1953 buf_T *buf = term_get_buf(argvars);
1954 term_T *term;
1955 char_u val[100];
1956
1957 rettv->v_type = VAR_STRING;
1958 if (buf == NULL)
1959 return;
1960 term = buf->b_term;
1961
1962 if (term_job_running(term))
1963 STRCPY(val, "running");
1964 else
1965 STRCPY(val, "finished");
1966 if (term->tl_terminal_mode)
1967 STRCAT(val, ",terminal");
1968 rettv->vval.v_string = vim_strsave(val);
1969}
1970
1971/*
1972 * "term_gettitle(buf)" function
1973 */
1974 void
1975f_term_gettitle(typval_T *argvars, typval_T *rettv)
1976{
1977 buf_T *buf = term_get_buf(argvars);
1978
1979 rettv->v_type = VAR_STRING;
1980 if (buf == NULL)
1981 return;
1982
1983 if (buf->b_term->tl_title != NULL)
1984 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1985}
1986
1987/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001988 * "term_gettty(buf)" function
1989 */
1990 void
1991f_term_gettty(typval_T *argvars, typval_T *rettv)
1992{
1993 buf_T *buf = term_get_buf(argvars);
1994 char_u *p;
1995
1996 rettv->v_type = VAR_STRING;
1997 if (buf == NULL)
1998 return;
1999 if (buf->b_term->tl_job != NULL)
2000 p = buf->b_term->tl_job->jv_tty_name;
2001 else
2002 p = buf->b_term->tl_tty_name;
2003 if (p != NULL)
2004 rettv->vval.v_string = vim_strsave(p);
2005}
2006
2007/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002008 * "term_list()" function
2009 */
2010 void
2011f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2012{
2013 term_T *tp;
2014 list_T *l;
2015
2016 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2017 return;
2018
2019 l = rettv->vval.v_list;
2020 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2021 if (tp != NULL && tp->tl_buffer != NULL)
2022 if (list_append_number(l,
2023 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2024 return;
2025}
2026
2027/*
2028 * "term_scrape(buf, row)" function
2029 */
2030 void
2031f_term_scrape(typval_T *argvars, typval_T *rettv)
2032{
2033 buf_T *buf = term_get_buf(argvars);
2034 VTermScreen *screen = NULL;
2035 VTermPos pos;
2036 list_T *l;
2037 term_T *term;
2038
2039 if (rettv_list_alloc(rettv) == FAIL)
2040 return;
2041 if (buf == NULL)
2042 return;
2043 term = buf->b_term;
2044 if (term->tl_vterm != NULL)
2045 screen = vterm_obtain_screen(term->tl_vterm);
2046
2047 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002048 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002049 for (pos.col = 0; pos.col < term->tl_cols; )
2050 {
2051 dict_T *dcell;
2052 VTermScreenCell cell;
2053 char_u rgb[8];
2054 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2055 int off = 0;
2056 int i;
2057
2058 if (screen == NULL)
2059 {
2060 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2061 sb_line_T *line;
2062
2063 /* vterm has finished, get the cell from scrollback */
2064 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2065 break;
2066 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2067 if (pos.col >= line->sb_cols)
2068 break;
2069 cell = line->sb_cells[pos.col];
2070 }
2071 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2072 break;
2073 dcell = dict_alloc();
2074 list_append_dict(l, dcell);
2075
2076 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2077 {
2078 if (cell.chars[i] == 0)
2079 break;
2080 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2081 }
2082 mbs[off] = NUL;
2083 dict_add_nr_str(dcell, "chars", 0, mbs);
2084
2085 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2086 cell.fg.red, cell.fg.green, cell.fg.blue);
2087 dict_add_nr_str(dcell, "fg", 0, rgb);
2088 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2089 cell.bg.red, cell.bg.green, cell.bg.blue);
2090 dict_add_nr_str(dcell, "bg", 0, rgb);
2091
2092 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2093 dict_add_nr_str(dcell, "width", cell.width, NULL);
2094
2095 ++pos.col;
2096 if (cell.width == 2)
2097 ++pos.col;
2098 }
2099}
2100
2101/*
2102 * "term_sendkeys(buf, keys)" function
2103 */
2104 void
2105f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2106{
2107 buf_T *buf = term_get_buf(argvars);
2108 char_u *msg;
2109 term_T *term;
2110
2111 rettv->v_type = VAR_UNKNOWN;
2112 if (buf == NULL)
2113 return;
2114
2115 msg = get_tv_string_chk(&argvars[1]);
2116 if (msg == NULL)
2117 return;
2118 term = buf->b_term;
2119 if (term->tl_vterm == NULL)
2120 return;
2121
2122 while (*msg != NUL)
2123 {
2124 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2125 msg += MB_PTR2LEN(msg);
2126 }
2127
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002128 if (!term->tl_terminal_mode)
2129 {
2130 /* TODO: only update once in a while. */
2131 update_screen(0);
2132 if (buf == curbuf)
2133 update_cursor(term, TRUE);
2134 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002135}
2136
2137/*
2138 * "term_start(command, options)" function
2139 */
2140 void
2141f_term_start(typval_T *argvars, typval_T *rettv)
2142{
2143 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002144 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002145
2146 if (cmd == NULL)
2147 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002148 init_job_options(&opt);
2149 /* TODO: allow more job options */
2150 if (argvars[1].v_type != VAR_UNKNOWN
2151 && get_job_options(&argvars[1], &opt,
2152 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002153 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2154 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002155 return;
2156
2157 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002158
2159 if (curbuf->b_term != NULL)
2160 rettv->vval.v_number = curbuf->b_fnum;
2161}
2162
2163/*
2164 * "term_wait" function
2165 */
2166 void
2167f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2168{
2169 buf_T *buf = term_get_buf(argvars);
2170
2171 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002172 {
2173 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002174 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002175 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002176 if (buf->b_term->tl_job == NULL)
2177 {
2178 ch_log(NULL, "term_wait(): no job to wait for");
2179 return;
2180 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002181
2182 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002183 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002184 {
2185 /* The job is dead, keep reading channel I/O until the channel is
2186 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002187 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002188 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2189 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002190 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002191 parse_queued_messages();
2192 ui_delay(10L, FALSE);
2193 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002194 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002195 parse_queued_messages();
2196 }
2197 else
2198 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002199 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002200 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002201
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002202 /* Wait for 10 msec for any channel I/O. */
2203 /* TODO: use delay from optional argument */
2204 ui_delay(10L, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002205 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002206
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002207 /* Flushing messages on channels is hopefully sufficient.
2208 * TODO: is there a better way? */
2209 parse_queued_messages();
2210 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002211}
2212
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002213# ifdef WIN3264
2214
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002215/**************************************
2216 * 2. MS-Windows implementation.
2217 */
2218
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002219#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2220#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2221
Bram Moolenaar8a773062017-07-24 22:29:21 +02002222void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002223void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002224void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002225BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2226void (*winpty_config_set_initial_size)(void*, int, int);
2227LPCWSTR (*winpty_conin_name)(void*);
2228LPCWSTR (*winpty_conout_name)(void*);
2229LPCWSTR (*winpty_conerr_name)(void*);
2230void (*winpty_free)(void*);
2231void (*winpty_config_free)(void*);
2232void (*winpty_spawn_config_free)(void*);
2233void (*winpty_error_free)(void*);
2234LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002235BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002236HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002237
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002238#define WINPTY_DLL "winpty.dll"
2239
2240static HINSTANCE hWinPtyDLL = NULL;
2241
2242 int
2243dyn_winpty_init(void)
2244{
2245 int i;
2246 static struct
2247 {
2248 char *name;
2249 FARPROC *ptr;
2250 } winpty_entry[] =
2251 {
2252 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2253 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2254 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2255 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2256 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2257 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2258 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2259 {"winpty_free", (FARPROC*)&winpty_free},
2260 {"winpty_open", (FARPROC*)&winpty_open},
2261 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2262 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2263 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2264 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002265 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002266 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002267 {NULL, NULL}
2268 };
2269
2270 /* No need to initialize twice. */
2271 if (hWinPtyDLL)
2272 return 1;
2273 /* Load winpty.dll */
2274 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2275 if (!hWinPtyDLL)
2276 {
2277 EMSG2(_(e_loadlib), WINPTY_DLL);
2278 return 0;
2279 }
2280 for (i = 0; winpty_entry[i].name != NULL
2281 && winpty_entry[i].ptr != NULL; ++i)
2282 {
2283 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2284 winpty_entry[i].name)) == NULL)
2285 {
2286 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2287 return 0;
2288 }
2289 }
2290
2291 return 1;
2292}
2293
2294/*
2295 * Create a new terminal of "rows" by "cols" cells.
2296 * Store a reference in "term".
2297 * Return OK or FAIL.
2298 */
2299 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002300term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002301{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002302 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002303 channel_T *channel = NULL;
2304 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002305 DWORD error;
2306 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2307 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002308 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002309 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002310
2311 if (!dyn_winpty_init())
2312 return FAIL;
2313
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002314 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002315 if (p == NULL)
2316 return FAIL;
2317
2318 job = job_alloc();
2319 if (job == NULL)
2320 goto failed;
2321
2322 channel = add_channel();
2323 if (channel == NULL)
2324 goto failed;
2325
2326 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2327 if (term->tl_winpty_config == NULL)
2328 goto failed;
2329
2330 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2331 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2332 if (term->tl_winpty == NULL)
2333 goto failed;
2334
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002335 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002336 spawn_config = winpty_spawn_config_new(
2337 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2338 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2339 NULL,
2340 p,
2341 NULL,
2342 NULL,
2343 &winpty_err);
2344 if (spawn_config == NULL)
2345 goto failed;
2346
2347 channel = add_channel();
2348 if (channel == NULL)
2349 goto failed;
2350
2351 job = job_alloc();
2352 if (job == NULL)
2353 goto failed;
2354
2355 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2356 &child_thread_handle, &error, &winpty_err))
2357 goto failed;
2358
2359 channel_set_pipes(channel,
2360 (sock_T) CreateFileW(
2361 winpty_conin_name(term->tl_winpty),
2362 GENERIC_WRITE, 0, NULL,
2363 OPEN_EXISTING, 0, NULL),
2364 (sock_T) CreateFileW(
2365 winpty_conout_name(term->tl_winpty),
2366 GENERIC_READ, 0, NULL,
2367 OPEN_EXISTING, 0, NULL),
2368 (sock_T) CreateFileW(
2369 winpty_conerr_name(term->tl_winpty),
2370 GENERIC_READ, 0, NULL,
2371 OPEN_EXISTING, 0, NULL));
2372
2373 jo = CreateJobObject(NULL, NULL);
2374 if (jo == NULL)
2375 goto failed;
2376
2377 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002378 {
2379 /* Failed, switch the way to terminate process with TerminateProcess. */
2380 CloseHandle(jo);
2381 jo = NULL;
2382 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002383
2384 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002385 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002386
2387 create_vterm(term, rows, cols);
2388
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002389 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002390 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002391
2392 job->jv_channel = channel;
2393 job->jv_proc_info.hProcess = child_process_handle;
2394 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2395 job->jv_job_object = jo;
2396 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002397 sprintf(buf, "winpty://%lu",
2398 GetProcessId(winpty_agent_process(term->tl_winpty)));
2399 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002400 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002401 term->tl_job = job;
2402
2403 return OK;
2404
2405failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002406 if (spawn_config != NULL)
2407 winpty_spawn_config_free(spawn_config);
2408 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002409 if (channel != NULL)
2410 channel_clear(channel);
2411 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002412 {
2413 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002414 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002415 }
2416 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002417 if (jo != NULL)
2418 CloseHandle(jo);
2419 if (term->tl_winpty != NULL)
2420 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002421 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002422 if (term->tl_winpty_config != NULL)
2423 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002424 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002425 if (winpty_err != NULL)
2426 {
2427 char_u *msg = utf16_to_enc(
2428 (short_u *)winpty_error_msg(winpty_err), NULL);
2429
2430 EMSG(msg);
2431 winpty_error_free(winpty_err);
2432 }
2433 return FAIL;
2434}
2435
2436/*
2437 * Free the terminal emulator part of "term".
2438 */
2439 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002440term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002441{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002442 if (term->tl_winpty != NULL)
2443 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002444 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002445 if (term->tl_winpty_config != NULL)
2446 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002447 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002448 if (term->tl_vterm != NULL)
2449 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002450 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002451}
2452
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002453/*
2454 * Request size to terminal.
2455 */
2456 static void
2457term_report_winsize(term_T *term, int rows, int cols)
2458{
2459 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2460}
2461
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002462# else
2463
2464/**************************************
2465 * 3. Unix-like implementation.
2466 */
2467
2468/*
2469 * Create a new terminal of "rows" by "cols" cells.
2470 * Start job for "cmd".
2471 * Store the pointers in "term".
2472 * Return OK or FAIL.
2473 */
2474 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002475term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002476{
2477 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002478
2479 create_vterm(term, rows, cols);
2480
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002481 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002482 argvars[0].v_type = VAR_STRING;
2483 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002484
2485 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002486 if (term->tl_job != NULL)
2487 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002488
Bram Moolenaar61a66052017-07-22 18:39:00 +02002489 return term->tl_job != NULL
2490 && term->tl_job->jv_channel != NULL
2491 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002492}
2493
2494/*
2495 * Free the terminal emulator part of "term".
2496 */
2497 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002498term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002499{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002500 if (term->tl_vterm != NULL)
2501 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002502 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002503}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002504
2505/*
2506 * Request size to terminal.
2507 */
2508 static void
2509term_report_winsize(term_T *term, int rows, int cols)
2510{
2511 /* Use an ioctl() to report the new window size to the job. */
2512 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2513 {
2514 int fd = -1;
2515 int part;
2516
2517 for (part = PART_OUT; part < PART_COUNT; ++part)
2518 {
2519 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2520 if (isatty(fd))
2521 break;
2522 }
2523 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2524 mch_stop_job(term->tl_job, (char_u *)"winch");
2525 }
2526}
2527
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002528# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002529
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002530#endif /* FEAT_TERMINAL */