blob: 7cd2dd2642dbcf5b709edee1fb6d491de4a6db2e [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020022 * this library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020035 * When the job ends the text is put in a buffer. Redrawing then happens from
36 * that buffer, attributes come from the scrollback buffer tl_scrollback.
37 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020038 * TODO:
Bram Moolenaard8dc1792017-08-03 11:55:21 +020039 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar423802d2017-07-30 16:52:24 +020040 * - in bash mouse clicks are inserting characters.
41 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020042 * - add argument to term_wait() for waiting time.
Bram Moolenaard85f2712017-07-28 21:51:57 +020043 * - For the scrollback buffer store lines in the buffer, only attributes in
44 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020045 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020046 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020047 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
48 * job finishes).
49 * - add option values to the command:
50 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020051 * - support different cursor shapes, colors and attributes
52 * - make term_getcursor() return type (none/block/bar/underline) and
53 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020054 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020055 * For the GUI fill termios with default values, perhaps like pangoterm:
56 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
57 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020058 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020059 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020060 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020061 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020062 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020063 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
64 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020065 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020067 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020068 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020069 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar69198192017-08-05 14:10:48 +020070 * terminal. Might allow reading stdin from a file or buffer, sending stderr
71 * to a file or /dev/null, but something must be connected to the terminal.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020072 * - support ":term NONE" to open a terminal with a pty but not running a job
73 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020074 * - if the job in the terminal does not support the mouse, we can use the
75 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020076 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
77 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020078 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020079 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020080 * - Copy text in the vterm to the Vim buffer once in a while, so that
81 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082 */
83
84#include "vim.h"
85
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020086#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020087
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020088#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020089# define MIN(x,y) (x < y ? x : y)
90# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020091#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020093#include "libvterm/include/vterm.h"
94
Bram Moolenaard85f2712017-07-28 21:51:57 +020095typedef struct sb_line_S {
96 int sb_cols; /* can differ per line */
97 VTermScreenCell *sb_cells; /* allocated */
98} sb_line_T;
99
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200100/* typedef term_T in structs.h */
101struct terminal_S {
102 term_T *tl_next;
103
Bram Moolenaar423802d2017-07-30 16:52:24 +0200104 VTerm *tl_vterm;
105 job_T *tl_job;
106 buf_T *tl_buffer;
107
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200108 /* used when tl_job is NULL and only a pty was created */
109 int tl_tty_fd;
110 char_u *tl_tty_name;
111
Bram Moolenaar423802d2017-07-30 16:52:24 +0200112 int tl_terminal_mode;
113 int tl_channel_closed;
114
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200115#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200116 void *tl_winpty_config;
117 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200118#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200119
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200120 /* last known vterm size */
121 int tl_rows;
122 int tl_cols;
123 /* vterm size does not follow window size */
124 int tl_rows_fixed;
125 int tl_cols_fixed;
126
Bram Moolenaar21554412017-07-24 21:44:43 +0200127 char_u *tl_title; /* NULL or allocated */
128 char_u *tl_status_text; /* NULL or allocated */
129
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200130 /* Range of screen rows to update. Zero based. */
131 int tl_dirty_row_start; /* -1 if nothing dirty */
132 int tl_dirty_row_end; /* row below last one to update */
133
Bram Moolenaard85f2712017-07-28 21:51:57 +0200134 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200135 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200136
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200137 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200138 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200139};
140
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200141/*
142 * List of all active terminals.
143 */
144static term_T *first_term = NULL;
145
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200146
147#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
148#define KEY_BUF_LEN 200
149
150/*
151 * Functions with separate implementation for MS-Windows and Unix-like systems.
152 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200153static 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 +0200154static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200155static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200156
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200157/**************************************
158 * 1. Generic code for all systems.
159 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200160
161/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200162 * Determine the terminal size from 'termsize' and the current window.
163 * Assumes term->tl_rows and term->tl_cols are zero.
164 */
165 static void
166set_term_and_win_size(term_T *term)
167{
168 if (*curwin->w_p_tms != NUL)
169 {
170 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
171
172 term->tl_rows = atoi((char *)curwin->w_p_tms);
173 term->tl_cols = atoi((char *)p);
174 }
175 if (term->tl_rows == 0)
176 term->tl_rows = curwin->w_height;
177 else
178 {
179 win_setheight_win(term->tl_rows, curwin);
180 term->tl_rows_fixed = TRUE;
181 }
182 if (term->tl_cols == 0)
183 term->tl_cols = curwin->w_width;
184 else
185 {
186 win_setwidth_win(term->tl_cols, curwin);
187 term->tl_cols_fixed = TRUE;
188 }
189}
190
191/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200192 * Initialize job options for a terminal job.
193 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200194 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200195 static void
196init_job_options(jobopt_T *opt)
197{
198 clear_job_options(opt);
199
200 opt->jo_mode = MODE_RAW;
201 opt->jo_out_mode = MODE_RAW;
202 opt->jo_err_mode = MODE_RAW;
203 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
204
205 opt->jo_io[PART_OUT] = JIO_BUFFER;
206 opt->jo_io[PART_ERR] = JIO_BUFFER;
207 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
208
209 opt->jo_modifiable[PART_OUT] = 0;
210 opt->jo_modifiable[PART_ERR] = 0;
211 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
212
213 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
214}
215
216/*
217 * Set job options mandatory for a terminal job.
218 */
219 static void
220setup_job_options(jobopt_T *opt, int rows, int cols)
221{
222 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
223 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
224 opt->jo_pty = TRUE;
225 opt->jo_term_rows = rows;
226 opt->jo_term_cols = cols;
227}
228
229 static void
230term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200231{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200232 exarg_T split_ea;
233 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200234 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200235
236 if (check_restricted() || check_secure())
237 return;
238
239 term = (term_T *)alloc_clear(sizeof(term_T));
240 if (term == NULL)
241 return;
242 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200243 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200244 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200245
246 /* Open a new window or tab. */
247 vim_memset(&split_ea, 0, sizeof(split_ea));
248 split_ea.cmdidx = CMD_new;
249 split_ea.cmd = (char_u *)"new";
250 split_ea.arg = (char_u *)"";
251 ex_splitview(&split_ea);
252 if (curwin == old_curwin)
253 {
254 /* split failed */
255 vim_free(term);
256 return;
257 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200258 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200259 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200260
261 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200262 term->tl_next = first_term;
263 first_term = term;
264
Bram Moolenaar293424c2017-07-26 23:11:01 +0200265 if (cmd == NULL || *cmd == NUL)
266 cmd = p_sh;
267
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200268 {
269 int i;
270 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200271 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200272
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200273 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200274 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200275 /* Prepend a ! to the command name to avoid the buffer name equals
276 * the executable, otherwise ":w!" would overwrite it. */
277 if (i == 0)
278 vim_snprintf((char *)p, len, "!%s", cmd);
279 else
280 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200281 if (buflist_findname(p) == NULL)
282 {
283 curbuf->b_ffname = p;
284 break;
285 }
286 }
287 }
288 curbuf->b_fname = curbuf->b_ffname;
289
Bram Moolenaareb44a682017-08-03 22:44:55 +0200290 set_string_option_direct((char_u *)"buftype", -1,
291 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
292
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200293 /* Mark the buffer as not modifiable. It can only be made modifiable after
294 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200295 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200296
297 /* Set 'bufhidden' to "hide": allow closing the window. */
298 set_string_option_direct((char_u *)"bufhidden", -1,
299 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200300
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200301 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200302 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200303
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200304 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200305 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200306 {
307 /* store the size we ended up with */
308 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200309 }
310 else
311 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200312 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200313
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200314 /* Wiping out the buffer will also close the window and call
315 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200316 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200317 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200318}
319
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200320/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200321 * ":terminal": open a terminal window and execute a job in it.
322 */
323 void
324ex_terminal(exarg_T *eap)
325{
326 jobopt_T opt;
327
328 init_job_options(&opt);
329 /* TODO: get options from before the command */
330
331 term_start(eap->arg, &opt);
332}
333
334/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200335 * Free the scrollback buffer for "term".
336 */
337 static void
338free_scrollback(term_T *term)
339{
340 int i;
341
342 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
343 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
344 ga_clear(&term->tl_scrollback);
345}
346
347/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200348 * Free a terminal and everything it refers to.
349 * Kills the job if there is one.
350 * Called when wiping out a buffer.
351 */
352 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200353free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200354{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200355 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200356 term_T *tp;
357
358 if (term == NULL)
359 return;
360 if (first_term == term)
361 first_term = term->tl_next;
362 else
363 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
364 if (tp->tl_next == term)
365 {
366 tp->tl_next = term->tl_next;
367 break;
368 }
369
370 if (term->tl_job != NULL)
371 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200372 if (term->tl_job->jv_status != JOB_ENDED
373 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200374 job_stop(term->tl_job, NULL, "kill");
375 job_unref(term->tl_job);
376 }
377
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200378 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200379
380 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200381 vim_free(term->tl_title);
382 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200383 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200384 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200385}
386
387/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200388 * Write job output "msg[len]" to the vterm.
389 */
390 static void
391term_write_job_output(term_T *term, char_u *msg, size_t len)
392{
393 VTerm *vterm = term->tl_vterm;
394 char_u *p;
395 size_t done;
396 size_t len_now;
397
398 for (done = 0; done < len; done += len_now)
399 {
400 for (p = msg + done; p < msg + len; )
401 {
402 if (*p == NL)
403 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200404 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200405 }
406 len_now = p - msg - done;
407 vterm_input_write(vterm, (char *)msg + done, len_now);
408 if (p < msg + len && *p == NL)
409 {
410 /* Convert NL to CR-NL, that appears to work best. */
411 vterm_input_write(vterm, "\r\n", 2);
412 ++len_now;
413 }
414 }
415
416 /* this invokes the damage callbacks */
417 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
418}
419
Bram Moolenaar1c844932017-07-24 23:36:41 +0200420 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200421update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200422{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200423 if (term->tl_terminal_mode)
424 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200425 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200426 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200427 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200428 if (term->tl_cursor_visible)
429 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200430 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200431#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200432 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200433 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200434#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200435 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200436}
437
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200438/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200439 * Invoked when "msg" output from a job was received. Write it to the terminal
440 * of "buffer".
441 */
442 void
443write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
444{
445 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200446 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200447
Bram Moolenaard85f2712017-07-28 21:51:57 +0200448 if (term->tl_vterm == NULL)
449 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200450 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200451 return;
452 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200453 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200454 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200455
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200456 if (!term->tl_terminal_mode)
457 {
458 /* TODO: only update once in a while. */
459 update_screen(0);
460 update_cursor(term, TRUE);
461 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200462}
463
464/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200465 * Send a mouse position and click to the vterm
466 */
467 static int
468term_send_mouse(VTerm *vterm, int button, int pressed)
469{
470 VTermModifier mod = VTERM_MOD_NONE;
471
472 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
473 mouse_col - W_WINCOL(curwin), mod);
474 vterm_mouse_button(vterm, button, pressed, mod);
475 return TRUE;
476}
477
478/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200479 * Convert typed key "c" into bytes to send to the job.
480 * Return the number of bytes in "buf".
481 */
482 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200483term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200484{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200485 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200486 VTermKey key = VTERM_KEY_NONE;
487 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200488 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200489
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200490 switch (c)
491 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200492 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200493 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200494 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
495 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200496 case K_DEL: key = VTERM_KEY_DEL; break;
497 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200498 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
499 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200500 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200501 case K_S_END: mod = VTERM_MOD_SHIFT;
502 key = VTERM_KEY_END; break;
503 case K_C_END: mod = VTERM_MOD_CTRL;
504 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200505 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
506 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
507 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
508 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
509 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
510 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
511 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
512 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
513 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
514 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
515 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
516 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
517 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200518 case K_S_HOME: mod = VTERM_MOD_SHIFT;
519 key = VTERM_KEY_HOME; break;
520 case K_C_HOME: mod = VTERM_MOD_CTRL;
521 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200522 case K_INS: key = VTERM_KEY_INS; break;
523 case K_K0: key = VTERM_KEY_KP_0; break;
524 case K_K1: key = VTERM_KEY_KP_1; break;
525 case K_K2: key = VTERM_KEY_KP_2; break;
526 case K_K3: key = VTERM_KEY_KP_3; break;
527 case K_K4: key = VTERM_KEY_KP_4; break;
528 case K_K5: key = VTERM_KEY_KP_5; break;
529 case K_K6: key = VTERM_KEY_KP_6; break;
530 case K_K7: key = VTERM_KEY_KP_7; break;
531 case K_K8: key = VTERM_KEY_KP_8; break;
532 case K_K9: key = VTERM_KEY_KP_9; break;
533 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
534 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
535 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
536 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
537 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
538 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
539 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
540 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
541 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
542 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
543 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
544 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
545 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200546 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
547 key = VTERM_KEY_LEFT; break;
548 case K_C_LEFT: mod = VTERM_MOD_CTRL;
549 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200550 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
551 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
552 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200553 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
554 key = VTERM_KEY_RIGHT; break;
555 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
556 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200557 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200558 case K_S_UP: mod = VTERM_MOD_SHIFT;
559 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200560 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200561
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200562 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
563 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
564 case K_MOUSELEFT: /* TODO */ return 0;
565 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200566
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200567 case K_LEFTMOUSE:
568 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
569 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
570 case K_LEFTRELEASE:
571 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
572 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
573 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
574 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
575 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
576 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
577 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
578 case K_X1MOUSE: /* TODO */ return 0;
579 case K_X1DRAG: /* TODO */ return 0;
580 case K_X1RELEASE: /* TODO */ return 0;
581 case K_X2MOUSE: /* TODO */ return 0;
582 case K_X2DRAG: /* TODO */ return 0;
583 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200584
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200585 case K_IGNORE: return 0;
586 case K_NOP: return 0;
587 case K_UNDO: return 0;
588 case K_HELP: return 0;
589 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
590 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
591 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
592 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
593 case K_SELECT: return 0;
594#ifdef FEAT_GUI
595 case K_VER_SCROLLBAR: return 0;
596 case K_HOR_SCROLLBAR: return 0;
597#endif
598#ifdef FEAT_GUI_TABLINE
599 case K_TABLINE: return 0;
600 case K_TABMENU: return 0;
601#endif
602#ifdef FEAT_NETBEANS_INTG
603 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
604#endif
605#ifdef FEAT_DND
606 case K_DROP: return 0;
607#endif
608#ifdef FEAT_AUTOCMD
609 case K_CURSORHOLD: return 0;
610#endif
611 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
612 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200613 }
614
615 /*
616 * Convert special keys to vterm keys:
617 * - Write keys to vterm: vterm_keyboard_key()
618 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200619 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200620 */
621 if (key != VTERM_KEY_NONE)
622 /* Special key, let vterm convert it. */
623 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200624 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200625 /* Normal character, let vterm convert it. */
626 vterm_keyboard_unichar(vterm, c, mod);
627
628 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200629 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200630}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200631
Bram Moolenaar938783d2017-07-16 20:13:26 +0200632/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200633 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200634 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200635 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200636term_job_running(term_T *term)
637{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200638 /* Also consider the job finished when the channel is closed, to avoid a
639 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200640 return term != NULL
641 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200642 && term->tl_job->jv_status == JOB_STARTED
643 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200644}
645
646/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200647 * Add the last line of the scrollback buffer to the buffer in the window.
648 */
649 static void
650add_scrollback_line_to_buffer(term_T *term)
651{
652 linenr_T lnum = term->tl_scrollback.ga_len - 1;
653 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
654 garray_T ga;
655 int c;
656 int col;
657 int i;
658
659 ga_init2(&ga, 1, 100);
660 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
661 {
662 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
663 goto failed;
664 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
665 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
666 (char_u *)ga.ga_data + ga.ga_len);
667 }
668 if (ga_grow(&ga, 1) == FAIL)
669 goto failed;
670 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
671 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
672
673 if (lnum == 0)
674 {
675 /* Delete the empty line that was in the empty buffer. */
676 curbuf = term->tl_buffer;
677 ml_delete(2, FALSE);
678 curbuf = curwin->w_buffer;
679 }
680
681failed:
682 ga_clear(&ga);
683}
684
685/*
686 * Add the current lines of the terminal to scrollback and to the buffer.
687 * Called after the job has ended and when switching to Terminal mode.
688 */
689 static void
690move_terminal_to_buffer(term_T *term)
691{
692 win_T *wp;
693 int len;
694 int lines_skipped = 0;
695 VTermPos pos;
696 VTermScreenCell cell;
697 VTermScreenCell *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200698 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200699
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200700 if (term->tl_vterm == NULL)
701 return;
702 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200703 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
704 {
705 len = 0;
706 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
707 if (vterm_screen_get_cell(screen, pos, &cell) != 0
708 && cell.chars[0] != NUL)
709 len = pos.col + 1;
710
711 if (len == 0)
712 ++lines_skipped;
713 else
714 {
715 while (lines_skipped > 0)
716 {
717 /* Line was skipped, add an empty line. */
718 --lines_skipped;
719 if (ga_grow(&term->tl_scrollback, 1) == OK)
720 {
721 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
722 + term->tl_scrollback.ga_len;
723
724 line->sb_cols = 0;
725 line->sb_cells = NULL;
726 ++term->tl_scrollback.ga_len;
727
728 add_scrollback_line_to_buffer(term);
729 }
730 }
731
732 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
733 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
734 {
735 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
736 + term->tl_scrollback.ga_len;
737
738 for (pos.col = 0; pos.col < len; ++pos.col)
739 {
740 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
741 vim_memset(p + pos.col, 0, sizeof(cell));
742 else
743 p[pos.col] = cell;
744 }
745 line->sb_cols = len;
746 line->sb_cells = p;
747 ++term->tl_scrollback.ga_len;
748
749 add_scrollback_line_to_buffer(term);
750 }
751 else
752 vim_free(p);
753 }
754 }
755
756 FOR_ALL_WINDOWS(wp)
757 {
758 if (wp->w_buffer == term->tl_buffer)
759 {
760 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
761 wp->w_cursor.col = 0;
762 wp->w_valid = 0;
763 redraw_win_later(wp, NOT_VALID);
764 }
765 }
766}
767
768 static void
769set_terminal_mode(term_T *term, int on)
770{
771 term->tl_terminal_mode = on;
772 vim_free(term->tl_status_text);
773 term->tl_status_text = NULL;
774 if (term->tl_buffer == curbuf)
775 maketitle();
776}
777
778/*
779 * Called after the job if finished and Terminal mode is not active:
780 * Move the vterm contents into the scrollback buffer and free the vterm.
781 */
782 static void
783cleanup_vterm(term_T *term)
784{
785 move_terminal_to_buffer(term);
786 term_free_vterm(term);
787 set_terminal_mode(term, FALSE);
788}
789
790/*
791 * Switch from sending keys to the job to Terminal-Normal mode.
792 * Suspends updating the terminal window.
793 */
794 static void
795term_enter_terminal_mode()
796{
797 term_T *term = curbuf->b_term;
798
799 /* Append the current terminal contents to the buffer. */
800 move_terminal_to_buffer(term);
801
802 set_terminal_mode(term, TRUE);
803}
804
805/*
806 * Returns TRUE if the current window contains a terminal and we are in
807 * Terminal-Normal mode.
808 */
809 int
810term_in_terminal_mode()
811{
812 term_T *term = curbuf->b_term;
813
814 return term != NULL && term->tl_terminal_mode;
815}
816
817/*
818 * Switch from Terminal-Normal mode to sending keys to the job.
819 * Restores updating the terminal window.
820 */
821 void
822term_leave_terminal_mode()
823{
824 term_T *term = curbuf->b_term;
825 sb_line_T *line;
826 garray_T *gap;
827
828 /* Remove the terminal contents from the scrollback and the buffer. */
829 gap = &term->tl_scrollback;
830 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
831 {
832 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
833 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
834 vim_free(line->sb_cells);
835 --gap->ga_len;
836 if (gap->ga_len == 0)
837 break;
838 }
839 check_cursor();
840
841 set_terminal_mode(term, FALSE);
842
843 if (term->tl_channel_closed)
844 cleanup_vterm(term);
845 redraw_buf_and_status_later(curbuf, NOT_VALID);
846}
847
848/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200849 * Get a key from the user without mapping.
850 * TODO: use terminal mode mappings.
851 */
852 static int
853term_vgetc()
854{
855 int c;
856
857 ++no_mapping;
858 ++allow_keys;
859 got_int = FALSE;
860 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200861 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200862 --no_mapping;
863 --allow_keys;
864 return c;
865}
866
867/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200868 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200869 * Return FAIL when the key needs to be handled in Normal mode.
870 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200871 */
872 static int
873send_keys_to_term(term_T *term, int c, int typed)
874{
875 char msg[KEY_BUF_LEN];
876 size_t len;
877 static int mouse_was_outside = FALSE;
878 int dragging_outside = FALSE;
879
880 /* Catch keys that need to be handled as in Normal mode. */
881 switch (c)
882 {
883 case NUL:
884 case K_ZERO:
885 if (typed)
886 stuffcharReadbuff(c);
887 return FAIL;
888
889 case K_IGNORE:
890 return FAIL;
891
892 case K_LEFTDRAG:
893 case K_MIDDLEDRAG:
894 case K_RIGHTDRAG:
895 case K_X1DRAG:
896 case K_X2DRAG:
897 dragging_outside = mouse_was_outside;
898 /* FALLTHROUGH */
899 case K_LEFTMOUSE:
900 case K_LEFTMOUSE_NM:
901 case K_LEFTRELEASE:
902 case K_LEFTRELEASE_NM:
903 case K_MIDDLEMOUSE:
904 case K_MIDDLERELEASE:
905 case K_RIGHTMOUSE:
906 case K_RIGHTRELEASE:
907 case K_X1MOUSE:
908 case K_X1RELEASE:
909 case K_X2MOUSE:
910 case K_X2RELEASE:
911 if (mouse_row < W_WINROW(curwin)
912 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
913 || mouse_col < W_WINCOL(curwin)
914 || mouse_col >= W_ENDCOL(curwin)
915 || dragging_outside)
916 {
917 /* click outside the current window */
918 if (typed)
919 {
920 stuffcharReadbuff(c);
921 mouse_was_outside = TRUE;
922 }
923 return FAIL;
924 }
925 }
926 if (typed)
927 mouse_was_outside = FALSE;
928
929 /* Convert the typed key to a sequence of bytes for the job. */
930 len = term_convert_key(term, c, msg);
931 if (len > 0)
932 /* TODO: if FAIL is returned, stop? */
933 channel_send(term->tl_job->jv_channel, PART_IN,
934 (char_u *)msg, (int)len, NULL);
935
936 return OK;
937}
938
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200939 static void
940position_cursor(win_T *wp, VTermPos *pos)
941{
942 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
943 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
944 wp->w_valid |= (VALID_WCOL|VALID_WROW);
945}
946
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200947/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200948 * Handle CTRL-W "": send register contents to the job.
949 */
950 static void
951term_paste_register(int prev_c UNUSED)
952{
953 int c;
954 list_T *l;
955 listitem_T *item;
956 long reglen = 0;
957 int type;
958
959#ifdef FEAT_CMDL_INFO
960 if (add_to_showcmd(prev_c))
961 if (add_to_showcmd('"'))
962 out_flush();
963#endif
964 c = term_vgetc();
965#ifdef FEAT_CMDL_INFO
966 clear_showcmd();
967#endif
968
969 /* CTRL-W "= prompt for expression to evaluate. */
970 if (c == '=' && get_expr_register() != '=')
971 return;
972
973 l = (list_T *)get_reg_contents(c, GREG_LIST);
974 if (l != NULL)
975 {
976 type = get_reg_type(c, &reglen);
977 for (item = l->lv_first; item != NULL; item = item->li_next)
978 {
979 char_u *s = get_tv_string(&item->li_tv);
980
981 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
982 s, STRLEN(s), NULL);
983 if (item->li_next != NULL || type == MLINE)
984 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
985 (char_u *)"\r", 1, NULL);
986 }
987 list_free(l);
988 }
989}
990
991/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200992 * Returns TRUE if the current window contains a terminal and we are sending
993 * keys to the job.
994 */
995 int
996term_use_loop()
997{
998 term_T *term = curbuf->b_term;
999
1000 return term != NULL
1001 && !term->tl_terminal_mode
1002 && term->tl_vterm != NULL
1003 && term_job_running(term);
1004}
1005
1006/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001007 * Wait for input and send it to the job.
1008 * Return when the start of a CTRL-W command is typed or anything else that
1009 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001010 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1011 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001012 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001013 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001014terminal_loop(void)
1015{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001016 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001017 int termkey = 0;
1018
1019 if (*curwin->w_p_tk != NUL)
1020 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001021 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001022
1023 for (;;)
1024 {
1025 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001026 /* Repeat redrawing in case a message is received while redrawing. */
1027 while (curwin->w_redr_type != 0)
1028 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001029 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001030
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001031 c = term_vgetc();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001032 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001033 /* job finished while waiting for a character */
1034 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001035
Bram Moolenaarfae42832017-08-01 22:24:26 +02001036#ifdef UNIX
1037 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1038#endif
1039#ifdef WIN3264
1040 if (c == Ctrl_C)
1041 /* We don't know if the job can handle CTRL-C itself or not, this
1042 * may kill the shell instead of killing the command running in the
1043 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001044 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001045#endif
1046
Bram Moolenaar69198192017-08-05 14:10:48 +02001047 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001048 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001049 int prev_c = c;
1050
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001051#ifdef FEAT_CMDL_INFO
1052 if (add_to_showcmd(c))
1053 out_flush();
1054#endif
1055 c = term_vgetc();
1056#ifdef FEAT_CMDL_INFO
1057 clear_showcmd();
1058#endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001059 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001060 /* job finished while waiting for a character */
1061 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001062
Bram Moolenaar69198192017-08-05 14:10:48 +02001063 if (prev_c == Ctrl_BSL)
1064 {
1065 if (c == Ctrl_N)
1066 /* CTRL-\ CTRL-N : execute one Normal mode command. */
1067 return OK;
1068 /* Send both keys to the terminal. */
1069 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1070 }
1071 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001072 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001073 /* "CTRL-W .": send CTRL-W to the job */
1074 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001075 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001076 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001077 {
1078 term_enter_terminal_mode();
1079 return FAIL;
1080 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001081 else if (c == '"')
1082 {
1083 term_paste_register(prev_c);
1084 continue;
1085 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001086 else if (termkey == 0 || c != termkey)
1087 {
1088 stuffcharReadbuff(Ctrl_W);
1089 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001090 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001091 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001092 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001093 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1094 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001095 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001096 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001097}
1098
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001099/*
1100 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001101 * This updates the title and status, but does not close the vter, because
1102 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001103 */
1104 void
1105term_job_ended(job_T *job)
1106{
1107 term_T *term;
1108 int did_one = FALSE;
1109
1110 for (term = first_term; term != NULL; term = term->tl_next)
1111 if (term->tl_job == job)
1112 {
1113 vim_free(term->tl_title);
1114 term->tl_title = NULL;
1115 vim_free(term->tl_status_text);
1116 term->tl_status_text = NULL;
1117 redraw_buf_and_status_later(term->tl_buffer, VALID);
1118 did_one = TRUE;
1119 }
1120 if (did_one)
1121 redraw_statuslines();
1122 if (curbuf->b_term != NULL)
1123 {
1124 if (curbuf->b_term->tl_job == job)
1125 maketitle();
1126 update_cursor(curbuf->b_term, TRUE);
1127 }
1128}
1129
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001130 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001131may_toggle_cursor(term_T *term)
1132{
1133 if (curbuf == term->tl_buffer)
1134 {
1135 if (term->tl_cursor_visible)
1136 cursor_on();
1137 else
1138 cursor_off();
1139 }
1140}
1141
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001142 static int
1143handle_damage(VTermRect rect, void *user)
1144{
1145 term_T *term = (term_T *)user;
1146
1147 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1148 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1149 redraw_buf_later(term->tl_buffer, NOT_VALID);
1150 return 1;
1151}
1152
1153 static int
1154handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1155{
1156 term_T *term = (term_T *)user;
1157
1158 /* TODO */
1159 redraw_buf_later(term->tl_buffer, NOT_VALID);
1160 return 1;
1161}
1162
1163 static int
1164handle_movecursor(
1165 VTermPos pos,
1166 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001167 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001168 void *user)
1169{
1170 term_T *term = (term_T *)user;
1171 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001172
1173 term->tl_cursor_pos = pos;
1174 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001175
1176 FOR_ALL_WINDOWS(wp)
1177 {
1178 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001179 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001180 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001181 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001182 {
1183 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001184 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001185 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001186
1187 return 1;
1188}
1189
Bram Moolenaar21554412017-07-24 21:44:43 +02001190 static int
1191handle_settermprop(
1192 VTermProp prop,
1193 VTermValue *value,
1194 void *user)
1195{
1196 term_T *term = (term_T *)user;
1197
1198 switch (prop)
1199 {
1200 case VTERM_PROP_TITLE:
1201 vim_free(term->tl_title);
1202 term->tl_title = vim_strsave((char_u *)value->string);
1203 vim_free(term->tl_status_text);
1204 term->tl_status_text = NULL;
1205 if (term == curbuf->b_term)
1206 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001207 break;
1208
1209 case VTERM_PROP_CURSORVISIBLE:
1210 term->tl_cursor_visible = value->boolean;
1211 may_toggle_cursor(term);
1212 out_flush();
1213 break;
1214
Bram Moolenaar21554412017-07-24 21:44:43 +02001215 default:
1216 break;
1217 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001218 /* Always return 1, otherwise vterm doesn't store the value internally. */
1219 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001220}
1221
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001222/*
1223 * The job running in the terminal resized the terminal.
1224 */
1225 static int
1226handle_resize(int rows, int cols, void *user)
1227{
1228 term_T *term = (term_T *)user;
1229 win_T *wp;
1230
1231 term->tl_rows = rows;
1232 term->tl_cols = cols;
1233 FOR_ALL_WINDOWS(wp)
1234 {
1235 if (wp->w_buffer == term->tl_buffer)
1236 {
1237 win_setheight_win(rows, wp);
1238 win_setwidth_win(cols, wp);
1239 }
1240 }
1241
1242 redraw_buf_later(term->tl_buffer, NOT_VALID);
1243 return 1;
1244}
1245
Bram Moolenaard85f2712017-07-28 21:51:57 +02001246/*
1247 * Handle a line that is pushed off the top of the screen.
1248 */
1249 static int
1250handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1251{
1252 term_T *term = (term_T *)user;
1253
1254 /* TODO: Limit the number of lines that are stored. */
1255 /* TODO: put the text in the buffer. */
1256 if (ga_grow(&term->tl_scrollback, 1) == OK)
1257 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001258 VTermScreenCell *p = NULL;
1259 int len = 0;
1260 int i;
1261 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001262
1263 /* do not store empty cells at the end */
1264 for (i = 0; i < cols; ++i)
1265 if (cells[i].chars[0] != 0)
1266 len = i + 1;
1267
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001268 if (len > 0)
1269 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001270 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001271 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001272
1273 line = (sb_line_T *)term->tl_scrollback.ga_data
1274 + term->tl_scrollback.ga_len;
1275 line->sb_cols = len;
1276 line->sb_cells = p;
1277 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001278 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001279
1280 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001281 }
1282 return 0; /* ignored */
1283}
1284
Bram Moolenaar21554412017-07-24 21:44:43 +02001285static VTermScreenCallbacks screen_callbacks = {
1286 handle_damage, /* damage */
1287 handle_moverect, /* moverect */
1288 handle_movecursor, /* movecursor */
1289 handle_settermprop, /* settermprop */
1290 NULL, /* bell */
1291 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001292 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001293 NULL /* sb_popline */
1294};
1295
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001296/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001297 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001298 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001299 */
1300 void
1301term_channel_closed(channel_T *ch)
1302{
1303 term_T *term;
1304 int did_one = FALSE;
1305
1306 for (term = first_term; term != NULL; term = term->tl_next)
1307 if (term->tl_job == ch->ch_job)
1308 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001309 term->tl_channel_closed = TRUE;
1310
Bram Moolenaard85f2712017-07-28 21:51:57 +02001311 vim_free(term->tl_title);
1312 term->tl_title = NULL;
1313 vim_free(term->tl_status_text);
1314 term->tl_status_text = NULL;
1315
Bram Moolenaar423802d2017-07-30 16:52:24 +02001316 /* Unless in Terminal-Normal mode: clear the vterm. */
1317 if (!term->tl_terminal_mode)
1318 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001319
1320 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1321 did_one = TRUE;
1322 }
1323 if (did_one)
1324 {
1325 redraw_statuslines();
1326
1327 /* Need to break out of vgetc(). */
1328 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001329 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001330
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001331 term = curbuf->b_term;
1332 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001333 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001334 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001335 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001336 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001337 }
1338 }
1339}
1340
1341/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001342 * Reverse engineer the RGB value into a cterm color index.
1343 * First color is 1. Return 0 if no match found.
1344 */
1345 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001346color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001347{
1348 int red = color->red;
1349 int blue = color->blue;
1350 int green = color->green;
1351
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001352 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001353 if (red == 0)
1354 {
1355 if (green == 0)
1356 {
1357 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001358 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001359 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001360 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001361 }
1362 else if (green == 224)
1363 {
1364 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001365 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001366 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001367 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001368 }
1369 }
1370 else if (red == 224)
1371 {
1372 if (green == 0)
1373 {
1374 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001375 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001376 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001377 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001378 }
1379 else if (green == 224)
1380 {
1381 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001382 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001383 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001384 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001385 }
1386 }
1387 else if (red == 128)
1388 {
1389 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001390 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001391 }
1392 else if (red == 255)
1393 {
1394 if (green == 64)
1395 {
1396 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001397 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001398 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001399 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001400 }
1401 else if (green == 255)
1402 {
1403 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001404 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001405 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001406 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001407 }
1408 }
1409 else if (red == 64)
1410 {
1411 if (green == 64)
1412 {
1413 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001414 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001415 }
1416 else if (green == 255)
1417 {
1418 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001419 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001420 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001421 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001422 }
1423 }
1424 if (t_colors >= 256)
1425 {
1426 if (red == blue && red == green)
1427 {
1428 /* 24-color greyscale */
1429 static int cutoff[23] = {
1430 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1431 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1432 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1433 int i;
1434
1435 for (i = 0; i < 23; ++i)
1436 if (red < cutoff[i])
1437 return i + 233;
1438 return 256;
1439 }
1440
1441 /* 216-color cube */
1442 return 17 + ((red + 25) / 0x33) * 36
1443 + ((green + 25) / 0x33) * 6
1444 + (blue + 25) / 0x33;
1445 }
1446 return 0;
1447}
1448
1449/*
1450 * Convert the attributes of a vterm cell into an attribute index.
1451 */
1452 static int
1453cell2attr(VTermScreenCell *cell)
1454{
1455 int attr = 0;
1456
1457 if (cell->attrs.bold)
1458 attr |= HL_BOLD;
1459 if (cell->attrs.underline)
1460 attr |= HL_UNDERLINE;
1461 if (cell->attrs.italic)
1462 attr |= HL_ITALIC;
1463 if (cell->attrs.strike)
1464 attr |= HL_STANDOUT;
1465 if (cell->attrs.reverse)
1466 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001467
1468#ifdef FEAT_GUI
1469 if (gui.in_use)
1470 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001471 guicolor_T fg, bg;
1472
1473 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1474 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1475 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001476 }
1477 else
1478#endif
1479#ifdef FEAT_TERMGUICOLORS
1480 if (p_tgc)
1481 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001482 guicolor_T fg, bg;
1483
1484 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1485 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1486
1487 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001488 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001489 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001490#endif
1491 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001492 int bold = MAYBE;
1493 int fg = color2index(&cell->fg, TRUE, &bold);
1494 int bg = color2index(&cell->bg, FALSE, &bold);
1495
1496 /* with 8 colors set the bold attribute to get a bright foreground */
1497 if (bold == TRUE)
1498 attr |= HL_BOLD;
1499 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001500 }
1501 return 0;
1502}
1503
1504/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001505 * Called to update the window that contains a terminal.
1506 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001507 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001508 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001509term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001510{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001511 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001512 VTerm *vterm;
1513 VTermScreen *screen;
1514 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001515 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001516
Bram Moolenaar423802d2017-07-30 16:52:24 +02001517 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001518 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001519
Bram Moolenaard85f2712017-07-28 21:51:57 +02001520 vterm = term->tl_vterm;
1521 screen = vterm_obtain_screen(vterm);
1522 state = vterm_obtain_state(vterm);
1523
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001524 /*
1525 * If the window was resized a redraw will be triggered and we get here.
1526 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1527 */
1528 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1529 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001530 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001531 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1532 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1533 win_T *twp;
1534
1535 FOR_ALL_WINDOWS(twp)
1536 {
1537 /* When more than one window shows the same terminal, use the
1538 * smallest size. */
1539 if (twp->w_buffer == term->tl_buffer)
1540 {
1541 if (!term->tl_rows_fixed && rows > twp->w_height)
1542 rows = twp->w_height;
1543 if (!term->tl_cols_fixed && cols > twp->w_width)
1544 cols = twp->w_width;
1545 }
1546 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001547
1548 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001549 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001550 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001551 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001552 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001553
1554 /* The cursor may have been moved when resizing. */
1555 vterm_state_get_cursorpos(state, &pos);
1556 position_cursor(wp, &pos);
1557
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001558 /* TODO: Only redraw what changed. */
1559 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001560 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001561 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001562 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001563
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001564 if (pos.row < term->tl_rows)
1565 {
1566 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001567 {
1568 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001569 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001570
Bram Moolenaareeac6772017-07-23 15:48:37 +02001571 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1572 vim_memset(&cell, 0, sizeof(cell));
1573
1574 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001575 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001576 if (c == NUL)
1577 {
1578 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001579#if defined(FEAT_MBYTE)
1580 if (enc_utf8)
1581 ScreenLinesUC[off] = NUL;
1582#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001583 }
1584 else
1585 {
1586#if defined(FEAT_MBYTE)
1587 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001588 {
1589 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001590 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001591 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001592 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001593 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001594 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001595 if (enc_utf8)
1596 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001597 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001598#else
1599 ScreenLines[off] = c;
1600#endif
1601 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001602 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001603
1604 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001605 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001606 if (cell.width == 2)
1607 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001608 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001609#if defined(FEAT_MBYTE)
1610 if (enc_utf8)
1611 ScreenLinesUC[off] = NUL;
1612#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001613 ++pos.col;
1614 ++off;
1615 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001616 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001617 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001618 else
1619 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001620
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001621 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1622 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001623 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001624
1625 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001626}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001627
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001628/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001629 * Return TRUE if "wp" is a terminal window where the job has finished.
1630 */
1631 int
1632term_is_finished(buf_T *buf)
1633{
1634 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1635}
1636
1637/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001638 * Return TRUE if "wp" is a terminal window where the job has finished or we
1639 * are in Terminal-Normal mode.
1640 */
1641 int
1642term_show_buffer(buf_T *buf)
1643{
1644 term_T *term = buf->b_term;
1645
1646 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1647}
1648
1649/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001650 * The current buffer is going to be changed. If there is terminal
1651 * highlighting remove it now.
1652 */
1653 void
1654term_change_in_curbuf(void)
1655{
1656 term_T *term = curbuf->b_term;
1657
1658 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1659 {
1660 free_scrollback(term);
1661 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001662
1663 /* The buffer is now like a normal buffer, it cannot be easily
1664 * abandoned when changed. */
1665 set_string_option_direct((char_u *)"buftype", -1,
1666 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001667 }
1668}
1669
1670/*
1671 * Get the screen attribute for a position in the buffer.
1672 */
1673 int
1674term_get_attr(buf_T *buf, linenr_T lnum, int col)
1675{
1676 term_T *term = buf->b_term;
1677 sb_line_T *line;
1678
Bram Moolenaar70229f92017-07-29 16:01:53 +02001679 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001680 return 0;
1681 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1682 if (col >= line->sb_cols)
1683 return 0;
1684 return cell2attr(line->sb_cells + col);
1685}
1686
1687/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001688 * Create a new vterm and initialize it.
1689 */
1690 static void
1691create_vterm(term_T *term, int rows, int cols)
1692{
1693 VTerm *vterm;
1694 VTermScreen *screen;
1695
1696 vterm = vterm_new(rows, cols);
1697 term->tl_vterm = vterm;
1698 screen = vterm_obtain_screen(vterm);
1699 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1700 /* TODO: depends on 'encoding'. */
1701 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001702
1703 /* Vterm uses a default black background. Set it to white when
1704 * 'background' is "light". */
1705 if (*p_bg == 'l')
1706 {
1707 VTermColor fg, bg;
1708
1709 fg.red = fg.green = fg.blue = 0;
1710 bg.red = bg.green = bg.blue = 255;
1711 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1712 }
1713
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001714 /* Required to initialize most things. */
1715 vterm_screen_reset(screen, 1 /* hard */);
1716}
1717
Bram Moolenaar21554412017-07-24 21:44:43 +02001718/*
1719 * Return the text to show for the buffer name and status.
1720 */
1721 char_u *
1722term_get_status_text(term_T *term)
1723{
1724 if (term->tl_status_text == NULL)
1725 {
1726 char_u *txt;
1727 size_t len;
1728
Bram Moolenaar423802d2017-07-30 16:52:24 +02001729 if (term->tl_terminal_mode)
1730 {
1731 if (term_job_running(term))
1732 txt = (char_u *)_("Terminal");
1733 else
1734 txt = (char_u *)_("Terminal-finished");
1735 }
1736 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001737 txt = term->tl_title;
1738 else if (term_job_running(term))
1739 txt = (char_u *)_("running");
1740 else
1741 txt = (char_u *)_("finished");
1742 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001743 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001744 if (term->tl_status_text != NULL)
1745 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1746 term->tl_buffer->b_fname, txt);
1747 }
1748 return term->tl_status_text;
1749}
1750
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001751/*
1752 * Mark references in jobs of terminals.
1753 */
1754 int
1755set_ref_in_term(int copyID)
1756{
1757 int abort = FALSE;
1758 term_T *term;
1759 typval_T tv;
1760
1761 for (term = first_term; term != NULL; term = term->tl_next)
1762 if (term->tl_job != NULL)
1763 {
1764 tv.v_type = VAR_JOB;
1765 tv.vval.v_job = term->tl_job;
1766 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1767 }
1768 return abort;
1769}
1770
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001771/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001772 * Get the buffer from the first argument in "argvars".
1773 * Returns NULL when the buffer is not for a terminal window.
1774 */
1775 static buf_T *
1776term_get_buf(typval_T *argvars)
1777{
1778 buf_T *buf;
1779
1780 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1781 ++emsg_off;
1782 buf = get_buf_tv(&argvars[0], FALSE);
1783 --emsg_off;
1784 if (buf == NULL || buf->b_term == NULL)
1785 return NULL;
1786 return buf;
1787}
1788
1789/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001790 * "term_getattr(attr, name)" function
1791 */
1792 void
1793f_term_getattr(typval_T *argvars, typval_T *rettv)
1794{
1795 int attr;
1796 size_t i;
1797 char_u *name;
1798
1799 static struct {
1800 char *name;
1801 int attr;
1802 } attrs[] = {
1803 {"bold", HL_BOLD},
1804 {"italic", HL_ITALIC},
1805 {"underline", HL_UNDERLINE},
1806 {"strike", HL_STANDOUT},
1807 {"reverse", HL_INVERSE},
1808 };
1809
1810 attr = get_tv_number(&argvars[0]);
1811 name = get_tv_string_chk(&argvars[1]);
1812 if (name == NULL)
1813 return;
1814
1815 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1816 if (STRCMP(name, attrs[i].name) == 0)
1817 {
1818 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1819 break;
1820 }
1821}
1822
1823/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001824 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001825 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001826 void
1827f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001828{
Bram Moolenaar97870002017-07-30 18:28:38 +02001829 buf_T *buf = term_get_buf(argvars);
1830 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001831
Bram Moolenaar97870002017-07-30 18:28:38 +02001832 if (rettv_list_alloc(rettv) == FAIL)
1833 return;
1834 if (buf == NULL)
1835 return;
1836
1837 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001838 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1839 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001840 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001841}
1842
1843/*
1844 * "term_getjob(buf)" function
1845 */
1846 void
1847f_term_getjob(typval_T *argvars, typval_T *rettv)
1848{
1849 buf_T *buf = term_get_buf(argvars);
1850
1851 rettv->v_type = VAR_JOB;
1852 rettv->vval.v_job = NULL;
1853 if (buf == NULL)
1854 return;
1855
1856 rettv->vval.v_job = buf->b_term->tl_job;
1857 if (rettv->vval.v_job != NULL)
1858 ++rettv->vval.v_job->jv_refcount;
1859}
1860
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001861 static int
1862get_row_number(typval_T *tv, term_T *term)
1863{
1864 if (tv->v_type == VAR_STRING
1865 && tv->vval.v_string != NULL
1866 && STRCMP(tv->vval.v_string, ".") == 0)
1867 return term->tl_cursor_pos.row;
1868 return (int)get_tv_number(tv) - 1;
1869}
1870
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001871/*
1872 * "term_getline(buf, row)" function
1873 */
1874 void
1875f_term_getline(typval_T *argvars, typval_T *rettv)
1876{
1877 buf_T *buf = term_get_buf(argvars);
1878 term_T *term;
1879 int row;
1880
1881 rettv->v_type = VAR_STRING;
1882 if (buf == NULL)
1883 return;
1884 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001885 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001886
1887 if (term->tl_vterm == NULL)
1888 {
1889 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1890
1891 /* vterm is finished, get the text from the buffer */
1892 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1893 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1894 }
1895 else
1896 {
1897 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1898 VTermRect rect;
1899 int len;
1900 char_u *p;
1901
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001902 if (row < 0 || row >= term->tl_rows)
1903 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001904 len = term->tl_cols * MB_MAXBYTES + 1;
1905 p = alloc(len);
1906 if (p == NULL)
1907 return;
1908 rettv->vval.v_string = p;
1909
1910 rect.start_col = 0;
1911 rect.end_col = term->tl_cols;
1912 rect.start_row = row;
1913 rect.end_row = row + 1;
1914 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1915 }
1916}
1917
1918/*
1919 * "term_getsize(buf)" function
1920 */
1921 void
1922f_term_getsize(typval_T *argvars, typval_T *rettv)
1923{
1924 buf_T *buf = term_get_buf(argvars);
1925 list_T *l;
1926
1927 if (rettv_list_alloc(rettv) == FAIL)
1928 return;
1929 if (buf == NULL)
1930 return;
1931
1932 l = rettv->vval.v_list;
1933 list_append_number(l, buf->b_term->tl_rows);
1934 list_append_number(l, buf->b_term->tl_cols);
1935}
1936
1937/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001938 * "term_getstatus(buf)" function
1939 */
1940 void
1941f_term_getstatus(typval_T *argvars, typval_T *rettv)
1942{
1943 buf_T *buf = term_get_buf(argvars);
1944 term_T *term;
1945 char_u val[100];
1946
1947 rettv->v_type = VAR_STRING;
1948 if (buf == NULL)
1949 return;
1950 term = buf->b_term;
1951
1952 if (term_job_running(term))
1953 STRCPY(val, "running");
1954 else
1955 STRCPY(val, "finished");
1956 if (term->tl_terminal_mode)
1957 STRCAT(val, ",terminal");
1958 rettv->vval.v_string = vim_strsave(val);
1959}
1960
1961/*
1962 * "term_gettitle(buf)" function
1963 */
1964 void
1965f_term_gettitle(typval_T *argvars, typval_T *rettv)
1966{
1967 buf_T *buf = term_get_buf(argvars);
1968
1969 rettv->v_type = VAR_STRING;
1970 if (buf == NULL)
1971 return;
1972
1973 if (buf->b_term->tl_title != NULL)
1974 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1975}
1976
1977/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001978 * "term_gettty(buf)" function
1979 */
1980 void
1981f_term_gettty(typval_T *argvars, typval_T *rettv)
1982{
1983 buf_T *buf = term_get_buf(argvars);
1984 char_u *p;
1985
1986 rettv->v_type = VAR_STRING;
1987 if (buf == NULL)
1988 return;
1989 if (buf->b_term->tl_job != NULL)
1990 p = buf->b_term->tl_job->jv_tty_name;
1991 else
1992 p = buf->b_term->tl_tty_name;
1993 if (p != NULL)
1994 rettv->vval.v_string = vim_strsave(p);
1995}
1996
1997/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001998 * "term_list()" function
1999 */
2000 void
2001f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2002{
2003 term_T *tp;
2004 list_T *l;
2005
2006 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2007 return;
2008
2009 l = rettv->vval.v_list;
2010 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2011 if (tp != NULL && tp->tl_buffer != NULL)
2012 if (list_append_number(l,
2013 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2014 return;
2015}
2016
2017/*
2018 * "term_scrape(buf, row)" function
2019 */
2020 void
2021f_term_scrape(typval_T *argvars, typval_T *rettv)
2022{
2023 buf_T *buf = term_get_buf(argvars);
2024 VTermScreen *screen = NULL;
2025 VTermPos pos;
2026 list_T *l;
2027 term_T *term;
2028
2029 if (rettv_list_alloc(rettv) == FAIL)
2030 return;
2031 if (buf == NULL)
2032 return;
2033 term = buf->b_term;
2034 if (term->tl_vterm != NULL)
2035 screen = vterm_obtain_screen(term->tl_vterm);
2036
2037 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002038 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002039 for (pos.col = 0; pos.col < term->tl_cols; )
2040 {
2041 dict_T *dcell;
2042 VTermScreenCell cell;
2043 char_u rgb[8];
2044 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2045 int off = 0;
2046 int i;
2047
2048 if (screen == NULL)
2049 {
2050 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2051 sb_line_T *line;
2052
2053 /* vterm has finished, get the cell from scrollback */
2054 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2055 break;
2056 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2057 if (pos.col >= line->sb_cols)
2058 break;
2059 cell = line->sb_cells[pos.col];
2060 }
2061 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2062 break;
2063 dcell = dict_alloc();
2064 list_append_dict(l, dcell);
2065
2066 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2067 {
2068 if (cell.chars[i] == 0)
2069 break;
2070 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2071 }
2072 mbs[off] = NUL;
2073 dict_add_nr_str(dcell, "chars", 0, mbs);
2074
2075 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2076 cell.fg.red, cell.fg.green, cell.fg.blue);
2077 dict_add_nr_str(dcell, "fg", 0, rgb);
2078 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2079 cell.bg.red, cell.bg.green, cell.bg.blue);
2080 dict_add_nr_str(dcell, "bg", 0, rgb);
2081
2082 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2083 dict_add_nr_str(dcell, "width", cell.width, NULL);
2084
2085 ++pos.col;
2086 if (cell.width == 2)
2087 ++pos.col;
2088 }
2089}
2090
2091/*
2092 * "term_sendkeys(buf, keys)" function
2093 */
2094 void
2095f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2096{
2097 buf_T *buf = term_get_buf(argvars);
2098 char_u *msg;
2099 term_T *term;
2100
2101 rettv->v_type = VAR_UNKNOWN;
2102 if (buf == NULL)
2103 return;
2104
2105 msg = get_tv_string_chk(&argvars[1]);
2106 if (msg == NULL)
2107 return;
2108 term = buf->b_term;
2109 if (term->tl_vterm == NULL)
2110 return;
2111
2112 while (*msg != NUL)
2113 {
2114 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2115 msg += MB_PTR2LEN(msg);
2116 }
2117
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002118 if (!term->tl_terminal_mode)
2119 {
2120 /* TODO: only update once in a while. */
2121 update_screen(0);
2122 if (buf == curbuf)
2123 update_cursor(term, TRUE);
2124 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002125}
2126
2127/*
2128 * "term_start(command, options)" function
2129 */
2130 void
2131f_term_start(typval_T *argvars, typval_T *rettv)
2132{
2133 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002134 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002135
2136 if (cmd == NULL)
2137 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002138 init_job_options(&opt);
2139 /* TODO: allow more job options */
2140 if (argvars[1].v_type != VAR_UNKNOWN
2141 && get_job_options(&argvars[1], &opt,
2142 JO_TIMEOUT_ALL + JO_STOPONEXIT
2143 + JO_EXIT_CB + JO_CLOSE_CALLBACK) == FAIL)
2144 return;
2145
2146 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002147
2148 if (curbuf->b_term != NULL)
2149 rettv->vval.v_number = curbuf->b_fnum;
2150}
2151
2152/*
2153 * "term_wait" function
2154 */
2155 void
2156f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2157{
2158 buf_T *buf = term_get_buf(argvars);
2159
2160 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002161 {
2162 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002163 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002164 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002165 if (buf->b_term->tl_job == NULL)
2166 {
2167 ch_log(NULL, "term_wait(): no job to wait for");
2168 return;
2169 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002170
2171 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002172 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002173 {
2174 /* The job is dead, keep reading channel I/O until the channel is
2175 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002176 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002177 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2178 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002179 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002180 parse_queued_messages();
2181 ui_delay(10L, FALSE);
2182 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002183 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002184 parse_queued_messages();
2185 }
2186 else
2187 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002188 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002189 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002190
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002191 /* Wait for 10 msec for any channel I/O. */
2192 /* TODO: use delay from optional argument */
2193 ui_delay(10L, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002194 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002195
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002196 /* Flushing messages on channels is hopefully sufficient.
2197 * TODO: is there a better way? */
2198 parse_queued_messages();
2199 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002200}
2201
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002202# ifdef WIN3264
2203
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002204/**************************************
2205 * 2. MS-Windows implementation.
2206 */
2207
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002208#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2209#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2210
Bram Moolenaar8a773062017-07-24 22:29:21 +02002211void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002212void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002213void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002214BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2215void (*winpty_config_set_initial_size)(void*, int, int);
2216LPCWSTR (*winpty_conin_name)(void*);
2217LPCWSTR (*winpty_conout_name)(void*);
2218LPCWSTR (*winpty_conerr_name)(void*);
2219void (*winpty_free)(void*);
2220void (*winpty_config_free)(void*);
2221void (*winpty_spawn_config_free)(void*);
2222void (*winpty_error_free)(void*);
2223LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002224BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002225HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002226
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002227#define WINPTY_DLL "winpty.dll"
2228
2229static HINSTANCE hWinPtyDLL = NULL;
2230
2231 int
2232dyn_winpty_init(void)
2233{
2234 int i;
2235 static struct
2236 {
2237 char *name;
2238 FARPROC *ptr;
2239 } winpty_entry[] =
2240 {
2241 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2242 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2243 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2244 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2245 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2246 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2247 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2248 {"winpty_free", (FARPROC*)&winpty_free},
2249 {"winpty_open", (FARPROC*)&winpty_open},
2250 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2251 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2252 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2253 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002254 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002255 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002256 {NULL, NULL}
2257 };
2258
2259 /* No need to initialize twice. */
2260 if (hWinPtyDLL)
2261 return 1;
2262 /* Load winpty.dll */
2263 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2264 if (!hWinPtyDLL)
2265 {
2266 EMSG2(_(e_loadlib), WINPTY_DLL);
2267 return 0;
2268 }
2269 for (i = 0; winpty_entry[i].name != NULL
2270 && winpty_entry[i].ptr != NULL; ++i)
2271 {
2272 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2273 winpty_entry[i].name)) == NULL)
2274 {
2275 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2276 return 0;
2277 }
2278 }
2279
2280 return 1;
2281}
2282
2283/*
2284 * Create a new terminal of "rows" by "cols" cells.
2285 * Store a reference in "term".
2286 * Return OK or FAIL.
2287 */
2288 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002289term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002290{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002291 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002292 channel_T *channel = NULL;
2293 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002294 DWORD error;
2295 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2296 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002297 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002298 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002299
2300 if (!dyn_winpty_init())
2301 return FAIL;
2302
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002303 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002304 if (p == NULL)
2305 return FAIL;
2306
2307 job = job_alloc();
2308 if (job == NULL)
2309 goto failed;
2310
2311 channel = add_channel();
2312 if (channel == NULL)
2313 goto failed;
2314
2315 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2316 if (term->tl_winpty_config == NULL)
2317 goto failed;
2318
2319 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2320 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2321 if (term->tl_winpty == NULL)
2322 goto failed;
2323
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002324 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002325 spawn_config = winpty_spawn_config_new(
2326 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2327 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2328 NULL,
2329 p,
2330 NULL,
2331 NULL,
2332 &winpty_err);
2333 if (spawn_config == NULL)
2334 goto failed;
2335
2336 channel = add_channel();
2337 if (channel == NULL)
2338 goto failed;
2339
2340 job = job_alloc();
2341 if (job == NULL)
2342 goto failed;
2343
2344 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2345 &child_thread_handle, &error, &winpty_err))
2346 goto failed;
2347
2348 channel_set_pipes(channel,
2349 (sock_T) CreateFileW(
2350 winpty_conin_name(term->tl_winpty),
2351 GENERIC_WRITE, 0, NULL,
2352 OPEN_EXISTING, 0, NULL),
2353 (sock_T) CreateFileW(
2354 winpty_conout_name(term->tl_winpty),
2355 GENERIC_READ, 0, NULL,
2356 OPEN_EXISTING, 0, NULL),
2357 (sock_T) CreateFileW(
2358 winpty_conerr_name(term->tl_winpty),
2359 GENERIC_READ, 0, NULL,
2360 OPEN_EXISTING, 0, NULL));
2361
2362 jo = CreateJobObject(NULL, NULL);
2363 if (jo == NULL)
2364 goto failed;
2365
2366 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002367 {
2368 /* Failed, switch the way to terminate process with TerminateProcess. */
2369 CloseHandle(jo);
2370 jo = NULL;
2371 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002372
2373 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002374 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002375
2376 create_vterm(term, rows, cols);
2377
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002378 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002379 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002380
2381 job->jv_channel = channel;
2382 job->jv_proc_info.hProcess = child_process_handle;
2383 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2384 job->jv_job_object = jo;
2385 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002386 sprintf(buf, "winpty://%lu",
2387 GetProcessId(winpty_agent_process(term->tl_winpty)));
2388 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002389 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002390 term->tl_job = job;
2391
2392 return OK;
2393
2394failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002395 if (spawn_config != NULL)
2396 winpty_spawn_config_free(spawn_config);
2397 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002398 if (channel != NULL)
2399 channel_clear(channel);
2400 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002401 {
2402 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002403 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002404 }
2405 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002406 if (jo != NULL)
2407 CloseHandle(jo);
2408 if (term->tl_winpty != NULL)
2409 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002410 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002411 if (term->tl_winpty_config != NULL)
2412 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002413 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002414 if (winpty_err != NULL)
2415 {
2416 char_u *msg = utf16_to_enc(
2417 (short_u *)winpty_error_msg(winpty_err), NULL);
2418
2419 EMSG(msg);
2420 winpty_error_free(winpty_err);
2421 }
2422 return FAIL;
2423}
2424
2425/*
2426 * Free the terminal emulator part of "term".
2427 */
2428 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002429term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002430{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002431 if (term->tl_winpty != NULL)
2432 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002433 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002434 if (term->tl_winpty_config != NULL)
2435 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002436 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002437 if (term->tl_vterm != NULL)
2438 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002439 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002440}
2441
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002442/*
2443 * Request size to terminal.
2444 */
2445 static void
2446term_report_winsize(term_T *term, int rows, int cols)
2447{
2448 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2449}
2450
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002451# else
2452
2453/**************************************
2454 * 3. Unix-like implementation.
2455 */
2456
2457/*
2458 * Create a new terminal of "rows" by "cols" cells.
2459 * Start job for "cmd".
2460 * Store the pointers in "term".
2461 * Return OK or FAIL.
2462 */
2463 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002464term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002465{
2466 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002467
2468 create_vterm(term, rows, cols);
2469
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002470 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002471 argvars[0].v_type = VAR_STRING;
2472 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002473
2474 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002475 if (term->tl_job != NULL)
2476 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002477
Bram Moolenaar61a66052017-07-22 18:39:00 +02002478 return term->tl_job != NULL
2479 && term->tl_job->jv_channel != NULL
2480 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002481}
2482
2483/*
2484 * Free the terminal emulator part of "term".
2485 */
2486 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002487term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002488{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002489 if (term->tl_vterm != NULL)
2490 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002491 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002492}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002493
2494/*
2495 * Request size to terminal.
2496 */
2497 static void
2498term_report_winsize(term_T *term, int rows, int cols)
2499{
2500 /* Use an ioctl() to report the new window size to the job. */
2501 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2502 {
2503 int fd = -1;
2504 int part;
2505
2506 for (part = PART_OUT; part < PART_COUNT; ++part)
2507 {
2508 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2509 if (isatty(fd))
2510 break;
2511 }
2512 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2513 mch_stop_job(term->tl_job, (char_u *)"winch");
2514 }
2515}
2516
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002517# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002518
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002519#endif /* FEAT_TERMINAL */