blob: 551fbe70e8044777bf37f5829f22123f5194c223 [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 Moolenaar0cbba822017-08-21 21:39:28 +020041 * - test for writing lines to terminal job does not work on MS-Windows
Bram Moolenaar94053a52017-08-01 21:44:33 +020042 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020043 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020044 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020045 * - support minimal size when 'termsize' is empty?
Bram Moolenaar58302322017-08-22 20:33:53 +020046 * - do not set bufhidden to "hide"? works like a buffer with changes.
47 * document that CTRL-W :hide can be used.
48 * - command argument with spaces doesn't work #1999
49 * :terminal ls dir\ with\ spaces
Bram Moolenaar4f44b882017-08-13 20:06:18 +020050 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020051 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
52 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
53 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
54 * Check that something is connected to the terminal.
55 * Test: "cat" reading from a file or buffer
Bram Moolenaar740c4332017-08-21 22:01:27 +020056 * "ls" writing stdout to a file or buffer
57 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020058 * - For the GUI fill termios with default values, perhaps like pangoterm:
59 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020060 * - support ":term NONE" to open a terminal with a pty but not running a job
61 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020062 * - if the job in the terminal does not support the mouse, we can use the
63 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020064 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
65 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020066 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020067 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020068 * - Copy text in the vterm to the Vim buffer once in a while, so that
69 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020070 */
71
72#include "vim.h"
73
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020074#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020075
Bram Moolenaard5310982017-08-05 15:16:32 +020076#ifndef MIN
77# define MIN(x,y) ((x) < (y) ? (x) : (y))
78#endif
79#ifndef MAX
80# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020081#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020083#include "libvterm/include/vterm.h"
84
Bram Moolenaar33a43be2017-08-06 21:36:22 +020085/* This is VTermScreenCell without the characters, thus much smaller. */
86typedef struct {
87 VTermScreenCellAttrs attrs;
88 char width;
89 VTermColor fg, bg;
90} cellattr_T;
91
Bram Moolenaard85f2712017-07-28 21:51:57 +020092typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020093 int sb_cols; /* can differ per line */
94 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020095} sb_line_T;
96
Bram Moolenaare4f25e42017-07-07 11:54:15 +020097/* typedef term_T in structs.h */
98struct terminal_S {
99 term_T *tl_next;
100
Bram Moolenaar423802d2017-07-30 16:52:24 +0200101 VTerm *tl_vterm;
102 job_T *tl_job;
103 buf_T *tl_buffer;
104
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200105 /* used when tl_job is NULL and only a pty was created */
106 int tl_tty_fd;
107 char_u *tl_tty_name;
108
Bram Moolenaar6d819742017-08-06 14:57:49 +0200109 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200110 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200111 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200112 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200113
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200114#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200115 void *tl_winpty_config;
116 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200117#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200118
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200119 /* last known vterm size */
120 int tl_rows;
121 int tl_cols;
122 /* vterm size does not follow window size */
123 int tl_rows_fixed;
124 int tl_cols_fixed;
125
Bram Moolenaar21554412017-07-24 21:44:43 +0200126 char_u *tl_title; /* NULL or allocated */
127 char_u *tl_status_text; /* NULL or allocated */
128
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200129 /* Range of screen rows to update. Zero based. */
130 int tl_dirty_row_start; /* -1 if nothing dirty */
131 int tl_dirty_row_end; /* row below last one to update */
132
Bram Moolenaard85f2712017-07-28 21:51:57 +0200133 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200134 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200135
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200136 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200137 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200138 int tl_cursor_blink;
139 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
140 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200141
142 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200143};
144
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200145#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
146#define TMODE_LOOP 2 /* CTRL-W N used */
147
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200148/*
149 * List of all active terminals.
150 */
151static term_T *first_term = NULL;
152
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200153/* Terminal active in terminal_loop(). */
154static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200155
156#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
157#define KEY_BUF_LEN 200
158
159/*
160 * Functions with separate implementation for MS-Windows and Unix-like systems.
161 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200162static int term_and_job_init(term_T *term, int rows, int cols,
163 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200164static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200165static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200166
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200167/* The characters that we know (or assume) that the terminal expects for the
168 * backspace and enter keys. */
169static int term_backspace_char = BS;
170static int term_enter_char = CAR;
171static int term_nl_does_cr = FALSE;
172
173
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200174/**************************************
175 * 1. Generic code for all systems.
176 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200177
178/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200179 * Determine the terminal size from 'termsize' and the current window.
180 * Assumes term->tl_rows and term->tl_cols are zero.
181 */
182 static void
183set_term_and_win_size(term_T *term)
184{
185 if (*curwin->w_p_tms != NUL)
186 {
187 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
188
189 term->tl_rows = atoi((char *)curwin->w_p_tms);
190 term->tl_cols = atoi((char *)p);
191 }
192 if (term->tl_rows == 0)
193 term->tl_rows = curwin->w_height;
194 else
195 {
196 win_setheight_win(term->tl_rows, curwin);
197 term->tl_rows_fixed = TRUE;
198 }
199 if (term->tl_cols == 0)
200 term->tl_cols = curwin->w_width;
201 else
202 {
203 win_setwidth_win(term->tl_cols, curwin);
204 term->tl_cols_fixed = TRUE;
205 }
206}
207
208/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200209 * Initialize job options for a terminal job.
210 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200211 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200212 static void
213init_job_options(jobopt_T *opt)
214{
215 clear_job_options(opt);
216
217 opt->jo_mode = MODE_RAW;
218 opt->jo_out_mode = MODE_RAW;
219 opt->jo_err_mode = MODE_RAW;
220 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
221
222 opt->jo_io[PART_OUT] = JIO_BUFFER;
223 opt->jo_io[PART_ERR] = JIO_BUFFER;
224 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
225
226 opt->jo_modifiable[PART_OUT] = 0;
227 opt->jo_modifiable[PART_ERR] = 0;
228 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
229
230 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
231}
232
233/*
234 * Set job options mandatory for a terminal job.
235 */
236 static void
237setup_job_options(jobopt_T *opt, int rows, int cols)
238{
239 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
240 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
241 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200242 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
243 opt->jo_term_rows = rows;
244 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
245 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200246}
247
248 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200249term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251 exarg_T split_ea;
252 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200253 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200254 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200255
256 if (check_restricted() || check_secure())
257 return;
258
259 term = (term_T *)alloc_clear(sizeof(term_T));
260 if (term == NULL)
261 return;
262 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200263 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200264 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200265 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200266 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200267
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200268 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200269 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200270 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200271 /* Create a new buffer in the current window. */
272 if (!can_abandon(curbuf, forceit))
273 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200274 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200275 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200276 return;
277 }
278 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
279 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200280 {
281 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200282 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200283 }
284 }
285 else if (opt->jo_hidden)
286 {
287 buf_T *buf;
288
289 /* Create a new buffer without a window. Make it the current buffer for
290 * a moment to be able to do the initialisations. */
291 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
292 BLN_NEW | BLN_LISTED);
293 if (buf == NULL || ml_open(buf) == FAIL)
294 {
295 vim_free(term);
296 return;
297 }
298 old_curbuf = curbuf;
299 --curbuf->b_nwindows;
300 curbuf = buf;
301 curwin->w_buffer = buf;
302 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200303 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200304 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200305 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200306 /* Open a new window or tab. */
307 split_ea.cmdidx = CMD_new;
308 split_ea.cmd = (char_u *)"new";
309 split_ea.arg = (char_u *)"";
310 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
311 {
312 split_ea.line2 = opt->jo_term_rows;
313 split_ea.addr_count = 1;
314 }
315 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
316 {
317 split_ea.line2 = opt->jo_term_cols;
318 split_ea.addr_count = 1;
319 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200320
Bram Moolenaarda43b612017-08-11 22:27:50 +0200321 ex_splitview(&split_ea);
322 if (curwin == old_curwin)
323 {
324 /* split failed */
325 vim_free(term);
326 return;
327 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200328 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200329 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200330 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200331
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200332 if (!opt->jo_hidden)
333 {
334 /* only one size was taken care of with :new, do the other one */
335 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
336 win_setheight(opt->jo_term_rows);
337 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
338 win_setwidth(opt->jo_term_cols);
339 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200340
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200341 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200342 term->tl_next = first_term;
343 first_term = term;
344
Bram Moolenaar78712a72017-08-05 14:50:12 +0200345 if (opt->jo_term_name != NULL)
346 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
347 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200348 {
349 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200350 size_t len;
351 char_u *cmd, *p;
352
353 if (argvar->v_type == VAR_STRING)
354 cmd = argvar->vval.v_string;
355 else if (argvar->v_type != VAR_LIST
356 || argvar->vval.v_list == NULL
357 || argvar->vval.v_list->lv_len < 1)
358 cmd = (char_u*)"";
359 else
360 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
361
362 len = STRLEN(cmd) + 10;
363 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200364
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200365 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200366 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200367 /* Prepend a ! to the command name to avoid the buffer name equals
368 * the executable, otherwise ":w!" would overwrite it. */
369 if (i == 0)
370 vim_snprintf((char *)p, len, "!%s", cmd);
371 else
372 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200373 if (buflist_findname(p) == NULL)
374 {
375 curbuf->b_ffname = p;
376 break;
377 }
378 }
379 }
380 curbuf->b_fname = curbuf->b_ffname;
381
Bram Moolenaar37c45832017-08-12 16:01:04 +0200382 if (opt->jo_term_opencmd != NULL)
383 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
384
Bram Moolenaareb44a682017-08-03 22:44:55 +0200385 set_string_option_direct((char_u *)"buftype", -1,
386 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
387
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200388 /* Mark the buffer as not modifiable. It can only be made modifiable after
389 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200390 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200391
392 /* Set 'bufhidden' to "hide": allow closing the window. */
393 set_string_option_direct((char_u *)"bufhidden", -1,
394 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200395
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200396 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200397 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200398
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200399 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200400 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt)
401 == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200402 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200403 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200404 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200405 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200406
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200407 /* Make sure we don't get stuck on sending keys to the job, it leads to
408 * a deadlock if the job is waiting for Vim to read. */
409 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
410
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200411 if (old_curbuf != NULL)
412 {
413 --curbuf->b_nwindows;
414 curbuf = old_curbuf;
415 curwin->w_buffer = curbuf;
416 ++curbuf->b_nwindows;
417 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200418 }
419 else
420 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200421 buf_T *buf = curbuf;
422
Bram Moolenaard85f2712017-07-28 21:51:57 +0200423 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200424 if (old_curbuf != NULL)
425 {
426 --curbuf->b_nwindows;
427 curbuf = old_curbuf;
428 curwin->w_buffer = curbuf;
429 ++curbuf->b_nwindows;
430 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200431
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200432 /* Wiping out the buffer will also close the window and call
433 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200434 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200435 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200436}
437
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200438/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200439 * ":terminal": open a terminal window and execute a job in it.
440 */
441 void
442ex_terminal(exarg_T *eap)
443{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200444 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200445 jobopt_T opt;
446 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200447 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200448
449 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200450
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200451 cmd = eap->arg;
452 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
453 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200454 char_u *p, *ep;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200455
456 cmd += 2;
457 p = skiptowhite(cmd);
Bram Moolenaarb2412082017-08-20 18:09:14 +0200458 ep = vim_strchr(cmd, '=');
459 if (ep != NULL && ep < p)
460 p = ep;
461
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200462 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
463 opt.jo_term_finish = 'c';
464 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
465 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200466 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
467 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200468 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
469 opt.jo_hidden = 1;
Bram Moolenaarb2412082017-08-20 18:09:14 +0200470 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
471 && ep != NULL && isdigit(ep[1]))
472 {
473 opt.jo_set2 |= JO2_TERM_ROWS;
474 opt.jo_term_rows = atoi((char *)ep + 1);
475 p = skiptowhite(cmd);
476 }
477 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
478 && ep != NULL && isdigit(ep[1]))
479 {
480 opt.jo_set2 |= JO2_TERM_COLS;
481 opt.jo_term_cols = atoi((char *)ep + 1);
482 p = skiptowhite(cmd);
483 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200484 else
485 {
486 if (*p)
487 *p = NUL;
488 EMSG2(_("E181: Invalid attribute: %s"), cmd);
489 return;
490 }
491 cmd = skipwhite(p);
492 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200493 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200494 /* Make a copy, an autocommand may set 'shell'. */
495 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200496
Bram Moolenaarb2412082017-08-20 18:09:14 +0200497 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200498 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200499 /* Write lines from current buffer to the job. */
500 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
501 opt.jo_io[PART_IN] = JIO_BUFFER;
502 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
503 opt.jo_in_top = eap->line1;
504 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200505 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200506
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200507 argvar.v_type = VAR_STRING;
508 argvar.vval.v_string = cmd;
509 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200510 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200511}
512
513/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200514 * Free the scrollback buffer for "term".
515 */
516 static void
517free_scrollback(term_T *term)
518{
519 int i;
520
521 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
522 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
523 ga_clear(&term->tl_scrollback);
524}
525
526/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200527 * Free a terminal and everything it refers to.
528 * Kills the job if there is one.
529 * Called when wiping out a buffer.
530 */
531 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200532free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200533{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200534 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200535 term_T *tp;
536
537 if (term == NULL)
538 return;
539 if (first_term == term)
540 first_term = term->tl_next;
541 else
542 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
543 if (tp->tl_next == term)
544 {
545 tp->tl_next = term->tl_next;
546 break;
547 }
548
549 if (term->tl_job != NULL)
550 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200551 if (term->tl_job->jv_status != JOB_ENDED
552 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200553 job_stop(term->tl_job, NULL, "kill");
554 job_unref(term->tl_job);
555 }
556
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200557 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200558
559 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200560 vim_free(term->tl_title);
561 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200562 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200563 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200564 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200565 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200566 if (in_terminal_loop == term)
567 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200568}
569
570/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200571 * Write job output "msg[len]" to the vterm.
572 */
573 static void
574term_write_job_output(term_T *term, char_u *msg, size_t len)
575{
576 VTerm *vterm = term->tl_vterm;
577 char_u *p;
578 size_t done;
579 size_t len_now;
580
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200581 if (term_nl_does_cr)
582 vterm_input_write(vterm, (char *)msg, len);
583 else
584 /* need to convert NL to CR-NL */
585 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200586 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200587 for (p = msg + done; p < msg + len; )
588 {
589 if (*p == NL)
590 break;
591 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
592 }
593 len_now = p - msg - done;
594 vterm_input_write(vterm, (char *)msg + done, len_now);
595 if (p < msg + len && *p == NL)
596 {
597 vterm_input_write(vterm, "\r\n", 2);
598 ++len_now;
599 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200600 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200601
602 /* this invokes the damage callbacks */
603 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
604}
605
Bram Moolenaar1c844932017-07-24 23:36:41 +0200606 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200607update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200608{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200609 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200610 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200611 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200612 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200613 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200614 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200615 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200616 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200617#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200618 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200619 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200620#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200621 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200622}
623
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200624/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200625 * Invoked when "msg" output from a job was received. Write it to the terminal
626 * of "buffer".
627 */
628 void
629write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
630{
631 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200632 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200633
Bram Moolenaard85f2712017-07-28 21:51:57 +0200634 if (term->tl_vterm == NULL)
635 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200636 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200637 return;
638 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200639 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200640 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200641
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200642 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
643 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200644 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200645 {
646 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200647 ch_log(term->tl_job->jv_channel, "updating screen");
648 if (buffer == curbuf)
649 {
650 update_screen(0);
651 update_cursor(term, TRUE);
652 }
653 else
654 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200655 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200656}
657
658/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200659 * Send a mouse position and click to the vterm
660 */
661 static int
662term_send_mouse(VTerm *vterm, int button, int pressed)
663{
664 VTermModifier mod = VTERM_MOD_NONE;
665
666 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
667 mouse_col - W_WINCOL(curwin), mod);
668 vterm_mouse_button(vterm, button, pressed, mod);
669 return TRUE;
670}
671
672/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200673 * Convert typed key "c" into bytes to send to the job.
674 * Return the number of bytes in "buf".
675 */
676 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200677term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200678{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200679 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200680 VTermKey key = VTERM_KEY_NONE;
681 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200682 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200683
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200684 switch (c)
685 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200686 case CAR: c = term_enter_char; break;
687 /* don't use VTERM_KEY_BACKSPACE, it always
688 * becomes 0x7f DEL */
689 case K_BS: c = term_backspace_char; break;
690
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200691 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200692 case K_DEL: key = VTERM_KEY_DEL; break;
693 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200694 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
695 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200696 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200697 case K_S_END: mod = VTERM_MOD_SHIFT;
698 key = VTERM_KEY_END; break;
699 case K_C_END: mod = VTERM_MOD_CTRL;
700 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200701 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
702 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
703 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
704 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
705 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
706 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
707 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
708 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
709 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
710 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
711 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
712 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
713 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200714 case K_S_HOME: mod = VTERM_MOD_SHIFT;
715 key = VTERM_KEY_HOME; break;
716 case K_C_HOME: mod = VTERM_MOD_CTRL;
717 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200718 case K_INS: key = VTERM_KEY_INS; break;
719 case K_K0: key = VTERM_KEY_KP_0; break;
720 case K_K1: key = VTERM_KEY_KP_1; break;
721 case K_K2: key = VTERM_KEY_KP_2; break;
722 case K_K3: key = VTERM_KEY_KP_3; break;
723 case K_K4: key = VTERM_KEY_KP_4; break;
724 case K_K5: key = VTERM_KEY_KP_5; break;
725 case K_K6: key = VTERM_KEY_KP_6; break;
726 case K_K7: key = VTERM_KEY_KP_7; break;
727 case K_K8: key = VTERM_KEY_KP_8; break;
728 case K_K9: key = VTERM_KEY_KP_9; break;
729 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
730 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
731 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
732 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
733 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
734 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
735 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
736 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
737 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
738 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
739 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
740 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
741 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200742 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
743 key = VTERM_KEY_LEFT; break;
744 case K_C_LEFT: mod = VTERM_MOD_CTRL;
745 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200746 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
747 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
748 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200749 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
750 key = VTERM_KEY_RIGHT; break;
751 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
752 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200753 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200754 case K_S_UP: mod = VTERM_MOD_SHIFT;
755 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200756 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200757
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200758 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
759 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
760 case K_MOUSELEFT: /* TODO */ return 0;
761 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200762
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200763 case K_LEFTMOUSE:
764 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
765 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
766 case K_LEFTRELEASE:
767 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
768 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
769 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
770 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
771 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
772 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
773 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
774 case K_X1MOUSE: /* TODO */ return 0;
775 case K_X1DRAG: /* TODO */ return 0;
776 case K_X1RELEASE: /* TODO */ return 0;
777 case K_X2MOUSE: /* TODO */ return 0;
778 case K_X2DRAG: /* TODO */ return 0;
779 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200780
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200781 case K_IGNORE: return 0;
782 case K_NOP: return 0;
783 case K_UNDO: return 0;
784 case K_HELP: return 0;
785 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
786 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
787 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
788 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
789 case K_SELECT: return 0;
790#ifdef FEAT_GUI
791 case K_VER_SCROLLBAR: return 0;
792 case K_HOR_SCROLLBAR: return 0;
793#endif
794#ifdef FEAT_GUI_TABLINE
795 case K_TABLINE: return 0;
796 case K_TABMENU: return 0;
797#endif
798#ifdef FEAT_NETBEANS_INTG
799 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
800#endif
801#ifdef FEAT_DND
802 case K_DROP: return 0;
803#endif
804#ifdef FEAT_AUTOCMD
805 case K_CURSORHOLD: return 0;
806#endif
807 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
808 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200809 }
810
811 /*
812 * Convert special keys to vterm keys:
813 * - Write keys to vterm: vterm_keyboard_key()
814 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200815 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200816 */
817 if (key != VTERM_KEY_NONE)
818 /* Special key, let vterm convert it. */
819 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200820 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200821 /* Normal character, let vterm convert it. */
822 vterm_keyboard_unichar(vterm, c, mod);
823
824 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200825 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200826}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200827
Bram Moolenaar938783d2017-07-16 20:13:26 +0200828/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200829 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200830 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200831 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200832term_job_running(term_T *term)
833{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200834 /* Also consider the job finished when the channel is closed, to avoid a
835 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200836 return term != NULL
837 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200838 && term->tl_job->jv_status == JOB_STARTED
839 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200840}
841
842/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200843 * Add the last line of the scrollback buffer to the buffer in the window.
844 */
845 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200846add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200847{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200848 buf_T *buf = term->tl_buffer;
849 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
850 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200851
Bram Moolenaar58302322017-08-22 20:33:53 +0200852#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +0200853 if (!enc_utf8 && enc_codepage > 0)
854 {
855 WCHAR *ret = NULL;
856 int length = 0;
857
858 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
859 &ret, &length);
860 if (ret != NULL)
861 {
862 WideCharToMultiByte_alloc(enc_codepage, 0,
863 ret, length, (char **)&text, &len, 0, 0);
864 vim_free(ret);
865 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
866 vim_free(text);
867 }
868 }
869 else
870#endif
871 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200872 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200873 {
874 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200875 curbuf = buf;
876 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200877 curbuf = curwin->w_buffer;
878 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200879}
880
881/*
882 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200883 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200884 */
885 static void
886move_terminal_to_buffer(term_T *term)
887{
888 win_T *wp;
889 int len;
890 int lines_skipped = 0;
891 VTermPos pos;
892 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200893 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200894 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200895
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200896 if (term->tl_vterm == NULL)
897 return;
898 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200899 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
900 {
901 len = 0;
902 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
903 if (vterm_screen_get_cell(screen, pos, &cell) != 0
904 && cell.chars[0] != NUL)
905 len = pos.col + 1;
906
907 if (len == 0)
908 ++lines_skipped;
909 else
910 {
911 while (lines_skipped > 0)
912 {
913 /* Line was skipped, add an empty line. */
914 --lines_skipped;
915 if (ga_grow(&term->tl_scrollback, 1) == OK)
916 {
917 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
918 + term->tl_scrollback.ga_len;
919
920 line->sb_cols = 0;
921 line->sb_cells = NULL;
922 ++term->tl_scrollback.ga_len;
923
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200924 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200925 }
926 }
927
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200928 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200929 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
930 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200931 garray_T ga;
932 int width;
933 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200934 + term->tl_scrollback.ga_len;
935
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200936 ga_init2(&ga, 1, 100);
937 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200938 {
939 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200940 {
941 width = 1;
942 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
943 if (ga_grow(&ga, 1) == OK)
944 ga.ga_len += mb_char2bytes(' ',
945 (char_u *)ga.ga_data + ga.ga_len);
946 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200947 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200948 {
949 width = cell.width;
950
951 p[pos.col].width = cell.width;
952 p[pos.col].attrs = cell.attrs;
953 p[pos.col].fg = cell.fg;
954 p[pos.col].bg = cell.bg;
955
956 if (ga_grow(&ga, MB_MAXBYTES) == OK)
957 {
958 int i;
959 int c;
960
961 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +0200962 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200963 (char_u *)ga.ga_data + ga.ga_len);
964 }
965 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200966 }
967 line->sb_cols = len;
968 line->sb_cells = p;
969 ++term->tl_scrollback.ga_len;
970
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200971 if (ga_grow(&ga, 1) == FAIL)
972 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
973 else
974 {
975 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
976 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
977 }
978 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200979 }
980 else
981 vim_free(p);
982 }
983 }
984
985 FOR_ALL_WINDOWS(wp)
986 {
987 if (wp->w_buffer == term->tl_buffer)
988 {
989 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
990 wp->w_cursor.col = 0;
991 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200992 if (wp->w_cursor.lnum >= wp->w_height)
993 {
994 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
995
996 if (wp->w_topline < min_topline)
997 wp->w_topline = min_topline;
998 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200999 redraw_win_later(wp, NOT_VALID);
1000 }
1001 }
1002}
1003
1004 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001005set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001006{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001007 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001008 vim_free(term->tl_status_text);
1009 term->tl_status_text = NULL;
1010 if (term->tl_buffer == curbuf)
1011 maketitle();
1012}
1013
1014/*
1015 * Called after the job if finished and Terminal mode is not active:
1016 * Move the vterm contents into the scrollback buffer and free the vterm.
1017 */
1018 static void
1019cleanup_vterm(term_T *term)
1020{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001021 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001022 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001023 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001024 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001025}
1026
1027/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001028 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001029 * Suspends updating the terminal window.
1030 */
1031 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001032term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001033{
1034 term_T *term = curbuf->b_term;
1035
1036 /* Append the current terminal contents to the buffer. */
1037 move_terminal_to_buffer(term);
1038
Bram Moolenaar6d819742017-08-06 14:57:49 +02001039 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001040
Bram Moolenaar6d819742017-08-06 14:57:49 +02001041 /* Move the window cursor to the position of the cursor in the
1042 * terminal. */
1043 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1044 + term->tl_cursor_pos.row + 1;
1045 check_cursor();
1046 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001047
Bram Moolenaar6d819742017-08-06 14:57:49 +02001048 /* Display the same lines as in the terminal. */
1049 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001050}
1051
1052/*
1053 * Returns TRUE if the current window contains a terminal and we are in
1054 * Terminal-Normal mode.
1055 */
1056 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001057term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001058{
1059 term_T *term = curbuf->b_term;
1060
Bram Moolenaar6d819742017-08-06 14:57:49 +02001061 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001062}
1063
1064/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001065 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001066 * Restores updating the terminal window.
1067 */
1068 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001069term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001070{
1071 term_T *term = curbuf->b_term;
1072 sb_line_T *line;
1073 garray_T *gap;
1074
1075 /* Remove the terminal contents from the scrollback and the buffer. */
1076 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001077 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1078 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001079 {
1080 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1081 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1082 vim_free(line->sb_cells);
1083 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001084 }
1085 check_cursor();
1086
Bram Moolenaar6d819742017-08-06 14:57:49 +02001087 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001088
1089 if (term->tl_channel_closed)
1090 cleanup_vterm(term);
1091 redraw_buf_and_status_later(curbuf, NOT_VALID);
1092}
1093
1094/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001095 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001096 * Note: while waiting a terminal may be closed and freed if the channel is
1097 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001098 * TODO: use terminal mode mappings.
1099 */
1100 static int
1101term_vgetc()
1102{
1103 int c;
1104
1105 ++no_mapping;
1106 ++allow_keys;
1107 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001108#ifdef WIN3264
1109 ctrl_break_was_pressed = FALSE;
1110#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001111 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001112 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001113 --no_mapping;
1114 --allow_keys;
1115 return c;
1116}
1117
1118/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001119 * Get the part that is connected to the tty. Normally this is PART_IN, but
1120 * when writing buffer lines to the job it can be another. This makes it
1121 * possible to do "1,5term vim -".
1122 */
1123 static ch_part_T
1124get_tty_part(term_T *term)
1125{
1126#ifdef UNIX
1127 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1128 int i;
1129
1130 for (i = 0; i < 3; ++i)
1131 {
1132 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1133
1134 if (isatty(fd))
1135 return parts[i];
1136 }
1137#endif
1138 return PART_IN;
1139}
1140
1141/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001142 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001143 * Return FAIL when the key needs to be handled in Normal mode.
1144 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001145 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001146 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001147send_keys_to_term(term_T *term, int c, int typed)
1148{
1149 char msg[KEY_BUF_LEN];
1150 size_t len;
1151 static int mouse_was_outside = FALSE;
1152 int dragging_outside = FALSE;
1153
1154 /* Catch keys that need to be handled as in Normal mode. */
1155 switch (c)
1156 {
1157 case NUL:
1158 case K_ZERO:
1159 if (typed)
1160 stuffcharReadbuff(c);
1161 return FAIL;
1162
1163 case K_IGNORE:
1164 return FAIL;
1165
1166 case K_LEFTDRAG:
1167 case K_MIDDLEDRAG:
1168 case K_RIGHTDRAG:
1169 case K_X1DRAG:
1170 case K_X2DRAG:
1171 dragging_outside = mouse_was_outside;
1172 /* FALLTHROUGH */
1173 case K_LEFTMOUSE:
1174 case K_LEFTMOUSE_NM:
1175 case K_LEFTRELEASE:
1176 case K_LEFTRELEASE_NM:
1177 case K_MIDDLEMOUSE:
1178 case K_MIDDLERELEASE:
1179 case K_RIGHTMOUSE:
1180 case K_RIGHTRELEASE:
1181 case K_X1MOUSE:
1182 case K_X1RELEASE:
1183 case K_X2MOUSE:
1184 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001185
1186 case K_MOUSEUP:
1187 case K_MOUSEDOWN:
1188 case K_MOUSELEFT:
1189 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001190 if (mouse_row < W_WINROW(curwin)
1191 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1192 || mouse_col < W_WINCOL(curwin)
1193 || mouse_col >= W_ENDCOL(curwin)
1194 || dragging_outside)
1195 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001196 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001197 if (typed)
1198 {
1199 stuffcharReadbuff(c);
1200 mouse_was_outside = TRUE;
1201 }
1202 return FAIL;
1203 }
1204 }
1205 if (typed)
1206 mouse_was_outside = FALSE;
1207
1208 /* Convert the typed key to a sequence of bytes for the job. */
1209 len = term_convert_key(term, c, msg);
1210 if (len > 0)
1211 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001212 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1213 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001214
1215 return OK;
1216}
1217
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001218 static void
1219position_cursor(win_T *wp, VTermPos *pos)
1220{
1221 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1222 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1223 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1224}
1225
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001226/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001227 * Handle CTRL-W "": send register contents to the job.
1228 */
1229 static void
1230term_paste_register(int prev_c UNUSED)
1231{
1232 int c;
1233 list_T *l;
1234 listitem_T *item;
1235 long reglen = 0;
1236 int type;
1237
1238#ifdef FEAT_CMDL_INFO
1239 if (add_to_showcmd(prev_c))
1240 if (add_to_showcmd('"'))
1241 out_flush();
1242#endif
1243 c = term_vgetc();
1244#ifdef FEAT_CMDL_INFO
1245 clear_showcmd();
1246#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001247 if (!term_use_loop())
1248 /* job finished while waiting for a character */
1249 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001250
1251 /* CTRL-W "= prompt for expression to evaluate. */
1252 if (c == '=' && get_expr_register() != '=')
1253 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001254 if (!term_use_loop())
1255 /* job finished while waiting for a character */
1256 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001257
1258 l = (list_T *)get_reg_contents(c, GREG_LIST);
1259 if (l != NULL)
1260 {
1261 type = get_reg_type(c, &reglen);
1262 for (item = l->lv_first; item != NULL; item = item->li_next)
1263 {
1264 char_u *s = get_tv_string(&item->li_tv);
1265
1266 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1267 s, STRLEN(s), NULL);
1268 if (item->li_next != NULL || type == MLINE)
1269 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1270 (char_u *)"\r", 1, NULL);
1271 }
1272 list_free(l);
1273 }
1274}
1275
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001276#if defined(FEAT_GUI) || defined(PROTO)
1277/*
1278 * Return TRUE when the cursor of the terminal should be displayed.
1279 */
1280 int
1281use_terminal_cursor()
1282{
1283 return in_terminal_loop != NULL;
1284}
1285
1286 cursorentry_T *
1287term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1288{
1289 term_T *term = in_terminal_loop;
1290 static cursorentry_T entry;
1291
1292 vim_memset(&entry, 0, sizeof(entry));
1293 entry.shape = entry.mshape =
1294 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1295 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1296 SHAPE_BLOCK;
1297 entry.percentage = 20;
1298 if (term->tl_cursor_blink)
1299 {
1300 entry.blinkwait = 700;
1301 entry.blinkon = 400;
Bram Moolenaar58302322017-08-22 20:33:53 +02001302 entry.blinkoff = 250;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001303 }
1304 *fg = gui.back_pixel;
1305 if (term->tl_cursor_color == NULL)
1306 *bg = gui.norm_pixel;
1307 else
1308 *bg = color_name2handle(term->tl_cursor_color);
1309 entry.name = "n";
1310 entry.used_for = SHAPE_CURSOR;
1311
1312 return &entry;
1313}
1314#endif
1315
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001316static int did_change_cursor = FALSE;
1317
1318 static void
1319may_set_cursor_props(term_T *term)
1320{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001321#ifdef FEAT_GUI
1322 /* For the GUI the cursor properties are obtained with
1323 * term_get_cursor_shape(). */
1324 if (gui.in_use)
1325 return;
1326#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001327 if (in_terminal_loop == term)
1328 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001329 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001330 if (term->tl_cursor_color != NULL)
1331 term_cursor_color(term->tl_cursor_color);
1332 else
1333 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001334 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1335 }
1336}
1337
1338 static void
1339may_restore_cursor_props(void)
1340{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001341#ifdef FEAT_GUI
1342 if (gui.in_use)
1343 return;
1344#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001345 if (did_change_cursor)
1346 {
1347 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001348 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001349 /* this will restore the initial cursor style, if possible */
1350 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001351 }
1352}
1353
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001354/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001355 * Returns TRUE if the current window contains a terminal and we are sending
1356 * keys to the job.
1357 */
1358 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001359term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001360{
1361 term_T *term = curbuf->b_term;
1362
1363 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001364 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001365 && term->tl_vterm != NULL
1366 && term_job_running(term);
1367}
1368
1369/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001370 * Wait for input and send it to the job.
1371 * Return when the start of a CTRL-W command is typed or anything else that
1372 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001373 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1374 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001375 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001376 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001377terminal_loop(void)
1378{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001379 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001380 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001381 int ret;
1382
Bram Moolenaar679653e2017-08-13 14:13:19 +02001383 /* Remember the terminal we are sending keys to. However, the terminal
1384 * might be closed while waiting for a character, e.g. typing "exit" in a
1385 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1386 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001387 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001388
1389 if (*curwin->w_p_tk != NUL)
1390 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001391 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001392 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001393
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001394#ifdef UNIX
1395 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001396 int part = get_tty_part(curbuf->b_term);
1397 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001398
1399 if (isatty(fd))
1400 {
1401 ttyinfo_T info;
1402
1403 /* Get the current backspace and enter characters of the pty. */
1404 if (get_tty_info(fd, &info) == OK)
1405 {
1406 term_backspace_char = info.backspace;
1407 term_enter_char = info.enter;
1408 term_nl_does_cr = info.nl_does_cr;
1409 }
1410 }
1411 }
1412#endif
1413
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001414 for (;;)
1415 {
1416 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001417 /* Repeat redrawing in case a message is received while redrawing. */
1418 while (curwin->w_redr_type != 0)
1419 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001420 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001421
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001422 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001423 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001424 /* job finished while waiting for a character */
1425 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001426 if (c == K_IGNORE)
1427 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001428
Bram Moolenaarfae42832017-08-01 22:24:26 +02001429#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001430 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001431 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001432 if (ctrl_break_was_pressed)
1433 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001434#endif
1435
Bram Moolenaar69198192017-08-05 14:10:48 +02001436 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001437 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001438 int prev_c = c;
1439
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001440#ifdef FEAT_CMDL_INFO
1441 if (add_to_showcmd(c))
1442 out_flush();
1443#endif
1444 c = term_vgetc();
1445#ifdef FEAT_CMDL_INFO
1446 clear_showcmd();
1447#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001448 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001449 /* job finished while waiting for a character */
1450 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001451
Bram Moolenaar69198192017-08-05 14:10:48 +02001452 if (prev_c == Ctrl_BSL)
1453 {
1454 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001455 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001456 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1457 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001458 ret = FAIL;
1459 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001460 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001461 /* Send both keys to the terminal. */
1462 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1463 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001464 else if (c == Ctrl_C)
1465 {
1466 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1467 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1468 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001469 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001470 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001471 /* "CTRL-W .": send CTRL-W to the job */
1472 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001473 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001474 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001475 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001476 /* CTRL-W N : go to Terminal-Normal mode. */
1477 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001478 ret = FAIL;
1479 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001480 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001481 else if (c == '"')
1482 {
1483 term_paste_register(prev_c);
1484 continue;
1485 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001486 else if (termkey == 0 || c != termkey)
1487 {
1488 stuffcharReadbuff(Ctrl_W);
1489 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001490 ret = OK;
1491 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001492 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001493 }
Bram Moolenaar58302322017-08-22 20:33:53 +02001494# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02001495 if (!enc_utf8 && has_mbyte && c >= 0x80)
1496 {
1497 WCHAR wc;
1498 char_u mb[3];
1499
1500 mb[0] = (unsigned)c >> 8;
1501 mb[1] = c;
1502 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1503 c = wc;
1504 }
1505# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001506 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001507 {
1508 ret = OK;
1509 goto theend;
1510 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001511 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001512 ret = FAIL;
1513
1514theend:
1515 in_terminal_loop = NULL;
1516 may_restore_cursor_props();
1517 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001518}
1519
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001520/*
1521 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001522 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001523 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001524 */
1525 void
1526term_job_ended(job_T *job)
1527{
1528 term_T *term;
1529 int did_one = FALSE;
1530
1531 for (term = first_term; term != NULL; term = term->tl_next)
1532 if (term->tl_job == job)
1533 {
1534 vim_free(term->tl_title);
1535 term->tl_title = NULL;
1536 vim_free(term->tl_status_text);
1537 term->tl_status_text = NULL;
1538 redraw_buf_and_status_later(term->tl_buffer, VALID);
1539 did_one = TRUE;
1540 }
1541 if (did_one)
1542 redraw_statuslines();
1543 if (curbuf->b_term != NULL)
1544 {
1545 if (curbuf->b_term->tl_job == job)
1546 maketitle();
1547 update_cursor(curbuf->b_term, TRUE);
1548 }
1549}
1550
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001551 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001552may_toggle_cursor(term_T *term)
1553{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001554 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001555 {
1556 if (term->tl_cursor_visible)
1557 cursor_on();
1558 else
1559 cursor_off();
1560 }
1561}
1562
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001563/*
1564 * Reverse engineer the RGB value into a cterm color index.
1565 * First color is 1. Return 0 if no match found.
1566 */
1567 static int
1568color2index(VTermColor *color, int fg, int *boldp)
1569{
1570 int red = color->red;
1571 int blue = color->blue;
1572 int green = color->green;
1573
1574 /* The argument for lookup_color() is for the color_names[] table. */
1575 if (red == 0)
1576 {
1577 if (green == 0)
1578 {
1579 if (blue == 0)
1580 return lookup_color(0, fg, boldp) + 1; /* black */
1581 if (blue == 224)
1582 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1583 }
1584 else if (green == 224)
1585 {
1586 if (blue == 0)
1587 return lookup_color(2, fg, boldp) + 1; /* dark green */
1588 if (blue == 224)
1589 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1590 }
1591 }
1592 else if (red == 224)
1593 {
1594 if (green == 0)
1595 {
1596 if (blue == 0)
1597 return lookup_color(4, fg, boldp) + 1; /* dark red */
1598 if (blue == 224)
1599 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1600 }
1601 else if (green == 224)
1602 {
1603 if (blue == 0)
1604 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1605 if (blue == 224)
1606 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1607 }
1608 }
1609 else if (red == 128)
1610 {
1611 if (green == 128 && blue == 128)
1612 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1613 }
1614 else if (red == 255)
1615 {
1616 if (green == 64)
1617 {
1618 if (blue == 64)
1619 return lookup_color(20, fg, boldp) + 1; /* light red */
1620 if (blue == 255)
1621 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1622 }
1623 else if (green == 255)
1624 {
1625 if (blue == 64)
1626 return lookup_color(24, fg, boldp) + 1; /* yellow */
1627 if (blue == 255)
1628 return lookup_color(26, fg, boldp) + 1; /* white */
1629 }
1630 }
1631 else if (red == 64)
1632 {
1633 if (green == 64)
1634 {
1635 if (blue == 255)
1636 return lookup_color(14, fg, boldp) + 1; /* light blue */
1637 }
1638 else if (green == 255)
1639 {
1640 if (blue == 64)
1641 return lookup_color(16, fg, boldp) + 1; /* light green */
1642 if (blue == 255)
1643 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1644 }
1645 }
1646 if (t_colors >= 256)
1647 {
1648 if (red == blue && red == green)
1649 {
1650 /* 24-color greyscale */
1651 static int cutoff[23] = {
1652 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1653 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1654 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1655 int i;
1656
1657 for (i = 0; i < 23; ++i)
1658 if (red < cutoff[i])
1659 return i + 233;
1660 return 256;
1661 }
1662
1663 /* 216-color cube */
1664 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001665 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001666 + (blue + 25) / 0x33;
1667 }
1668 return 0;
1669}
1670
1671/*
1672 * Convert the attributes of a vterm cell into an attribute index.
1673 */
1674 static int
1675cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1676{
1677 int attr = 0;
1678
1679 if (cellattrs.bold)
1680 attr |= HL_BOLD;
1681 if (cellattrs.underline)
1682 attr |= HL_UNDERLINE;
1683 if (cellattrs.italic)
1684 attr |= HL_ITALIC;
1685 if (cellattrs.strike)
1686 attr |= HL_STANDOUT;
1687 if (cellattrs.reverse)
1688 attr |= HL_INVERSE;
1689
1690#ifdef FEAT_GUI
1691 if (gui.in_use)
1692 {
1693 guicolor_T fg, bg;
1694
1695 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1696 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1697 return get_gui_attr_idx(attr, fg, bg);
1698 }
1699 else
1700#endif
1701#ifdef FEAT_TERMGUICOLORS
1702 if (p_tgc)
1703 {
1704 guicolor_T fg, bg;
1705
1706 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1707 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1708
1709 return get_tgc_attr_idx(attr, fg, bg);
1710 }
1711 else
1712#endif
1713 {
1714 int bold = MAYBE;
1715 int fg = color2index(&cellfg, TRUE, &bold);
1716 int bg = color2index(&cellbg, FALSE, &bold);
1717
1718 /* with 8 colors set the bold attribute to get a bright foreground */
1719 if (bold == TRUE)
1720 attr |= HL_BOLD;
1721 return get_cterm_attr_idx(attr, fg, bg);
1722 }
1723 return 0;
1724}
1725
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001726 static int
1727handle_damage(VTermRect rect, void *user)
1728{
1729 term_T *term = (term_T *)user;
1730
1731 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1732 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1733 redraw_buf_later(term->tl_buffer, NOT_VALID);
1734 return 1;
1735}
1736
1737 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001738handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001739{
1740 term_T *term = (term_T *)user;
1741
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001742 /* Scrolling up is done much more efficiently by deleting lines instead of
1743 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001744 if (dest.start_col == src.start_col
1745 && dest.end_col == src.end_col
1746 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001747 {
1748 win_T *wp;
1749 VTermColor fg, bg;
1750 VTermScreenCellAttrs attr;
1751 int clear_attr;
1752
1753 /* Set the color to clear lines with. */
1754 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1755 &fg, &bg);
1756 vim_memset(&attr, 0, sizeof(attr));
1757 clear_attr = cell2attr(attr, fg, bg);
1758
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001759 FOR_ALL_WINDOWS(wp)
1760 {
1761 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001762 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001763 src.start_row - dest.start_row, FALSE, FALSE,
1764 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001765 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001766 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001767 redraw_buf_later(term->tl_buffer, NOT_VALID);
1768 return 1;
1769}
1770
1771 static int
1772handle_movecursor(
1773 VTermPos pos,
1774 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001775 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001776 void *user)
1777{
1778 term_T *term = (term_T *)user;
1779 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001780
1781 term->tl_cursor_pos = pos;
1782 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001783
1784 FOR_ALL_WINDOWS(wp)
1785 {
1786 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001787 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001788 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001789 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001790 {
1791 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001792 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001793 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001794
1795 return 1;
1796}
1797
Bram Moolenaar21554412017-07-24 21:44:43 +02001798 static int
1799handle_settermprop(
1800 VTermProp prop,
1801 VTermValue *value,
1802 void *user)
1803{
1804 term_T *term = (term_T *)user;
1805
1806 switch (prop)
1807 {
1808 case VTERM_PROP_TITLE:
1809 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001810 /* a blank title isn't useful, make it empty, so that "running" is
1811 * displayed */
1812 if (*skipwhite((char_u *)value->string) == NUL)
1813 term->tl_title = NULL;
1814 else
1815 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001816 vim_free(term->tl_status_text);
1817 term->tl_status_text = NULL;
1818 if (term == curbuf->b_term)
1819 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001820 break;
1821
1822 case VTERM_PROP_CURSORVISIBLE:
1823 term->tl_cursor_visible = value->boolean;
1824 may_toggle_cursor(term);
1825 out_flush();
1826 break;
1827
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001828 case VTERM_PROP_CURSORBLINK:
1829 term->tl_cursor_blink = value->boolean;
1830 may_set_cursor_props(term);
1831 break;
1832
1833 case VTERM_PROP_CURSORSHAPE:
1834 term->tl_cursor_shape = value->number;
1835 may_set_cursor_props(term);
1836 break;
1837
1838 case VTERM_PROP_CURSORCOLOR:
1839 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001840 if (*value->string == NUL)
1841 term->tl_cursor_color = NULL;
1842 else
1843 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001844 may_set_cursor_props(term);
1845 break;
1846
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001847 case VTERM_PROP_ALTSCREEN:
1848 /* TODO: do anything else? */
1849 term->tl_using_altscreen = value->boolean;
1850 break;
1851
Bram Moolenaar21554412017-07-24 21:44:43 +02001852 default:
1853 break;
1854 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001855 /* Always return 1, otherwise vterm doesn't store the value internally. */
1856 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001857}
1858
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001859/*
1860 * The job running in the terminal resized the terminal.
1861 */
1862 static int
1863handle_resize(int rows, int cols, void *user)
1864{
1865 term_T *term = (term_T *)user;
1866 win_T *wp;
1867
1868 term->tl_rows = rows;
1869 term->tl_cols = cols;
1870 FOR_ALL_WINDOWS(wp)
1871 {
1872 if (wp->w_buffer == term->tl_buffer)
1873 {
1874 win_setheight_win(rows, wp);
1875 win_setwidth_win(cols, wp);
1876 }
1877 }
1878
1879 redraw_buf_later(term->tl_buffer, NOT_VALID);
1880 return 1;
1881}
1882
Bram Moolenaard85f2712017-07-28 21:51:57 +02001883/*
1884 * Handle a line that is pushed off the top of the screen.
1885 */
1886 static int
1887handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1888{
1889 term_T *term = (term_T *)user;
1890
1891 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001892 if (ga_grow(&term->tl_scrollback, 1) == OK)
1893 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001894 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001895 int len = 0;
1896 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001897 int c;
1898 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001899 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001900 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001901
1902 /* do not store empty cells at the end */
1903 for (i = 0; i < cols; ++i)
1904 if (cells[i].chars[0] != 0)
1905 len = i + 1;
1906
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001907 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001908 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001909 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001910 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001911 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001912 for (col = 0; col < len; col += cells[col].width)
1913 {
1914 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1915 {
1916 ga.ga_len = 0;
1917 break;
1918 }
1919 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1920 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1921 (char_u *)ga.ga_data + ga.ga_len);
1922 p[col].width = cells[col].width;
1923 p[col].attrs = cells[col].attrs;
1924 p[col].fg = cells[col].fg;
1925 p[col].bg = cells[col].bg;
1926 }
1927 }
1928 if (ga_grow(&ga, 1) == FAIL)
1929 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1930 else
1931 {
1932 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1933 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1934 }
1935 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001936
1937 line = (sb_line_T *)term->tl_scrollback.ga_data
1938 + term->tl_scrollback.ga_len;
1939 line->sb_cols = len;
1940 line->sb_cells = p;
1941 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001942 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001943 }
1944 return 0; /* ignored */
1945}
1946
Bram Moolenaar21554412017-07-24 21:44:43 +02001947static VTermScreenCallbacks screen_callbacks = {
1948 handle_damage, /* damage */
1949 handle_moverect, /* moverect */
1950 handle_movecursor, /* movecursor */
1951 handle_settermprop, /* settermprop */
1952 NULL, /* bell */
1953 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001954 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001955 NULL /* sb_popline */
1956};
1957
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001958/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001959 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001960 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001961 */
1962 void
1963term_channel_closed(channel_T *ch)
1964{
1965 term_T *term;
1966 int did_one = FALSE;
1967
1968 for (term = first_term; term != NULL; term = term->tl_next)
1969 if (term->tl_job == ch->ch_job)
1970 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001971 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001972 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001973
Bram Moolenaard85f2712017-07-28 21:51:57 +02001974 vim_free(term->tl_title);
1975 term->tl_title = NULL;
1976 vim_free(term->tl_status_text);
1977 term->tl_status_text = NULL;
1978
Bram Moolenaar423802d2017-07-30 16:52:24 +02001979 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001980 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001981 {
1982 int fnum = term->tl_buffer->b_fnum;
1983
Bram Moolenaar423802d2017-07-30 16:52:24 +02001984 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001985
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001986 if (term->tl_finish == 'c')
1987 {
1988 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001989 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001990 curbuf = term->tl_buffer;
1991 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1992 break;
1993 }
1994 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1995 {
1996 char buf[50];
1997
1998 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001999 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02002000 vim_snprintf(buf, sizeof(buf),
2001 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02002002 ? "botright sbuf %d"
2003 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002004 do_cmdline_cmd((char_u *)buf);
2005 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002006 else
2007 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002008 }
2009
Bram Moolenaard85f2712017-07-28 21:51:57 +02002010 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002011 }
2012 if (did_one)
2013 {
2014 redraw_statuslines();
2015
2016 /* Need to break out of vgetc(). */
2017 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002018 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002019
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002020 term = curbuf->b_term;
2021 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002022 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002023 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002024 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002025 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002026 }
2027 }
2028}
2029
2030/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002031 * Called to update a window that contains an active terminal.
2032 * Returns FAIL when there is no terminal running in this window or in
2033 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002034 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002035 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002036term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002037{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002038 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002039 VTerm *vterm;
2040 VTermScreen *screen;
2041 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002042 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002043
Bram Moolenaar6d819742017-08-06 14:57:49 +02002044 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002045 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002046
Bram Moolenaard85f2712017-07-28 21:51:57 +02002047 vterm = term->tl_vterm;
2048 screen = vterm_obtain_screen(vterm);
2049 state = vterm_obtain_state(vterm);
2050
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002051 /*
2052 * If the window was resized a redraw will be triggered and we get here.
2053 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2054 */
2055 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2056 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002057 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002058 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2059 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2060 win_T *twp;
2061
2062 FOR_ALL_WINDOWS(twp)
2063 {
2064 /* When more than one window shows the same terminal, use the
2065 * smallest size. */
2066 if (twp->w_buffer == term->tl_buffer)
2067 {
2068 if (!term->tl_rows_fixed && rows > twp->w_height)
2069 rows = twp->w_height;
2070 if (!term->tl_cols_fixed && cols > twp->w_width)
2071 cols = twp->w_width;
2072 }
2073 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002074
2075 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002076 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002077 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002078 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002079 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002080
2081 /* The cursor may have been moved when resizing. */
2082 vterm_state_get_cursorpos(state, &pos);
2083 position_cursor(wp, &pos);
2084
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002085 /* TODO: Only redraw what changed. */
2086 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002087 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002088 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002089 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002090
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002091 if (pos.row < term->tl_rows)
2092 {
2093 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002094 {
2095 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002096 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002097
Bram Moolenaareeac6772017-07-23 15:48:37 +02002098 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2099 vim_memset(&cell, 0, sizeof(cell));
2100
2101 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002102 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002103 if (c == NUL)
2104 {
2105 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002106#if defined(FEAT_MBYTE)
2107 if (enc_utf8)
2108 ScreenLinesUC[off] = NUL;
2109#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002110 }
2111 else
2112 {
2113#if defined(FEAT_MBYTE)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002114 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002115 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002116 if (c >= 0x80)
2117 {
2118 ScreenLines[off] = ' ';
2119 ScreenLinesUC[off] = c;
2120 }
2121 else
2122 {
2123 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002124 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002125 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002126 }
Bram Moolenaar58302322017-08-22 20:33:53 +02002127# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02002128 else if (has_mbyte && c >= 0x80)
2129 {
2130 char_u mb[MB_MAXBYTES+1];
2131 WCHAR wc = c;
2132
2133 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2134 (char*)mb, 2, 0, 0) > 1)
2135 {
2136 ScreenLines[off] = mb[0];
2137 ScreenLines[off+1] = mb[1];
2138 cell.width = mb_ptr2cells(mb);
2139 }
2140 else
2141 ScreenLines[off] = c;
2142 }
2143# endif
2144 else
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002145#endif
Bram Moolenaar740c4332017-08-21 22:01:27 +02002146 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002147 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002148 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002149
2150 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002151 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002152 if (cell.width == 2)
2153 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002154#if defined(FEAT_MBYTE)
2155 if (enc_utf8)
2156 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002157 else if (!has_mbyte)
Bram Moolenaar8a773062017-07-24 22:29:21 +02002158#endif
Bram Moolenaar740c4332017-08-21 22:01:27 +02002159 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002160 ++pos.col;
2161 ++off;
2162 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002163 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002164 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002165 else
2166 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002167
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002168 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2169 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002170 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002171
2172 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002173}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002174
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002175/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002176 * Return TRUE if "wp" is a terminal window where the job has finished.
2177 */
2178 int
2179term_is_finished(buf_T *buf)
2180{
2181 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2182}
2183
2184/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002185 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002186 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002187 */
2188 int
2189term_show_buffer(buf_T *buf)
2190{
2191 term_T *term = buf->b_term;
2192
Bram Moolenaar6d819742017-08-06 14:57:49 +02002193 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002194}
2195
2196/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002197 * The current buffer is going to be changed. If there is terminal
2198 * highlighting remove it now.
2199 */
2200 void
2201term_change_in_curbuf(void)
2202{
2203 term_T *term = curbuf->b_term;
2204
2205 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2206 {
2207 free_scrollback(term);
2208 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002209
2210 /* The buffer is now like a normal buffer, it cannot be easily
2211 * abandoned when changed. */
2212 set_string_option_direct((char_u *)"buftype", -1,
2213 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002214 }
2215}
2216
2217/*
2218 * Get the screen attribute for a position in the buffer.
2219 */
2220 int
2221term_get_attr(buf_T *buf, linenr_T lnum, int col)
2222{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002223 term_T *term = buf->b_term;
2224 sb_line_T *line;
2225 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002226
Bram Moolenaar70229f92017-07-29 16:01:53 +02002227 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002228 return 0;
2229 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2230 if (col >= line->sb_cols)
2231 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002232 cellattr = line->sb_cells + col;
2233 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002234}
2235
2236/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002237 * Create a new vterm and initialize it.
2238 */
2239 static void
2240create_vterm(term_T *term, int rows, int cols)
2241{
2242 VTerm *vterm;
2243 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002244 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002245
2246 vterm = vterm_new(rows, cols);
2247 term->tl_vterm = vterm;
2248 screen = vterm_obtain_screen(vterm);
2249 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2250 /* TODO: depends on 'encoding'. */
2251 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002252
2253 /* Vterm uses a default black background. Set it to white when
2254 * 'background' is "light". */
2255 if (*p_bg == 'l')
2256 {
2257 VTermColor fg, bg;
2258
2259 fg.red = fg.green = fg.blue = 0;
2260 bg.red = bg.green = bg.blue = 255;
2261 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2262 }
2263
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002264 /* Required to initialize most things. */
2265 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002266
2267 /* Allow using alternate screen. */
2268 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002269
Bram Moolenaar58302322017-08-22 20:33:53 +02002270 /* For unix do not use a blinking cursor. In an xterm this causes the
2271 * cursor to blink if it's blinking in the xterm.
2272 * We do want a blinking cursor by default on Windows, since that's what
2273 * the default is for a console. */
2274#ifdef WIN3264
2275 value.boolean = 1;
2276#else
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002277 value.boolean = 0;
Bram Moolenaar58302322017-08-22 20:33:53 +02002278#endif
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002279 vterm_state_set_termprop(vterm_obtain_state(vterm),
2280 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002281}
2282
Bram Moolenaar21554412017-07-24 21:44:43 +02002283/*
2284 * Return the text to show for the buffer name and status.
2285 */
2286 char_u *
2287term_get_status_text(term_T *term)
2288{
2289 if (term->tl_status_text == NULL)
2290 {
2291 char_u *txt;
2292 size_t len;
2293
Bram Moolenaar6d819742017-08-06 14:57:49 +02002294 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002295 {
2296 if (term_job_running(term))
2297 txt = (char_u *)_("Terminal");
2298 else
2299 txt = (char_u *)_("Terminal-finished");
2300 }
2301 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002302 txt = term->tl_title;
2303 else if (term_job_running(term))
2304 txt = (char_u *)_("running");
2305 else
2306 txt = (char_u *)_("finished");
2307 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002308 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002309 if (term->tl_status_text != NULL)
2310 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2311 term->tl_buffer->b_fname, txt);
2312 }
2313 return term->tl_status_text;
2314}
2315
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002316/*
2317 * Mark references in jobs of terminals.
2318 */
2319 int
2320set_ref_in_term(int copyID)
2321{
2322 int abort = FALSE;
2323 term_T *term;
2324 typval_T tv;
2325
2326 for (term = first_term; term != NULL; term = term->tl_next)
2327 if (term->tl_job != NULL)
2328 {
2329 tv.v_type = VAR_JOB;
2330 tv.vval.v_job = term->tl_job;
2331 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2332 }
2333 return abort;
2334}
2335
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002336/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002337 * Get the buffer from the first argument in "argvars".
2338 * Returns NULL when the buffer is not for a terminal window.
2339 */
2340 static buf_T *
2341term_get_buf(typval_T *argvars)
2342{
2343 buf_T *buf;
2344
2345 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2346 ++emsg_off;
2347 buf = get_buf_tv(&argvars[0], FALSE);
2348 --emsg_off;
2349 if (buf == NULL || buf->b_term == NULL)
2350 return NULL;
2351 return buf;
2352}
2353
2354/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002355 * "term_getaltscreen(buf)" function
2356 */
2357 void
2358f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2359{
2360 buf_T *buf = term_get_buf(argvars);
2361
2362 if (buf == NULL)
2363 return;
2364 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2365}
2366
2367/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002368 * "term_getattr(attr, name)" function
2369 */
2370 void
2371f_term_getattr(typval_T *argvars, typval_T *rettv)
2372{
2373 int attr;
2374 size_t i;
2375 char_u *name;
2376
2377 static struct {
2378 char *name;
2379 int attr;
2380 } attrs[] = {
2381 {"bold", HL_BOLD},
2382 {"italic", HL_ITALIC},
2383 {"underline", HL_UNDERLINE},
2384 {"strike", HL_STANDOUT},
2385 {"reverse", HL_INVERSE},
2386 };
2387
2388 attr = get_tv_number(&argvars[0]);
2389 name = get_tv_string_chk(&argvars[1]);
2390 if (name == NULL)
2391 return;
2392
2393 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2394 if (STRCMP(name, attrs[i].name) == 0)
2395 {
2396 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2397 break;
2398 }
2399}
2400
2401/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002402 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002403 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002404 void
2405f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002406{
Bram Moolenaar97870002017-07-30 18:28:38 +02002407 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002408 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002409 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002410 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002411
Bram Moolenaar97870002017-07-30 18:28:38 +02002412 if (rettv_list_alloc(rettv) == FAIL)
2413 return;
2414 if (buf == NULL)
2415 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002416 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002417
2418 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002419 list_append_number(l, term->tl_cursor_pos.row + 1);
2420 list_append_number(l, term->tl_cursor_pos.col + 1);
2421
2422 d = dict_alloc();
2423 if (d != NULL)
2424 {
2425 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2426 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2427 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2428 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2429 ? (char_u *)"" : term->tl_cursor_color);
2430 list_append_dict(l, d);
2431 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002432}
2433
2434/*
2435 * "term_getjob(buf)" function
2436 */
2437 void
2438f_term_getjob(typval_T *argvars, typval_T *rettv)
2439{
2440 buf_T *buf = term_get_buf(argvars);
2441
2442 rettv->v_type = VAR_JOB;
2443 rettv->vval.v_job = NULL;
2444 if (buf == NULL)
2445 return;
2446
2447 rettv->vval.v_job = buf->b_term->tl_job;
2448 if (rettv->vval.v_job != NULL)
2449 ++rettv->vval.v_job->jv_refcount;
2450}
2451
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002452 static int
2453get_row_number(typval_T *tv, term_T *term)
2454{
2455 if (tv->v_type == VAR_STRING
2456 && tv->vval.v_string != NULL
2457 && STRCMP(tv->vval.v_string, ".") == 0)
2458 return term->tl_cursor_pos.row;
2459 return (int)get_tv_number(tv) - 1;
2460}
2461
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002462/*
2463 * "term_getline(buf, row)" function
2464 */
2465 void
2466f_term_getline(typval_T *argvars, typval_T *rettv)
2467{
2468 buf_T *buf = term_get_buf(argvars);
2469 term_T *term;
2470 int row;
2471
2472 rettv->v_type = VAR_STRING;
2473 if (buf == NULL)
2474 return;
2475 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002476 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002477
2478 if (term->tl_vterm == NULL)
2479 {
2480 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2481
2482 /* vterm is finished, get the text from the buffer */
2483 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2484 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2485 }
2486 else
2487 {
2488 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2489 VTermRect rect;
2490 int len;
2491 char_u *p;
2492
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002493 if (row < 0 || row >= term->tl_rows)
2494 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002495 len = term->tl_cols * MB_MAXBYTES + 1;
2496 p = alloc(len);
2497 if (p == NULL)
2498 return;
2499 rettv->vval.v_string = p;
2500
2501 rect.start_col = 0;
2502 rect.end_col = term->tl_cols;
2503 rect.start_row = row;
2504 rect.end_row = row + 1;
2505 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2506 }
2507}
2508
2509/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002510 * "term_getscrolled(buf)" function
2511 */
2512 void
2513f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2514{
2515 buf_T *buf = term_get_buf(argvars);
2516
2517 if (buf == NULL)
2518 return;
2519 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2520}
2521
2522/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002523 * "term_getsize(buf)" function
2524 */
2525 void
2526f_term_getsize(typval_T *argvars, typval_T *rettv)
2527{
2528 buf_T *buf = term_get_buf(argvars);
2529 list_T *l;
2530
2531 if (rettv_list_alloc(rettv) == FAIL)
2532 return;
2533 if (buf == NULL)
2534 return;
2535
2536 l = rettv->vval.v_list;
2537 list_append_number(l, buf->b_term->tl_rows);
2538 list_append_number(l, buf->b_term->tl_cols);
2539}
2540
2541/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002542 * "term_getstatus(buf)" function
2543 */
2544 void
2545f_term_getstatus(typval_T *argvars, typval_T *rettv)
2546{
2547 buf_T *buf = term_get_buf(argvars);
2548 term_T *term;
2549 char_u val[100];
2550
2551 rettv->v_type = VAR_STRING;
2552 if (buf == NULL)
2553 return;
2554 term = buf->b_term;
2555
2556 if (term_job_running(term))
2557 STRCPY(val, "running");
2558 else
2559 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002560 if (term->tl_normal_mode)
2561 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002562 rettv->vval.v_string = vim_strsave(val);
2563}
2564
2565/*
2566 * "term_gettitle(buf)" function
2567 */
2568 void
2569f_term_gettitle(typval_T *argvars, typval_T *rettv)
2570{
2571 buf_T *buf = term_get_buf(argvars);
2572
2573 rettv->v_type = VAR_STRING;
2574 if (buf == NULL)
2575 return;
2576
2577 if (buf->b_term->tl_title != NULL)
2578 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2579}
2580
2581/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002582 * "term_gettty(buf)" function
2583 */
2584 void
2585f_term_gettty(typval_T *argvars, typval_T *rettv)
2586{
2587 buf_T *buf = term_get_buf(argvars);
2588 char_u *p;
2589
2590 rettv->v_type = VAR_STRING;
2591 if (buf == NULL)
2592 return;
2593 if (buf->b_term->tl_job != NULL)
2594 p = buf->b_term->tl_job->jv_tty_name;
2595 else
2596 p = buf->b_term->tl_tty_name;
2597 if (p != NULL)
2598 rettv->vval.v_string = vim_strsave(p);
2599}
2600
2601/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002602 * "term_list()" function
2603 */
2604 void
2605f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2606{
2607 term_T *tp;
2608 list_T *l;
2609
2610 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2611 return;
2612
2613 l = rettv->vval.v_list;
2614 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2615 if (tp != NULL && tp->tl_buffer != NULL)
2616 if (list_append_number(l,
2617 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2618 return;
2619}
2620
2621/*
2622 * "term_scrape(buf, row)" function
2623 */
2624 void
2625f_term_scrape(typval_T *argvars, typval_T *rettv)
2626{
2627 buf_T *buf = term_get_buf(argvars);
2628 VTermScreen *screen = NULL;
2629 VTermPos pos;
2630 list_T *l;
2631 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002632 char_u *p;
2633 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002634
2635 if (rettv_list_alloc(rettv) == FAIL)
2636 return;
2637 if (buf == NULL)
2638 return;
2639 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002640
2641 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002642 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002643
2644 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002645 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002646 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002647 p = NULL;
2648 line = NULL;
2649 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002650 else
2651 {
2652 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2653
2654 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2655 return;
2656 p = ml_get_buf(buf, lnum + 1, FALSE);
2657 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2658 }
2659
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002660 for (pos.col = 0; pos.col < term->tl_cols; )
2661 {
2662 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002663 int width;
2664 VTermScreenCellAttrs attrs;
2665 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002666 char_u rgb[8];
2667 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2668 int off = 0;
2669 int i;
2670
2671 if (screen == NULL)
2672 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002673 cellattr_T *cellattr;
2674 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002675
2676 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002677 if (pos.col >= line->sb_cols)
2678 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002679 cellattr = line->sb_cells + pos.col;
2680 width = cellattr->width;
2681 attrs = cellattr->attrs;
2682 fg = cellattr->fg;
2683 bg = cellattr->bg;
2684 len = MB_PTR2LEN(p);
2685 mch_memmove(mbs, p, len);
2686 mbs[len] = NUL;
2687 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002688 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002689 else
2690 {
2691 VTermScreenCell cell;
2692 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2693 break;
2694 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2695 {
2696 if (cell.chars[i] == 0)
2697 break;
2698 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2699 }
2700 mbs[off] = NUL;
2701 width = cell.width;
2702 attrs = cell.attrs;
2703 fg = cell.fg;
2704 bg = cell.bg;
2705 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002706 dcell = dict_alloc();
2707 list_append_dict(l, dcell);
2708
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002709 dict_add_nr_str(dcell, "chars", 0, mbs);
2710
2711 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002712 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002713 dict_add_nr_str(dcell, "fg", 0, rgb);
2714 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002715 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002716 dict_add_nr_str(dcell, "bg", 0, rgb);
2717
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002718 dict_add_nr_str(dcell, "attr",
2719 cell2attr(attrs, fg, bg), NULL);
2720 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002721
2722 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002723 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002724 ++pos.col;
2725 }
2726}
2727
2728/*
2729 * "term_sendkeys(buf, keys)" function
2730 */
2731 void
2732f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2733{
2734 buf_T *buf = term_get_buf(argvars);
2735 char_u *msg;
2736 term_T *term;
2737
2738 rettv->v_type = VAR_UNKNOWN;
2739 if (buf == NULL)
2740 return;
2741
2742 msg = get_tv_string_chk(&argvars[1]);
2743 if (msg == NULL)
2744 return;
2745 term = buf->b_term;
2746 if (term->tl_vterm == NULL)
2747 return;
2748
2749 while (*msg != NUL)
2750 {
2751 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2752 msg += MB_PTR2LEN(msg);
2753 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002754}
2755
2756/*
2757 * "term_start(command, options)" function
2758 */
2759 void
2760f_term_start(typval_T *argvars, typval_T *rettv)
2761{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002762 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002763
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002764 init_job_options(&opt);
2765 /* TODO: allow more job options */
2766 if (argvars[1].v_type != VAR_UNKNOWN
2767 && get_job_options(&argvars[1], &opt,
2768 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002769 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002770 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002771 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002772 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002773 return;
2774
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002775 if (opt.jo_vertical)
2776 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002777 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002778
2779 if (curbuf->b_term != NULL)
2780 rettv->vval.v_number = curbuf->b_fnum;
2781}
2782
2783/*
2784 * "term_wait" function
2785 */
2786 void
2787f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2788{
2789 buf_T *buf = term_get_buf(argvars);
2790
2791 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002792 {
2793 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002794 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002795 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002796 if (buf->b_term->tl_job == NULL)
2797 {
2798 ch_log(NULL, "term_wait(): no job to wait for");
2799 return;
2800 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002801
2802 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002803 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002804 {
2805 /* The job is dead, keep reading channel I/O until the channel is
2806 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002807 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002808 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2809 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002810 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002811 parse_queued_messages();
2812 ui_delay(10L, FALSE);
2813 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002814 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002815 parse_queued_messages();
2816 }
2817 else
2818 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002819 long wait = 10L;
2820
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002821 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002822 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002823
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002824 /* Wait for some time for any channel I/O. */
2825 if (argvars[1].v_type != VAR_UNKNOWN)
2826 wait = get_tv_number(&argvars[1]);
2827 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002828 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002829
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002830 /* Flushing messages on channels is hopefully sufficient.
2831 * TODO: is there a better way? */
2832 parse_queued_messages();
2833 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002834}
2835
Bram Moolenaara83e3962017-08-17 14:39:07 +02002836# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002837
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002838/**************************************
2839 * 2. MS-Windows implementation.
2840 */
2841
Bram Moolenaara83e3962017-08-17 14:39:07 +02002842# ifndef PROTO
2843
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002844#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2845#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2846
Bram Moolenaar8a773062017-07-24 22:29:21 +02002847void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002848void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002849void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002850BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2851void (*winpty_config_set_initial_size)(void*, int, int);
2852LPCWSTR (*winpty_conin_name)(void*);
2853LPCWSTR (*winpty_conout_name)(void*);
2854LPCWSTR (*winpty_conerr_name)(void*);
2855void (*winpty_free)(void*);
2856void (*winpty_config_free)(void*);
2857void (*winpty_spawn_config_free)(void*);
2858void (*winpty_error_free)(void*);
2859LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002860BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002861HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002862
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002863#define WINPTY_DLL "winpty.dll"
2864
2865static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002866# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002867
Bram Moolenaara83e3962017-08-17 14:39:07 +02002868 static int
2869dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002870{
2871 int i;
2872 static struct
2873 {
2874 char *name;
2875 FARPROC *ptr;
2876 } winpty_entry[] =
2877 {
2878 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2879 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2880 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2881 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2882 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2883 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2884 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2885 {"winpty_free", (FARPROC*)&winpty_free},
2886 {"winpty_open", (FARPROC*)&winpty_open},
2887 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2888 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2889 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2890 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002891 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002892 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002893 {NULL, NULL}
2894 };
2895
2896 /* No need to initialize twice. */
2897 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002898 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002899 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2900 * winpty.dll. */
2901 if (*p_winptydll != NUL)
2902 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2903 if (!hWinPtyDLL)
2904 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002905 if (!hWinPtyDLL)
2906 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002907 if (verbose)
2908 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2909 : (char_u *)WINPTY_DLL);
2910 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002911 }
2912 for (i = 0; winpty_entry[i].name != NULL
2913 && winpty_entry[i].ptr != NULL; ++i)
2914 {
2915 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2916 winpty_entry[i].name)) == NULL)
2917 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002918 if (verbose)
2919 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2920 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002921 }
2922 }
2923
Bram Moolenaara83e3962017-08-17 14:39:07 +02002924 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002925}
2926
2927/*
2928 * Create a new terminal of "rows" by "cols" cells.
2929 * Store a reference in "term".
2930 * Return OK or FAIL.
2931 */
2932 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02002933term_and_job_init(
2934 term_T *term,
2935 int rows,
2936 int cols,
2937 typval_T *argvar,
2938 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002939{
Bram Moolenaar5983d502017-08-20 19:22:56 +02002940 WCHAR *cmd_wchar = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002941 channel_T *channel = NULL;
2942 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002943 DWORD error;
Bram Moolenaar5983d502017-08-20 19:22:56 +02002944 HANDLE jo = NULL;
2945 HANDLE child_process_handle;
2946 HANDLE child_thread_handle;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002947 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002948 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002949 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002950 garray_T ga;
2951 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002952
Bram Moolenaara83e3962017-08-17 14:39:07 +02002953 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002954 return FAIL;
2955
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002956 if (argvar->v_type == VAR_STRING)
2957 cmd = argvar->vval.v_string;
2958 else
2959 {
2960 ga_init2(&ga, (int)sizeof(char*), 20);
2961 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2962 goto failed;
2963 cmd = ga.ga_data;
2964 }
2965
Bram Moolenaar5983d502017-08-20 19:22:56 +02002966 cmd_wchar = enc_to_utf16(cmd, NULL);
2967 if (cmd_wchar == NULL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002968 return FAIL;
2969
2970 job = job_alloc();
2971 if (job == NULL)
2972 goto failed;
2973
2974 channel = add_channel();
2975 if (channel == NULL)
2976 goto failed;
2977
2978 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2979 if (term->tl_winpty_config == NULL)
2980 goto failed;
2981
2982 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2983 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2984 if (term->tl_winpty == NULL)
2985 goto failed;
2986
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002987 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002988 spawn_config = winpty_spawn_config_new(
2989 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2990 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2991 NULL,
Bram Moolenaar5983d502017-08-20 19:22:56 +02002992 cmd_wchar,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002993 NULL,
2994 NULL,
2995 &winpty_err);
2996 if (spawn_config == NULL)
2997 goto failed;
2998
2999 channel = add_channel();
3000 if (channel == NULL)
3001 goto failed;
3002
3003 job = job_alloc();
3004 if (job == NULL)
3005 goto failed;
3006
Bram Moolenaar5983d502017-08-20 19:22:56 +02003007 /* TODO: when all lines are written and the fd is closed, the command
3008 * doesn't get EOF and hangs. */
3009 if (opt->jo_set & JO_IN_BUF)
3010 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
3011
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003012 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
3013 &child_thread_handle, &error, &winpty_err))
3014 goto failed;
3015
3016 channel_set_pipes(channel,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003017 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003018 winpty_conin_name(term->tl_winpty),
3019 GENERIC_WRITE, 0, NULL,
3020 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003021 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003022 winpty_conout_name(term->tl_winpty),
3023 GENERIC_READ, 0, NULL,
3024 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003025 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003026 winpty_conerr_name(term->tl_winpty),
3027 GENERIC_READ, 0, NULL,
3028 OPEN_EXISTING, 0, NULL));
3029
3030 jo = CreateJobObject(NULL, NULL);
3031 if (jo == NULL)
3032 goto failed;
3033
3034 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003035 {
3036 /* Failed, switch the way to terminate process with TerminateProcess. */
3037 CloseHandle(jo);
3038 jo = NULL;
3039 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003040
3041 winpty_spawn_config_free(spawn_config);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003042 vim_free(cmd_wchar);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003043
3044 create_vterm(term, rows, cols);
3045
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02003046 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02003047 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003048
3049 job->jv_channel = channel;
3050 job->jv_proc_info.hProcess = child_process_handle;
3051 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
3052 job->jv_job_object = jo;
3053 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003054 sprintf(buf, "winpty://%lu",
3055 GetProcessId(winpty_agent_process(term->tl_winpty)));
3056 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003057 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003058 term->tl_job = job;
3059
3060 return OK;
3061
3062failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003063 if (argvar->v_type == VAR_LIST)
3064 vim_free(ga.ga_data);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003065 if (cmd_wchar != NULL)
3066 vim_free(cmd_wchar);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003067 if (spawn_config != NULL)
3068 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003069 if (channel != NULL)
3070 channel_clear(channel);
3071 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003072 {
3073 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003074 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003075 }
3076 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003077 if (jo != NULL)
3078 CloseHandle(jo);
3079 if (term->tl_winpty != NULL)
3080 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003081 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003082 if (term->tl_winpty_config != NULL)
3083 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003084 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003085 if (winpty_err != NULL)
3086 {
3087 char_u *msg = utf16_to_enc(
3088 (short_u *)winpty_error_msg(winpty_err), NULL);
3089
3090 EMSG(msg);
3091 winpty_error_free(winpty_err);
3092 }
3093 return FAIL;
3094}
3095
3096/*
3097 * Free the terminal emulator part of "term".
3098 */
3099 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003100term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003101{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003102 if (term->tl_winpty != NULL)
3103 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003104 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003105 if (term->tl_winpty_config != NULL)
3106 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003107 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003108 if (term->tl_vterm != NULL)
3109 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003110 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003111}
3112
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003113/*
3114 * Request size to terminal.
3115 */
3116 static void
3117term_report_winsize(term_T *term, int rows, int cols)
3118{
3119 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3120}
3121
Bram Moolenaara83e3962017-08-17 14:39:07 +02003122 int
3123terminal_enabled(void)
3124{
3125 return dyn_winpty_init(FALSE) == OK;
3126}
3127
3128
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003129# else
3130
3131/**************************************
3132 * 3. Unix-like implementation.
3133 */
3134
3135/*
3136 * Create a new terminal of "rows" by "cols" cells.
3137 * Start job for "cmd".
3138 * Store the pointers in "term".
3139 * Return OK or FAIL.
3140 */
3141 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003142term_and_job_init(
3143 term_T *term,
3144 int rows,
3145 int cols,
3146 typval_T *argvar,
3147 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003148{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003149 create_vterm(term, rows, cols);
3150
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003151 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003152 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003153 if (term->tl_job != NULL)
3154 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003155
Bram Moolenaar61a66052017-07-22 18:39:00 +02003156 return term->tl_job != NULL
3157 && term->tl_job->jv_channel != NULL
3158 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003159}
3160
3161/*
3162 * Free the terminal emulator part of "term".
3163 */
3164 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003165term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003166{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003167 if (term->tl_vterm != NULL)
3168 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003169 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003170}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003171
3172/*
3173 * Request size to terminal.
3174 */
3175 static void
3176term_report_winsize(term_T *term, int rows, int cols)
3177{
3178 /* Use an ioctl() to report the new window size to the job. */
3179 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3180 {
3181 int fd = -1;
3182 int part;
3183
3184 for (part = PART_OUT; part < PART_COUNT; ++part)
3185 {
3186 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3187 if (isatty(fd))
3188 break;
3189 }
3190 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003191 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003192 }
3193}
3194
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003195# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003196
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003197#endif /* FEAT_TERMINAL */