blob: c559d11731054a644c1f4c35518b00971d08062b [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.
Bram Moolenaar679653e2017-08-13 14:13:19 +020037 * When the buffer is changed it is turned into a normal buffer, the attributes
38 * in tl_scrollback are no longer used.
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020039 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020040 * TODO:
Bram Moolenaar285f2432017-08-23 23:10:21 +020041 * - better check for blinking - reply from Thomas Dickey Aug 22
Bram Moolenaar0cbba822017-08-21 21:39:28 +020042 * - test for writing lines to terminal job does not work on MS-Windows
Bram Moolenaar94053a52017-08-01 21:44:33 +020043 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020044 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020045 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020046 * - support minimal size when 'termsize' is empty?
Bram Moolenaar58302322017-08-22 20:33:53 +020047 * - do not set bufhidden to "hide"? works like a buffer with changes.
48 * document that CTRL-W :hide can be used.
Bram Moolenaar285f2432017-08-23 23:10:21 +020049 * - GUI: when using tabs, focus in terminal, click on tab does not work.
50 * - GUI: when 'confirm' is set and trying to exit Vim, dialog offers to save
51 * changes to "!shell".
52 * (justrajdeep, 2017 Aug 22)
Bram Moolenaar58302322017-08-22 20:33:53 +020053 * - command argument with spaces doesn't work #1999
54 * :terminal ls dir\ with\ spaces
Bram Moolenaar4f44b882017-08-13 20:06:18 +020055 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020056 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
57 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
58 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
59 * Check that something is connected to the terminal.
60 * Test: "cat" reading from a file or buffer
Bram Moolenaar740c4332017-08-21 22:01:27 +020061 * "ls" writing stdout to a file or buffer
62 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020063 * - For the GUI fill termios with default values, perhaps like pangoterm:
64 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020065 * - support ":term NONE" to open a terminal with a pty but not running a job
66 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020067 * - if the job in the terminal does not support the mouse, we can use the
68 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020069 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
70 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020071 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020072 * - Copy text in the vterm to the Vim buffer once in a while, so that
73 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020074 */
75
76#include "vim.h"
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079
Bram Moolenaard5310982017-08-05 15:16:32 +020080#ifndef MIN
81# define MIN(x,y) ((x) < (y) ? (x) : (y))
82#endif
83#ifndef MAX
84# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020085#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020086
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020087#include "libvterm/include/vterm.h"
88
Bram Moolenaar33a43be2017-08-06 21:36:22 +020089/* This is VTermScreenCell without the characters, thus much smaller. */
90typedef struct {
91 VTermScreenCellAttrs attrs;
92 char width;
93 VTermColor fg, bg;
94} cellattr_T;
95
Bram Moolenaard85f2712017-07-28 21:51:57 +020096typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020097 int sb_cols; /* can differ per line */
98 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020099} sb_line_T;
100
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200101/* typedef term_T in structs.h */
102struct terminal_S {
103 term_T *tl_next;
104
Bram Moolenaar423802d2017-07-30 16:52:24 +0200105 VTerm *tl_vterm;
106 job_T *tl_job;
107 buf_T *tl_buffer;
108
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200109 /* used when tl_job is NULL and only a pty was created */
110 int tl_tty_fd;
111 char_u *tl_tty_name;
112
Bram Moolenaar6d819742017-08-06 14:57:49 +0200113 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200114 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200115 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200116 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200117
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200118#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200119 void *tl_winpty_config;
120 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200121#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200122
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200123 /* last known vterm size */
124 int tl_rows;
125 int tl_cols;
126 /* vterm size does not follow window size */
127 int tl_rows_fixed;
128 int tl_cols_fixed;
129
Bram Moolenaar21554412017-07-24 21:44:43 +0200130 char_u *tl_title; /* NULL or allocated */
131 char_u *tl_status_text; /* NULL or allocated */
132
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200133 /* Range of screen rows to update. Zero based. */
134 int tl_dirty_row_start; /* -1 if nothing dirty */
135 int tl_dirty_row_end; /* row below last one to update */
136
Bram Moolenaard85f2712017-07-28 21:51:57 +0200137 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200138 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200139
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200140 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200141 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200142 int tl_cursor_blink;
143 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
144 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200145
146 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200147};
148
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200149#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
150#define TMODE_LOOP 2 /* CTRL-W N used */
151
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200152/*
153 * List of all active terminals.
154 */
155static term_T *first_term = NULL;
156
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200157/* Terminal active in terminal_loop(). */
158static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200159
160#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
161#define KEY_BUF_LEN 200
162
163/*
164 * Functions with separate implementation for MS-Windows and Unix-like systems.
165 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200166static int term_and_job_init(term_T *term, int rows, int cols,
167 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200168static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200169static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200170
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200171/* The characters that we know (or assume) that the terminal expects for the
172 * backspace and enter keys. */
173static int term_backspace_char = BS;
174static int term_enter_char = CAR;
175static int term_nl_does_cr = FALSE;
176
177
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200178/**************************************
179 * 1. Generic code for all systems.
180 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200181
182/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200183 * Determine the terminal size from 'termsize' and the current window.
184 * Assumes term->tl_rows and term->tl_cols are zero.
185 */
186 static void
187set_term_and_win_size(term_T *term)
188{
189 if (*curwin->w_p_tms != NUL)
190 {
191 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
192
193 term->tl_rows = atoi((char *)curwin->w_p_tms);
194 term->tl_cols = atoi((char *)p);
195 }
196 if (term->tl_rows == 0)
197 term->tl_rows = curwin->w_height;
198 else
199 {
200 win_setheight_win(term->tl_rows, curwin);
201 term->tl_rows_fixed = TRUE;
202 }
203 if (term->tl_cols == 0)
204 term->tl_cols = curwin->w_width;
205 else
206 {
207 win_setwidth_win(term->tl_cols, curwin);
208 term->tl_cols_fixed = TRUE;
209 }
210}
211
212/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200213 * Initialize job options for a terminal job.
214 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200215 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200216 static void
217init_job_options(jobopt_T *opt)
218{
219 clear_job_options(opt);
220
221 opt->jo_mode = MODE_RAW;
222 opt->jo_out_mode = MODE_RAW;
223 opt->jo_err_mode = MODE_RAW;
224 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
225
226 opt->jo_io[PART_OUT] = JIO_BUFFER;
227 opt->jo_io[PART_ERR] = JIO_BUFFER;
228 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
229
230 opt->jo_modifiable[PART_OUT] = 0;
231 opt->jo_modifiable[PART_ERR] = 0;
232 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
233
234 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
235}
236
237/*
238 * Set job options mandatory for a terminal job.
239 */
240 static void
241setup_job_options(jobopt_T *opt, int rows, int cols)
242{
243 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
244 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
245 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200246 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
247 opt->jo_term_rows = rows;
248 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
249 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200250}
251
252 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200253term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200255 exarg_T split_ea;
256 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200257 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200258 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200259
260 if (check_restricted() || check_secure())
261 return;
262
263 term = (term_T *)alloc_clear(sizeof(term_T));
264 if (term == NULL)
265 return;
266 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200267 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200268 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200269 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200270 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200271
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200272 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200273 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200274 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200275 /* Create a new buffer in the current window. */
276 if (!can_abandon(curbuf, forceit))
277 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200278 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200279 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200280 return;
281 }
282 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
283 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200284 {
285 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200286 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200287 }
288 }
289 else if (opt->jo_hidden)
290 {
291 buf_T *buf;
292
293 /* Create a new buffer without a window. Make it the current buffer for
294 * a moment to be able to do the initialisations. */
295 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
296 BLN_NEW | BLN_LISTED);
297 if (buf == NULL || ml_open(buf) == FAIL)
298 {
299 vim_free(term);
300 return;
301 }
302 old_curbuf = curbuf;
303 --curbuf->b_nwindows;
304 curbuf = buf;
305 curwin->w_buffer = buf;
306 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200307 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200308 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200309 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200310 /* Open a new window or tab. */
311 split_ea.cmdidx = CMD_new;
312 split_ea.cmd = (char_u *)"new";
313 split_ea.arg = (char_u *)"";
314 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
315 {
316 split_ea.line2 = opt->jo_term_rows;
317 split_ea.addr_count = 1;
318 }
319 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
320 {
321 split_ea.line2 = opt->jo_term_cols;
322 split_ea.addr_count = 1;
323 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200324
Bram Moolenaarda43b612017-08-11 22:27:50 +0200325 ex_splitview(&split_ea);
326 if (curwin == old_curwin)
327 {
328 /* split failed */
329 vim_free(term);
330 return;
331 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200332 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200333 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200334 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200335
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200336 if (!opt->jo_hidden)
337 {
338 /* only one size was taken care of with :new, do the other one */
339 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
340 win_setheight(opt->jo_term_rows);
341 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
342 win_setwidth(opt->jo_term_cols);
343 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200344
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200345 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200346 term->tl_next = first_term;
347 first_term = term;
348
Bram Moolenaar78712a72017-08-05 14:50:12 +0200349 if (opt->jo_term_name != NULL)
350 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
351 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200352 {
353 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200354 size_t len;
355 char_u *cmd, *p;
356
357 if (argvar->v_type == VAR_STRING)
358 cmd = argvar->vval.v_string;
359 else if (argvar->v_type != VAR_LIST
360 || argvar->vval.v_list == NULL
361 || argvar->vval.v_list->lv_len < 1)
362 cmd = (char_u*)"";
363 else
364 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
365
366 len = STRLEN(cmd) + 10;
367 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200368
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200369 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200370 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200371 /* Prepend a ! to the command name to avoid the buffer name equals
372 * the executable, otherwise ":w!" would overwrite it. */
373 if (i == 0)
374 vim_snprintf((char *)p, len, "!%s", cmd);
375 else
376 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200377 if (buflist_findname(p) == NULL)
378 {
379 curbuf->b_ffname = p;
380 break;
381 }
382 }
383 }
384 curbuf->b_fname = curbuf->b_ffname;
385
Bram Moolenaar37c45832017-08-12 16:01:04 +0200386 if (opt->jo_term_opencmd != NULL)
387 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
388
Bram Moolenaareb44a682017-08-03 22:44:55 +0200389 set_string_option_direct((char_u *)"buftype", -1,
390 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
391
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200392 /* Mark the buffer as not modifiable. It can only be made modifiable after
393 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200394 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200395
396 /* Set 'bufhidden' to "hide": allow closing the window. */
397 set_string_option_direct((char_u *)"bufhidden", -1,
398 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200399
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200400 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200401 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200402
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200403 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200404 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt)
405 == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200406 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200407 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200408 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200409 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200410
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200411 /* Make sure we don't get stuck on sending keys to the job, it leads to
412 * a deadlock if the job is waiting for Vim to read. */
413 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
414
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200415 if (old_curbuf != NULL)
416 {
417 --curbuf->b_nwindows;
418 curbuf = old_curbuf;
419 curwin->w_buffer = curbuf;
420 ++curbuf->b_nwindows;
421 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200422 }
423 else
424 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200425 buf_T *buf = curbuf;
426
Bram Moolenaard85f2712017-07-28 21:51:57 +0200427 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200428 if (old_curbuf != NULL)
429 {
430 --curbuf->b_nwindows;
431 curbuf = old_curbuf;
432 curwin->w_buffer = curbuf;
433 ++curbuf->b_nwindows;
434 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200435
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200436 /* Wiping out the buffer will also close the window and call
437 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200438 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200439 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200440}
441
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200442/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200443 * ":terminal": open a terminal window and execute a job in it.
444 */
445 void
446ex_terminal(exarg_T *eap)
447{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200448 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200449 jobopt_T opt;
450 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200451 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200452
453 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200454
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200455 cmd = eap->arg;
456 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
457 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200458 char_u *p, *ep;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200459
460 cmd += 2;
461 p = skiptowhite(cmd);
Bram Moolenaarb2412082017-08-20 18:09:14 +0200462 ep = vim_strchr(cmd, '=');
463 if (ep != NULL && ep < p)
464 p = ep;
465
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200466 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
467 opt.jo_term_finish = 'c';
468 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
469 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200470 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
471 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200472 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
473 opt.jo_hidden = 1;
Bram Moolenaarb2412082017-08-20 18:09:14 +0200474 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
475 && ep != NULL && isdigit(ep[1]))
476 {
477 opt.jo_set2 |= JO2_TERM_ROWS;
478 opt.jo_term_rows = atoi((char *)ep + 1);
479 p = skiptowhite(cmd);
480 }
481 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
482 && ep != NULL && isdigit(ep[1]))
483 {
484 opt.jo_set2 |= JO2_TERM_COLS;
485 opt.jo_term_cols = atoi((char *)ep + 1);
486 p = skiptowhite(cmd);
487 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200488 else
489 {
490 if (*p)
491 *p = NUL;
492 EMSG2(_("E181: Invalid attribute: %s"), cmd);
493 return;
494 }
495 cmd = skipwhite(p);
496 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200497 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200498 /* Make a copy, an autocommand may set 'shell'. */
499 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200500
Bram Moolenaarb2412082017-08-20 18:09:14 +0200501 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200502 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200503 /* Write lines from current buffer to the job. */
504 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
505 opt.jo_io[PART_IN] = JIO_BUFFER;
506 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
507 opt.jo_in_top = eap->line1;
508 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200509 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200510
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200511 argvar.v_type = VAR_STRING;
512 argvar.vval.v_string = cmd;
513 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200514 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200515}
516
517/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200518 * Free the scrollback buffer for "term".
519 */
520 static void
521free_scrollback(term_T *term)
522{
523 int i;
524
525 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
526 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
527 ga_clear(&term->tl_scrollback);
528}
529
530/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200531 * Free a terminal and everything it refers to.
532 * Kills the job if there is one.
533 * Called when wiping out a buffer.
534 */
535 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200536free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200537{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200538 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200539 term_T *tp;
540
541 if (term == NULL)
542 return;
543 if (first_term == term)
544 first_term = term->tl_next;
545 else
546 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
547 if (tp->tl_next == term)
548 {
549 tp->tl_next = term->tl_next;
550 break;
551 }
552
553 if (term->tl_job != NULL)
554 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200555 if (term->tl_job->jv_status != JOB_ENDED
556 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200557 job_stop(term->tl_job, NULL, "kill");
558 job_unref(term->tl_job);
559 }
560
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200561 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200562
563 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200564 vim_free(term->tl_title);
565 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200566 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200567 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200568 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200569 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200570 if (in_terminal_loop == term)
571 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200572}
573
574/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200575 * Write job output "msg[len]" to the vterm.
576 */
577 static void
578term_write_job_output(term_T *term, char_u *msg, size_t len)
579{
580 VTerm *vterm = term->tl_vterm;
581 char_u *p;
582 size_t done;
583 size_t len_now;
584
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200585 if (term_nl_does_cr)
586 vterm_input_write(vterm, (char *)msg, len);
587 else
588 /* need to convert NL to CR-NL */
589 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200590 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200591 for (p = msg + done; p < msg + len; )
592 {
593 if (*p == NL)
594 break;
595 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
596 }
597 len_now = p - msg - done;
598 vterm_input_write(vterm, (char *)msg + done, len_now);
599 if (p < msg + len && *p == NL)
600 {
601 vterm_input_write(vterm, "\r\n", 2);
602 ++len_now;
603 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200604 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200605
606 /* this invokes the damage callbacks */
607 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
608}
609
Bram Moolenaar1c844932017-07-24 23:36:41 +0200610 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200611update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200612{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200613 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200614 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200615 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200616 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200617 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200618 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200619 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200620 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200621#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200622 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200623 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200624#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200625 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200626}
627
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200628/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200629 * Invoked when "msg" output from a job was received. Write it to the terminal
630 * of "buffer".
631 */
632 void
633write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
634{
635 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200636 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200637
Bram Moolenaard85f2712017-07-28 21:51:57 +0200638 if (term->tl_vterm == NULL)
639 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200640 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200641 return;
642 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200643 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200644 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200645
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200646 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
647 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200648 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200649 {
650 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200651 ch_log(term->tl_job->jv_channel, "updating screen");
652 if (buffer == curbuf)
653 {
654 update_screen(0);
655 update_cursor(term, TRUE);
656 }
657 else
658 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200659 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200660}
661
662/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200663 * Send a mouse position and click to the vterm
664 */
665 static int
666term_send_mouse(VTerm *vterm, int button, int pressed)
667{
668 VTermModifier mod = VTERM_MOD_NONE;
669
670 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
671 mouse_col - W_WINCOL(curwin), mod);
672 vterm_mouse_button(vterm, button, pressed, mod);
673 return TRUE;
674}
675
676/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200677 * Convert typed key "c" into bytes to send to the job.
678 * Return the number of bytes in "buf".
679 */
680 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200681term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200682{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200683 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200684 VTermKey key = VTERM_KEY_NONE;
685 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200686 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200687
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200688 switch (c)
689 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200690 case CAR: c = term_enter_char; break;
691 /* don't use VTERM_KEY_BACKSPACE, it always
692 * becomes 0x7f DEL */
693 case K_BS: c = term_backspace_char; break;
694
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200695 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200696 case K_DEL: key = VTERM_KEY_DEL; break;
697 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200698 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
699 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200700 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200701 case K_S_END: mod = VTERM_MOD_SHIFT;
702 key = VTERM_KEY_END; break;
703 case K_C_END: mod = VTERM_MOD_CTRL;
704 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200705 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
706 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
707 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
708 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
709 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
710 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
711 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
712 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
713 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
714 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
715 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
716 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
717 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200718 case K_S_HOME: mod = VTERM_MOD_SHIFT;
719 key = VTERM_KEY_HOME; break;
720 case K_C_HOME: mod = VTERM_MOD_CTRL;
721 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200722 case K_INS: key = VTERM_KEY_INS; break;
723 case K_K0: key = VTERM_KEY_KP_0; break;
724 case K_K1: key = VTERM_KEY_KP_1; break;
725 case K_K2: key = VTERM_KEY_KP_2; break;
726 case K_K3: key = VTERM_KEY_KP_3; break;
727 case K_K4: key = VTERM_KEY_KP_4; break;
728 case K_K5: key = VTERM_KEY_KP_5; break;
729 case K_K6: key = VTERM_KEY_KP_6; break;
730 case K_K7: key = VTERM_KEY_KP_7; break;
731 case K_K8: key = VTERM_KEY_KP_8; break;
732 case K_K9: key = VTERM_KEY_KP_9; break;
733 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
734 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
735 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
736 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
737 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
738 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
739 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
740 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
741 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
742 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
743 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
744 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
745 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200746 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
747 key = VTERM_KEY_LEFT; break;
748 case K_C_LEFT: mod = VTERM_MOD_CTRL;
749 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200750 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
751 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
752 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200753 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
754 key = VTERM_KEY_RIGHT; break;
755 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
756 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200757 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200758 case K_S_UP: mod = VTERM_MOD_SHIFT;
759 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200760 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200761
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200762 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
763 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
764 case K_MOUSELEFT: /* TODO */ return 0;
765 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200766
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200767 case K_LEFTMOUSE:
768 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
769 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
770 case K_LEFTRELEASE:
771 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
772 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
773 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
774 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
775 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
776 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
777 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
778 case K_X1MOUSE: /* TODO */ return 0;
779 case K_X1DRAG: /* TODO */ return 0;
780 case K_X1RELEASE: /* TODO */ return 0;
781 case K_X2MOUSE: /* TODO */ return 0;
782 case K_X2DRAG: /* TODO */ return 0;
783 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200784
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200785 case K_IGNORE: return 0;
786 case K_NOP: return 0;
787 case K_UNDO: return 0;
788 case K_HELP: return 0;
789 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
790 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
791 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
792 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
793 case K_SELECT: return 0;
794#ifdef FEAT_GUI
795 case K_VER_SCROLLBAR: return 0;
796 case K_HOR_SCROLLBAR: return 0;
797#endif
798#ifdef FEAT_GUI_TABLINE
799 case K_TABLINE: return 0;
800 case K_TABMENU: return 0;
801#endif
802#ifdef FEAT_NETBEANS_INTG
803 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
804#endif
805#ifdef FEAT_DND
806 case K_DROP: return 0;
807#endif
808#ifdef FEAT_AUTOCMD
809 case K_CURSORHOLD: return 0;
810#endif
811 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
812 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200813 }
814
815 /*
816 * Convert special keys to vterm keys:
817 * - Write keys to vterm: vterm_keyboard_key()
818 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200819 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200820 */
821 if (key != VTERM_KEY_NONE)
822 /* Special key, let vterm convert it. */
823 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200824 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200825 /* Normal character, let vterm convert it. */
826 vterm_keyboard_unichar(vterm, c, mod);
827
828 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200829 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200830}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200831
Bram Moolenaar938783d2017-07-16 20:13:26 +0200832/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200833 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200834 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200835 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200836term_job_running(term_T *term)
837{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200838 /* Also consider the job finished when the channel is closed, to avoid a
839 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200840 return term != NULL
841 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200842 && term->tl_job->jv_status == JOB_STARTED
843 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200844}
845
846/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200847 * Add the last line of the scrollback buffer to the buffer in the window.
848 */
849 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200850add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200851{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200852 buf_T *buf = term->tl_buffer;
853 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
854 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200855
Bram Moolenaar58302322017-08-22 20:33:53 +0200856#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +0200857 if (!enc_utf8 && enc_codepage > 0)
858 {
859 WCHAR *ret = NULL;
860 int length = 0;
861
862 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
863 &ret, &length);
864 if (ret != NULL)
865 {
866 WideCharToMultiByte_alloc(enc_codepage, 0,
867 ret, length, (char **)&text, &len, 0, 0);
868 vim_free(ret);
869 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
870 vim_free(text);
871 }
872 }
873 else
874#endif
875 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200876 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200877 {
878 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200879 curbuf = buf;
880 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200881 curbuf = curwin->w_buffer;
882 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200883}
884
885/*
886 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200887 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200888 */
889 static void
890move_terminal_to_buffer(term_T *term)
891{
892 win_T *wp;
893 int len;
894 int lines_skipped = 0;
895 VTermPos pos;
896 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200897 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200898 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200899
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200900 if (term->tl_vterm == NULL)
901 return;
902 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200903 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
904 {
905 len = 0;
906 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
907 if (vterm_screen_get_cell(screen, pos, &cell) != 0
908 && cell.chars[0] != NUL)
909 len = pos.col + 1;
910
911 if (len == 0)
912 ++lines_skipped;
913 else
914 {
915 while (lines_skipped > 0)
916 {
917 /* Line was skipped, add an empty line. */
918 --lines_skipped;
919 if (ga_grow(&term->tl_scrollback, 1) == OK)
920 {
921 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
922 + term->tl_scrollback.ga_len;
923
924 line->sb_cols = 0;
925 line->sb_cells = NULL;
926 ++term->tl_scrollback.ga_len;
927
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200928 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200929 }
930 }
931
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200932 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200933 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
934 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200935 garray_T ga;
936 int width;
937 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200938 + term->tl_scrollback.ga_len;
939
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200940 ga_init2(&ga, 1, 100);
941 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200942 {
943 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200944 {
945 width = 1;
946 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
947 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +0200948 ga.ga_len += utf_char2bytes(' ',
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200949 (char_u *)ga.ga_data + ga.ga_len);
950 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200951 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200952 {
953 width = cell.width;
954
955 p[pos.col].width = cell.width;
956 p[pos.col].attrs = cell.attrs;
957 p[pos.col].fg = cell.fg;
958 p[pos.col].bg = cell.bg;
959
960 if (ga_grow(&ga, MB_MAXBYTES) == OK)
961 {
962 int i;
963 int c;
964
965 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +0200966 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200967 (char_u *)ga.ga_data + ga.ga_len);
968 }
969 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200970 }
971 line->sb_cols = len;
972 line->sb_cells = p;
973 ++term->tl_scrollback.ga_len;
974
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200975 if (ga_grow(&ga, 1) == FAIL)
976 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
977 else
978 {
979 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
980 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
981 }
982 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200983 }
984 else
985 vim_free(p);
986 }
987 }
988
989 FOR_ALL_WINDOWS(wp)
990 {
991 if (wp->w_buffer == term->tl_buffer)
992 {
993 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
994 wp->w_cursor.col = 0;
995 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200996 if (wp->w_cursor.lnum >= wp->w_height)
997 {
998 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
999
1000 if (wp->w_topline < min_topline)
1001 wp->w_topline = min_topline;
1002 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02001003 redraw_win_later(wp, NOT_VALID);
1004 }
1005 }
1006}
1007
1008 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001009set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001010{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001011 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001012 vim_free(term->tl_status_text);
1013 term->tl_status_text = NULL;
1014 if (term->tl_buffer == curbuf)
1015 maketitle();
1016}
1017
1018/*
1019 * Called after the job if finished and Terminal mode is not active:
1020 * Move the vterm contents into the scrollback buffer and free the vterm.
1021 */
1022 static void
1023cleanup_vterm(term_T *term)
1024{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001025 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001026 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001027 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001028 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001029}
1030
1031/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001032 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001033 * Suspends updating the terminal window.
1034 */
1035 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001036term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001037{
1038 term_T *term = curbuf->b_term;
1039
1040 /* Append the current terminal contents to the buffer. */
1041 move_terminal_to_buffer(term);
1042
Bram Moolenaar6d819742017-08-06 14:57:49 +02001043 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001044
Bram Moolenaar6d819742017-08-06 14:57:49 +02001045 /* Move the window cursor to the position of the cursor in the
1046 * terminal. */
1047 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1048 + term->tl_cursor_pos.row + 1;
1049 check_cursor();
1050 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001051
Bram Moolenaar6d819742017-08-06 14:57:49 +02001052 /* Display the same lines as in the terminal. */
1053 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001054}
1055
1056/*
1057 * Returns TRUE if the current window contains a terminal and we are in
1058 * Terminal-Normal mode.
1059 */
1060 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001061term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001062{
1063 term_T *term = curbuf->b_term;
1064
Bram Moolenaar6d819742017-08-06 14:57:49 +02001065 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001066}
1067
1068/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001069 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001070 * Restores updating the terminal window.
1071 */
1072 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001073term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001074{
1075 term_T *term = curbuf->b_term;
1076 sb_line_T *line;
1077 garray_T *gap;
1078
1079 /* Remove the terminal contents from the scrollback and the buffer. */
1080 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001081 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1082 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001083 {
1084 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1085 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1086 vim_free(line->sb_cells);
1087 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001088 }
1089 check_cursor();
1090
Bram Moolenaar6d819742017-08-06 14:57:49 +02001091 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001092
1093 if (term->tl_channel_closed)
1094 cleanup_vterm(term);
1095 redraw_buf_and_status_later(curbuf, NOT_VALID);
1096}
1097
1098/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001099 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001100 * Note: while waiting a terminal may be closed and freed if the channel is
1101 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001102 * TODO: use terminal mode mappings.
1103 */
1104 static int
1105term_vgetc()
1106{
1107 int c;
1108
1109 ++no_mapping;
1110 ++allow_keys;
1111 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001112#ifdef WIN3264
1113 ctrl_break_was_pressed = FALSE;
1114#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001115 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001116 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001117 --no_mapping;
1118 --allow_keys;
1119 return c;
1120}
1121
1122/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001123 * Get the part that is connected to the tty. Normally this is PART_IN, but
1124 * when writing buffer lines to the job it can be another. This makes it
1125 * possible to do "1,5term vim -".
1126 */
1127 static ch_part_T
1128get_tty_part(term_T *term)
1129{
1130#ifdef UNIX
1131 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1132 int i;
1133
1134 for (i = 0; i < 3; ++i)
1135 {
1136 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1137
1138 if (isatty(fd))
1139 return parts[i];
1140 }
1141#endif
1142 return PART_IN;
1143}
1144
1145/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001146 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001147 * Return FAIL when the key needs to be handled in Normal mode.
1148 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001149 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001150 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001151send_keys_to_term(term_T *term, int c, int typed)
1152{
1153 char msg[KEY_BUF_LEN];
1154 size_t len;
1155 static int mouse_was_outside = FALSE;
1156 int dragging_outside = FALSE;
1157
1158 /* Catch keys that need to be handled as in Normal mode. */
1159 switch (c)
1160 {
1161 case NUL:
1162 case K_ZERO:
1163 if (typed)
1164 stuffcharReadbuff(c);
1165 return FAIL;
1166
1167 case K_IGNORE:
1168 return FAIL;
1169
1170 case K_LEFTDRAG:
1171 case K_MIDDLEDRAG:
1172 case K_RIGHTDRAG:
1173 case K_X1DRAG:
1174 case K_X2DRAG:
1175 dragging_outside = mouse_was_outside;
1176 /* FALLTHROUGH */
1177 case K_LEFTMOUSE:
1178 case K_LEFTMOUSE_NM:
1179 case K_LEFTRELEASE:
1180 case K_LEFTRELEASE_NM:
1181 case K_MIDDLEMOUSE:
1182 case K_MIDDLERELEASE:
1183 case K_RIGHTMOUSE:
1184 case K_RIGHTRELEASE:
1185 case K_X1MOUSE:
1186 case K_X1RELEASE:
1187 case K_X2MOUSE:
1188 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001189
1190 case K_MOUSEUP:
1191 case K_MOUSEDOWN:
1192 case K_MOUSELEFT:
1193 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001194 if (mouse_row < W_WINROW(curwin)
1195 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1196 || mouse_col < W_WINCOL(curwin)
1197 || mouse_col >= W_ENDCOL(curwin)
1198 || dragging_outside)
1199 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001200 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001201 if (typed)
1202 {
1203 stuffcharReadbuff(c);
1204 mouse_was_outside = TRUE;
1205 }
1206 return FAIL;
1207 }
1208 }
1209 if (typed)
1210 mouse_was_outside = FALSE;
1211
1212 /* Convert the typed key to a sequence of bytes for the job. */
1213 len = term_convert_key(term, c, msg);
1214 if (len > 0)
1215 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001216 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1217 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001218
1219 return OK;
1220}
1221
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001222 static void
1223position_cursor(win_T *wp, VTermPos *pos)
1224{
1225 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1226 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1227 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1228}
1229
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001230/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001231 * Handle CTRL-W "": send register contents to the job.
1232 */
1233 static void
1234term_paste_register(int prev_c UNUSED)
1235{
1236 int c;
1237 list_T *l;
1238 listitem_T *item;
1239 long reglen = 0;
1240 int type;
1241
1242#ifdef FEAT_CMDL_INFO
1243 if (add_to_showcmd(prev_c))
1244 if (add_to_showcmd('"'))
1245 out_flush();
1246#endif
1247 c = term_vgetc();
1248#ifdef FEAT_CMDL_INFO
1249 clear_showcmd();
1250#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001251 if (!term_use_loop())
1252 /* job finished while waiting for a character */
1253 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001254
1255 /* CTRL-W "= prompt for expression to evaluate. */
1256 if (c == '=' && get_expr_register() != '=')
1257 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001258 if (!term_use_loop())
1259 /* job finished while waiting for a character */
1260 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001261
1262 l = (list_T *)get_reg_contents(c, GREG_LIST);
1263 if (l != NULL)
1264 {
1265 type = get_reg_type(c, &reglen);
1266 for (item = l->lv_first; item != NULL; item = item->li_next)
1267 {
1268 char_u *s = get_tv_string(&item->li_tv);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001269#ifdef WIN3264
1270 char_u *tmp = s;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001271
Bram Moolenaar285f2432017-08-23 23:10:21 +02001272 if (!enc_utf8 && enc_codepage > 0)
1273 {
1274 WCHAR *ret = NULL;
1275 int length = 0;
1276
1277 MultiByteToWideChar_alloc(enc_codepage, 0, (char*)s, STRLEN(s),
1278 &ret, &length);
1279 if (ret != NULL)
1280 {
1281 WideCharToMultiByte_alloc(CP_UTF8, 0,
1282 ret, length, (char **)&s, &length, 0, 0);
1283 vim_free(ret);
1284 }
1285 }
1286#endif
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001287 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1288 s, STRLEN(s), NULL);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001289#ifdef WIN3264
1290 if (tmp != s)
1291 vim_free(s);
1292#endif
1293
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001294 if (item->li_next != NULL || type == MLINE)
1295 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1296 (char_u *)"\r", 1, NULL);
1297 }
1298 list_free(l);
1299 }
1300}
1301
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001302#if defined(FEAT_GUI) || defined(PROTO)
1303/*
1304 * Return TRUE when the cursor of the terminal should be displayed.
1305 */
1306 int
1307use_terminal_cursor()
1308{
1309 return in_terminal_loop != NULL;
1310}
1311
1312 cursorentry_T *
1313term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1314{
1315 term_T *term = in_terminal_loop;
1316 static cursorentry_T entry;
1317
1318 vim_memset(&entry, 0, sizeof(entry));
1319 entry.shape = entry.mshape =
1320 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1321 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1322 SHAPE_BLOCK;
1323 entry.percentage = 20;
1324 if (term->tl_cursor_blink)
1325 {
1326 entry.blinkwait = 700;
1327 entry.blinkon = 400;
Bram Moolenaar58302322017-08-22 20:33:53 +02001328 entry.blinkoff = 250;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001329 }
1330 *fg = gui.back_pixel;
1331 if (term->tl_cursor_color == NULL)
1332 *bg = gui.norm_pixel;
1333 else
1334 *bg = color_name2handle(term->tl_cursor_color);
1335 entry.name = "n";
1336 entry.used_for = SHAPE_CURSOR;
1337
1338 return &entry;
1339}
1340#endif
1341
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001342static int did_change_cursor = FALSE;
1343
1344 static void
1345may_set_cursor_props(term_T *term)
1346{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001347#ifdef FEAT_GUI
1348 /* For the GUI the cursor properties are obtained with
1349 * term_get_cursor_shape(). */
1350 if (gui.in_use)
1351 return;
1352#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001353 if (in_terminal_loop == term)
1354 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001355 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001356 if (term->tl_cursor_color != NULL)
1357 term_cursor_color(term->tl_cursor_color);
1358 else
1359 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001360 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1361 }
1362}
1363
1364 static void
1365may_restore_cursor_props(void)
1366{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001367#ifdef FEAT_GUI
1368 if (gui.in_use)
1369 return;
1370#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001371 if (did_change_cursor)
1372 {
1373 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001374 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001375 /* this will restore the initial cursor style, if possible */
1376 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001377 }
1378}
1379
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001380/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001381 * Returns TRUE if the current window contains a terminal and we are sending
1382 * keys to the job.
1383 */
1384 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001385term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001386{
1387 term_T *term = curbuf->b_term;
1388
1389 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001390 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001391 && term->tl_vterm != NULL
1392 && term_job_running(term);
1393}
1394
1395/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001396 * Wait for input and send it to the job.
1397 * Return when the start of a CTRL-W command is typed or anything else that
1398 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001399 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1400 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001401 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001402 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001403terminal_loop(void)
1404{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001405 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001406 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001407 int ret;
1408
Bram Moolenaar679653e2017-08-13 14:13:19 +02001409 /* Remember the terminal we are sending keys to. However, the terminal
1410 * might be closed while waiting for a character, e.g. typing "exit" in a
1411 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1412 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001413 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001414
1415 if (*curwin->w_p_tk != NUL)
1416 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001417 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001418 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001419
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001420#ifdef UNIX
1421 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001422 int part = get_tty_part(curbuf->b_term);
1423 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001424
1425 if (isatty(fd))
1426 {
1427 ttyinfo_T info;
1428
1429 /* Get the current backspace and enter characters of the pty. */
1430 if (get_tty_info(fd, &info) == OK)
1431 {
1432 term_backspace_char = info.backspace;
1433 term_enter_char = info.enter;
1434 term_nl_does_cr = info.nl_does_cr;
1435 }
1436 }
1437 }
1438#endif
1439
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001440 for (;;)
1441 {
1442 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001443 /* Repeat redrawing in case a message is received while redrawing. */
1444 while (curwin->w_redr_type != 0)
1445 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001446 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001447
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001448 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001449 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001450 /* job finished while waiting for a character */
1451 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001452 if (c == K_IGNORE)
1453 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001454
Bram Moolenaarfae42832017-08-01 22:24:26 +02001455#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001456 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001457 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001458 if (ctrl_break_was_pressed)
1459 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001460#endif
1461
Bram Moolenaar69198192017-08-05 14:10:48 +02001462 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001463 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001464 int prev_c = c;
1465
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001466#ifdef FEAT_CMDL_INFO
1467 if (add_to_showcmd(c))
1468 out_flush();
1469#endif
1470 c = term_vgetc();
1471#ifdef FEAT_CMDL_INFO
1472 clear_showcmd();
1473#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001474 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001475 /* job finished while waiting for a character */
1476 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001477
Bram Moolenaar69198192017-08-05 14:10:48 +02001478 if (prev_c == Ctrl_BSL)
1479 {
1480 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001481 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001482 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1483 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001484 ret = FAIL;
1485 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001486 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001487 /* Send both keys to the terminal. */
1488 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1489 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001490 else if (c == Ctrl_C)
1491 {
1492 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1493 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1494 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001495 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001496 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001497 /* "CTRL-W .": send CTRL-W to the job */
1498 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001499 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001500 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001501 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001502 /* CTRL-W N : go to Terminal-Normal mode. */
1503 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001504 ret = FAIL;
1505 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001506 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001507 else if (c == '"')
1508 {
1509 term_paste_register(prev_c);
1510 continue;
1511 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001512 else if (termkey == 0 || c != termkey)
1513 {
1514 stuffcharReadbuff(Ctrl_W);
1515 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001516 ret = OK;
1517 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001518 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001519 }
Bram Moolenaar58302322017-08-22 20:33:53 +02001520# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02001521 if (!enc_utf8 && has_mbyte && c >= 0x80)
1522 {
1523 WCHAR wc;
1524 char_u mb[3];
1525
1526 mb[0] = (unsigned)c >> 8;
1527 mb[1] = c;
1528 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1529 c = wc;
1530 }
1531# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001532 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001533 {
1534 ret = OK;
1535 goto theend;
1536 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001537 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001538 ret = FAIL;
1539
1540theend:
1541 in_terminal_loop = NULL;
1542 may_restore_cursor_props();
1543 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001544}
1545
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001546/*
1547 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001548 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001549 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001550 */
1551 void
1552term_job_ended(job_T *job)
1553{
1554 term_T *term;
1555 int did_one = FALSE;
1556
1557 for (term = first_term; term != NULL; term = term->tl_next)
1558 if (term->tl_job == job)
1559 {
1560 vim_free(term->tl_title);
1561 term->tl_title = NULL;
1562 vim_free(term->tl_status_text);
1563 term->tl_status_text = NULL;
1564 redraw_buf_and_status_later(term->tl_buffer, VALID);
1565 did_one = TRUE;
1566 }
1567 if (did_one)
1568 redraw_statuslines();
1569 if (curbuf->b_term != NULL)
1570 {
1571 if (curbuf->b_term->tl_job == job)
1572 maketitle();
1573 update_cursor(curbuf->b_term, TRUE);
1574 }
1575}
1576
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001577 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001578may_toggle_cursor(term_T *term)
1579{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001580 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001581 {
1582 if (term->tl_cursor_visible)
1583 cursor_on();
1584 else
1585 cursor_off();
1586 }
1587}
1588
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001589/*
1590 * Reverse engineer the RGB value into a cterm color index.
1591 * First color is 1. Return 0 if no match found.
1592 */
1593 static int
1594color2index(VTermColor *color, int fg, int *boldp)
1595{
1596 int red = color->red;
1597 int blue = color->blue;
1598 int green = color->green;
1599
1600 /* The argument for lookup_color() is for the color_names[] table. */
1601 if (red == 0)
1602 {
1603 if (green == 0)
1604 {
1605 if (blue == 0)
1606 return lookup_color(0, fg, boldp) + 1; /* black */
1607 if (blue == 224)
1608 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1609 }
1610 else if (green == 224)
1611 {
1612 if (blue == 0)
1613 return lookup_color(2, fg, boldp) + 1; /* dark green */
1614 if (blue == 224)
1615 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1616 }
1617 }
1618 else if (red == 224)
1619 {
1620 if (green == 0)
1621 {
1622 if (blue == 0)
1623 return lookup_color(4, fg, boldp) + 1; /* dark red */
1624 if (blue == 224)
1625 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1626 }
1627 else if (green == 224)
1628 {
1629 if (blue == 0)
1630 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1631 if (blue == 224)
1632 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1633 }
1634 }
1635 else if (red == 128)
1636 {
1637 if (green == 128 && blue == 128)
1638 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1639 }
1640 else if (red == 255)
1641 {
1642 if (green == 64)
1643 {
1644 if (blue == 64)
1645 return lookup_color(20, fg, boldp) + 1; /* light red */
1646 if (blue == 255)
1647 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1648 }
1649 else if (green == 255)
1650 {
1651 if (blue == 64)
1652 return lookup_color(24, fg, boldp) + 1; /* yellow */
1653 if (blue == 255)
1654 return lookup_color(26, fg, boldp) + 1; /* white */
1655 }
1656 }
1657 else if (red == 64)
1658 {
1659 if (green == 64)
1660 {
1661 if (blue == 255)
1662 return lookup_color(14, fg, boldp) + 1; /* light blue */
1663 }
1664 else if (green == 255)
1665 {
1666 if (blue == 64)
1667 return lookup_color(16, fg, boldp) + 1; /* light green */
1668 if (blue == 255)
1669 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1670 }
1671 }
1672 if (t_colors >= 256)
1673 {
1674 if (red == blue && red == green)
1675 {
1676 /* 24-color greyscale */
1677 static int cutoff[23] = {
1678 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1679 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1680 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1681 int i;
1682
1683 for (i = 0; i < 23; ++i)
1684 if (red < cutoff[i])
1685 return i + 233;
1686 return 256;
1687 }
1688
1689 /* 216-color cube */
1690 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001691 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001692 + (blue + 25) / 0x33;
1693 }
1694 return 0;
1695}
1696
1697/*
1698 * Convert the attributes of a vterm cell into an attribute index.
1699 */
1700 static int
1701cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1702{
1703 int attr = 0;
1704
1705 if (cellattrs.bold)
1706 attr |= HL_BOLD;
1707 if (cellattrs.underline)
1708 attr |= HL_UNDERLINE;
1709 if (cellattrs.italic)
1710 attr |= HL_ITALIC;
1711 if (cellattrs.strike)
1712 attr |= HL_STANDOUT;
1713 if (cellattrs.reverse)
1714 attr |= HL_INVERSE;
1715
1716#ifdef FEAT_GUI
1717 if (gui.in_use)
1718 {
1719 guicolor_T fg, bg;
1720
1721 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1722 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1723 return get_gui_attr_idx(attr, fg, bg);
1724 }
1725 else
1726#endif
1727#ifdef FEAT_TERMGUICOLORS
1728 if (p_tgc)
1729 {
1730 guicolor_T fg, bg;
1731
1732 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1733 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1734
1735 return get_tgc_attr_idx(attr, fg, bg);
1736 }
1737 else
1738#endif
1739 {
1740 int bold = MAYBE;
1741 int fg = color2index(&cellfg, TRUE, &bold);
1742 int bg = color2index(&cellbg, FALSE, &bold);
1743
1744 /* with 8 colors set the bold attribute to get a bright foreground */
1745 if (bold == TRUE)
1746 attr |= HL_BOLD;
1747 return get_cterm_attr_idx(attr, fg, bg);
1748 }
1749 return 0;
1750}
1751
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001752 static int
1753handle_damage(VTermRect rect, void *user)
1754{
1755 term_T *term = (term_T *)user;
1756
1757 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1758 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1759 redraw_buf_later(term->tl_buffer, NOT_VALID);
1760 return 1;
1761}
1762
1763 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001764handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001765{
1766 term_T *term = (term_T *)user;
1767
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001768 /* Scrolling up is done much more efficiently by deleting lines instead of
1769 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001770 if (dest.start_col == src.start_col
1771 && dest.end_col == src.end_col
1772 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001773 {
1774 win_T *wp;
1775 VTermColor fg, bg;
1776 VTermScreenCellAttrs attr;
1777 int clear_attr;
1778
1779 /* Set the color to clear lines with. */
1780 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1781 &fg, &bg);
1782 vim_memset(&attr, 0, sizeof(attr));
1783 clear_attr = cell2attr(attr, fg, bg);
1784
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001785 FOR_ALL_WINDOWS(wp)
1786 {
1787 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001788 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001789 src.start_row - dest.start_row, FALSE, FALSE,
1790 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001791 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001792 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001793 redraw_buf_later(term->tl_buffer, NOT_VALID);
1794 return 1;
1795}
1796
1797 static int
1798handle_movecursor(
1799 VTermPos pos,
1800 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001801 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001802 void *user)
1803{
1804 term_T *term = (term_T *)user;
1805 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001806
1807 term->tl_cursor_pos = pos;
1808 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001809
1810 FOR_ALL_WINDOWS(wp)
1811 {
1812 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001813 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001814 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001815 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001816 {
1817 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001818 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001819 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001820
1821 return 1;
1822}
1823
Bram Moolenaar21554412017-07-24 21:44:43 +02001824 static int
1825handle_settermprop(
1826 VTermProp prop,
1827 VTermValue *value,
1828 void *user)
1829{
1830 term_T *term = (term_T *)user;
1831
1832 switch (prop)
1833 {
1834 case VTERM_PROP_TITLE:
1835 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001836 /* a blank title isn't useful, make it empty, so that "running" is
1837 * displayed */
1838 if (*skipwhite((char_u *)value->string) == NUL)
1839 term->tl_title = NULL;
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001840#ifdef WIN3264
1841 else if (!enc_utf8 && enc_codepage > 0)
1842 {
1843 WCHAR *ret = NULL;
1844 int length = 0;
1845
1846 MultiByteToWideChar_alloc(CP_UTF8, 0,
1847 (char*)value->string, STRLEN(value->string),
1848 &ret, &length);
1849 if (ret != NULL)
1850 {
1851 WideCharToMultiByte_alloc(enc_codepage, 0,
1852 ret, length, (char**)&term->tl_title,
1853 &length, 0, 0);
1854 vim_free(ret);
1855 }
1856 }
1857#endif
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001858 else
1859 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001860 vim_free(term->tl_status_text);
1861 term->tl_status_text = NULL;
1862 if (term == curbuf->b_term)
1863 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001864 break;
1865
1866 case VTERM_PROP_CURSORVISIBLE:
1867 term->tl_cursor_visible = value->boolean;
1868 may_toggle_cursor(term);
1869 out_flush();
1870 break;
1871
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001872 case VTERM_PROP_CURSORBLINK:
1873 term->tl_cursor_blink = value->boolean;
1874 may_set_cursor_props(term);
1875 break;
1876
1877 case VTERM_PROP_CURSORSHAPE:
1878 term->tl_cursor_shape = value->number;
1879 may_set_cursor_props(term);
1880 break;
1881
1882 case VTERM_PROP_CURSORCOLOR:
1883 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001884 if (*value->string == NUL)
1885 term->tl_cursor_color = NULL;
1886 else
1887 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001888 may_set_cursor_props(term);
1889 break;
1890
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001891 case VTERM_PROP_ALTSCREEN:
1892 /* TODO: do anything else? */
1893 term->tl_using_altscreen = value->boolean;
1894 break;
1895
Bram Moolenaar21554412017-07-24 21:44:43 +02001896 default:
1897 break;
1898 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001899 /* Always return 1, otherwise vterm doesn't store the value internally. */
1900 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001901}
1902
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001903/*
1904 * The job running in the terminal resized the terminal.
1905 */
1906 static int
1907handle_resize(int rows, int cols, void *user)
1908{
1909 term_T *term = (term_T *)user;
1910 win_T *wp;
1911
1912 term->tl_rows = rows;
1913 term->tl_cols = cols;
1914 FOR_ALL_WINDOWS(wp)
1915 {
1916 if (wp->w_buffer == term->tl_buffer)
1917 {
1918 win_setheight_win(rows, wp);
1919 win_setwidth_win(cols, wp);
1920 }
1921 }
1922
1923 redraw_buf_later(term->tl_buffer, NOT_VALID);
1924 return 1;
1925}
1926
Bram Moolenaard85f2712017-07-28 21:51:57 +02001927/*
1928 * Handle a line that is pushed off the top of the screen.
1929 */
1930 static int
1931handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1932{
1933 term_T *term = (term_T *)user;
1934
1935 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001936 if (ga_grow(&term->tl_scrollback, 1) == OK)
1937 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001938 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001939 int len = 0;
1940 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001941 int c;
1942 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001943 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001944 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001945
1946 /* do not store empty cells at the end */
1947 for (i = 0; i < cols; ++i)
1948 if (cells[i].chars[0] != 0)
1949 len = i + 1;
1950
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001951 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001952 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001953 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001954 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001955 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001956 for (col = 0; col < len; col += cells[col].width)
1957 {
1958 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1959 {
1960 ga.ga_len = 0;
1961 break;
1962 }
1963 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +02001964 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001965 (char_u *)ga.ga_data + ga.ga_len);
1966 p[col].width = cells[col].width;
1967 p[col].attrs = cells[col].attrs;
1968 p[col].fg = cells[col].fg;
1969 p[col].bg = cells[col].bg;
1970 }
1971 }
1972 if (ga_grow(&ga, 1) == FAIL)
1973 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1974 else
1975 {
1976 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1977 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1978 }
1979 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001980
1981 line = (sb_line_T *)term->tl_scrollback.ga_data
1982 + term->tl_scrollback.ga_len;
1983 line->sb_cols = len;
1984 line->sb_cells = p;
1985 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001986 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001987 }
1988 return 0; /* ignored */
1989}
1990
Bram Moolenaar21554412017-07-24 21:44:43 +02001991static VTermScreenCallbacks screen_callbacks = {
1992 handle_damage, /* damage */
1993 handle_moverect, /* moverect */
1994 handle_movecursor, /* movecursor */
1995 handle_settermprop, /* settermprop */
1996 NULL, /* bell */
1997 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001998 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001999 NULL /* sb_popline */
2000};
2001
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002002/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02002003 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002004 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02002005 */
2006 void
2007term_channel_closed(channel_T *ch)
2008{
2009 term_T *term;
2010 int did_one = FALSE;
2011
2012 for (term = first_term; term != NULL; term = term->tl_next)
2013 if (term->tl_job == ch->ch_job)
2014 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002015 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002016 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002017
Bram Moolenaard85f2712017-07-28 21:51:57 +02002018 vim_free(term->tl_title);
2019 term->tl_title = NULL;
2020 vim_free(term->tl_status_text);
2021 term->tl_status_text = NULL;
2022
Bram Moolenaar423802d2017-07-30 16:52:24 +02002023 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02002024 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002025 {
2026 int fnum = term->tl_buffer->b_fnum;
2027
Bram Moolenaar423802d2017-07-30 16:52:24 +02002028 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002029
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002030 if (term->tl_finish == 'c')
2031 {
2032 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002033 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002034 curbuf = term->tl_buffer;
2035 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
2036 break;
2037 }
2038 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
2039 {
2040 char buf[50];
2041
2042 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002043 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02002044 vim_snprintf(buf, sizeof(buf),
2045 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02002046 ? "botright sbuf %d"
2047 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002048 do_cmdline_cmd((char_u *)buf);
2049 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002050 else
2051 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002052 }
2053
Bram Moolenaard85f2712017-07-28 21:51:57 +02002054 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002055 }
2056 if (did_one)
2057 {
2058 redraw_statuslines();
2059
2060 /* Need to break out of vgetc(). */
2061 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002062 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002063
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002064 term = curbuf->b_term;
2065 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002066 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002067 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002068 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002069 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002070 }
2071 }
2072}
2073
2074/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002075 * Called to update a window that contains an active terminal.
2076 * Returns FAIL when there is no terminal running in this window or in
2077 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002078 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002079 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002080term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002081{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002082 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002083 VTerm *vterm;
2084 VTermScreen *screen;
2085 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002086 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002087
Bram Moolenaar6d819742017-08-06 14:57:49 +02002088 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002089 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002090
Bram Moolenaard85f2712017-07-28 21:51:57 +02002091 vterm = term->tl_vterm;
2092 screen = vterm_obtain_screen(vterm);
2093 state = vterm_obtain_state(vterm);
2094
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002095 /*
2096 * If the window was resized a redraw will be triggered and we get here.
2097 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2098 */
2099 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2100 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002101 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002102 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2103 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2104 win_T *twp;
2105
2106 FOR_ALL_WINDOWS(twp)
2107 {
2108 /* When more than one window shows the same terminal, use the
2109 * smallest size. */
2110 if (twp->w_buffer == term->tl_buffer)
2111 {
2112 if (!term->tl_rows_fixed && rows > twp->w_height)
2113 rows = twp->w_height;
2114 if (!term->tl_cols_fixed && cols > twp->w_width)
2115 cols = twp->w_width;
2116 }
2117 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002118
2119 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002120 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002121 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002122 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002123 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002124
2125 /* The cursor may have been moved when resizing. */
2126 vterm_state_get_cursorpos(state, &pos);
2127 position_cursor(wp, &pos);
2128
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002129 /* TODO: Only redraw what changed. */
2130 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002131 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002132 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002133 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002134
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002135 if (pos.row < term->tl_rows)
2136 {
2137 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002138 {
2139 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002140 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002141
Bram Moolenaareeac6772017-07-23 15:48:37 +02002142 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2143 vim_memset(&cell, 0, sizeof(cell));
2144
2145 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002146 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002147 if (c == NUL)
2148 {
2149 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002150 if (enc_utf8)
2151 ScreenLinesUC[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002152 }
2153 else
2154 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002155 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002156 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002157 if (c >= 0x80)
2158 {
2159 ScreenLines[off] = ' ';
2160 ScreenLinesUC[off] = c;
2161 }
2162 else
2163 {
2164 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002165 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002166 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002167 }
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002168#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02002169 else if (has_mbyte && c >= 0x80)
2170 {
2171 char_u mb[MB_MAXBYTES+1];
2172 WCHAR wc = c;
2173
2174 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2175 (char*)mb, 2, 0, 0) > 1)
2176 {
2177 ScreenLines[off] = mb[0];
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002178 ScreenLines[off + 1] = mb[1];
Bram Moolenaar740c4332017-08-21 22:01:27 +02002179 cell.width = mb_ptr2cells(mb);
2180 }
2181 else
2182 ScreenLines[off] = c;
2183 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002184#endif
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002185 else
Bram Moolenaar740c4332017-08-21 22:01:27 +02002186 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002187 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002188 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002189
2190 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002191 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002192 if (cell.width == 2)
2193 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002194 if (enc_utf8)
2195 ScreenLinesUC[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002196
2197 /* don't set the second byte to NUL for a DBCS encoding, it
2198 * has been set above */
2199 if (enc_utf8 || !has_mbyte)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002200 ScreenLines[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002201
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002202 ++pos.col;
2203 ++off;
2204 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002205 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002206 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002207 else
2208 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002209
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002210 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2211 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002212 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002213
2214 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002215}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002216
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002217/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002218 * Return TRUE if "wp" is a terminal window where the job has finished.
2219 */
2220 int
2221term_is_finished(buf_T *buf)
2222{
2223 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2224}
2225
2226/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002227 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002228 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002229 */
2230 int
2231term_show_buffer(buf_T *buf)
2232{
2233 term_T *term = buf->b_term;
2234
Bram Moolenaar6d819742017-08-06 14:57:49 +02002235 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002236}
2237
2238/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002239 * The current buffer is going to be changed. If there is terminal
2240 * highlighting remove it now.
2241 */
2242 void
2243term_change_in_curbuf(void)
2244{
2245 term_T *term = curbuf->b_term;
2246
2247 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2248 {
2249 free_scrollback(term);
2250 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002251
2252 /* The buffer is now like a normal buffer, it cannot be easily
2253 * abandoned when changed. */
2254 set_string_option_direct((char_u *)"buftype", -1,
2255 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002256 }
2257}
2258
2259/*
2260 * Get the screen attribute for a position in the buffer.
2261 */
2262 int
2263term_get_attr(buf_T *buf, linenr_T lnum, int col)
2264{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002265 term_T *term = buf->b_term;
2266 sb_line_T *line;
2267 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002268
Bram Moolenaar70229f92017-07-29 16:01:53 +02002269 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002270 return 0;
2271 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2272 if (col >= line->sb_cols)
2273 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002274 cellattr = line->sb_cells + col;
2275 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002276}
2277
2278/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002279 * Create a new vterm and initialize it.
2280 */
2281 static void
2282create_vterm(term_T *term, int rows, int cols)
2283{
2284 VTerm *vterm;
2285 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002286 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002287
2288 vterm = vterm_new(rows, cols);
2289 term->tl_vterm = vterm;
2290 screen = vterm_obtain_screen(vterm);
2291 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2292 /* TODO: depends on 'encoding'. */
2293 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002294
2295 /* Vterm uses a default black background. Set it to white when
2296 * 'background' is "light". */
2297 if (*p_bg == 'l')
2298 {
2299 VTermColor fg, bg;
2300
2301 fg.red = fg.green = fg.blue = 0;
2302 bg.red = bg.green = bg.blue = 255;
2303 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2304 }
2305
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002306 /* Required to initialize most things. */
2307 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002308
2309 /* Allow using alternate screen. */
2310 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002311
Bram Moolenaar58302322017-08-22 20:33:53 +02002312 /* For unix do not use a blinking cursor. In an xterm this causes the
2313 * cursor to blink if it's blinking in the xterm.
2314 * We do want a blinking cursor by default on Windows, since that's what
2315 * the default is for a console. */
2316#ifdef WIN3264
2317 value.boolean = 1;
2318#else
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002319 value.boolean = 0;
Bram Moolenaar58302322017-08-22 20:33:53 +02002320#endif
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002321 vterm_state_set_termprop(vterm_obtain_state(vterm),
2322 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002323}
2324
Bram Moolenaar21554412017-07-24 21:44:43 +02002325/*
2326 * Return the text to show for the buffer name and status.
2327 */
2328 char_u *
2329term_get_status_text(term_T *term)
2330{
2331 if (term->tl_status_text == NULL)
2332 {
2333 char_u *txt;
2334 size_t len;
2335
Bram Moolenaar6d819742017-08-06 14:57:49 +02002336 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002337 {
2338 if (term_job_running(term))
2339 txt = (char_u *)_("Terminal");
2340 else
2341 txt = (char_u *)_("Terminal-finished");
2342 }
2343 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002344 txt = term->tl_title;
2345 else if (term_job_running(term))
2346 txt = (char_u *)_("running");
2347 else
2348 txt = (char_u *)_("finished");
2349 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002350 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002351 if (term->tl_status_text != NULL)
2352 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2353 term->tl_buffer->b_fname, txt);
2354 }
2355 return term->tl_status_text;
2356}
2357
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002358/*
2359 * Mark references in jobs of terminals.
2360 */
2361 int
2362set_ref_in_term(int copyID)
2363{
2364 int abort = FALSE;
2365 term_T *term;
2366 typval_T tv;
2367
2368 for (term = first_term; term != NULL; term = term->tl_next)
2369 if (term->tl_job != NULL)
2370 {
2371 tv.v_type = VAR_JOB;
2372 tv.vval.v_job = term->tl_job;
2373 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2374 }
2375 return abort;
2376}
2377
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002378/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002379 * Get the buffer from the first argument in "argvars".
2380 * Returns NULL when the buffer is not for a terminal window.
2381 */
2382 static buf_T *
2383term_get_buf(typval_T *argvars)
2384{
2385 buf_T *buf;
2386
2387 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2388 ++emsg_off;
2389 buf = get_buf_tv(&argvars[0], FALSE);
2390 --emsg_off;
2391 if (buf == NULL || buf->b_term == NULL)
2392 return NULL;
2393 return buf;
2394}
2395
2396/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002397 * "term_getaltscreen(buf)" function
2398 */
2399 void
2400f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2401{
2402 buf_T *buf = term_get_buf(argvars);
2403
2404 if (buf == NULL)
2405 return;
2406 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2407}
2408
2409/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002410 * "term_getattr(attr, name)" function
2411 */
2412 void
2413f_term_getattr(typval_T *argvars, typval_T *rettv)
2414{
2415 int attr;
2416 size_t i;
2417 char_u *name;
2418
2419 static struct {
2420 char *name;
2421 int attr;
2422 } attrs[] = {
2423 {"bold", HL_BOLD},
2424 {"italic", HL_ITALIC},
2425 {"underline", HL_UNDERLINE},
2426 {"strike", HL_STANDOUT},
2427 {"reverse", HL_INVERSE},
2428 };
2429
2430 attr = get_tv_number(&argvars[0]);
2431 name = get_tv_string_chk(&argvars[1]);
2432 if (name == NULL)
2433 return;
2434
2435 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2436 if (STRCMP(name, attrs[i].name) == 0)
2437 {
2438 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2439 break;
2440 }
2441}
2442
2443/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002444 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002445 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002446 void
2447f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002448{
Bram Moolenaar97870002017-07-30 18:28:38 +02002449 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002450 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002451 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002452 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002453
Bram Moolenaar97870002017-07-30 18:28:38 +02002454 if (rettv_list_alloc(rettv) == FAIL)
2455 return;
2456 if (buf == NULL)
2457 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002458 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002459
2460 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002461 list_append_number(l, term->tl_cursor_pos.row + 1);
2462 list_append_number(l, term->tl_cursor_pos.col + 1);
2463
2464 d = dict_alloc();
2465 if (d != NULL)
2466 {
2467 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2468 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2469 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2470 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2471 ? (char_u *)"" : term->tl_cursor_color);
2472 list_append_dict(l, d);
2473 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002474}
2475
2476/*
2477 * "term_getjob(buf)" function
2478 */
2479 void
2480f_term_getjob(typval_T *argvars, typval_T *rettv)
2481{
2482 buf_T *buf = term_get_buf(argvars);
2483
2484 rettv->v_type = VAR_JOB;
2485 rettv->vval.v_job = NULL;
2486 if (buf == NULL)
2487 return;
2488
2489 rettv->vval.v_job = buf->b_term->tl_job;
2490 if (rettv->vval.v_job != NULL)
2491 ++rettv->vval.v_job->jv_refcount;
2492}
2493
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002494 static int
2495get_row_number(typval_T *tv, term_T *term)
2496{
2497 if (tv->v_type == VAR_STRING
2498 && tv->vval.v_string != NULL
2499 && STRCMP(tv->vval.v_string, ".") == 0)
2500 return term->tl_cursor_pos.row;
2501 return (int)get_tv_number(tv) - 1;
2502}
2503
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002504/*
2505 * "term_getline(buf, row)" function
2506 */
2507 void
2508f_term_getline(typval_T *argvars, typval_T *rettv)
2509{
2510 buf_T *buf = term_get_buf(argvars);
2511 term_T *term;
2512 int row;
2513
2514 rettv->v_type = VAR_STRING;
2515 if (buf == NULL)
2516 return;
2517 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002518 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002519
2520 if (term->tl_vterm == NULL)
2521 {
2522 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2523
2524 /* vterm is finished, get the text from the buffer */
2525 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2526 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2527 }
2528 else
2529 {
2530 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2531 VTermRect rect;
2532 int len;
2533 char_u *p;
2534
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002535 if (row < 0 || row >= term->tl_rows)
2536 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002537 len = term->tl_cols * MB_MAXBYTES + 1;
2538 p = alloc(len);
2539 if (p == NULL)
2540 return;
2541 rettv->vval.v_string = p;
2542
2543 rect.start_col = 0;
2544 rect.end_col = term->tl_cols;
2545 rect.start_row = row;
2546 rect.end_row = row + 1;
2547 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2548 }
2549}
2550
2551/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002552 * "term_getscrolled(buf)" function
2553 */
2554 void
2555f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2556{
2557 buf_T *buf = term_get_buf(argvars);
2558
2559 if (buf == NULL)
2560 return;
2561 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2562}
2563
2564/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002565 * "term_getsize(buf)" function
2566 */
2567 void
2568f_term_getsize(typval_T *argvars, typval_T *rettv)
2569{
2570 buf_T *buf = term_get_buf(argvars);
2571 list_T *l;
2572
2573 if (rettv_list_alloc(rettv) == FAIL)
2574 return;
2575 if (buf == NULL)
2576 return;
2577
2578 l = rettv->vval.v_list;
2579 list_append_number(l, buf->b_term->tl_rows);
2580 list_append_number(l, buf->b_term->tl_cols);
2581}
2582
2583/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002584 * "term_getstatus(buf)" function
2585 */
2586 void
2587f_term_getstatus(typval_T *argvars, typval_T *rettv)
2588{
2589 buf_T *buf = term_get_buf(argvars);
2590 term_T *term;
2591 char_u val[100];
2592
2593 rettv->v_type = VAR_STRING;
2594 if (buf == NULL)
2595 return;
2596 term = buf->b_term;
2597
2598 if (term_job_running(term))
2599 STRCPY(val, "running");
2600 else
2601 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002602 if (term->tl_normal_mode)
2603 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002604 rettv->vval.v_string = vim_strsave(val);
2605}
2606
2607/*
2608 * "term_gettitle(buf)" function
2609 */
2610 void
2611f_term_gettitle(typval_T *argvars, typval_T *rettv)
2612{
2613 buf_T *buf = term_get_buf(argvars);
2614
2615 rettv->v_type = VAR_STRING;
2616 if (buf == NULL)
2617 return;
2618
2619 if (buf->b_term->tl_title != NULL)
2620 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2621}
2622
2623/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002624 * "term_gettty(buf)" function
2625 */
2626 void
2627f_term_gettty(typval_T *argvars, typval_T *rettv)
2628{
2629 buf_T *buf = term_get_buf(argvars);
2630 char_u *p;
2631
2632 rettv->v_type = VAR_STRING;
2633 if (buf == NULL)
2634 return;
2635 if (buf->b_term->tl_job != NULL)
2636 p = buf->b_term->tl_job->jv_tty_name;
2637 else
2638 p = buf->b_term->tl_tty_name;
2639 if (p != NULL)
2640 rettv->vval.v_string = vim_strsave(p);
2641}
2642
2643/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002644 * "term_list()" function
2645 */
2646 void
2647f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2648{
2649 term_T *tp;
2650 list_T *l;
2651
2652 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2653 return;
2654
2655 l = rettv->vval.v_list;
2656 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2657 if (tp != NULL && tp->tl_buffer != NULL)
2658 if (list_append_number(l,
2659 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2660 return;
2661}
2662
2663/*
2664 * "term_scrape(buf, row)" function
2665 */
2666 void
2667f_term_scrape(typval_T *argvars, typval_T *rettv)
2668{
2669 buf_T *buf = term_get_buf(argvars);
2670 VTermScreen *screen = NULL;
2671 VTermPos pos;
2672 list_T *l;
2673 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002674 char_u *p;
2675 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002676
2677 if (rettv_list_alloc(rettv) == FAIL)
2678 return;
2679 if (buf == NULL)
2680 return;
2681 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002682
2683 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002684 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002685
2686 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002687 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002688 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002689 p = NULL;
2690 line = NULL;
2691 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002692 else
2693 {
2694 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2695
2696 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2697 return;
2698 p = ml_get_buf(buf, lnum + 1, FALSE);
2699 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2700 }
2701
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002702 for (pos.col = 0; pos.col < term->tl_cols; )
2703 {
2704 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002705 int width;
2706 VTermScreenCellAttrs attrs;
2707 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002708 char_u rgb[8];
2709 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2710 int off = 0;
2711 int i;
2712
2713 if (screen == NULL)
2714 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002715 cellattr_T *cellattr;
2716 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002717
2718 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002719 if (pos.col >= line->sb_cols)
2720 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002721 cellattr = line->sb_cells + pos.col;
2722 width = cellattr->width;
2723 attrs = cellattr->attrs;
2724 fg = cellattr->fg;
2725 bg = cellattr->bg;
2726 len = MB_PTR2LEN(p);
2727 mch_memmove(mbs, p, len);
2728 mbs[len] = NUL;
2729 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002730 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002731 else
2732 {
2733 VTermScreenCell cell;
2734 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2735 break;
2736 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2737 {
2738 if (cell.chars[i] == 0)
2739 break;
2740 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2741 }
2742 mbs[off] = NUL;
2743 width = cell.width;
2744 attrs = cell.attrs;
2745 fg = cell.fg;
2746 bg = cell.bg;
2747 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002748 dcell = dict_alloc();
2749 list_append_dict(l, dcell);
2750
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002751 dict_add_nr_str(dcell, "chars", 0, mbs);
2752
2753 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002754 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002755 dict_add_nr_str(dcell, "fg", 0, rgb);
2756 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002757 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002758 dict_add_nr_str(dcell, "bg", 0, rgb);
2759
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002760 dict_add_nr_str(dcell, "attr",
2761 cell2attr(attrs, fg, bg), NULL);
2762 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002763
2764 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002765 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002766 ++pos.col;
2767 }
2768}
2769
2770/*
2771 * "term_sendkeys(buf, keys)" function
2772 */
2773 void
2774f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2775{
2776 buf_T *buf = term_get_buf(argvars);
2777 char_u *msg;
2778 term_T *term;
2779
2780 rettv->v_type = VAR_UNKNOWN;
2781 if (buf == NULL)
2782 return;
2783
2784 msg = get_tv_string_chk(&argvars[1]);
2785 if (msg == NULL)
2786 return;
2787 term = buf->b_term;
2788 if (term->tl_vterm == NULL)
2789 return;
2790
2791 while (*msg != NUL)
2792 {
2793 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2794 msg += MB_PTR2LEN(msg);
2795 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002796}
2797
2798/*
2799 * "term_start(command, options)" function
2800 */
2801 void
2802f_term_start(typval_T *argvars, typval_T *rettv)
2803{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002804 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002805
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002806 init_job_options(&opt);
2807 /* TODO: allow more job options */
2808 if (argvars[1].v_type != VAR_UNKNOWN
2809 && get_job_options(&argvars[1], &opt,
2810 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002811 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002812 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002813 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002814 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002815 return;
2816
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002817 if (opt.jo_vertical)
2818 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002819 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002820
2821 if (curbuf->b_term != NULL)
2822 rettv->vval.v_number = curbuf->b_fnum;
2823}
2824
2825/*
2826 * "term_wait" function
2827 */
2828 void
2829f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2830{
2831 buf_T *buf = term_get_buf(argvars);
2832
2833 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002834 {
2835 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002836 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002837 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002838 if (buf->b_term->tl_job == NULL)
2839 {
2840 ch_log(NULL, "term_wait(): no job to wait for");
2841 return;
2842 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002843
2844 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002845 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002846 {
2847 /* The job is dead, keep reading channel I/O until the channel is
2848 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002849 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002850 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2851 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002852 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002853 parse_queued_messages();
2854 ui_delay(10L, FALSE);
2855 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002856 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002857 parse_queued_messages();
2858 }
2859 else
2860 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002861 long wait = 10L;
2862
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002863 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002864 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002865
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002866 /* Wait for some time for any channel I/O. */
2867 if (argvars[1].v_type != VAR_UNKNOWN)
2868 wait = get_tv_number(&argvars[1]);
2869 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002870 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002871
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002872 /* Flushing messages on channels is hopefully sufficient.
2873 * TODO: is there a better way? */
2874 parse_queued_messages();
2875 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002876}
2877
Bram Moolenaara83e3962017-08-17 14:39:07 +02002878# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002879
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002880/**************************************
2881 * 2. MS-Windows implementation.
2882 */
2883
Bram Moolenaara83e3962017-08-17 14:39:07 +02002884# ifndef PROTO
2885
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002886#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2887#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2888
Bram Moolenaar8a773062017-07-24 22:29:21 +02002889void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002890void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002891void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002892BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2893void (*winpty_config_set_initial_size)(void*, int, int);
2894LPCWSTR (*winpty_conin_name)(void*);
2895LPCWSTR (*winpty_conout_name)(void*);
2896LPCWSTR (*winpty_conerr_name)(void*);
2897void (*winpty_free)(void*);
2898void (*winpty_config_free)(void*);
2899void (*winpty_spawn_config_free)(void*);
2900void (*winpty_error_free)(void*);
2901LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002902BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002903HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002904
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002905#define WINPTY_DLL "winpty.dll"
2906
2907static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002908# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002909
Bram Moolenaara83e3962017-08-17 14:39:07 +02002910 static int
2911dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002912{
2913 int i;
2914 static struct
2915 {
2916 char *name;
2917 FARPROC *ptr;
2918 } winpty_entry[] =
2919 {
2920 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2921 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2922 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2923 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2924 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2925 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2926 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2927 {"winpty_free", (FARPROC*)&winpty_free},
2928 {"winpty_open", (FARPROC*)&winpty_open},
2929 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2930 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2931 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2932 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002933 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002934 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002935 {NULL, NULL}
2936 };
2937
2938 /* No need to initialize twice. */
2939 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002940 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002941 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2942 * winpty.dll. */
2943 if (*p_winptydll != NUL)
2944 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2945 if (!hWinPtyDLL)
2946 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002947 if (!hWinPtyDLL)
2948 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002949 if (verbose)
2950 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2951 : (char_u *)WINPTY_DLL);
2952 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002953 }
2954 for (i = 0; winpty_entry[i].name != NULL
2955 && winpty_entry[i].ptr != NULL; ++i)
2956 {
2957 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2958 winpty_entry[i].name)) == NULL)
2959 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002960 if (verbose)
2961 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2962 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002963 }
2964 }
2965
Bram Moolenaara83e3962017-08-17 14:39:07 +02002966 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002967}
2968
2969/*
2970 * Create a new terminal of "rows" by "cols" cells.
2971 * Store a reference in "term".
2972 * Return OK or FAIL.
2973 */
2974 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02002975term_and_job_init(
2976 term_T *term,
2977 int rows,
2978 int cols,
2979 typval_T *argvar,
2980 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002981{
Bram Moolenaar5983d502017-08-20 19:22:56 +02002982 WCHAR *cmd_wchar = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002983 channel_T *channel = NULL;
2984 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002985 DWORD error;
Bram Moolenaar5983d502017-08-20 19:22:56 +02002986 HANDLE jo = NULL;
2987 HANDLE child_process_handle;
2988 HANDLE child_thread_handle;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002989 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002990 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002991 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002992 garray_T ga;
2993 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002994
Bram Moolenaara83e3962017-08-17 14:39:07 +02002995 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002996 return FAIL;
2997
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002998 if (argvar->v_type == VAR_STRING)
2999 cmd = argvar->vval.v_string;
3000 else
3001 {
3002 ga_init2(&ga, (int)sizeof(char*), 20);
3003 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
3004 goto failed;
3005 cmd = ga.ga_data;
3006 }
3007
Bram Moolenaar5983d502017-08-20 19:22:56 +02003008 cmd_wchar = enc_to_utf16(cmd, NULL);
3009 if (cmd_wchar == NULL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003010 return FAIL;
3011
3012 job = job_alloc();
3013 if (job == NULL)
3014 goto failed;
3015
3016 channel = add_channel();
3017 if (channel == NULL)
3018 goto failed;
3019
3020 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
3021 if (term->tl_winpty_config == NULL)
3022 goto failed;
3023
3024 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
3025 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
3026 if (term->tl_winpty == NULL)
3027 goto failed;
3028
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003029 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003030 spawn_config = winpty_spawn_config_new(
3031 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
3032 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
3033 NULL,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003034 cmd_wchar,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003035 NULL,
3036 NULL,
3037 &winpty_err);
3038 if (spawn_config == NULL)
3039 goto failed;
3040
3041 channel = add_channel();
3042 if (channel == NULL)
3043 goto failed;
3044
3045 job = job_alloc();
3046 if (job == NULL)
3047 goto failed;
3048
Bram Moolenaar5983d502017-08-20 19:22:56 +02003049 /* TODO: when all lines are written and the fd is closed, the command
3050 * doesn't get EOF and hangs. */
3051 if (opt->jo_set & JO_IN_BUF)
3052 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
3053
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003054 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
3055 &child_thread_handle, &error, &winpty_err))
3056 goto failed;
3057
3058 channel_set_pipes(channel,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003059 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003060 winpty_conin_name(term->tl_winpty),
3061 GENERIC_WRITE, 0, NULL,
3062 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003063 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003064 winpty_conout_name(term->tl_winpty),
3065 GENERIC_READ, 0, NULL,
3066 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003067 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003068 winpty_conerr_name(term->tl_winpty),
3069 GENERIC_READ, 0, NULL,
3070 OPEN_EXISTING, 0, NULL));
3071
3072 jo = CreateJobObject(NULL, NULL);
3073 if (jo == NULL)
3074 goto failed;
3075
3076 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003077 {
3078 /* Failed, switch the way to terminate process with TerminateProcess. */
3079 CloseHandle(jo);
3080 jo = NULL;
3081 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003082
3083 winpty_spawn_config_free(spawn_config);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003084 vim_free(cmd_wchar);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003085
3086 create_vterm(term, rows, cols);
3087
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02003088 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02003089 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003090
3091 job->jv_channel = channel;
3092 job->jv_proc_info.hProcess = child_process_handle;
3093 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
3094 job->jv_job_object = jo;
3095 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003096 sprintf(buf, "winpty://%lu",
3097 GetProcessId(winpty_agent_process(term->tl_winpty)));
3098 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003099 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003100 term->tl_job = job;
3101
3102 return OK;
3103
3104failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003105 if (argvar->v_type == VAR_LIST)
3106 vim_free(ga.ga_data);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003107 if (cmd_wchar != NULL)
3108 vim_free(cmd_wchar);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003109 if (spawn_config != NULL)
3110 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003111 if (channel != NULL)
3112 channel_clear(channel);
3113 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003114 {
3115 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003116 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003117 }
3118 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003119 if (jo != NULL)
3120 CloseHandle(jo);
3121 if (term->tl_winpty != NULL)
3122 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003123 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003124 if (term->tl_winpty_config != NULL)
3125 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003126 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003127 if (winpty_err != NULL)
3128 {
3129 char_u *msg = utf16_to_enc(
3130 (short_u *)winpty_error_msg(winpty_err), NULL);
3131
3132 EMSG(msg);
3133 winpty_error_free(winpty_err);
3134 }
3135 return FAIL;
3136}
3137
3138/*
3139 * Free the terminal emulator part of "term".
3140 */
3141 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003142term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003143{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003144 if (term->tl_winpty != NULL)
3145 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003146 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003147 if (term->tl_winpty_config != NULL)
3148 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003149 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003150 if (term->tl_vterm != NULL)
3151 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003152 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003153}
3154
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003155/*
3156 * Request size to terminal.
3157 */
3158 static void
3159term_report_winsize(term_T *term, int rows, int cols)
3160{
3161 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3162}
3163
Bram Moolenaara83e3962017-08-17 14:39:07 +02003164 int
3165terminal_enabled(void)
3166{
3167 return dyn_winpty_init(FALSE) == OK;
3168}
3169
3170
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003171# else
3172
3173/**************************************
3174 * 3. Unix-like implementation.
3175 */
3176
3177/*
3178 * Create a new terminal of "rows" by "cols" cells.
3179 * Start job for "cmd".
3180 * Store the pointers in "term".
3181 * Return OK or FAIL.
3182 */
3183 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003184term_and_job_init(
3185 term_T *term,
3186 int rows,
3187 int cols,
3188 typval_T *argvar,
3189 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003190{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003191 create_vterm(term, rows, cols);
3192
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003193 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003194 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003195 if (term->tl_job != NULL)
3196 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003197
Bram Moolenaar61a66052017-07-22 18:39:00 +02003198 return term->tl_job != NULL
3199 && term->tl_job->jv_channel != NULL
3200 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003201}
3202
3203/*
3204 * Free the terminal emulator part of "term".
3205 */
3206 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003207term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003208{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003209 if (term->tl_vterm != NULL)
3210 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003211 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003212}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003213
3214/*
3215 * Request size to terminal.
3216 */
3217 static void
3218term_report_winsize(term_T *term, int rows, int cols)
3219{
3220 /* Use an ioctl() to report the new window size to the job. */
3221 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3222 {
3223 int fd = -1;
3224 int part;
3225
3226 for (part = PART_OUT; part < PART_COUNT; ++part)
3227 {
3228 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3229 if (isatty(fd))
3230 break;
3231 }
3232 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003233 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003234 }
3235}
3236
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003237# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003238
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003239#endif /* FEAT_TERMINAL */