blob: 3ee50e490f7d3c0b55d186742e500dbe7cc40b6f [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 Moolenaardbe948d2017-07-23 22:50:51 +020066 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020067 * - Copy text in the vterm to the Vim buffer once in a while, so that
68 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020069 */
70
71#include "vim.h"
72
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020073#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020074
Bram Moolenaard5310982017-08-05 15:16:32 +020075#ifndef MIN
76# define MIN(x,y) ((x) < (y) ? (x) : (y))
77#endif
78#ifndef MAX
79# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020080#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020081
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020082#include "libvterm/include/vterm.h"
83
Bram Moolenaar33a43be2017-08-06 21:36:22 +020084/* This is VTermScreenCell without the characters, thus much smaller. */
85typedef struct {
86 VTermScreenCellAttrs attrs;
87 char width;
88 VTermColor fg, bg;
89} cellattr_T;
90
Bram Moolenaard85f2712017-07-28 21:51:57 +020091typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020092 int sb_cols; /* can differ per line */
93 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020094} sb_line_T;
95
Bram Moolenaare4f25e42017-07-07 11:54:15 +020096/* typedef term_T in structs.h */
97struct terminal_S {
98 term_T *tl_next;
99
Bram Moolenaar423802d2017-07-30 16:52:24 +0200100 VTerm *tl_vterm;
101 job_T *tl_job;
102 buf_T *tl_buffer;
103
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200104 /* used when tl_job is NULL and only a pty was created */
105 int tl_tty_fd;
106 char_u *tl_tty_name;
107
Bram Moolenaar6d819742017-08-06 14:57:49 +0200108 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200109 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200110 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200111 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200112
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200113#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200114 void *tl_winpty_config;
115 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200116#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200117
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200118 /* last known vterm size */
119 int tl_rows;
120 int tl_cols;
121 /* vterm size does not follow window size */
122 int tl_rows_fixed;
123 int tl_cols_fixed;
124
Bram Moolenaar21554412017-07-24 21:44:43 +0200125 char_u *tl_title; /* NULL or allocated */
126 char_u *tl_status_text; /* NULL or allocated */
127
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200128 /* Range of screen rows to update. Zero based. */
129 int tl_dirty_row_start; /* -1 if nothing dirty */
130 int tl_dirty_row_end; /* row below last one to update */
131
Bram Moolenaard85f2712017-07-28 21:51:57 +0200132 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200133 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200134
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200135 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200136 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200137 int tl_cursor_blink;
138 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
139 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200140
141 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200142};
143
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200144#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
145#define TMODE_LOOP 2 /* CTRL-W N used */
146
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200147/*
148 * List of all active terminals.
149 */
150static term_T *first_term = NULL;
151
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200152/* Terminal active in terminal_loop(). */
153static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200154
155#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
156#define KEY_BUF_LEN 200
157
158/*
159 * Functions with separate implementation for MS-Windows and Unix-like systems.
160 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200161static int term_and_job_init(term_T *term, int rows, int cols,
162 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200163static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200164static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200165
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200166/* The characters that we know (or assume) that the terminal expects for the
167 * backspace and enter keys. */
168static int term_backspace_char = BS;
169static int term_enter_char = CAR;
170static int term_nl_does_cr = FALSE;
171
172
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200173/**************************************
174 * 1. Generic code for all systems.
175 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200176
177/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200178 * Determine the terminal size from 'termsize' and the current window.
179 * Assumes term->tl_rows and term->tl_cols are zero.
180 */
181 static void
182set_term_and_win_size(term_T *term)
183{
184 if (*curwin->w_p_tms != NUL)
185 {
186 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
187
188 term->tl_rows = atoi((char *)curwin->w_p_tms);
189 term->tl_cols = atoi((char *)p);
190 }
191 if (term->tl_rows == 0)
192 term->tl_rows = curwin->w_height;
193 else
194 {
195 win_setheight_win(term->tl_rows, curwin);
196 term->tl_rows_fixed = TRUE;
197 }
198 if (term->tl_cols == 0)
199 term->tl_cols = curwin->w_width;
200 else
201 {
202 win_setwidth_win(term->tl_cols, curwin);
203 term->tl_cols_fixed = TRUE;
204 }
205}
206
207/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200208 * Initialize job options for a terminal job.
209 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200210 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200211 static void
212init_job_options(jobopt_T *opt)
213{
214 clear_job_options(opt);
215
216 opt->jo_mode = MODE_RAW;
217 opt->jo_out_mode = MODE_RAW;
218 opt->jo_err_mode = MODE_RAW;
219 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
220
221 opt->jo_io[PART_OUT] = JIO_BUFFER;
222 opt->jo_io[PART_ERR] = JIO_BUFFER;
223 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
224
225 opt->jo_modifiable[PART_OUT] = 0;
226 opt->jo_modifiable[PART_ERR] = 0;
227 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
228
229 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
230}
231
232/*
233 * Set job options mandatory for a terminal job.
234 */
235 static void
236setup_job_options(jobopt_T *opt, int rows, int cols)
237{
238 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
239 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
240 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200241 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
242 opt->jo_term_rows = rows;
243 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
244 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200245}
246
247 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200248term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250 exarg_T split_ea;
251 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200253 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254
255 if (check_restricted() || check_secure())
256 return;
257
258 term = (term_T *)alloc_clear(sizeof(term_T));
259 if (term == NULL)
260 return;
261 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200262 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200263 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200264 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200265 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200266
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200267 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200268 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200269 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200270 /* Create a new buffer in the current window. */
271 if (!can_abandon(curbuf, forceit))
272 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200273 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200274 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200275 return;
276 }
277 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
278 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200279 {
280 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200281 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200282 }
283 }
284 else if (opt->jo_hidden)
285 {
286 buf_T *buf;
287
288 /* Create a new buffer without a window. Make it the current buffer for
289 * a moment to be able to do the initialisations. */
290 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
291 BLN_NEW | BLN_LISTED);
292 if (buf == NULL || ml_open(buf) == FAIL)
293 {
294 vim_free(term);
295 return;
296 }
297 old_curbuf = curbuf;
298 --curbuf->b_nwindows;
299 curbuf = buf;
300 curwin->w_buffer = buf;
301 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200302 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200303 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200304 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200305 /* Open a new window or tab. */
306 split_ea.cmdidx = CMD_new;
307 split_ea.cmd = (char_u *)"new";
308 split_ea.arg = (char_u *)"";
309 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
310 {
311 split_ea.line2 = opt->jo_term_rows;
312 split_ea.addr_count = 1;
313 }
314 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
315 {
316 split_ea.line2 = opt->jo_term_cols;
317 split_ea.addr_count = 1;
318 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200319
Bram Moolenaarda43b612017-08-11 22:27:50 +0200320 ex_splitview(&split_ea);
321 if (curwin == old_curwin)
322 {
323 /* split failed */
324 vim_free(term);
325 return;
326 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200327 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200328 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200329 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200330
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200331 if (!opt->jo_hidden)
332 {
333 /* only one size was taken care of with :new, do the other one */
334 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
335 win_setheight(opt->jo_term_rows);
336 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
337 win_setwidth(opt->jo_term_cols);
338 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200339
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200340 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200341 term->tl_next = first_term;
342 first_term = term;
343
Bram Moolenaar78712a72017-08-05 14:50:12 +0200344 if (opt->jo_term_name != NULL)
345 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
346 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200347 {
348 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200349 size_t len;
350 char_u *cmd, *p;
351
352 if (argvar->v_type == VAR_STRING)
353 cmd = argvar->vval.v_string;
354 else if (argvar->v_type != VAR_LIST
355 || argvar->vval.v_list == NULL
356 || argvar->vval.v_list->lv_len < 1)
357 cmd = (char_u*)"";
358 else
359 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
360
361 len = STRLEN(cmd) + 10;
362 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200363
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200364 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200365 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200366 /* Prepend a ! to the command name to avoid the buffer name equals
367 * the executable, otherwise ":w!" would overwrite it. */
368 if (i == 0)
369 vim_snprintf((char *)p, len, "!%s", cmd);
370 else
371 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200372 if (buflist_findname(p) == NULL)
373 {
374 curbuf->b_ffname = p;
375 break;
376 }
377 }
378 }
379 curbuf->b_fname = curbuf->b_ffname;
380
Bram Moolenaar37c45832017-08-12 16:01:04 +0200381 if (opt->jo_term_opencmd != NULL)
382 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
383
Bram Moolenaareb44a682017-08-03 22:44:55 +0200384 set_string_option_direct((char_u *)"buftype", -1,
385 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
386
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200387 /* Mark the buffer as not modifiable. It can only be made modifiable after
388 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200389 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200390
391 /* Set 'bufhidden' to "hide": allow closing the window. */
392 set_string_option_direct((char_u *)"bufhidden", -1,
393 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200394
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200395 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200396 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200397
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200398 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200399 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt)
400 == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200401 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200402 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200403 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200404 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200405
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200406 /* Make sure we don't get stuck on sending keys to the job, it leads to
407 * a deadlock if the job is waiting for Vim to read. */
408 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
409
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200410 if (old_curbuf != NULL)
411 {
412 --curbuf->b_nwindows;
413 curbuf = old_curbuf;
414 curwin->w_buffer = curbuf;
415 ++curbuf->b_nwindows;
416 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200417 }
418 else
419 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200420 buf_T *buf = curbuf;
421
Bram Moolenaard85f2712017-07-28 21:51:57 +0200422 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200423 if (old_curbuf != NULL)
424 {
425 --curbuf->b_nwindows;
426 curbuf = old_curbuf;
427 curwin->w_buffer = curbuf;
428 ++curbuf->b_nwindows;
429 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200430
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200431 /* Wiping out the buffer will also close the window and call
432 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200433 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200434 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200435}
436
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200437/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200438 * ":terminal": open a terminal window and execute a job in it.
439 */
440 void
441ex_terminal(exarg_T *eap)
442{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200443 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200444 jobopt_T opt;
445 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200446 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200447
448 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200449
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200450 cmd = eap->arg;
451 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
452 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200453 char_u *p, *ep;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200454
455 cmd += 2;
456 p = skiptowhite(cmd);
Bram Moolenaarb2412082017-08-20 18:09:14 +0200457 ep = vim_strchr(cmd, '=');
458 if (ep != NULL && ep < p)
459 p = ep;
460
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200461 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
462 opt.jo_term_finish = 'c';
463 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
464 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200465 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
466 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200467 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
468 opt.jo_hidden = 1;
Bram Moolenaarb2412082017-08-20 18:09:14 +0200469 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
470 && ep != NULL && isdigit(ep[1]))
471 {
472 opt.jo_set2 |= JO2_TERM_ROWS;
473 opt.jo_term_rows = atoi((char *)ep + 1);
474 p = skiptowhite(cmd);
475 }
476 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
477 && ep != NULL && isdigit(ep[1]))
478 {
479 opt.jo_set2 |= JO2_TERM_COLS;
480 opt.jo_term_cols = atoi((char *)ep + 1);
481 p = skiptowhite(cmd);
482 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200483 else
484 {
485 if (*p)
486 *p = NUL;
487 EMSG2(_("E181: Invalid attribute: %s"), cmd);
488 return;
489 }
490 cmd = skipwhite(p);
491 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200492 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200493 /* Make a copy, an autocommand may set 'shell'. */
494 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200495
Bram Moolenaarb2412082017-08-20 18:09:14 +0200496 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200497 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200498 /* Write lines from current buffer to the job. */
499 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
500 opt.jo_io[PART_IN] = JIO_BUFFER;
501 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
502 opt.jo_in_top = eap->line1;
503 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200504 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200505
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200506 argvar.v_type = VAR_STRING;
507 argvar.vval.v_string = cmd;
508 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200509 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200510}
511
512/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200513 * Free the scrollback buffer for "term".
514 */
515 static void
516free_scrollback(term_T *term)
517{
518 int i;
519
520 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
521 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
522 ga_clear(&term->tl_scrollback);
523}
524
525/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200526 * Free a terminal and everything it refers to.
527 * Kills the job if there is one.
528 * Called when wiping out a buffer.
529 */
530 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200531free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200532{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200533 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200534 term_T *tp;
535
536 if (term == NULL)
537 return;
538 if (first_term == term)
539 first_term = term->tl_next;
540 else
541 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
542 if (tp->tl_next == term)
543 {
544 tp->tl_next = term->tl_next;
545 break;
546 }
547
548 if (term->tl_job != NULL)
549 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200550 if (term->tl_job->jv_status != JOB_ENDED
551 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200552 job_stop(term->tl_job, NULL, "kill");
553 job_unref(term->tl_job);
554 }
555
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200556 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200557
558 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200559 vim_free(term->tl_title);
560 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200561 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200562 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200563 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200564 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200565 if (in_terminal_loop == term)
566 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200567}
568
569/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200570 * Write job output "msg[len]" to the vterm.
571 */
572 static void
573term_write_job_output(term_T *term, char_u *msg, size_t len)
574{
575 VTerm *vterm = term->tl_vterm;
576 char_u *p;
577 size_t done;
578 size_t len_now;
579
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200580 if (term_nl_does_cr)
581 vterm_input_write(vterm, (char *)msg, len);
582 else
583 /* need to convert NL to CR-NL */
584 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200585 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200586 for (p = msg + done; p < msg + len; )
587 {
588 if (*p == NL)
589 break;
590 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
591 }
592 len_now = p - msg - done;
593 vterm_input_write(vterm, (char *)msg + done, len_now);
594 if (p < msg + len && *p == NL)
595 {
596 vterm_input_write(vterm, "\r\n", 2);
597 ++len_now;
598 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200599 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200600
601 /* this invokes the damage callbacks */
602 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
603}
604
Bram Moolenaar1c844932017-07-24 23:36:41 +0200605 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200606update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200607{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200608 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200609 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200610 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200611 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200612 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200613 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200614 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200615 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200616#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200617 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200618 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200619#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200620 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200621}
622
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200623/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200624 * Invoked when "msg" output from a job was received. Write it to the terminal
625 * of "buffer".
626 */
627 void
628write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
629{
630 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200631 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200632
Bram Moolenaard85f2712017-07-28 21:51:57 +0200633 if (term->tl_vterm == NULL)
634 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200635 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200636 return;
637 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200638 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200639 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200640
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200641 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
642 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200643 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200644 {
645 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200646 ch_log(term->tl_job->jv_channel, "updating screen");
647 if (buffer == curbuf)
648 {
649 update_screen(0);
650 update_cursor(term, TRUE);
651 }
652 else
653 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200654 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200655}
656
657/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200658 * Send a mouse position and click to the vterm
659 */
660 static int
661term_send_mouse(VTerm *vterm, int button, int pressed)
662{
663 VTermModifier mod = VTERM_MOD_NONE;
664
665 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
666 mouse_col - W_WINCOL(curwin), mod);
667 vterm_mouse_button(vterm, button, pressed, mod);
668 return TRUE;
669}
670
671/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200672 * Convert typed key "c" into bytes to send to the job.
673 * Return the number of bytes in "buf".
674 */
675 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200676term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200677{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200678 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200679 VTermKey key = VTERM_KEY_NONE;
680 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200681 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200682
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200683 switch (c)
684 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200685 case CAR: c = term_enter_char; break;
686 /* don't use VTERM_KEY_BACKSPACE, it always
687 * becomes 0x7f DEL */
688 case K_BS: c = term_backspace_char; break;
689
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200690 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200691 case K_DEL: key = VTERM_KEY_DEL; break;
692 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200693 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
694 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200695 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200696 case K_S_END: mod = VTERM_MOD_SHIFT;
697 key = VTERM_KEY_END; break;
698 case K_C_END: mod = VTERM_MOD_CTRL;
699 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200700 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
701 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
702 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
703 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
704 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
705 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
706 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
707 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
708 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
709 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
710 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
711 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
712 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200713 case K_S_HOME: mod = VTERM_MOD_SHIFT;
714 key = VTERM_KEY_HOME; break;
715 case K_C_HOME: mod = VTERM_MOD_CTRL;
716 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200717 case K_INS: key = VTERM_KEY_INS; break;
718 case K_K0: key = VTERM_KEY_KP_0; break;
719 case K_K1: key = VTERM_KEY_KP_1; break;
720 case K_K2: key = VTERM_KEY_KP_2; break;
721 case K_K3: key = VTERM_KEY_KP_3; break;
722 case K_K4: key = VTERM_KEY_KP_4; break;
723 case K_K5: key = VTERM_KEY_KP_5; break;
724 case K_K6: key = VTERM_KEY_KP_6; break;
725 case K_K7: key = VTERM_KEY_KP_7; break;
726 case K_K8: key = VTERM_KEY_KP_8; break;
727 case K_K9: key = VTERM_KEY_KP_9; break;
728 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
729 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
730 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
731 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
732 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
733 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
734 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
735 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
736 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
737 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
738 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
739 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
740 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200741 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
742 key = VTERM_KEY_LEFT; break;
743 case K_C_LEFT: mod = VTERM_MOD_CTRL;
744 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200745 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
746 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
747 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200748 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
749 key = VTERM_KEY_RIGHT; break;
750 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
751 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200752 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200753 case K_S_UP: mod = VTERM_MOD_SHIFT;
754 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200755 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200756
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200757 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
758 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
759 case K_MOUSELEFT: /* TODO */ return 0;
760 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200761
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200762 case K_LEFTMOUSE:
763 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
764 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
765 case K_LEFTRELEASE:
766 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
767 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
768 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
769 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
770 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
771 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
772 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
773 case K_X1MOUSE: /* TODO */ return 0;
774 case K_X1DRAG: /* TODO */ return 0;
775 case K_X1RELEASE: /* TODO */ return 0;
776 case K_X2MOUSE: /* TODO */ return 0;
777 case K_X2DRAG: /* TODO */ return 0;
778 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200779
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200780 case K_IGNORE: return 0;
781 case K_NOP: return 0;
782 case K_UNDO: return 0;
783 case K_HELP: return 0;
784 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
785 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
786 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
787 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
788 case K_SELECT: return 0;
789#ifdef FEAT_GUI
790 case K_VER_SCROLLBAR: return 0;
791 case K_HOR_SCROLLBAR: return 0;
792#endif
793#ifdef FEAT_GUI_TABLINE
794 case K_TABLINE: return 0;
795 case K_TABMENU: return 0;
796#endif
797#ifdef FEAT_NETBEANS_INTG
798 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
799#endif
800#ifdef FEAT_DND
801 case K_DROP: return 0;
802#endif
803#ifdef FEAT_AUTOCMD
804 case K_CURSORHOLD: return 0;
805#endif
806 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
807 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200808 }
809
810 /*
811 * Convert special keys to vterm keys:
812 * - Write keys to vterm: vterm_keyboard_key()
813 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200814 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200815 */
816 if (key != VTERM_KEY_NONE)
817 /* Special key, let vterm convert it. */
818 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200819 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200820 /* Normal character, let vterm convert it. */
821 vterm_keyboard_unichar(vterm, c, mod);
822
823 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200824 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200825}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200826
Bram Moolenaar938783d2017-07-16 20:13:26 +0200827/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200828 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200829 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200830 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200831term_job_running(term_T *term)
832{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200833 /* Also consider the job finished when the channel is closed, to avoid a
834 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200835 return term != NULL
836 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200837 && term->tl_job->jv_status == JOB_STARTED
838 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200839}
840
841/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200842 * Add the last line of the scrollback buffer to the buffer in the window.
843 */
844 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200845add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200846{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200847 buf_T *buf = term->tl_buffer;
848 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
849 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200850
Bram Moolenaar58302322017-08-22 20:33:53 +0200851#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +0200852 if (!enc_utf8 && enc_codepage > 0)
853 {
854 WCHAR *ret = NULL;
855 int length = 0;
856
857 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
858 &ret, &length);
859 if (ret != NULL)
860 {
861 WideCharToMultiByte_alloc(enc_codepage, 0,
862 ret, length, (char **)&text, &len, 0, 0);
863 vim_free(ret);
864 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
865 vim_free(text);
866 }
867 }
868 else
869#endif
870 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200871 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200872 {
873 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200874 curbuf = buf;
875 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200876 curbuf = curwin->w_buffer;
877 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200878}
879
880/*
881 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200882 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200883 */
884 static void
885move_terminal_to_buffer(term_T *term)
886{
887 win_T *wp;
888 int len;
889 int lines_skipped = 0;
890 VTermPos pos;
891 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200892 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200893 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200894
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200895 if (term->tl_vterm == NULL)
896 return;
897 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200898 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
899 {
900 len = 0;
901 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
902 if (vterm_screen_get_cell(screen, pos, &cell) != 0
903 && cell.chars[0] != NUL)
904 len = pos.col + 1;
905
906 if (len == 0)
907 ++lines_skipped;
908 else
909 {
910 while (lines_skipped > 0)
911 {
912 /* Line was skipped, add an empty line. */
913 --lines_skipped;
914 if (ga_grow(&term->tl_scrollback, 1) == OK)
915 {
916 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
917 + term->tl_scrollback.ga_len;
918
919 line->sb_cols = 0;
920 line->sb_cells = NULL;
921 ++term->tl_scrollback.ga_len;
922
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200923 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200924 }
925 }
926
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200927 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200928 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
929 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200930 garray_T ga;
931 int width;
932 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200933 + term->tl_scrollback.ga_len;
934
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200935 ga_init2(&ga, 1, 100);
936 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200937 {
938 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200939 {
940 width = 1;
941 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
942 if (ga_grow(&ga, 1) == OK)
943 ga.ga_len += mb_char2bytes(' ',
944 (char_u *)ga.ga_data + ga.ga_len);
945 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200946 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200947 {
948 width = cell.width;
949
950 p[pos.col].width = cell.width;
951 p[pos.col].attrs = cell.attrs;
952 p[pos.col].fg = cell.fg;
953 p[pos.col].bg = cell.bg;
954
955 if (ga_grow(&ga, MB_MAXBYTES) == OK)
956 {
957 int i;
958 int c;
959
960 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +0200961 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200962 (char_u *)ga.ga_data + ga.ga_len);
963 }
964 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200965 }
966 line->sb_cols = len;
967 line->sb_cells = p;
968 ++term->tl_scrollback.ga_len;
969
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200970 if (ga_grow(&ga, 1) == FAIL)
971 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
972 else
973 {
974 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
975 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
976 }
977 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200978 }
979 else
980 vim_free(p);
981 }
982 }
983
984 FOR_ALL_WINDOWS(wp)
985 {
986 if (wp->w_buffer == term->tl_buffer)
987 {
988 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
989 wp->w_cursor.col = 0;
990 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200991 if (wp->w_cursor.lnum >= wp->w_height)
992 {
993 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
994
995 if (wp->w_topline < min_topline)
996 wp->w_topline = min_topline;
997 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200998 redraw_win_later(wp, NOT_VALID);
999 }
1000 }
1001}
1002
1003 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001004set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001005{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001006 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001007 vim_free(term->tl_status_text);
1008 term->tl_status_text = NULL;
1009 if (term->tl_buffer == curbuf)
1010 maketitle();
1011}
1012
1013/*
1014 * Called after the job if finished and Terminal mode is not active:
1015 * Move the vterm contents into the scrollback buffer and free the vterm.
1016 */
1017 static void
1018cleanup_vterm(term_T *term)
1019{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001020 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001021 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001022 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001023 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001024}
1025
1026/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001027 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001028 * Suspends updating the terminal window.
1029 */
1030 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001031term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001032{
1033 term_T *term = curbuf->b_term;
1034
1035 /* Append the current terminal contents to the buffer. */
1036 move_terminal_to_buffer(term);
1037
Bram Moolenaar6d819742017-08-06 14:57:49 +02001038 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001039
Bram Moolenaar6d819742017-08-06 14:57:49 +02001040 /* Move the window cursor to the position of the cursor in the
1041 * terminal. */
1042 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1043 + term->tl_cursor_pos.row + 1;
1044 check_cursor();
1045 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001046
Bram Moolenaar6d819742017-08-06 14:57:49 +02001047 /* Display the same lines as in the terminal. */
1048 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001049}
1050
1051/*
1052 * Returns TRUE if the current window contains a terminal and we are in
1053 * Terminal-Normal mode.
1054 */
1055 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001056term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001057{
1058 term_T *term = curbuf->b_term;
1059
Bram Moolenaar6d819742017-08-06 14:57:49 +02001060 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001061}
1062
1063/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001064 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001065 * Restores updating the terminal window.
1066 */
1067 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001068term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001069{
1070 term_T *term = curbuf->b_term;
1071 sb_line_T *line;
1072 garray_T *gap;
1073
1074 /* Remove the terminal contents from the scrollback and the buffer. */
1075 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001076 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1077 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001078 {
1079 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1080 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1081 vim_free(line->sb_cells);
1082 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001083 }
1084 check_cursor();
1085
Bram Moolenaar6d819742017-08-06 14:57:49 +02001086 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001087
1088 if (term->tl_channel_closed)
1089 cleanup_vterm(term);
1090 redraw_buf_and_status_later(curbuf, NOT_VALID);
1091}
1092
1093/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001094 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001095 * Note: while waiting a terminal may be closed and freed if the channel is
1096 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001097 * TODO: use terminal mode mappings.
1098 */
1099 static int
1100term_vgetc()
1101{
1102 int c;
1103
1104 ++no_mapping;
1105 ++allow_keys;
1106 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001107#ifdef WIN3264
1108 ctrl_break_was_pressed = FALSE;
1109#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001110 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001111 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001112 --no_mapping;
1113 --allow_keys;
1114 return c;
1115}
1116
1117/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001118 * Get the part that is connected to the tty. Normally this is PART_IN, but
1119 * when writing buffer lines to the job it can be another. This makes it
1120 * possible to do "1,5term vim -".
1121 */
1122 static ch_part_T
1123get_tty_part(term_T *term)
1124{
1125#ifdef UNIX
1126 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1127 int i;
1128
1129 for (i = 0; i < 3; ++i)
1130 {
1131 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1132
1133 if (isatty(fd))
1134 return parts[i];
1135 }
1136#endif
1137 return PART_IN;
1138}
1139
1140/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001141 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001142 * Return FAIL when the key needs to be handled in Normal mode.
1143 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001144 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001145 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001146send_keys_to_term(term_T *term, int c, int typed)
1147{
1148 char msg[KEY_BUF_LEN];
1149 size_t len;
1150 static int mouse_was_outside = FALSE;
1151 int dragging_outside = FALSE;
1152
1153 /* Catch keys that need to be handled as in Normal mode. */
1154 switch (c)
1155 {
1156 case NUL:
1157 case K_ZERO:
1158 if (typed)
1159 stuffcharReadbuff(c);
1160 return FAIL;
1161
1162 case K_IGNORE:
1163 return FAIL;
1164
1165 case K_LEFTDRAG:
1166 case K_MIDDLEDRAG:
1167 case K_RIGHTDRAG:
1168 case K_X1DRAG:
1169 case K_X2DRAG:
1170 dragging_outside = mouse_was_outside;
1171 /* FALLTHROUGH */
1172 case K_LEFTMOUSE:
1173 case K_LEFTMOUSE_NM:
1174 case K_LEFTRELEASE:
1175 case K_LEFTRELEASE_NM:
1176 case K_MIDDLEMOUSE:
1177 case K_MIDDLERELEASE:
1178 case K_RIGHTMOUSE:
1179 case K_RIGHTRELEASE:
1180 case K_X1MOUSE:
1181 case K_X1RELEASE:
1182 case K_X2MOUSE:
1183 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001184
1185 case K_MOUSEUP:
1186 case K_MOUSEDOWN:
1187 case K_MOUSELEFT:
1188 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001189 if (mouse_row < W_WINROW(curwin)
1190 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1191 || mouse_col < W_WINCOL(curwin)
1192 || mouse_col >= W_ENDCOL(curwin)
1193 || dragging_outside)
1194 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001195 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001196 if (typed)
1197 {
1198 stuffcharReadbuff(c);
1199 mouse_was_outside = TRUE;
1200 }
1201 return FAIL;
1202 }
1203 }
1204 if (typed)
1205 mouse_was_outside = FALSE;
1206
1207 /* Convert the typed key to a sequence of bytes for the job. */
1208 len = term_convert_key(term, c, msg);
1209 if (len > 0)
1210 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001211 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1212 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001213
1214 return OK;
1215}
1216
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001217 static void
1218position_cursor(win_T *wp, VTermPos *pos)
1219{
1220 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1221 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1222 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1223}
1224
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001225/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001226 * Handle CTRL-W "": send register contents to the job.
1227 */
1228 static void
1229term_paste_register(int prev_c UNUSED)
1230{
1231 int c;
1232 list_T *l;
1233 listitem_T *item;
1234 long reglen = 0;
1235 int type;
1236
1237#ifdef FEAT_CMDL_INFO
1238 if (add_to_showcmd(prev_c))
1239 if (add_to_showcmd('"'))
1240 out_flush();
1241#endif
1242 c = term_vgetc();
1243#ifdef FEAT_CMDL_INFO
1244 clear_showcmd();
1245#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001246 if (!term_use_loop())
1247 /* job finished while waiting for a character */
1248 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001249
1250 /* CTRL-W "= prompt for expression to evaluate. */
1251 if (c == '=' && get_expr_register() != '=')
1252 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001253 if (!term_use_loop())
1254 /* job finished while waiting for a character */
1255 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001256
1257 l = (list_T *)get_reg_contents(c, GREG_LIST);
1258 if (l != NULL)
1259 {
1260 type = get_reg_type(c, &reglen);
1261 for (item = l->lv_first; item != NULL; item = item->li_next)
1262 {
1263 char_u *s = get_tv_string(&item->li_tv);
1264
1265 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1266 s, STRLEN(s), NULL);
1267 if (item->li_next != NULL || type == MLINE)
1268 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1269 (char_u *)"\r", 1, NULL);
1270 }
1271 list_free(l);
1272 }
1273}
1274
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001275#if defined(FEAT_GUI) || defined(PROTO)
1276/*
1277 * Return TRUE when the cursor of the terminal should be displayed.
1278 */
1279 int
1280use_terminal_cursor()
1281{
1282 return in_terminal_loop != NULL;
1283}
1284
1285 cursorentry_T *
1286term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1287{
1288 term_T *term = in_terminal_loop;
1289 static cursorentry_T entry;
1290
1291 vim_memset(&entry, 0, sizeof(entry));
1292 entry.shape = entry.mshape =
1293 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1294 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1295 SHAPE_BLOCK;
1296 entry.percentage = 20;
1297 if (term->tl_cursor_blink)
1298 {
1299 entry.blinkwait = 700;
1300 entry.blinkon = 400;
Bram Moolenaar58302322017-08-22 20:33:53 +02001301 entry.blinkoff = 250;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001302 }
1303 *fg = gui.back_pixel;
1304 if (term->tl_cursor_color == NULL)
1305 *bg = gui.norm_pixel;
1306 else
1307 *bg = color_name2handle(term->tl_cursor_color);
1308 entry.name = "n";
1309 entry.used_for = SHAPE_CURSOR;
1310
1311 return &entry;
1312}
1313#endif
1314
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001315static int did_change_cursor = FALSE;
1316
1317 static void
1318may_set_cursor_props(term_T *term)
1319{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001320#ifdef FEAT_GUI
1321 /* For the GUI the cursor properties are obtained with
1322 * term_get_cursor_shape(). */
1323 if (gui.in_use)
1324 return;
1325#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001326 if (in_terminal_loop == term)
1327 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001328 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001329 if (term->tl_cursor_color != NULL)
1330 term_cursor_color(term->tl_cursor_color);
1331 else
1332 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001333 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1334 }
1335}
1336
1337 static void
1338may_restore_cursor_props(void)
1339{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001340#ifdef FEAT_GUI
1341 if (gui.in_use)
1342 return;
1343#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001344 if (did_change_cursor)
1345 {
1346 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001347 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001348 /* this will restore the initial cursor style, if possible */
1349 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001350 }
1351}
1352
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001353/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001354 * Returns TRUE if the current window contains a terminal and we are sending
1355 * keys to the job.
1356 */
1357 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001358term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001359{
1360 term_T *term = curbuf->b_term;
1361
1362 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001363 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001364 && term->tl_vterm != NULL
1365 && term_job_running(term);
1366}
1367
1368/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001369 * Wait for input and send it to the job.
1370 * Return when the start of a CTRL-W command is typed or anything else that
1371 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001372 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1373 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001374 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001375 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001376terminal_loop(void)
1377{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001378 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001379 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001380 int ret;
1381
Bram Moolenaar679653e2017-08-13 14:13:19 +02001382 /* Remember the terminal we are sending keys to. However, the terminal
1383 * might be closed while waiting for a character, e.g. typing "exit" in a
1384 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1385 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001386 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001387
1388 if (*curwin->w_p_tk != NUL)
1389 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001390 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001391 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001392
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001393#ifdef UNIX
1394 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001395 int part = get_tty_part(curbuf->b_term);
1396 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001397
1398 if (isatty(fd))
1399 {
1400 ttyinfo_T info;
1401
1402 /* Get the current backspace and enter characters of the pty. */
1403 if (get_tty_info(fd, &info) == OK)
1404 {
1405 term_backspace_char = info.backspace;
1406 term_enter_char = info.enter;
1407 term_nl_does_cr = info.nl_does_cr;
1408 }
1409 }
1410 }
1411#endif
1412
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001413 for (;;)
1414 {
1415 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001416 /* Repeat redrawing in case a message is received while redrawing. */
1417 while (curwin->w_redr_type != 0)
1418 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001419 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001420
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001421 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001422 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001423 /* job finished while waiting for a character */
1424 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001425 if (c == K_IGNORE)
1426 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001427
Bram Moolenaarfae42832017-08-01 22:24:26 +02001428#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001429 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001430 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001431 if (ctrl_break_was_pressed)
1432 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001433#endif
1434
Bram Moolenaar69198192017-08-05 14:10:48 +02001435 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001436 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001437 int prev_c = c;
1438
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001439#ifdef FEAT_CMDL_INFO
1440 if (add_to_showcmd(c))
1441 out_flush();
1442#endif
1443 c = term_vgetc();
1444#ifdef FEAT_CMDL_INFO
1445 clear_showcmd();
1446#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001447 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001448 /* job finished while waiting for a character */
1449 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001450
Bram Moolenaar69198192017-08-05 14:10:48 +02001451 if (prev_c == Ctrl_BSL)
1452 {
1453 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001454 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001455 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1456 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001457 ret = FAIL;
1458 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001459 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001460 /* Send both keys to the terminal. */
1461 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1462 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001463 else if (c == Ctrl_C)
1464 {
1465 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1466 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1467 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001468 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001469 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001470 /* "CTRL-W .": send CTRL-W to the job */
1471 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001472 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001473 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001474 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001475 /* CTRL-W N : go to Terminal-Normal mode. */
1476 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001477 ret = FAIL;
1478 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001479 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001480 else if (c == '"')
1481 {
1482 term_paste_register(prev_c);
1483 continue;
1484 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001485 else if (termkey == 0 || c != termkey)
1486 {
1487 stuffcharReadbuff(Ctrl_W);
1488 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001489 ret = OK;
1490 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001491 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001492 }
Bram Moolenaar58302322017-08-22 20:33:53 +02001493# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02001494 if (!enc_utf8 && has_mbyte && c >= 0x80)
1495 {
1496 WCHAR wc;
1497 char_u mb[3];
1498
1499 mb[0] = (unsigned)c >> 8;
1500 mb[1] = c;
1501 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1502 c = wc;
1503 }
1504# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001505 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001506 {
1507 ret = OK;
1508 goto theend;
1509 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001510 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001511 ret = FAIL;
1512
1513theend:
1514 in_terminal_loop = NULL;
1515 may_restore_cursor_props();
1516 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001517}
1518
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001519/*
1520 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001521 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001522 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001523 */
1524 void
1525term_job_ended(job_T *job)
1526{
1527 term_T *term;
1528 int did_one = FALSE;
1529
1530 for (term = first_term; term != NULL; term = term->tl_next)
1531 if (term->tl_job == job)
1532 {
1533 vim_free(term->tl_title);
1534 term->tl_title = NULL;
1535 vim_free(term->tl_status_text);
1536 term->tl_status_text = NULL;
1537 redraw_buf_and_status_later(term->tl_buffer, VALID);
1538 did_one = TRUE;
1539 }
1540 if (did_one)
1541 redraw_statuslines();
1542 if (curbuf->b_term != NULL)
1543 {
1544 if (curbuf->b_term->tl_job == job)
1545 maketitle();
1546 update_cursor(curbuf->b_term, TRUE);
1547 }
1548}
1549
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001550 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001551may_toggle_cursor(term_T *term)
1552{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001553 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001554 {
1555 if (term->tl_cursor_visible)
1556 cursor_on();
1557 else
1558 cursor_off();
1559 }
1560}
1561
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001562/*
1563 * Reverse engineer the RGB value into a cterm color index.
1564 * First color is 1. Return 0 if no match found.
1565 */
1566 static int
1567color2index(VTermColor *color, int fg, int *boldp)
1568{
1569 int red = color->red;
1570 int blue = color->blue;
1571 int green = color->green;
1572
1573 /* The argument for lookup_color() is for the color_names[] table. */
1574 if (red == 0)
1575 {
1576 if (green == 0)
1577 {
1578 if (blue == 0)
1579 return lookup_color(0, fg, boldp) + 1; /* black */
1580 if (blue == 224)
1581 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1582 }
1583 else if (green == 224)
1584 {
1585 if (blue == 0)
1586 return lookup_color(2, fg, boldp) + 1; /* dark green */
1587 if (blue == 224)
1588 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1589 }
1590 }
1591 else if (red == 224)
1592 {
1593 if (green == 0)
1594 {
1595 if (blue == 0)
1596 return lookup_color(4, fg, boldp) + 1; /* dark red */
1597 if (blue == 224)
1598 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1599 }
1600 else if (green == 224)
1601 {
1602 if (blue == 0)
1603 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1604 if (blue == 224)
1605 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1606 }
1607 }
1608 else if (red == 128)
1609 {
1610 if (green == 128 && blue == 128)
1611 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1612 }
1613 else if (red == 255)
1614 {
1615 if (green == 64)
1616 {
1617 if (blue == 64)
1618 return lookup_color(20, fg, boldp) + 1; /* light red */
1619 if (blue == 255)
1620 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1621 }
1622 else if (green == 255)
1623 {
1624 if (blue == 64)
1625 return lookup_color(24, fg, boldp) + 1; /* yellow */
1626 if (blue == 255)
1627 return lookup_color(26, fg, boldp) + 1; /* white */
1628 }
1629 }
1630 else if (red == 64)
1631 {
1632 if (green == 64)
1633 {
1634 if (blue == 255)
1635 return lookup_color(14, fg, boldp) + 1; /* light blue */
1636 }
1637 else if (green == 255)
1638 {
1639 if (blue == 64)
1640 return lookup_color(16, fg, boldp) + 1; /* light green */
1641 if (blue == 255)
1642 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1643 }
1644 }
1645 if (t_colors >= 256)
1646 {
1647 if (red == blue && red == green)
1648 {
1649 /* 24-color greyscale */
1650 static int cutoff[23] = {
1651 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1652 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1653 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1654 int i;
1655
1656 for (i = 0; i < 23; ++i)
1657 if (red < cutoff[i])
1658 return i + 233;
1659 return 256;
1660 }
1661
1662 /* 216-color cube */
1663 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001664 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001665 + (blue + 25) / 0x33;
1666 }
1667 return 0;
1668}
1669
1670/*
1671 * Convert the attributes of a vterm cell into an attribute index.
1672 */
1673 static int
1674cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1675{
1676 int attr = 0;
1677
1678 if (cellattrs.bold)
1679 attr |= HL_BOLD;
1680 if (cellattrs.underline)
1681 attr |= HL_UNDERLINE;
1682 if (cellattrs.italic)
1683 attr |= HL_ITALIC;
1684 if (cellattrs.strike)
1685 attr |= HL_STANDOUT;
1686 if (cellattrs.reverse)
1687 attr |= HL_INVERSE;
1688
1689#ifdef FEAT_GUI
1690 if (gui.in_use)
1691 {
1692 guicolor_T fg, bg;
1693
1694 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1695 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1696 return get_gui_attr_idx(attr, fg, bg);
1697 }
1698 else
1699#endif
1700#ifdef FEAT_TERMGUICOLORS
1701 if (p_tgc)
1702 {
1703 guicolor_T fg, bg;
1704
1705 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1706 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1707
1708 return get_tgc_attr_idx(attr, fg, bg);
1709 }
1710 else
1711#endif
1712 {
1713 int bold = MAYBE;
1714 int fg = color2index(&cellfg, TRUE, &bold);
1715 int bg = color2index(&cellbg, FALSE, &bold);
1716
1717 /* with 8 colors set the bold attribute to get a bright foreground */
1718 if (bold == TRUE)
1719 attr |= HL_BOLD;
1720 return get_cterm_attr_idx(attr, fg, bg);
1721 }
1722 return 0;
1723}
1724
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001725 static int
1726handle_damage(VTermRect rect, void *user)
1727{
1728 term_T *term = (term_T *)user;
1729
1730 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1731 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1732 redraw_buf_later(term->tl_buffer, NOT_VALID);
1733 return 1;
1734}
1735
1736 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001737handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001738{
1739 term_T *term = (term_T *)user;
1740
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001741 /* Scrolling up is done much more efficiently by deleting lines instead of
1742 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001743 if (dest.start_col == src.start_col
1744 && dest.end_col == src.end_col
1745 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001746 {
1747 win_T *wp;
1748 VTermColor fg, bg;
1749 VTermScreenCellAttrs attr;
1750 int clear_attr;
1751
1752 /* Set the color to clear lines with. */
1753 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1754 &fg, &bg);
1755 vim_memset(&attr, 0, sizeof(attr));
1756 clear_attr = cell2attr(attr, fg, bg);
1757
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001758 FOR_ALL_WINDOWS(wp)
1759 {
1760 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001761 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001762 src.start_row - dest.start_row, FALSE, FALSE,
1763 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001764 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001765 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001766 redraw_buf_later(term->tl_buffer, NOT_VALID);
1767 return 1;
1768}
1769
1770 static int
1771handle_movecursor(
1772 VTermPos pos,
1773 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001774 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001775 void *user)
1776{
1777 term_T *term = (term_T *)user;
1778 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001779
1780 term->tl_cursor_pos = pos;
1781 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001782
1783 FOR_ALL_WINDOWS(wp)
1784 {
1785 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001786 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001787 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001788 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001789 {
1790 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001791 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001792 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001793
1794 return 1;
1795}
1796
Bram Moolenaar21554412017-07-24 21:44:43 +02001797 static int
1798handle_settermprop(
1799 VTermProp prop,
1800 VTermValue *value,
1801 void *user)
1802{
1803 term_T *term = (term_T *)user;
1804
1805 switch (prop)
1806 {
1807 case VTERM_PROP_TITLE:
1808 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001809 /* a blank title isn't useful, make it empty, so that "running" is
1810 * displayed */
1811 if (*skipwhite((char_u *)value->string) == NUL)
1812 term->tl_title = NULL;
1813 else
1814 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001815 vim_free(term->tl_status_text);
1816 term->tl_status_text = NULL;
1817 if (term == curbuf->b_term)
1818 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001819 break;
1820
1821 case VTERM_PROP_CURSORVISIBLE:
1822 term->tl_cursor_visible = value->boolean;
1823 may_toggle_cursor(term);
1824 out_flush();
1825 break;
1826
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001827 case VTERM_PROP_CURSORBLINK:
1828 term->tl_cursor_blink = value->boolean;
1829 may_set_cursor_props(term);
1830 break;
1831
1832 case VTERM_PROP_CURSORSHAPE:
1833 term->tl_cursor_shape = value->number;
1834 may_set_cursor_props(term);
1835 break;
1836
1837 case VTERM_PROP_CURSORCOLOR:
1838 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001839 if (*value->string == NUL)
1840 term->tl_cursor_color = NULL;
1841 else
1842 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001843 may_set_cursor_props(term);
1844 break;
1845
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001846 case VTERM_PROP_ALTSCREEN:
1847 /* TODO: do anything else? */
1848 term->tl_using_altscreen = value->boolean;
1849 break;
1850
Bram Moolenaar21554412017-07-24 21:44:43 +02001851 default:
1852 break;
1853 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001854 /* Always return 1, otherwise vterm doesn't store the value internally. */
1855 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001856}
1857
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001858/*
1859 * The job running in the terminal resized the terminal.
1860 */
1861 static int
1862handle_resize(int rows, int cols, void *user)
1863{
1864 term_T *term = (term_T *)user;
1865 win_T *wp;
1866
1867 term->tl_rows = rows;
1868 term->tl_cols = cols;
1869 FOR_ALL_WINDOWS(wp)
1870 {
1871 if (wp->w_buffer == term->tl_buffer)
1872 {
1873 win_setheight_win(rows, wp);
1874 win_setwidth_win(cols, wp);
1875 }
1876 }
1877
1878 redraw_buf_later(term->tl_buffer, NOT_VALID);
1879 return 1;
1880}
1881
Bram Moolenaard85f2712017-07-28 21:51:57 +02001882/*
1883 * Handle a line that is pushed off the top of the screen.
1884 */
1885 static int
1886handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1887{
1888 term_T *term = (term_T *)user;
1889
1890 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001891 if (ga_grow(&term->tl_scrollback, 1) == OK)
1892 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001893 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001894 int len = 0;
1895 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001896 int c;
1897 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001898 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001899 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001900
1901 /* do not store empty cells at the end */
1902 for (i = 0; i < cols; ++i)
1903 if (cells[i].chars[0] != 0)
1904 len = i + 1;
1905
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001906 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001907 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001908 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001909 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001910 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001911 for (col = 0; col < len; col += cells[col].width)
1912 {
1913 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1914 {
1915 ga.ga_len = 0;
1916 break;
1917 }
1918 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1919 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1920 (char_u *)ga.ga_data + ga.ga_len);
1921 p[col].width = cells[col].width;
1922 p[col].attrs = cells[col].attrs;
1923 p[col].fg = cells[col].fg;
1924 p[col].bg = cells[col].bg;
1925 }
1926 }
1927 if (ga_grow(&ga, 1) == FAIL)
1928 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1929 else
1930 {
1931 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1932 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1933 }
1934 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001935
1936 line = (sb_line_T *)term->tl_scrollback.ga_data
1937 + term->tl_scrollback.ga_len;
1938 line->sb_cols = len;
1939 line->sb_cells = p;
1940 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001941 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001942 }
1943 return 0; /* ignored */
1944}
1945
Bram Moolenaar21554412017-07-24 21:44:43 +02001946static VTermScreenCallbacks screen_callbacks = {
1947 handle_damage, /* damage */
1948 handle_moverect, /* moverect */
1949 handle_movecursor, /* movecursor */
1950 handle_settermprop, /* settermprop */
1951 NULL, /* bell */
1952 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001953 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001954 NULL /* sb_popline */
1955};
1956
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001957/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001958 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001959 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001960 */
1961 void
1962term_channel_closed(channel_T *ch)
1963{
1964 term_T *term;
1965 int did_one = FALSE;
1966
1967 for (term = first_term; term != NULL; term = term->tl_next)
1968 if (term->tl_job == ch->ch_job)
1969 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001970 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001971 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001972
Bram Moolenaard85f2712017-07-28 21:51:57 +02001973 vim_free(term->tl_title);
1974 term->tl_title = NULL;
1975 vim_free(term->tl_status_text);
1976 term->tl_status_text = NULL;
1977
Bram Moolenaar423802d2017-07-30 16:52:24 +02001978 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001979 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001980 {
1981 int fnum = term->tl_buffer->b_fnum;
1982
Bram Moolenaar423802d2017-07-30 16:52:24 +02001983 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001984
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001985 if (term->tl_finish == 'c')
1986 {
1987 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001988 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001989 curbuf = term->tl_buffer;
1990 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1991 break;
1992 }
1993 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1994 {
1995 char buf[50];
1996
1997 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001998 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001999 vim_snprintf(buf, sizeof(buf),
2000 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02002001 ? "botright sbuf %d"
2002 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002003 do_cmdline_cmd((char_u *)buf);
2004 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002005 else
2006 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002007 }
2008
Bram Moolenaard85f2712017-07-28 21:51:57 +02002009 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002010 }
2011 if (did_one)
2012 {
2013 redraw_statuslines();
2014
2015 /* Need to break out of vgetc(). */
2016 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002017 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002018
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002019 term = curbuf->b_term;
2020 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002021 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002022 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002023 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002024 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002025 }
2026 }
2027}
2028
2029/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002030 * Called to update a window that contains an active terminal.
2031 * Returns FAIL when there is no terminal running in this window or in
2032 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002033 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002034 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002035term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002036{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002037 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002038 VTerm *vterm;
2039 VTermScreen *screen;
2040 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002041 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002042
Bram Moolenaar6d819742017-08-06 14:57:49 +02002043 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002044 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002045
Bram Moolenaard85f2712017-07-28 21:51:57 +02002046 vterm = term->tl_vterm;
2047 screen = vterm_obtain_screen(vterm);
2048 state = vterm_obtain_state(vterm);
2049
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002050 /*
2051 * If the window was resized a redraw will be triggered and we get here.
2052 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2053 */
2054 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2055 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002056 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002057 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2058 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2059 win_T *twp;
2060
2061 FOR_ALL_WINDOWS(twp)
2062 {
2063 /* When more than one window shows the same terminal, use the
2064 * smallest size. */
2065 if (twp->w_buffer == term->tl_buffer)
2066 {
2067 if (!term->tl_rows_fixed && rows > twp->w_height)
2068 rows = twp->w_height;
2069 if (!term->tl_cols_fixed && cols > twp->w_width)
2070 cols = twp->w_width;
2071 }
2072 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002073
2074 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002075 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002076 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002077 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002078 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002079
2080 /* The cursor may have been moved when resizing. */
2081 vterm_state_get_cursorpos(state, &pos);
2082 position_cursor(wp, &pos);
2083
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002084 /* TODO: Only redraw what changed. */
2085 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002086 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002087 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002088 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002089
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002090 if (pos.row < term->tl_rows)
2091 {
2092 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002093 {
2094 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002095 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002096
Bram Moolenaareeac6772017-07-23 15:48:37 +02002097 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2098 vim_memset(&cell, 0, sizeof(cell));
2099
2100 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002101 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002102 if (c == NUL)
2103 {
2104 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002105 if (enc_utf8)
2106 ScreenLinesUC[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002107 }
2108 else
2109 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002110 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002111 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002112 if (c >= 0x80)
2113 {
2114 ScreenLines[off] = ' ';
2115 ScreenLinesUC[off] = c;
2116 }
2117 else
2118 {
2119 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002120 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002121 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002122 }
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002123#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02002124 else if (has_mbyte && c >= 0x80)
2125 {
2126 char_u mb[MB_MAXBYTES+1];
2127 WCHAR wc = c;
2128
2129 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2130 (char*)mb, 2, 0, 0) > 1)
2131 {
2132 ScreenLines[off] = mb[0];
2133 ScreenLines[off+1] = mb[1];
2134 cell.width = mb_ptr2cells(mb);
2135 }
2136 else
2137 ScreenLines[off] = c;
2138 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002139#endif
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002140 else
Bram Moolenaar740c4332017-08-21 22:01:27 +02002141 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002142 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002143 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002144
2145 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002146 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002147 if (cell.width == 2)
2148 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002149 if (enc_utf8)
2150 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002151 else if (!has_mbyte)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002152 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002153 ++pos.col;
2154 ++off;
2155 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002156 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002157 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002158 else
2159 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002160
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002161 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2162 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002163 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002164
2165 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002166}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002167
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002168/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002169 * Return TRUE if "wp" is a terminal window where the job has finished.
2170 */
2171 int
2172term_is_finished(buf_T *buf)
2173{
2174 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2175}
2176
2177/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002178 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002179 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002180 */
2181 int
2182term_show_buffer(buf_T *buf)
2183{
2184 term_T *term = buf->b_term;
2185
Bram Moolenaar6d819742017-08-06 14:57:49 +02002186 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002187}
2188
2189/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002190 * The current buffer is going to be changed. If there is terminal
2191 * highlighting remove it now.
2192 */
2193 void
2194term_change_in_curbuf(void)
2195{
2196 term_T *term = curbuf->b_term;
2197
2198 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2199 {
2200 free_scrollback(term);
2201 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002202
2203 /* The buffer is now like a normal buffer, it cannot be easily
2204 * abandoned when changed. */
2205 set_string_option_direct((char_u *)"buftype", -1,
2206 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002207 }
2208}
2209
2210/*
2211 * Get the screen attribute for a position in the buffer.
2212 */
2213 int
2214term_get_attr(buf_T *buf, linenr_T lnum, int col)
2215{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002216 term_T *term = buf->b_term;
2217 sb_line_T *line;
2218 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002219
Bram Moolenaar70229f92017-07-29 16:01:53 +02002220 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002221 return 0;
2222 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2223 if (col >= line->sb_cols)
2224 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002225 cellattr = line->sb_cells + col;
2226 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002227}
2228
2229/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002230 * Create a new vterm and initialize it.
2231 */
2232 static void
2233create_vterm(term_T *term, int rows, int cols)
2234{
2235 VTerm *vterm;
2236 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002237 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002238
2239 vterm = vterm_new(rows, cols);
2240 term->tl_vterm = vterm;
2241 screen = vterm_obtain_screen(vterm);
2242 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2243 /* TODO: depends on 'encoding'. */
2244 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002245
2246 /* Vterm uses a default black background. Set it to white when
2247 * 'background' is "light". */
2248 if (*p_bg == 'l')
2249 {
2250 VTermColor fg, bg;
2251
2252 fg.red = fg.green = fg.blue = 0;
2253 bg.red = bg.green = bg.blue = 255;
2254 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2255 }
2256
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002257 /* Required to initialize most things. */
2258 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002259
2260 /* Allow using alternate screen. */
2261 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002262
Bram Moolenaar58302322017-08-22 20:33:53 +02002263 /* For unix do not use a blinking cursor. In an xterm this causes the
2264 * cursor to blink if it's blinking in the xterm.
2265 * We do want a blinking cursor by default on Windows, since that's what
2266 * the default is for a console. */
2267#ifdef WIN3264
2268 value.boolean = 1;
2269#else
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002270 value.boolean = 0;
Bram Moolenaar58302322017-08-22 20:33:53 +02002271#endif
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002272 vterm_state_set_termprop(vterm_obtain_state(vterm),
2273 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002274}
2275
Bram Moolenaar21554412017-07-24 21:44:43 +02002276/*
2277 * Return the text to show for the buffer name and status.
2278 */
2279 char_u *
2280term_get_status_text(term_T *term)
2281{
2282 if (term->tl_status_text == NULL)
2283 {
2284 char_u *txt;
2285 size_t len;
2286
Bram Moolenaar6d819742017-08-06 14:57:49 +02002287 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002288 {
2289 if (term_job_running(term))
2290 txt = (char_u *)_("Terminal");
2291 else
2292 txt = (char_u *)_("Terminal-finished");
2293 }
2294 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002295 txt = term->tl_title;
2296 else if (term_job_running(term))
2297 txt = (char_u *)_("running");
2298 else
2299 txt = (char_u *)_("finished");
2300 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002301 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002302 if (term->tl_status_text != NULL)
2303 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2304 term->tl_buffer->b_fname, txt);
2305 }
2306 return term->tl_status_text;
2307}
2308
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002309/*
2310 * Mark references in jobs of terminals.
2311 */
2312 int
2313set_ref_in_term(int copyID)
2314{
2315 int abort = FALSE;
2316 term_T *term;
2317 typval_T tv;
2318
2319 for (term = first_term; term != NULL; term = term->tl_next)
2320 if (term->tl_job != NULL)
2321 {
2322 tv.v_type = VAR_JOB;
2323 tv.vval.v_job = term->tl_job;
2324 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2325 }
2326 return abort;
2327}
2328
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002329/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002330 * Get the buffer from the first argument in "argvars".
2331 * Returns NULL when the buffer is not for a terminal window.
2332 */
2333 static buf_T *
2334term_get_buf(typval_T *argvars)
2335{
2336 buf_T *buf;
2337
2338 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2339 ++emsg_off;
2340 buf = get_buf_tv(&argvars[0], FALSE);
2341 --emsg_off;
2342 if (buf == NULL || buf->b_term == NULL)
2343 return NULL;
2344 return buf;
2345}
2346
2347/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002348 * "term_getaltscreen(buf)" function
2349 */
2350 void
2351f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2352{
2353 buf_T *buf = term_get_buf(argvars);
2354
2355 if (buf == NULL)
2356 return;
2357 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2358}
2359
2360/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002361 * "term_getattr(attr, name)" function
2362 */
2363 void
2364f_term_getattr(typval_T *argvars, typval_T *rettv)
2365{
2366 int attr;
2367 size_t i;
2368 char_u *name;
2369
2370 static struct {
2371 char *name;
2372 int attr;
2373 } attrs[] = {
2374 {"bold", HL_BOLD},
2375 {"italic", HL_ITALIC},
2376 {"underline", HL_UNDERLINE},
2377 {"strike", HL_STANDOUT},
2378 {"reverse", HL_INVERSE},
2379 };
2380
2381 attr = get_tv_number(&argvars[0]);
2382 name = get_tv_string_chk(&argvars[1]);
2383 if (name == NULL)
2384 return;
2385
2386 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2387 if (STRCMP(name, attrs[i].name) == 0)
2388 {
2389 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2390 break;
2391 }
2392}
2393
2394/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002395 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002396 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002397 void
2398f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002399{
Bram Moolenaar97870002017-07-30 18:28:38 +02002400 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002401 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002402 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002403 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002404
Bram Moolenaar97870002017-07-30 18:28:38 +02002405 if (rettv_list_alloc(rettv) == FAIL)
2406 return;
2407 if (buf == NULL)
2408 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002409 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002410
2411 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002412 list_append_number(l, term->tl_cursor_pos.row + 1);
2413 list_append_number(l, term->tl_cursor_pos.col + 1);
2414
2415 d = dict_alloc();
2416 if (d != NULL)
2417 {
2418 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2419 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2420 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2421 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2422 ? (char_u *)"" : term->tl_cursor_color);
2423 list_append_dict(l, d);
2424 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002425}
2426
2427/*
2428 * "term_getjob(buf)" function
2429 */
2430 void
2431f_term_getjob(typval_T *argvars, typval_T *rettv)
2432{
2433 buf_T *buf = term_get_buf(argvars);
2434
2435 rettv->v_type = VAR_JOB;
2436 rettv->vval.v_job = NULL;
2437 if (buf == NULL)
2438 return;
2439
2440 rettv->vval.v_job = buf->b_term->tl_job;
2441 if (rettv->vval.v_job != NULL)
2442 ++rettv->vval.v_job->jv_refcount;
2443}
2444
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002445 static int
2446get_row_number(typval_T *tv, term_T *term)
2447{
2448 if (tv->v_type == VAR_STRING
2449 && tv->vval.v_string != NULL
2450 && STRCMP(tv->vval.v_string, ".") == 0)
2451 return term->tl_cursor_pos.row;
2452 return (int)get_tv_number(tv) - 1;
2453}
2454
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002455/*
2456 * "term_getline(buf, row)" function
2457 */
2458 void
2459f_term_getline(typval_T *argvars, typval_T *rettv)
2460{
2461 buf_T *buf = term_get_buf(argvars);
2462 term_T *term;
2463 int row;
2464
2465 rettv->v_type = VAR_STRING;
2466 if (buf == NULL)
2467 return;
2468 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002469 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002470
2471 if (term->tl_vterm == NULL)
2472 {
2473 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2474
2475 /* vterm is finished, get the text from the buffer */
2476 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2477 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2478 }
2479 else
2480 {
2481 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2482 VTermRect rect;
2483 int len;
2484 char_u *p;
2485
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002486 if (row < 0 || row >= term->tl_rows)
2487 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002488 len = term->tl_cols * MB_MAXBYTES + 1;
2489 p = alloc(len);
2490 if (p == NULL)
2491 return;
2492 rettv->vval.v_string = p;
2493
2494 rect.start_col = 0;
2495 rect.end_col = term->tl_cols;
2496 rect.start_row = row;
2497 rect.end_row = row + 1;
2498 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2499 }
2500}
2501
2502/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002503 * "term_getscrolled(buf)" function
2504 */
2505 void
2506f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2507{
2508 buf_T *buf = term_get_buf(argvars);
2509
2510 if (buf == NULL)
2511 return;
2512 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2513}
2514
2515/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002516 * "term_getsize(buf)" function
2517 */
2518 void
2519f_term_getsize(typval_T *argvars, typval_T *rettv)
2520{
2521 buf_T *buf = term_get_buf(argvars);
2522 list_T *l;
2523
2524 if (rettv_list_alloc(rettv) == FAIL)
2525 return;
2526 if (buf == NULL)
2527 return;
2528
2529 l = rettv->vval.v_list;
2530 list_append_number(l, buf->b_term->tl_rows);
2531 list_append_number(l, buf->b_term->tl_cols);
2532}
2533
2534/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002535 * "term_getstatus(buf)" function
2536 */
2537 void
2538f_term_getstatus(typval_T *argvars, typval_T *rettv)
2539{
2540 buf_T *buf = term_get_buf(argvars);
2541 term_T *term;
2542 char_u val[100];
2543
2544 rettv->v_type = VAR_STRING;
2545 if (buf == NULL)
2546 return;
2547 term = buf->b_term;
2548
2549 if (term_job_running(term))
2550 STRCPY(val, "running");
2551 else
2552 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002553 if (term->tl_normal_mode)
2554 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002555 rettv->vval.v_string = vim_strsave(val);
2556}
2557
2558/*
2559 * "term_gettitle(buf)" function
2560 */
2561 void
2562f_term_gettitle(typval_T *argvars, typval_T *rettv)
2563{
2564 buf_T *buf = term_get_buf(argvars);
2565
2566 rettv->v_type = VAR_STRING;
2567 if (buf == NULL)
2568 return;
2569
2570 if (buf->b_term->tl_title != NULL)
2571 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2572}
2573
2574/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002575 * "term_gettty(buf)" function
2576 */
2577 void
2578f_term_gettty(typval_T *argvars, typval_T *rettv)
2579{
2580 buf_T *buf = term_get_buf(argvars);
2581 char_u *p;
2582
2583 rettv->v_type = VAR_STRING;
2584 if (buf == NULL)
2585 return;
2586 if (buf->b_term->tl_job != NULL)
2587 p = buf->b_term->tl_job->jv_tty_name;
2588 else
2589 p = buf->b_term->tl_tty_name;
2590 if (p != NULL)
2591 rettv->vval.v_string = vim_strsave(p);
2592}
2593
2594/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002595 * "term_list()" function
2596 */
2597 void
2598f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2599{
2600 term_T *tp;
2601 list_T *l;
2602
2603 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2604 return;
2605
2606 l = rettv->vval.v_list;
2607 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2608 if (tp != NULL && tp->tl_buffer != NULL)
2609 if (list_append_number(l,
2610 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2611 return;
2612}
2613
2614/*
2615 * "term_scrape(buf, row)" function
2616 */
2617 void
2618f_term_scrape(typval_T *argvars, typval_T *rettv)
2619{
2620 buf_T *buf = term_get_buf(argvars);
2621 VTermScreen *screen = NULL;
2622 VTermPos pos;
2623 list_T *l;
2624 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002625 char_u *p;
2626 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002627
2628 if (rettv_list_alloc(rettv) == FAIL)
2629 return;
2630 if (buf == NULL)
2631 return;
2632 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002633
2634 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002635 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002636
2637 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002638 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002639 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002640 p = NULL;
2641 line = NULL;
2642 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002643 else
2644 {
2645 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2646
2647 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2648 return;
2649 p = ml_get_buf(buf, lnum + 1, FALSE);
2650 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2651 }
2652
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002653 for (pos.col = 0; pos.col < term->tl_cols; )
2654 {
2655 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002656 int width;
2657 VTermScreenCellAttrs attrs;
2658 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002659 char_u rgb[8];
2660 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2661 int off = 0;
2662 int i;
2663
2664 if (screen == NULL)
2665 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002666 cellattr_T *cellattr;
2667 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002668
2669 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002670 if (pos.col >= line->sb_cols)
2671 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002672 cellattr = line->sb_cells + pos.col;
2673 width = cellattr->width;
2674 attrs = cellattr->attrs;
2675 fg = cellattr->fg;
2676 bg = cellattr->bg;
2677 len = MB_PTR2LEN(p);
2678 mch_memmove(mbs, p, len);
2679 mbs[len] = NUL;
2680 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002681 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002682 else
2683 {
2684 VTermScreenCell cell;
2685 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2686 break;
2687 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2688 {
2689 if (cell.chars[i] == 0)
2690 break;
2691 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2692 }
2693 mbs[off] = NUL;
2694 width = cell.width;
2695 attrs = cell.attrs;
2696 fg = cell.fg;
2697 bg = cell.bg;
2698 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002699 dcell = dict_alloc();
2700 list_append_dict(l, dcell);
2701
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002702 dict_add_nr_str(dcell, "chars", 0, mbs);
2703
2704 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002705 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002706 dict_add_nr_str(dcell, "fg", 0, rgb);
2707 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002708 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002709 dict_add_nr_str(dcell, "bg", 0, rgb);
2710
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002711 dict_add_nr_str(dcell, "attr",
2712 cell2attr(attrs, fg, bg), NULL);
2713 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002714
2715 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002716 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002717 ++pos.col;
2718 }
2719}
2720
2721/*
2722 * "term_sendkeys(buf, keys)" function
2723 */
2724 void
2725f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2726{
2727 buf_T *buf = term_get_buf(argvars);
2728 char_u *msg;
2729 term_T *term;
2730
2731 rettv->v_type = VAR_UNKNOWN;
2732 if (buf == NULL)
2733 return;
2734
2735 msg = get_tv_string_chk(&argvars[1]);
2736 if (msg == NULL)
2737 return;
2738 term = buf->b_term;
2739 if (term->tl_vterm == NULL)
2740 return;
2741
2742 while (*msg != NUL)
2743 {
2744 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2745 msg += MB_PTR2LEN(msg);
2746 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002747}
2748
2749/*
2750 * "term_start(command, options)" function
2751 */
2752 void
2753f_term_start(typval_T *argvars, typval_T *rettv)
2754{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002755 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002756
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002757 init_job_options(&opt);
2758 /* TODO: allow more job options */
2759 if (argvars[1].v_type != VAR_UNKNOWN
2760 && get_job_options(&argvars[1], &opt,
2761 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002762 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002763 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002764 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002765 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002766 return;
2767
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002768 if (opt.jo_vertical)
2769 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002770 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002771
2772 if (curbuf->b_term != NULL)
2773 rettv->vval.v_number = curbuf->b_fnum;
2774}
2775
2776/*
2777 * "term_wait" function
2778 */
2779 void
2780f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2781{
2782 buf_T *buf = term_get_buf(argvars);
2783
2784 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002785 {
2786 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002787 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002788 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002789 if (buf->b_term->tl_job == NULL)
2790 {
2791 ch_log(NULL, "term_wait(): no job to wait for");
2792 return;
2793 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002794
2795 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002796 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002797 {
2798 /* The job is dead, keep reading channel I/O until the channel is
2799 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002800 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002801 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2802 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002803 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002804 parse_queued_messages();
2805 ui_delay(10L, FALSE);
2806 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002807 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002808 parse_queued_messages();
2809 }
2810 else
2811 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002812 long wait = 10L;
2813
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002814 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002815 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002816
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002817 /* Wait for some time for any channel I/O. */
2818 if (argvars[1].v_type != VAR_UNKNOWN)
2819 wait = get_tv_number(&argvars[1]);
2820 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002821 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002822
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002823 /* Flushing messages on channels is hopefully sufficient.
2824 * TODO: is there a better way? */
2825 parse_queued_messages();
2826 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002827}
2828
Bram Moolenaara83e3962017-08-17 14:39:07 +02002829# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002830
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002831/**************************************
2832 * 2. MS-Windows implementation.
2833 */
2834
Bram Moolenaara83e3962017-08-17 14:39:07 +02002835# ifndef PROTO
2836
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002837#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2838#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2839
Bram Moolenaar8a773062017-07-24 22:29:21 +02002840void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002841void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002842void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002843BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2844void (*winpty_config_set_initial_size)(void*, int, int);
2845LPCWSTR (*winpty_conin_name)(void*);
2846LPCWSTR (*winpty_conout_name)(void*);
2847LPCWSTR (*winpty_conerr_name)(void*);
2848void (*winpty_free)(void*);
2849void (*winpty_config_free)(void*);
2850void (*winpty_spawn_config_free)(void*);
2851void (*winpty_error_free)(void*);
2852LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002853BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002854HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002855
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002856#define WINPTY_DLL "winpty.dll"
2857
2858static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002859# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002860
Bram Moolenaara83e3962017-08-17 14:39:07 +02002861 static int
2862dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002863{
2864 int i;
2865 static struct
2866 {
2867 char *name;
2868 FARPROC *ptr;
2869 } winpty_entry[] =
2870 {
2871 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2872 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2873 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2874 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2875 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2876 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2877 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2878 {"winpty_free", (FARPROC*)&winpty_free},
2879 {"winpty_open", (FARPROC*)&winpty_open},
2880 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2881 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2882 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2883 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002884 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002885 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002886 {NULL, NULL}
2887 };
2888
2889 /* No need to initialize twice. */
2890 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002891 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002892 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2893 * winpty.dll. */
2894 if (*p_winptydll != NUL)
2895 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2896 if (!hWinPtyDLL)
2897 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002898 if (!hWinPtyDLL)
2899 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002900 if (verbose)
2901 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2902 : (char_u *)WINPTY_DLL);
2903 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002904 }
2905 for (i = 0; winpty_entry[i].name != NULL
2906 && winpty_entry[i].ptr != NULL; ++i)
2907 {
2908 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2909 winpty_entry[i].name)) == NULL)
2910 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002911 if (verbose)
2912 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2913 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002914 }
2915 }
2916
Bram Moolenaara83e3962017-08-17 14:39:07 +02002917 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002918}
2919
2920/*
2921 * Create a new terminal of "rows" by "cols" cells.
2922 * Store a reference in "term".
2923 * Return OK or FAIL.
2924 */
2925 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02002926term_and_job_init(
2927 term_T *term,
2928 int rows,
2929 int cols,
2930 typval_T *argvar,
2931 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002932{
Bram Moolenaar5983d502017-08-20 19:22:56 +02002933 WCHAR *cmd_wchar = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002934 channel_T *channel = NULL;
2935 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002936 DWORD error;
Bram Moolenaar5983d502017-08-20 19:22:56 +02002937 HANDLE jo = NULL;
2938 HANDLE child_process_handle;
2939 HANDLE child_thread_handle;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002940 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002941 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002942 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002943 garray_T ga;
2944 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002945
Bram Moolenaara83e3962017-08-17 14:39:07 +02002946 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002947 return FAIL;
2948
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002949 if (argvar->v_type == VAR_STRING)
2950 cmd = argvar->vval.v_string;
2951 else
2952 {
2953 ga_init2(&ga, (int)sizeof(char*), 20);
2954 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2955 goto failed;
2956 cmd = ga.ga_data;
2957 }
2958
Bram Moolenaar5983d502017-08-20 19:22:56 +02002959 cmd_wchar = enc_to_utf16(cmd, NULL);
2960 if (cmd_wchar == NULL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002961 return FAIL;
2962
2963 job = job_alloc();
2964 if (job == NULL)
2965 goto failed;
2966
2967 channel = add_channel();
2968 if (channel == NULL)
2969 goto failed;
2970
2971 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2972 if (term->tl_winpty_config == NULL)
2973 goto failed;
2974
2975 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2976 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2977 if (term->tl_winpty == NULL)
2978 goto failed;
2979
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002980 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002981 spawn_config = winpty_spawn_config_new(
2982 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2983 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2984 NULL,
Bram Moolenaar5983d502017-08-20 19:22:56 +02002985 cmd_wchar,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002986 NULL,
2987 NULL,
2988 &winpty_err);
2989 if (spawn_config == NULL)
2990 goto failed;
2991
2992 channel = add_channel();
2993 if (channel == NULL)
2994 goto failed;
2995
2996 job = job_alloc();
2997 if (job == NULL)
2998 goto failed;
2999
Bram Moolenaar5983d502017-08-20 19:22:56 +02003000 /* TODO: when all lines are written and the fd is closed, the command
3001 * doesn't get EOF and hangs. */
3002 if (opt->jo_set & JO_IN_BUF)
3003 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
3004
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003005 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
3006 &child_thread_handle, &error, &winpty_err))
3007 goto failed;
3008
3009 channel_set_pipes(channel,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003010 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003011 winpty_conin_name(term->tl_winpty),
3012 GENERIC_WRITE, 0, NULL,
3013 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003014 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003015 winpty_conout_name(term->tl_winpty),
3016 GENERIC_READ, 0, NULL,
3017 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003018 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003019 winpty_conerr_name(term->tl_winpty),
3020 GENERIC_READ, 0, NULL,
3021 OPEN_EXISTING, 0, NULL));
3022
3023 jo = CreateJobObject(NULL, NULL);
3024 if (jo == NULL)
3025 goto failed;
3026
3027 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003028 {
3029 /* Failed, switch the way to terminate process with TerminateProcess. */
3030 CloseHandle(jo);
3031 jo = NULL;
3032 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003033
3034 winpty_spawn_config_free(spawn_config);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003035 vim_free(cmd_wchar);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003036
3037 create_vterm(term, rows, cols);
3038
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02003039 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02003040 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003041
3042 job->jv_channel = channel;
3043 job->jv_proc_info.hProcess = child_process_handle;
3044 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
3045 job->jv_job_object = jo;
3046 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003047 sprintf(buf, "winpty://%lu",
3048 GetProcessId(winpty_agent_process(term->tl_winpty)));
3049 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003050 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003051 term->tl_job = job;
3052
3053 return OK;
3054
3055failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003056 if (argvar->v_type == VAR_LIST)
3057 vim_free(ga.ga_data);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003058 if (cmd_wchar != NULL)
3059 vim_free(cmd_wchar);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003060 if (spawn_config != NULL)
3061 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003062 if (channel != NULL)
3063 channel_clear(channel);
3064 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003065 {
3066 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003067 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003068 }
3069 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003070 if (jo != NULL)
3071 CloseHandle(jo);
3072 if (term->tl_winpty != NULL)
3073 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003074 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003075 if (term->tl_winpty_config != NULL)
3076 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003077 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003078 if (winpty_err != NULL)
3079 {
3080 char_u *msg = utf16_to_enc(
3081 (short_u *)winpty_error_msg(winpty_err), NULL);
3082
3083 EMSG(msg);
3084 winpty_error_free(winpty_err);
3085 }
3086 return FAIL;
3087}
3088
3089/*
3090 * Free the terminal emulator part of "term".
3091 */
3092 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003093term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003094{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003095 if (term->tl_winpty != NULL)
3096 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003097 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003098 if (term->tl_winpty_config != NULL)
3099 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003100 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003101 if (term->tl_vterm != NULL)
3102 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003103 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003104}
3105
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003106/*
3107 * Request size to terminal.
3108 */
3109 static void
3110term_report_winsize(term_T *term, int rows, int cols)
3111{
3112 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3113}
3114
Bram Moolenaara83e3962017-08-17 14:39:07 +02003115 int
3116terminal_enabled(void)
3117{
3118 return dyn_winpty_init(FALSE) == OK;
3119}
3120
3121
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003122# else
3123
3124/**************************************
3125 * 3. Unix-like implementation.
3126 */
3127
3128/*
3129 * Create a new terminal of "rows" by "cols" cells.
3130 * Start job for "cmd".
3131 * Store the pointers in "term".
3132 * Return OK or FAIL.
3133 */
3134 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003135term_and_job_init(
3136 term_T *term,
3137 int rows,
3138 int cols,
3139 typval_T *argvar,
3140 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003141{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003142 create_vterm(term, rows, cols);
3143
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003144 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003145 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003146 if (term->tl_job != NULL)
3147 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003148
Bram Moolenaar61a66052017-07-22 18:39:00 +02003149 return term->tl_job != NULL
3150 && term->tl_job->jv_channel != NULL
3151 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003152}
3153
3154/*
3155 * Free the terminal emulator part of "term".
3156 */
3157 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003158term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003159{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003160 if (term->tl_vterm != NULL)
3161 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003162 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003163}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003164
3165/*
3166 * Request size to terminal.
3167 */
3168 static void
3169term_report_winsize(term_T *term, int rows, int cols)
3170{
3171 /* Use an ioctl() to report the new window size to the job. */
3172 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3173 {
3174 int fd = -1;
3175 int part;
3176
3177 for (part = PART_OUT; part < PART_COUNT; ++part)
3178 {
3179 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3180 if (isatty(fd))
3181 break;
3182 }
3183 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003184 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003185 }
3186}
3187
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003188# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003189
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003190#endif /* FEAT_TERMINAL */