blob: bb2dba90cd9ff0dbbad7ea24446226db5c8ad7f2 [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 Moolenaar3eee06e2017-08-19 19:40:50 +020041 * - help index for winptydll, optwin entry for winptydll
Bram Moolenaar9e13aa72017-08-16 23:14:08 +020042 * - make [range]terminal pipe [range] lines to the terminal
Bram Moolenaar94053a52017-08-01 21:44:33 +020043 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020044 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020045 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020046 * - support minimal size when 'termsize' is empty?
Bram Moolenaar4f44b882017-08-13 20:06:18 +020047 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020048 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
49 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
50 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
51 * Check that something is connected to the terminal.
52 * Test: "cat" reading from a file or buffer
53 * "ls" writing stdout to a file or buffer
54 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020055 * - For the GUI fill termios with default values, perhaps like pangoterm:
56 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020057 * - support ":term NONE" to open a terminal with a pty but not running a job
58 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020059 * - if the job in the terminal does not support the mouse, we can use the
60 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020061 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
62 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020063 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020064 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020065 * - Copy text in the vterm to the Vim buffer once in a while, so that
66 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020067 */
68
69#include "vim.h"
70
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020071#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020072
Bram Moolenaard5310982017-08-05 15:16:32 +020073#ifndef MIN
74# define MIN(x,y) ((x) < (y) ? (x) : (y))
75#endif
76#ifndef MAX
77# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020078#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020080#include "libvterm/include/vterm.h"
81
Bram Moolenaar33a43be2017-08-06 21:36:22 +020082/* This is VTermScreenCell without the characters, thus much smaller. */
83typedef struct {
84 VTermScreenCellAttrs attrs;
85 char width;
86 VTermColor fg, bg;
87} cellattr_T;
88
Bram Moolenaard85f2712017-07-28 21:51:57 +020089typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020090 int sb_cols; /* can differ per line */
91 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020092} sb_line_T;
93
Bram Moolenaare4f25e42017-07-07 11:54:15 +020094/* typedef term_T in structs.h */
95struct terminal_S {
96 term_T *tl_next;
97
Bram Moolenaar423802d2017-07-30 16:52:24 +020098 VTerm *tl_vterm;
99 job_T *tl_job;
100 buf_T *tl_buffer;
101
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200102 /* used when tl_job is NULL and only a pty was created */
103 int tl_tty_fd;
104 char_u *tl_tty_name;
105
Bram Moolenaar6d819742017-08-06 14:57:49 +0200106 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200107 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200108 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200109 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200110
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200111#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200112 void *tl_winpty_config;
113 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200114#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200115
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200116 /* last known vterm size */
117 int tl_rows;
118 int tl_cols;
119 /* vterm size does not follow window size */
120 int tl_rows_fixed;
121 int tl_cols_fixed;
122
Bram Moolenaar21554412017-07-24 21:44:43 +0200123 char_u *tl_title; /* NULL or allocated */
124 char_u *tl_status_text; /* NULL or allocated */
125
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126 /* Range of screen rows to update. Zero based. */
127 int tl_dirty_row_start; /* -1 if nothing dirty */
128 int tl_dirty_row_end; /* row below last one to update */
129
Bram Moolenaard85f2712017-07-28 21:51:57 +0200130 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200131 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200132
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200133 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200134 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200135 int tl_cursor_blink;
136 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
137 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200138
139 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200140};
141
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200142#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
143#define TMODE_LOOP 2 /* CTRL-W N used */
144
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200145/*
146 * List of all active terminals.
147 */
148static term_T *first_term = NULL;
149
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200150/* Terminal active in terminal_loop(). */
151static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200152
153#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
154#define KEY_BUF_LEN 200
155
156/*
157 * Functions with separate implementation for MS-Windows and Unix-like systems.
158 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200159static int term_and_job_init(term_T *term, int rows, int cols,
160 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200161static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200162static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200163
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200164/* The characters that we know (or assume) that the terminal expects for the
165 * backspace and enter keys. */
166static int term_backspace_char = BS;
167static int term_enter_char = CAR;
168static int term_nl_does_cr = FALSE;
169
170
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200171/**************************************
172 * 1. Generic code for all systems.
173 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200174
175/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200176 * Determine the terminal size from 'termsize' and the current window.
177 * Assumes term->tl_rows and term->tl_cols are zero.
178 */
179 static void
180set_term_and_win_size(term_T *term)
181{
182 if (*curwin->w_p_tms != NUL)
183 {
184 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
185
186 term->tl_rows = atoi((char *)curwin->w_p_tms);
187 term->tl_cols = atoi((char *)p);
188 }
189 if (term->tl_rows == 0)
190 term->tl_rows = curwin->w_height;
191 else
192 {
193 win_setheight_win(term->tl_rows, curwin);
194 term->tl_rows_fixed = TRUE;
195 }
196 if (term->tl_cols == 0)
197 term->tl_cols = curwin->w_width;
198 else
199 {
200 win_setwidth_win(term->tl_cols, curwin);
201 term->tl_cols_fixed = TRUE;
202 }
203}
204
205/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200206 * Initialize job options for a terminal job.
207 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200208 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200209 static void
210init_job_options(jobopt_T *opt)
211{
212 clear_job_options(opt);
213
214 opt->jo_mode = MODE_RAW;
215 opt->jo_out_mode = MODE_RAW;
216 opt->jo_err_mode = MODE_RAW;
217 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
218
219 opt->jo_io[PART_OUT] = JIO_BUFFER;
220 opt->jo_io[PART_ERR] = JIO_BUFFER;
221 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
222
223 opt->jo_modifiable[PART_OUT] = 0;
224 opt->jo_modifiable[PART_ERR] = 0;
225 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
226
227 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
228}
229
230/*
231 * Set job options mandatory for a terminal job.
232 */
233 static void
234setup_job_options(jobopt_T *opt, int rows, int cols)
235{
236 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
237 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
238 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200239 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
240 opt->jo_term_rows = rows;
241 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
242 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200243}
244
245 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200246term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248 exarg_T split_ea;
249 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200251 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252
253 if (check_restricted() || check_secure())
254 return;
255
256 term = (term_T *)alloc_clear(sizeof(term_T));
257 if (term == NULL)
258 return;
259 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200260 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200261 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200262 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200263 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200265 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200266 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200267 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200268 /* Create a new buffer in the current window. */
269 if (!can_abandon(curbuf, forceit))
270 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200271 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200272 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200273 return;
274 }
275 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
276 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200277 {
278 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200279 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200280 }
281 }
282 else if (opt->jo_hidden)
283 {
284 buf_T *buf;
285
286 /* Create a new buffer without a window. Make it the current buffer for
287 * a moment to be able to do the initialisations. */
288 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
289 BLN_NEW | BLN_LISTED);
290 if (buf == NULL || ml_open(buf) == FAIL)
291 {
292 vim_free(term);
293 return;
294 }
295 old_curbuf = curbuf;
296 --curbuf->b_nwindows;
297 curbuf = buf;
298 curwin->w_buffer = buf;
299 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200300 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200301 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200302 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200303 /* Open a new window or tab. */
304 split_ea.cmdidx = CMD_new;
305 split_ea.cmd = (char_u *)"new";
306 split_ea.arg = (char_u *)"";
307 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
308 {
309 split_ea.line2 = opt->jo_term_rows;
310 split_ea.addr_count = 1;
311 }
312 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
313 {
314 split_ea.line2 = opt->jo_term_cols;
315 split_ea.addr_count = 1;
316 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200317
Bram Moolenaarda43b612017-08-11 22:27:50 +0200318 ex_splitview(&split_ea);
319 if (curwin == old_curwin)
320 {
321 /* split failed */
322 vim_free(term);
323 return;
324 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200325 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200326 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200327 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200328
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200329 if (!opt->jo_hidden)
330 {
331 /* only one size was taken care of with :new, do the other one */
332 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
333 win_setheight(opt->jo_term_rows);
334 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
335 win_setwidth(opt->jo_term_cols);
336 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200337
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200338 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200339 term->tl_next = first_term;
340 first_term = term;
341
Bram Moolenaar78712a72017-08-05 14:50:12 +0200342 if (opt->jo_term_name != NULL)
343 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
344 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200345 {
346 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200347 size_t len;
348 char_u *cmd, *p;
349
350 if (argvar->v_type == VAR_STRING)
351 cmd = argvar->vval.v_string;
352 else if (argvar->v_type != VAR_LIST
353 || argvar->vval.v_list == NULL
354 || argvar->vval.v_list->lv_len < 1)
355 cmd = (char_u*)"";
356 else
357 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
358
359 len = STRLEN(cmd) + 10;
360 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200361
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200362 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200363 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200364 /* Prepend a ! to the command name to avoid the buffer name equals
365 * the executable, otherwise ":w!" would overwrite it. */
366 if (i == 0)
367 vim_snprintf((char *)p, len, "!%s", cmd);
368 else
369 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200370 if (buflist_findname(p) == NULL)
371 {
372 curbuf->b_ffname = p;
373 break;
374 }
375 }
376 }
377 curbuf->b_fname = curbuf->b_ffname;
378
Bram Moolenaar37c45832017-08-12 16:01:04 +0200379 if (opt->jo_term_opencmd != NULL)
380 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
381
Bram Moolenaareb44a682017-08-03 22:44:55 +0200382 set_string_option_direct((char_u *)"buftype", -1,
383 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
384
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200385 /* Mark the buffer as not modifiable. It can only be made modifiable after
386 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200387 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200388
389 /* Set 'bufhidden' to "hide": allow closing the window. */
390 set_string_option_direct((char_u *)"bufhidden", -1,
391 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200392
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200393 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200394 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200395
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200396 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200397 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt)
398 == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200399 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200400 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200401 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200402 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200403
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200404 /* Make sure we don't get stuck on sending keys to the job, it leads to
405 * a deadlock if the job is waiting for Vim to read. */
406 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
407
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200408 if (old_curbuf != NULL)
409 {
410 --curbuf->b_nwindows;
411 curbuf = old_curbuf;
412 curwin->w_buffer = curbuf;
413 ++curbuf->b_nwindows;
414 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200415 }
416 else
417 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200418 buf_T *buf = curbuf;
419
Bram Moolenaard85f2712017-07-28 21:51:57 +0200420 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200421 if (old_curbuf != NULL)
422 {
423 --curbuf->b_nwindows;
424 curbuf = old_curbuf;
425 curwin->w_buffer = curbuf;
426 ++curbuf->b_nwindows;
427 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200428
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200429 /* Wiping out the buffer will also close the window and call
430 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200431 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200432 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200433}
434
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200435/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200436 * ":terminal": open a terminal window and execute a job in it.
437 */
438 void
439ex_terminal(exarg_T *eap)
440{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200441 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200442 jobopt_T opt;
443 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200444 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200445
446 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200447
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200448 cmd = eap->arg;
449 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
450 {
451 char_u *p;
452
453 cmd += 2;
454 p = skiptowhite(cmd);
455 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
456 opt.jo_term_finish = 'c';
457 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
458 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200459 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
460 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200461 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
462 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200463 else
464 {
465 if (*p)
466 *p = NUL;
467 EMSG2(_("E181: Invalid attribute: %s"), cmd);
468 return;
469 }
470 cmd = skipwhite(p);
471 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200472 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200473 /* Make a copy, an autocommand may set 'shell'. */
474 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200475
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200476 if (eap->addr_count == 2)
477 {
478 opt.jo_term_rows = eap->line1;
479 opt.jo_term_cols = eap->line2;
480 }
481 else if (eap->addr_count == 1)
482 {
483 if (cmdmod.split & WSP_VERT)
484 opt.jo_term_cols = eap->line2;
485 else
486 opt.jo_term_rows = eap->line2;
487 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200488
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200489 argvar.v_type = VAR_STRING;
490 argvar.vval.v_string = cmd;
491 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200492 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200493}
494
495/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200496 * Free the scrollback buffer for "term".
497 */
498 static void
499free_scrollback(term_T *term)
500{
501 int i;
502
503 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
504 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
505 ga_clear(&term->tl_scrollback);
506}
507
508/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200509 * Free a terminal and everything it refers to.
510 * Kills the job if there is one.
511 * Called when wiping out a buffer.
512 */
513 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200514free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200515{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200516 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200517 term_T *tp;
518
519 if (term == NULL)
520 return;
521 if (first_term == term)
522 first_term = term->tl_next;
523 else
524 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
525 if (tp->tl_next == term)
526 {
527 tp->tl_next = term->tl_next;
528 break;
529 }
530
531 if (term->tl_job != NULL)
532 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200533 if (term->tl_job->jv_status != JOB_ENDED
534 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200535 job_stop(term->tl_job, NULL, "kill");
536 job_unref(term->tl_job);
537 }
538
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200539 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200540
541 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200542 vim_free(term->tl_title);
543 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200544 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200545 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200546 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200547 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200548 if (in_terminal_loop == term)
549 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200550}
551
552/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200553 * Write job output "msg[len]" to the vterm.
554 */
555 static void
556term_write_job_output(term_T *term, char_u *msg, size_t len)
557{
558 VTerm *vterm = term->tl_vterm;
559 char_u *p;
560 size_t done;
561 size_t len_now;
562
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200563 if (term_nl_does_cr)
564 vterm_input_write(vterm, (char *)msg, len);
565 else
566 /* need to convert NL to CR-NL */
567 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200568 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200569 for (p = msg + done; p < msg + len; )
570 {
571 if (*p == NL)
572 break;
573 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
574 }
575 len_now = p - msg - done;
576 vterm_input_write(vterm, (char *)msg + done, len_now);
577 if (p < msg + len && *p == NL)
578 {
579 vterm_input_write(vterm, "\r\n", 2);
580 ++len_now;
581 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200582 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200583
584 /* this invokes the damage callbacks */
585 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
586}
587
Bram Moolenaar1c844932017-07-24 23:36:41 +0200588 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200589update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200590{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200591 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200592 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200593 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200594 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200595 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200596 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200597 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200598 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200599#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200600 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200601 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200602#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200603 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200604}
605
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200606/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200607 * Invoked when "msg" output from a job was received. Write it to the terminal
608 * of "buffer".
609 */
610 void
611write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
612{
613 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200614 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200615
Bram Moolenaard85f2712017-07-28 21:51:57 +0200616 if (term->tl_vterm == NULL)
617 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200618 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200619 return;
620 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200621 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200622 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200623
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200624 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
625 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200626 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200627 {
628 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200629 ch_log(term->tl_job->jv_channel, "updating screen");
630 if (buffer == curbuf)
631 {
632 update_screen(0);
633 update_cursor(term, TRUE);
634 }
635 else
636 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200637 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200638}
639
640/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200641 * Send a mouse position and click to the vterm
642 */
643 static int
644term_send_mouse(VTerm *vterm, int button, int pressed)
645{
646 VTermModifier mod = VTERM_MOD_NONE;
647
648 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
649 mouse_col - W_WINCOL(curwin), mod);
650 vterm_mouse_button(vterm, button, pressed, mod);
651 return TRUE;
652}
653
654/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200655 * Convert typed key "c" into bytes to send to the job.
656 * Return the number of bytes in "buf".
657 */
658 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200659term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200660{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200661 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200662 VTermKey key = VTERM_KEY_NONE;
663 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200664 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200665
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200666 switch (c)
667 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200668 case CAR: c = term_enter_char; break;
669 /* don't use VTERM_KEY_BACKSPACE, it always
670 * becomes 0x7f DEL */
671 case K_BS: c = term_backspace_char; break;
672
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200673 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200674 case K_DEL: key = VTERM_KEY_DEL; break;
675 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200676 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
677 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200678 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200679 case K_S_END: mod = VTERM_MOD_SHIFT;
680 key = VTERM_KEY_END; break;
681 case K_C_END: mod = VTERM_MOD_CTRL;
682 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200683 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
684 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
685 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
686 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
687 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
688 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
689 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
690 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
691 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
692 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
693 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
694 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
695 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200696 case K_S_HOME: mod = VTERM_MOD_SHIFT;
697 key = VTERM_KEY_HOME; break;
698 case K_C_HOME: mod = VTERM_MOD_CTRL;
699 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200700 case K_INS: key = VTERM_KEY_INS; break;
701 case K_K0: key = VTERM_KEY_KP_0; break;
702 case K_K1: key = VTERM_KEY_KP_1; break;
703 case K_K2: key = VTERM_KEY_KP_2; break;
704 case K_K3: key = VTERM_KEY_KP_3; break;
705 case K_K4: key = VTERM_KEY_KP_4; break;
706 case K_K5: key = VTERM_KEY_KP_5; break;
707 case K_K6: key = VTERM_KEY_KP_6; break;
708 case K_K7: key = VTERM_KEY_KP_7; break;
709 case K_K8: key = VTERM_KEY_KP_8; break;
710 case K_K9: key = VTERM_KEY_KP_9; break;
711 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
712 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
713 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
714 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
715 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
716 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
717 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
718 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
719 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
720 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
721 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
722 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
723 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200724 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
725 key = VTERM_KEY_LEFT; break;
726 case K_C_LEFT: mod = VTERM_MOD_CTRL;
727 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200728 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
729 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
730 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200731 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
732 key = VTERM_KEY_RIGHT; break;
733 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
734 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200735 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200736 case K_S_UP: mod = VTERM_MOD_SHIFT;
737 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200738 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200739
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200740 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
741 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
742 case K_MOUSELEFT: /* TODO */ return 0;
743 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200744
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200745 case K_LEFTMOUSE:
746 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
747 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
748 case K_LEFTRELEASE:
749 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
750 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
751 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
752 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
753 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
754 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
755 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
756 case K_X1MOUSE: /* TODO */ return 0;
757 case K_X1DRAG: /* TODO */ return 0;
758 case K_X1RELEASE: /* TODO */ return 0;
759 case K_X2MOUSE: /* TODO */ return 0;
760 case K_X2DRAG: /* TODO */ return 0;
761 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200762
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200763 case K_IGNORE: return 0;
764 case K_NOP: return 0;
765 case K_UNDO: return 0;
766 case K_HELP: return 0;
767 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
768 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
769 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
770 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
771 case K_SELECT: return 0;
772#ifdef FEAT_GUI
773 case K_VER_SCROLLBAR: return 0;
774 case K_HOR_SCROLLBAR: return 0;
775#endif
776#ifdef FEAT_GUI_TABLINE
777 case K_TABLINE: return 0;
778 case K_TABMENU: return 0;
779#endif
780#ifdef FEAT_NETBEANS_INTG
781 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
782#endif
783#ifdef FEAT_DND
784 case K_DROP: return 0;
785#endif
786#ifdef FEAT_AUTOCMD
787 case K_CURSORHOLD: return 0;
788#endif
789 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
790 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200791 }
792
793 /*
794 * Convert special keys to vterm keys:
795 * - Write keys to vterm: vterm_keyboard_key()
796 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200797 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200798 */
799 if (key != VTERM_KEY_NONE)
800 /* Special key, let vterm convert it. */
801 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200802 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200803 /* Normal character, let vterm convert it. */
804 vterm_keyboard_unichar(vterm, c, mod);
805
806 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200807 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200808}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200809
Bram Moolenaar938783d2017-07-16 20:13:26 +0200810/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200811 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200812 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200813 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200814term_job_running(term_T *term)
815{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200816 /* Also consider the job finished when the channel is closed, to avoid a
817 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200818 return term != NULL
819 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200820 && term->tl_job->jv_status == JOB_STARTED
821 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200822}
823
824/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200825 * Add the last line of the scrollback buffer to the buffer in the window.
826 */
827 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200828add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200829{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200830 buf_T *buf = term->tl_buffer;
831 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
832 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200833
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200834 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200835 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200836 {
837 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200838 curbuf = buf;
839 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200840 curbuf = curwin->w_buffer;
841 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200842}
843
844/*
845 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200846 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200847 */
848 static void
849move_terminal_to_buffer(term_T *term)
850{
851 win_T *wp;
852 int len;
853 int lines_skipped = 0;
854 VTermPos pos;
855 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200856 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200857 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200858
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200859 if (term->tl_vterm == NULL)
860 return;
861 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200862 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
863 {
864 len = 0;
865 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
866 if (vterm_screen_get_cell(screen, pos, &cell) != 0
867 && cell.chars[0] != NUL)
868 len = pos.col + 1;
869
870 if (len == 0)
871 ++lines_skipped;
872 else
873 {
874 while (lines_skipped > 0)
875 {
876 /* Line was skipped, add an empty line. */
877 --lines_skipped;
878 if (ga_grow(&term->tl_scrollback, 1) == OK)
879 {
880 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
881 + term->tl_scrollback.ga_len;
882
883 line->sb_cols = 0;
884 line->sb_cells = NULL;
885 ++term->tl_scrollback.ga_len;
886
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200887 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200888 }
889 }
890
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200891 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200892 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
893 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200894 garray_T ga;
895 int width;
896 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200897 + term->tl_scrollback.ga_len;
898
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200899 ga_init2(&ga, 1, 100);
900 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200901 {
902 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200903 {
904 width = 1;
905 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
906 if (ga_grow(&ga, 1) == OK)
907 ga.ga_len += mb_char2bytes(' ',
908 (char_u *)ga.ga_data + ga.ga_len);
909 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200910 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200911 {
912 width = cell.width;
913
914 p[pos.col].width = cell.width;
915 p[pos.col].attrs = cell.attrs;
916 p[pos.col].fg = cell.fg;
917 p[pos.col].bg = cell.bg;
918
919 if (ga_grow(&ga, MB_MAXBYTES) == OK)
920 {
921 int i;
922 int c;
923
924 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
925 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
926 (char_u *)ga.ga_data + ga.ga_len);
927 }
928 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200929 }
930 line->sb_cols = len;
931 line->sb_cells = p;
932 ++term->tl_scrollback.ga_len;
933
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200934 if (ga_grow(&ga, 1) == FAIL)
935 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
936 else
937 {
938 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
939 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
940 }
941 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200942 }
943 else
944 vim_free(p);
945 }
946 }
947
948 FOR_ALL_WINDOWS(wp)
949 {
950 if (wp->w_buffer == term->tl_buffer)
951 {
952 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
953 wp->w_cursor.col = 0;
954 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200955 if (wp->w_cursor.lnum >= wp->w_height)
956 {
957 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
958
959 if (wp->w_topline < min_topline)
960 wp->w_topline = min_topline;
961 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200962 redraw_win_later(wp, NOT_VALID);
963 }
964 }
965}
966
967 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200968set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200969{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200970 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200971 vim_free(term->tl_status_text);
972 term->tl_status_text = NULL;
973 if (term->tl_buffer == curbuf)
974 maketitle();
975}
976
977/*
978 * Called after the job if finished and Terminal mode is not active:
979 * Move the vterm contents into the scrollback buffer and free the vterm.
980 */
981 static void
982cleanup_vterm(term_T *term)
983{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200984 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200985 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200986 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200987 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200988}
989
990/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200991 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200992 * Suspends updating the terminal window.
993 */
994 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200995term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200996{
997 term_T *term = curbuf->b_term;
998
999 /* Append the current terminal contents to the buffer. */
1000 move_terminal_to_buffer(term);
1001
Bram Moolenaar6d819742017-08-06 14:57:49 +02001002 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001003
Bram Moolenaar6d819742017-08-06 14:57:49 +02001004 /* Move the window cursor to the position of the cursor in the
1005 * terminal. */
1006 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1007 + term->tl_cursor_pos.row + 1;
1008 check_cursor();
1009 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001010
Bram Moolenaar6d819742017-08-06 14:57:49 +02001011 /* Display the same lines as in the terminal. */
1012 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001013}
1014
1015/*
1016 * Returns TRUE if the current window contains a terminal and we are in
1017 * Terminal-Normal mode.
1018 */
1019 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001020term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001021{
1022 term_T *term = curbuf->b_term;
1023
Bram Moolenaar6d819742017-08-06 14:57:49 +02001024 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001025}
1026
1027/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001028 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001029 * Restores updating the terminal window.
1030 */
1031 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001032term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001033{
1034 term_T *term = curbuf->b_term;
1035 sb_line_T *line;
1036 garray_T *gap;
1037
1038 /* Remove the terminal contents from the scrollback and the buffer. */
1039 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001040 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1041 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001042 {
1043 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1044 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1045 vim_free(line->sb_cells);
1046 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001047 }
1048 check_cursor();
1049
Bram Moolenaar6d819742017-08-06 14:57:49 +02001050 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001051
1052 if (term->tl_channel_closed)
1053 cleanup_vterm(term);
1054 redraw_buf_and_status_later(curbuf, NOT_VALID);
1055}
1056
1057/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001058 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001059 * Note: while waiting a terminal may be closed and freed if the channel is
1060 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001061 * TODO: use terminal mode mappings.
1062 */
1063 static int
1064term_vgetc()
1065{
1066 int c;
1067
1068 ++no_mapping;
1069 ++allow_keys;
1070 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001071#ifdef WIN3264
1072 ctrl_break_was_pressed = FALSE;
1073#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001074 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001075 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001076 --no_mapping;
1077 --allow_keys;
1078 return c;
1079}
1080
1081/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001082 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001083 * Return FAIL when the key needs to be handled in Normal mode.
1084 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001085 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001086 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001087send_keys_to_term(term_T *term, int c, int typed)
1088{
1089 char msg[KEY_BUF_LEN];
1090 size_t len;
1091 static int mouse_was_outside = FALSE;
1092 int dragging_outside = FALSE;
1093
1094 /* Catch keys that need to be handled as in Normal mode. */
1095 switch (c)
1096 {
1097 case NUL:
1098 case K_ZERO:
1099 if (typed)
1100 stuffcharReadbuff(c);
1101 return FAIL;
1102
1103 case K_IGNORE:
1104 return FAIL;
1105
1106 case K_LEFTDRAG:
1107 case K_MIDDLEDRAG:
1108 case K_RIGHTDRAG:
1109 case K_X1DRAG:
1110 case K_X2DRAG:
1111 dragging_outside = mouse_was_outside;
1112 /* FALLTHROUGH */
1113 case K_LEFTMOUSE:
1114 case K_LEFTMOUSE_NM:
1115 case K_LEFTRELEASE:
1116 case K_LEFTRELEASE_NM:
1117 case K_MIDDLEMOUSE:
1118 case K_MIDDLERELEASE:
1119 case K_RIGHTMOUSE:
1120 case K_RIGHTRELEASE:
1121 case K_X1MOUSE:
1122 case K_X1RELEASE:
1123 case K_X2MOUSE:
1124 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001125
1126 case K_MOUSEUP:
1127 case K_MOUSEDOWN:
1128 case K_MOUSELEFT:
1129 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001130 if (mouse_row < W_WINROW(curwin)
1131 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1132 || mouse_col < W_WINCOL(curwin)
1133 || mouse_col >= W_ENDCOL(curwin)
1134 || dragging_outside)
1135 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001136 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001137 if (typed)
1138 {
1139 stuffcharReadbuff(c);
1140 mouse_was_outside = TRUE;
1141 }
1142 return FAIL;
1143 }
1144 }
1145 if (typed)
1146 mouse_was_outside = FALSE;
1147
1148 /* Convert the typed key to a sequence of bytes for the job. */
1149 len = term_convert_key(term, c, msg);
1150 if (len > 0)
1151 /* TODO: if FAIL is returned, stop? */
1152 channel_send(term->tl_job->jv_channel, PART_IN,
1153 (char_u *)msg, (int)len, NULL);
1154
1155 return OK;
1156}
1157
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001158 static void
1159position_cursor(win_T *wp, VTermPos *pos)
1160{
1161 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1162 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1163 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1164}
1165
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001166/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001167 * Handle CTRL-W "": send register contents to the job.
1168 */
1169 static void
1170term_paste_register(int prev_c UNUSED)
1171{
1172 int c;
1173 list_T *l;
1174 listitem_T *item;
1175 long reglen = 0;
1176 int type;
1177
1178#ifdef FEAT_CMDL_INFO
1179 if (add_to_showcmd(prev_c))
1180 if (add_to_showcmd('"'))
1181 out_flush();
1182#endif
1183 c = term_vgetc();
1184#ifdef FEAT_CMDL_INFO
1185 clear_showcmd();
1186#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001187 if (!term_use_loop())
1188 /* job finished while waiting for a character */
1189 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001190
1191 /* CTRL-W "= prompt for expression to evaluate. */
1192 if (c == '=' && get_expr_register() != '=')
1193 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001194 if (!term_use_loop())
1195 /* job finished while waiting for a character */
1196 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001197
1198 l = (list_T *)get_reg_contents(c, GREG_LIST);
1199 if (l != NULL)
1200 {
1201 type = get_reg_type(c, &reglen);
1202 for (item = l->lv_first; item != NULL; item = item->li_next)
1203 {
1204 char_u *s = get_tv_string(&item->li_tv);
1205
1206 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1207 s, STRLEN(s), NULL);
1208 if (item->li_next != NULL || type == MLINE)
1209 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1210 (char_u *)"\r", 1, NULL);
1211 }
1212 list_free(l);
1213 }
1214}
1215
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001216#if defined(FEAT_GUI) || defined(PROTO)
1217/*
1218 * Return TRUE when the cursor of the terminal should be displayed.
1219 */
1220 int
1221use_terminal_cursor()
1222{
1223 return in_terminal_loop != NULL;
1224}
1225
1226 cursorentry_T *
1227term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1228{
1229 term_T *term = in_terminal_loop;
1230 static cursorentry_T entry;
1231
1232 vim_memset(&entry, 0, sizeof(entry));
1233 entry.shape = entry.mshape =
1234 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1235 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1236 SHAPE_BLOCK;
1237 entry.percentage = 20;
1238 if (term->tl_cursor_blink)
1239 {
1240 entry.blinkwait = 700;
1241 entry.blinkon = 400;
1242 entry.blinkon = 250;
1243 }
1244 *fg = gui.back_pixel;
1245 if (term->tl_cursor_color == NULL)
1246 *bg = gui.norm_pixel;
1247 else
1248 *bg = color_name2handle(term->tl_cursor_color);
1249 entry.name = "n";
1250 entry.used_for = SHAPE_CURSOR;
1251
1252 return &entry;
1253}
1254#endif
1255
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001256static int did_change_cursor = FALSE;
1257
1258 static void
1259may_set_cursor_props(term_T *term)
1260{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001261#ifdef FEAT_GUI
1262 /* For the GUI the cursor properties are obtained with
1263 * term_get_cursor_shape(). */
1264 if (gui.in_use)
1265 return;
1266#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001267 if (in_terminal_loop == term)
1268 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001269 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001270 if (term->tl_cursor_color != NULL)
1271 term_cursor_color(term->tl_cursor_color);
1272 else
1273 term_cursor_color((char_u *)"");
1274 /* do both blink and shape+blink, in case setting shape does not work */
1275 term_cursor_blink(term->tl_cursor_blink);
1276 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1277 }
1278}
1279
1280 static void
1281may_restore_cursor_props(void)
1282{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001283#ifdef FEAT_GUI
1284 if (gui.in_use)
1285 return;
1286#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001287 if (did_change_cursor)
1288 {
1289 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001290 term_cursor_color((char_u *)"");
1291 term_cursor_blink(FALSE);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001292 /* this will restore the initial cursor style, if possible */
1293 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001294 }
1295}
1296
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001297/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001298 * Returns TRUE if the current window contains a terminal and we are sending
1299 * keys to the job.
1300 */
1301 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001302term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001303{
1304 term_T *term = curbuf->b_term;
1305
1306 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001307 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001308 && term->tl_vterm != NULL
1309 && term_job_running(term);
1310}
1311
1312/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001313 * Wait for input and send it to the job.
1314 * Return when the start of a CTRL-W command is typed or anything else that
1315 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001316 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1317 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001318 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001319 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001320terminal_loop(void)
1321{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001322 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001323 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001324 int ret;
1325
Bram Moolenaar679653e2017-08-13 14:13:19 +02001326 /* Remember the terminal we are sending keys to. However, the terminal
1327 * might be closed while waiting for a character, e.g. typing "exit" in a
1328 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1329 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001330 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001331
1332 if (*curwin->w_p_tk != NUL)
1333 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001334 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001335 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001336
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001337#ifdef UNIX
1338 {
1339 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[PART_IN].ch_fd;
1340
1341 if (isatty(fd))
1342 {
1343 ttyinfo_T info;
1344
1345 /* Get the current backspace and enter characters of the pty. */
1346 if (get_tty_info(fd, &info) == OK)
1347 {
1348 term_backspace_char = info.backspace;
1349 term_enter_char = info.enter;
1350 term_nl_does_cr = info.nl_does_cr;
1351 }
1352 }
1353 }
1354#endif
1355
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001356 for (;;)
1357 {
1358 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001359 /* Repeat redrawing in case a message is received while redrawing. */
1360 while (curwin->w_redr_type != 0)
1361 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001362 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001363
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001364 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001365 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001366 /* job finished while waiting for a character */
1367 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001368 if (c == K_IGNORE)
1369 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001370
Bram Moolenaarfae42832017-08-01 22:24:26 +02001371#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001372 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001373 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001374 if (ctrl_break_was_pressed)
1375 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001376#endif
1377
Bram Moolenaar69198192017-08-05 14:10:48 +02001378 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001379 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001380 int prev_c = c;
1381
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001382#ifdef FEAT_CMDL_INFO
1383 if (add_to_showcmd(c))
1384 out_flush();
1385#endif
1386 c = term_vgetc();
1387#ifdef FEAT_CMDL_INFO
1388 clear_showcmd();
1389#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001390 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001391 /* job finished while waiting for a character */
1392 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001393
Bram Moolenaar69198192017-08-05 14:10:48 +02001394 if (prev_c == Ctrl_BSL)
1395 {
1396 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001397 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001398 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1399 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001400 ret = FAIL;
1401 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001402 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001403 /* Send both keys to the terminal. */
1404 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1405 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001406 else if (c == Ctrl_C)
1407 {
1408 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1409 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1410 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001411 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001412 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001413 /* "CTRL-W .": send CTRL-W to the job */
1414 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001415 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001416 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001417 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001418 /* CTRL-W N : go to Terminal-Normal mode. */
1419 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001420 ret = FAIL;
1421 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001422 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001423 else if (c == '"')
1424 {
1425 term_paste_register(prev_c);
1426 continue;
1427 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001428 else if (termkey == 0 || c != termkey)
1429 {
1430 stuffcharReadbuff(Ctrl_W);
1431 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001432 ret = OK;
1433 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001434 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001435 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001436 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001437 {
1438 ret = OK;
1439 goto theend;
1440 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001441 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001442 ret = FAIL;
1443
1444theend:
1445 in_terminal_loop = NULL;
1446 may_restore_cursor_props();
1447 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001448}
1449
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001450/*
1451 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001452 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001453 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001454 */
1455 void
1456term_job_ended(job_T *job)
1457{
1458 term_T *term;
1459 int did_one = FALSE;
1460
1461 for (term = first_term; term != NULL; term = term->tl_next)
1462 if (term->tl_job == job)
1463 {
1464 vim_free(term->tl_title);
1465 term->tl_title = NULL;
1466 vim_free(term->tl_status_text);
1467 term->tl_status_text = NULL;
1468 redraw_buf_and_status_later(term->tl_buffer, VALID);
1469 did_one = TRUE;
1470 }
1471 if (did_one)
1472 redraw_statuslines();
1473 if (curbuf->b_term != NULL)
1474 {
1475 if (curbuf->b_term->tl_job == job)
1476 maketitle();
1477 update_cursor(curbuf->b_term, TRUE);
1478 }
1479}
1480
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001481 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001482may_toggle_cursor(term_T *term)
1483{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001484 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001485 {
1486 if (term->tl_cursor_visible)
1487 cursor_on();
1488 else
1489 cursor_off();
1490 }
1491}
1492
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001493/*
1494 * Reverse engineer the RGB value into a cterm color index.
1495 * First color is 1. Return 0 if no match found.
1496 */
1497 static int
1498color2index(VTermColor *color, int fg, int *boldp)
1499{
1500 int red = color->red;
1501 int blue = color->blue;
1502 int green = color->green;
1503
1504 /* The argument for lookup_color() is for the color_names[] table. */
1505 if (red == 0)
1506 {
1507 if (green == 0)
1508 {
1509 if (blue == 0)
1510 return lookup_color(0, fg, boldp) + 1; /* black */
1511 if (blue == 224)
1512 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1513 }
1514 else if (green == 224)
1515 {
1516 if (blue == 0)
1517 return lookup_color(2, fg, boldp) + 1; /* dark green */
1518 if (blue == 224)
1519 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1520 }
1521 }
1522 else if (red == 224)
1523 {
1524 if (green == 0)
1525 {
1526 if (blue == 0)
1527 return lookup_color(4, fg, boldp) + 1; /* dark red */
1528 if (blue == 224)
1529 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1530 }
1531 else if (green == 224)
1532 {
1533 if (blue == 0)
1534 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1535 if (blue == 224)
1536 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1537 }
1538 }
1539 else if (red == 128)
1540 {
1541 if (green == 128 && blue == 128)
1542 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1543 }
1544 else if (red == 255)
1545 {
1546 if (green == 64)
1547 {
1548 if (blue == 64)
1549 return lookup_color(20, fg, boldp) + 1; /* light red */
1550 if (blue == 255)
1551 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1552 }
1553 else if (green == 255)
1554 {
1555 if (blue == 64)
1556 return lookup_color(24, fg, boldp) + 1; /* yellow */
1557 if (blue == 255)
1558 return lookup_color(26, fg, boldp) + 1; /* white */
1559 }
1560 }
1561 else if (red == 64)
1562 {
1563 if (green == 64)
1564 {
1565 if (blue == 255)
1566 return lookup_color(14, fg, boldp) + 1; /* light blue */
1567 }
1568 else if (green == 255)
1569 {
1570 if (blue == 64)
1571 return lookup_color(16, fg, boldp) + 1; /* light green */
1572 if (blue == 255)
1573 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1574 }
1575 }
1576 if (t_colors >= 256)
1577 {
1578 if (red == blue && red == green)
1579 {
1580 /* 24-color greyscale */
1581 static int cutoff[23] = {
1582 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1583 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1584 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1585 int i;
1586
1587 for (i = 0; i < 23; ++i)
1588 if (red < cutoff[i])
1589 return i + 233;
1590 return 256;
1591 }
1592
1593 /* 216-color cube */
1594 return 17 + ((red + 25) / 0x33) * 36
1595 + ((green + 25) / 0x33) * 6
1596 + (blue + 25) / 0x33;
1597 }
1598 return 0;
1599}
1600
1601/*
1602 * Convert the attributes of a vterm cell into an attribute index.
1603 */
1604 static int
1605cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1606{
1607 int attr = 0;
1608
1609 if (cellattrs.bold)
1610 attr |= HL_BOLD;
1611 if (cellattrs.underline)
1612 attr |= HL_UNDERLINE;
1613 if (cellattrs.italic)
1614 attr |= HL_ITALIC;
1615 if (cellattrs.strike)
1616 attr |= HL_STANDOUT;
1617 if (cellattrs.reverse)
1618 attr |= HL_INVERSE;
1619
1620#ifdef FEAT_GUI
1621 if (gui.in_use)
1622 {
1623 guicolor_T fg, bg;
1624
1625 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1626 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1627 return get_gui_attr_idx(attr, fg, bg);
1628 }
1629 else
1630#endif
1631#ifdef FEAT_TERMGUICOLORS
1632 if (p_tgc)
1633 {
1634 guicolor_T fg, bg;
1635
1636 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1637 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1638
1639 return get_tgc_attr_idx(attr, fg, bg);
1640 }
1641 else
1642#endif
1643 {
1644 int bold = MAYBE;
1645 int fg = color2index(&cellfg, TRUE, &bold);
1646 int bg = color2index(&cellbg, FALSE, &bold);
1647
1648 /* with 8 colors set the bold attribute to get a bright foreground */
1649 if (bold == TRUE)
1650 attr |= HL_BOLD;
1651 return get_cterm_attr_idx(attr, fg, bg);
1652 }
1653 return 0;
1654}
1655
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001656 static int
1657handle_damage(VTermRect rect, void *user)
1658{
1659 term_T *term = (term_T *)user;
1660
1661 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1662 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1663 redraw_buf_later(term->tl_buffer, NOT_VALID);
1664 return 1;
1665}
1666
1667 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001668handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001669{
1670 term_T *term = (term_T *)user;
1671
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001672 /* Scrolling up is done much more efficiently by deleting lines instead of
1673 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001674 if (dest.start_col == src.start_col
1675 && dest.end_col == src.end_col
1676 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001677 {
1678 win_T *wp;
1679 VTermColor fg, bg;
1680 VTermScreenCellAttrs attr;
1681 int clear_attr;
1682
1683 /* Set the color to clear lines with. */
1684 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1685 &fg, &bg);
1686 vim_memset(&attr, 0, sizeof(attr));
1687 clear_attr = cell2attr(attr, fg, bg);
1688
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001689 FOR_ALL_WINDOWS(wp)
1690 {
1691 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001692 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001693 src.start_row - dest.start_row, FALSE, FALSE,
1694 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001695 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001696 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001697 redraw_buf_later(term->tl_buffer, NOT_VALID);
1698 return 1;
1699}
1700
1701 static int
1702handle_movecursor(
1703 VTermPos pos,
1704 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001705 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001706 void *user)
1707{
1708 term_T *term = (term_T *)user;
1709 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001710
1711 term->tl_cursor_pos = pos;
1712 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001713
1714 FOR_ALL_WINDOWS(wp)
1715 {
1716 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001717 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001718 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001719 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001720 {
1721 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001722 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001723 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001724
1725 return 1;
1726}
1727
Bram Moolenaar21554412017-07-24 21:44:43 +02001728 static int
1729handle_settermprop(
1730 VTermProp prop,
1731 VTermValue *value,
1732 void *user)
1733{
1734 term_T *term = (term_T *)user;
1735
1736 switch (prop)
1737 {
1738 case VTERM_PROP_TITLE:
1739 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001740 /* a blank title isn't useful, make it empty, so that "running" is
1741 * displayed */
1742 if (*skipwhite((char_u *)value->string) == NUL)
1743 term->tl_title = NULL;
1744 else
1745 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001746 vim_free(term->tl_status_text);
1747 term->tl_status_text = NULL;
1748 if (term == curbuf->b_term)
1749 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001750 break;
1751
1752 case VTERM_PROP_CURSORVISIBLE:
1753 term->tl_cursor_visible = value->boolean;
1754 may_toggle_cursor(term);
1755 out_flush();
1756 break;
1757
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001758 case VTERM_PROP_CURSORBLINK:
1759 term->tl_cursor_blink = value->boolean;
1760 may_set_cursor_props(term);
1761 break;
1762
1763 case VTERM_PROP_CURSORSHAPE:
1764 term->tl_cursor_shape = value->number;
1765 may_set_cursor_props(term);
1766 break;
1767
1768 case VTERM_PROP_CURSORCOLOR:
1769 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001770 if (*value->string == NUL)
1771 term->tl_cursor_color = NULL;
1772 else
1773 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001774 may_set_cursor_props(term);
1775 break;
1776
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001777 case VTERM_PROP_ALTSCREEN:
1778 /* TODO: do anything else? */
1779 term->tl_using_altscreen = value->boolean;
1780 break;
1781
Bram Moolenaar21554412017-07-24 21:44:43 +02001782 default:
1783 break;
1784 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001785 /* Always return 1, otherwise vterm doesn't store the value internally. */
1786 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001787}
1788
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001789/*
1790 * The job running in the terminal resized the terminal.
1791 */
1792 static int
1793handle_resize(int rows, int cols, void *user)
1794{
1795 term_T *term = (term_T *)user;
1796 win_T *wp;
1797
1798 term->tl_rows = rows;
1799 term->tl_cols = cols;
1800 FOR_ALL_WINDOWS(wp)
1801 {
1802 if (wp->w_buffer == term->tl_buffer)
1803 {
1804 win_setheight_win(rows, wp);
1805 win_setwidth_win(cols, wp);
1806 }
1807 }
1808
1809 redraw_buf_later(term->tl_buffer, NOT_VALID);
1810 return 1;
1811}
1812
Bram Moolenaard85f2712017-07-28 21:51:57 +02001813/*
1814 * Handle a line that is pushed off the top of the screen.
1815 */
1816 static int
1817handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1818{
1819 term_T *term = (term_T *)user;
1820
1821 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001822 if (ga_grow(&term->tl_scrollback, 1) == OK)
1823 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001824 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001825 int len = 0;
1826 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001827 int c;
1828 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001829 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001830 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001831
1832 /* do not store empty cells at the end */
1833 for (i = 0; i < cols; ++i)
1834 if (cells[i].chars[0] != 0)
1835 len = i + 1;
1836
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001837 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001838 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001839 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001840 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001841 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001842 for (col = 0; col < len; col += cells[col].width)
1843 {
1844 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1845 {
1846 ga.ga_len = 0;
1847 break;
1848 }
1849 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1850 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1851 (char_u *)ga.ga_data + ga.ga_len);
1852 p[col].width = cells[col].width;
1853 p[col].attrs = cells[col].attrs;
1854 p[col].fg = cells[col].fg;
1855 p[col].bg = cells[col].bg;
1856 }
1857 }
1858 if (ga_grow(&ga, 1) == FAIL)
1859 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1860 else
1861 {
1862 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1863 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1864 }
1865 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001866
1867 line = (sb_line_T *)term->tl_scrollback.ga_data
1868 + term->tl_scrollback.ga_len;
1869 line->sb_cols = len;
1870 line->sb_cells = p;
1871 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001872 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001873 }
1874 return 0; /* ignored */
1875}
1876
Bram Moolenaar21554412017-07-24 21:44:43 +02001877static VTermScreenCallbacks screen_callbacks = {
1878 handle_damage, /* damage */
1879 handle_moverect, /* moverect */
1880 handle_movecursor, /* movecursor */
1881 handle_settermprop, /* settermprop */
1882 NULL, /* bell */
1883 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001884 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001885 NULL /* sb_popline */
1886};
1887
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001888/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001889 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001890 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001891 */
1892 void
1893term_channel_closed(channel_T *ch)
1894{
1895 term_T *term;
1896 int did_one = FALSE;
1897
1898 for (term = first_term; term != NULL; term = term->tl_next)
1899 if (term->tl_job == ch->ch_job)
1900 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001901 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001902 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001903
Bram Moolenaard85f2712017-07-28 21:51:57 +02001904 vim_free(term->tl_title);
1905 term->tl_title = NULL;
1906 vim_free(term->tl_status_text);
1907 term->tl_status_text = NULL;
1908
Bram Moolenaar423802d2017-07-30 16:52:24 +02001909 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001910 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001911 {
1912 int fnum = term->tl_buffer->b_fnum;
1913
Bram Moolenaar423802d2017-07-30 16:52:24 +02001914 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001915
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001916 if (term->tl_finish == 'c')
1917 {
1918 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001919 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001920 curbuf = term->tl_buffer;
1921 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1922 break;
1923 }
1924 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1925 {
1926 char buf[50];
1927
1928 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001929 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001930 vim_snprintf(buf, sizeof(buf),
1931 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001932 ? "botright sbuf %d"
1933 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001934 do_cmdline_cmd((char_u *)buf);
1935 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001936 else
1937 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001938 }
1939
Bram Moolenaard85f2712017-07-28 21:51:57 +02001940 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001941 }
1942 if (did_one)
1943 {
1944 redraw_statuslines();
1945
1946 /* Need to break out of vgetc(). */
1947 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001948 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001949
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001950 term = curbuf->b_term;
1951 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001952 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001953 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001954 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001955 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001956 }
1957 }
1958}
1959
1960/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001961 * Called to update a window that contains an active terminal.
1962 * Returns FAIL when there is no terminal running in this window or in
1963 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001964 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001965 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001966term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001967{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001968 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001969 VTerm *vterm;
1970 VTermScreen *screen;
1971 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001972 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001973
Bram Moolenaar6d819742017-08-06 14:57:49 +02001974 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001975 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001976
Bram Moolenaard85f2712017-07-28 21:51:57 +02001977 vterm = term->tl_vterm;
1978 screen = vterm_obtain_screen(vterm);
1979 state = vterm_obtain_state(vterm);
1980
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001981 /*
1982 * If the window was resized a redraw will be triggered and we get here.
1983 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1984 */
1985 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1986 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001987 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001988 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1989 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1990 win_T *twp;
1991
1992 FOR_ALL_WINDOWS(twp)
1993 {
1994 /* When more than one window shows the same terminal, use the
1995 * smallest size. */
1996 if (twp->w_buffer == term->tl_buffer)
1997 {
1998 if (!term->tl_rows_fixed && rows > twp->w_height)
1999 rows = twp->w_height;
2000 if (!term->tl_cols_fixed && cols > twp->w_width)
2001 cols = twp->w_width;
2002 }
2003 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002004
2005 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002006 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002007 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002008 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002009 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002010
2011 /* The cursor may have been moved when resizing. */
2012 vterm_state_get_cursorpos(state, &pos);
2013 position_cursor(wp, &pos);
2014
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002015 /* TODO: Only redraw what changed. */
2016 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002017 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002018 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002019 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002020
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002021 if (pos.row < term->tl_rows)
2022 {
2023 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002024 {
2025 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002026 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002027
Bram Moolenaareeac6772017-07-23 15:48:37 +02002028 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2029 vim_memset(&cell, 0, sizeof(cell));
2030
2031 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002032 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002033 if (c == NUL)
2034 {
2035 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002036#if defined(FEAT_MBYTE)
2037 if (enc_utf8)
2038 ScreenLinesUC[off] = NUL;
2039#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002040 }
2041 else
2042 {
2043#if defined(FEAT_MBYTE)
2044 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002045 {
2046 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002047 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002048 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002049 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002050 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002051 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002052 if (enc_utf8)
2053 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002054 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002055#else
2056 ScreenLines[off] = c;
2057#endif
2058 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002059 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002060
2061 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002062 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002063 if (cell.width == 2)
2064 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002065 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002066#if defined(FEAT_MBYTE)
2067 if (enc_utf8)
2068 ScreenLinesUC[off] = NUL;
2069#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002070 ++pos.col;
2071 ++off;
2072 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002073 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002074 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002075 else
2076 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002077
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002078 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2079 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002080 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002081
2082 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002083}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002084
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002085/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002086 * Return TRUE if "wp" is a terminal window where the job has finished.
2087 */
2088 int
2089term_is_finished(buf_T *buf)
2090{
2091 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2092}
2093
2094/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002095 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002096 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002097 */
2098 int
2099term_show_buffer(buf_T *buf)
2100{
2101 term_T *term = buf->b_term;
2102
Bram Moolenaar6d819742017-08-06 14:57:49 +02002103 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002104}
2105
2106/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002107 * The current buffer is going to be changed. If there is terminal
2108 * highlighting remove it now.
2109 */
2110 void
2111term_change_in_curbuf(void)
2112{
2113 term_T *term = curbuf->b_term;
2114
2115 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2116 {
2117 free_scrollback(term);
2118 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002119
2120 /* The buffer is now like a normal buffer, it cannot be easily
2121 * abandoned when changed. */
2122 set_string_option_direct((char_u *)"buftype", -1,
2123 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002124 }
2125}
2126
2127/*
2128 * Get the screen attribute for a position in the buffer.
2129 */
2130 int
2131term_get_attr(buf_T *buf, linenr_T lnum, int col)
2132{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002133 term_T *term = buf->b_term;
2134 sb_line_T *line;
2135 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002136
Bram Moolenaar70229f92017-07-29 16:01:53 +02002137 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002138 return 0;
2139 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2140 if (col >= line->sb_cols)
2141 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002142 cellattr = line->sb_cells + col;
2143 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002144}
2145
2146/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002147 * Create a new vterm and initialize it.
2148 */
2149 static void
2150create_vterm(term_T *term, int rows, int cols)
2151{
2152 VTerm *vterm;
2153 VTermScreen *screen;
2154
2155 vterm = vterm_new(rows, cols);
2156 term->tl_vterm = vterm;
2157 screen = vterm_obtain_screen(vterm);
2158 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2159 /* TODO: depends on 'encoding'. */
2160 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002161
2162 /* Vterm uses a default black background. Set it to white when
2163 * 'background' is "light". */
2164 if (*p_bg == 'l')
2165 {
2166 VTermColor fg, bg;
2167
2168 fg.red = fg.green = fg.blue = 0;
2169 bg.red = bg.green = bg.blue = 255;
2170 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2171 }
2172
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002173 /* Required to initialize most things. */
2174 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002175
2176 /* Allow using alternate screen. */
2177 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002178}
2179
Bram Moolenaar21554412017-07-24 21:44:43 +02002180/*
2181 * Return the text to show for the buffer name and status.
2182 */
2183 char_u *
2184term_get_status_text(term_T *term)
2185{
2186 if (term->tl_status_text == NULL)
2187 {
2188 char_u *txt;
2189 size_t len;
2190
Bram Moolenaar6d819742017-08-06 14:57:49 +02002191 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002192 {
2193 if (term_job_running(term))
2194 txt = (char_u *)_("Terminal");
2195 else
2196 txt = (char_u *)_("Terminal-finished");
2197 }
2198 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002199 txt = term->tl_title;
2200 else if (term_job_running(term))
2201 txt = (char_u *)_("running");
2202 else
2203 txt = (char_u *)_("finished");
2204 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002205 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002206 if (term->tl_status_text != NULL)
2207 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2208 term->tl_buffer->b_fname, txt);
2209 }
2210 return term->tl_status_text;
2211}
2212
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002213/*
2214 * Mark references in jobs of terminals.
2215 */
2216 int
2217set_ref_in_term(int copyID)
2218{
2219 int abort = FALSE;
2220 term_T *term;
2221 typval_T tv;
2222
2223 for (term = first_term; term != NULL; term = term->tl_next)
2224 if (term->tl_job != NULL)
2225 {
2226 tv.v_type = VAR_JOB;
2227 tv.vval.v_job = term->tl_job;
2228 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2229 }
2230 return abort;
2231}
2232
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002233/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002234 * Get the buffer from the first argument in "argvars".
2235 * Returns NULL when the buffer is not for a terminal window.
2236 */
2237 static buf_T *
2238term_get_buf(typval_T *argvars)
2239{
2240 buf_T *buf;
2241
2242 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2243 ++emsg_off;
2244 buf = get_buf_tv(&argvars[0], FALSE);
2245 --emsg_off;
2246 if (buf == NULL || buf->b_term == NULL)
2247 return NULL;
2248 return buf;
2249}
2250
2251/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002252 * "term_getaltscreen(buf)" function
2253 */
2254 void
2255f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2256{
2257 buf_T *buf = term_get_buf(argvars);
2258
2259 if (buf == NULL)
2260 return;
2261 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2262}
2263
2264/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002265 * "term_getattr(attr, name)" function
2266 */
2267 void
2268f_term_getattr(typval_T *argvars, typval_T *rettv)
2269{
2270 int attr;
2271 size_t i;
2272 char_u *name;
2273
2274 static struct {
2275 char *name;
2276 int attr;
2277 } attrs[] = {
2278 {"bold", HL_BOLD},
2279 {"italic", HL_ITALIC},
2280 {"underline", HL_UNDERLINE},
2281 {"strike", HL_STANDOUT},
2282 {"reverse", HL_INVERSE},
2283 };
2284
2285 attr = get_tv_number(&argvars[0]);
2286 name = get_tv_string_chk(&argvars[1]);
2287 if (name == NULL)
2288 return;
2289
2290 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2291 if (STRCMP(name, attrs[i].name) == 0)
2292 {
2293 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2294 break;
2295 }
2296}
2297
2298/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002299 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002300 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002301 void
2302f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002303{
Bram Moolenaar97870002017-07-30 18:28:38 +02002304 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002305 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002306 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002307 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002308
Bram Moolenaar97870002017-07-30 18:28:38 +02002309 if (rettv_list_alloc(rettv) == FAIL)
2310 return;
2311 if (buf == NULL)
2312 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002313 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002314
2315 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002316 list_append_number(l, term->tl_cursor_pos.row + 1);
2317 list_append_number(l, term->tl_cursor_pos.col + 1);
2318
2319 d = dict_alloc();
2320 if (d != NULL)
2321 {
2322 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2323 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2324 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2325 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2326 ? (char_u *)"" : term->tl_cursor_color);
2327 list_append_dict(l, d);
2328 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002329}
2330
2331/*
2332 * "term_getjob(buf)" function
2333 */
2334 void
2335f_term_getjob(typval_T *argvars, typval_T *rettv)
2336{
2337 buf_T *buf = term_get_buf(argvars);
2338
2339 rettv->v_type = VAR_JOB;
2340 rettv->vval.v_job = NULL;
2341 if (buf == NULL)
2342 return;
2343
2344 rettv->vval.v_job = buf->b_term->tl_job;
2345 if (rettv->vval.v_job != NULL)
2346 ++rettv->vval.v_job->jv_refcount;
2347}
2348
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002349 static int
2350get_row_number(typval_T *tv, term_T *term)
2351{
2352 if (tv->v_type == VAR_STRING
2353 && tv->vval.v_string != NULL
2354 && STRCMP(tv->vval.v_string, ".") == 0)
2355 return term->tl_cursor_pos.row;
2356 return (int)get_tv_number(tv) - 1;
2357}
2358
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002359/*
2360 * "term_getline(buf, row)" function
2361 */
2362 void
2363f_term_getline(typval_T *argvars, typval_T *rettv)
2364{
2365 buf_T *buf = term_get_buf(argvars);
2366 term_T *term;
2367 int row;
2368
2369 rettv->v_type = VAR_STRING;
2370 if (buf == NULL)
2371 return;
2372 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002373 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002374
2375 if (term->tl_vterm == NULL)
2376 {
2377 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2378
2379 /* vterm is finished, get the text from the buffer */
2380 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2381 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2382 }
2383 else
2384 {
2385 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2386 VTermRect rect;
2387 int len;
2388 char_u *p;
2389
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002390 if (row < 0 || row >= term->tl_rows)
2391 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002392 len = term->tl_cols * MB_MAXBYTES + 1;
2393 p = alloc(len);
2394 if (p == NULL)
2395 return;
2396 rettv->vval.v_string = p;
2397
2398 rect.start_col = 0;
2399 rect.end_col = term->tl_cols;
2400 rect.start_row = row;
2401 rect.end_row = row + 1;
2402 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2403 }
2404}
2405
2406/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002407 * "term_getscrolled(buf)" function
2408 */
2409 void
2410f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2411{
2412 buf_T *buf = term_get_buf(argvars);
2413
2414 if (buf == NULL)
2415 return;
2416 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2417}
2418
2419/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002420 * "term_getsize(buf)" function
2421 */
2422 void
2423f_term_getsize(typval_T *argvars, typval_T *rettv)
2424{
2425 buf_T *buf = term_get_buf(argvars);
2426 list_T *l;
2427
2428 if (rettv_list_alloc(rettv) == FAIL)
2429 return;
2430 if (buf == NULL)
2431 return;
2432
2433 l = rettv->vval.v_list;
2434 list_append_number(l, buf->b_term->tl_rows);
2435 list_append_number(l, buf->b_term->tl_cols);
2436}
2437
2438/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002439 * "term_getstatus(buf)" function
2440 */
2441 void
2442f_term_getstatus(typval_T *argvars, typval_T *rettv)
2443{
2444 buf_T *buf = term_get_buf(argvars);
2445 term_T *term;
2446 char_u val[100];
2447
2448 rettv->v_type = VAR_STRING;
2449 if (buf == NULL)
2450 return;
2451 term = buf->b_term;
2452
2453 if (term_job_running(term))
2454 STRCPY(val, "running");
2455 else
2456 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002457 if (term->tl_normal_mode)
2458 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002459 rettv->vval.v_string = vim_strsave(val);
2460}
2461
2462/*
2463 * "term_gettitle(buf)" function
2464 */
2465 void
2466f_term_gettitle(typval_T *argvars, typval_T *rettv)
2467{
2468 buf_T *buf = term_get_buf(argvars);
2469
2470 rettv->v_type = VAR_STRING;
2471 if (buf == NULL)
2472 return;
2473
2474 if (buf->b_term->tl_title != NULL)
2475 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2476}
2477
2478/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002479 * "term_gettty(buf)" function
2480 */
2481 void
2482f_term_gettty(typval_T *argvars, typval_T *rettv)
2483{
2484 buf_T *buf = term_get_buf(argvars);
2485 char_u *p;
2486
2487 rettv->v_type = VAR_STRING;
2488 if (buf == NULL)
2489 return;
2490 if (buf->b_term->tl_job != NULL)
2491 p = buf->b_term->tl_job->jv_tty_name;
2492 else
2493 p = buf->b_term->tl_tty_name;
2494 if (p != NULL)
2495 rettv->vval.v_string = vim_strsave(p);
2496}
2497
2498/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002499 * "term_list()" function
2500 */
2501 void
2502f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2503{
2504 term_T *tp;
2505 list_T *l;
2506
2507 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2508 return;
2509
2510 l = rettv->vval.v_list;
2511 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2512 if (tp != NULL && tp->tl_buffer != NULL)
2513 if (list_append_number(l,
2514 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2515 return;
2516}
2517
2518/*
2519 * "term_scrape(buf, row)" function
2520 */
2521 void
2522f_term_scrape(typval_T *argvars, typval_T *rettv)
2523{
2524 buf_T *buf = term_get_buf(argvars);
2525 VTermScreen *screen = NULL;
2526 VTermPos pos;
2527 list_T *l;
2528 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002529 char_u *p;
2530 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002531
2532 if (rettv_list_alloc(rettv) == FAIL)
2533 return;
2534 if (buf == NULL)
2535 return;
2536 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002537
2538 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002539 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002540
2541 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002542 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002543 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002544 p = NULL;
2545 line = NULL;
2546 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002547 else
2548 {
2549 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2550
2551 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2552 return;
2553 p = ml_get_buf(buf, lnum + 1, FALSE);
2554 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2555 }
2556
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002557 for (pos.col = 0; pos.col < term->tl_cols; )
2558 {
2559 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002560 int width;
2561 VTermScreenCellAttrs attrs;
2562 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002563 char_u rgb[8];
2564 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2565 int off = 0;
2566 int i;
2567
2568 if (screen == NULL)
2569 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002570 cellattr_T *cellattr;
2571 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002572
2573 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002574 if (pos.col >= line->sb_cols)
2575 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002576 cellattr = line->sb_cells + pos.col;
2577 width = cellattr->width;
2578 attrs = cellattr->attrs;
2579 fg = cellattr->fg;
2580 bg = cellattr->bg;
2581 len = MB_PTR2LEN(p);
2582 mch_memmove(mbs, p, len);
2583 mbs[len] = NUL;
2584 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002585 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002586 else
2587 {
2588 VTermScreenCell cell;
2589 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2590 break;
2591 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2592 {
2593 if (cell.chars[i] == 0)
2594 break;
2595 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2596 }
2597 mbs[off] = NUL;
2598 width = cell.width;
2599 attrs = cell.attrs;
2600 fg = cell.fg;
2601 bg = cell.bg;
2602 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002603 dcell = dict_alloc();
2604 list_append_dict(l, dcell);
2605
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002606 dict_add_nr_str(dcell, "chars", 0, mbs);
2607
2608 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002609 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002610 dict_add_nr_str(dcell, "fg", 0, rgb);
2611 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002612 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002613 dict_add_nr_str(dcell, "bg", 0, rgb);
2614
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002615 dict_add_nr_str(dcell, "attr",
2616 cell2attr(attrs, fg, bg), NULL);
2617 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002618
2619 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002620 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002621 ++pos.col;
2622 }
2623}
2624
2625/*
2626 * "term_sendkeys(buf, keys)" function
2627 */
2628 void
2629f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2630{
2631 buf_T *buf = term_get_buf(argvars);
2632 char_u *msg;
2633 term_T *term;
2634
2635 rettv->v_type = VAR_UNKNOWN;
2636 if (buf == NULL)
2637 return;
2638
2639 msg = get_tv_string_chk(&argvars[1]);
2640 if (msg == NULL)
2641 return;
2642 term = buf->b_term;
2643 if (term->tl_vterm == NULL)
2644 return;
2645
2646 while (*msg != NUL)
2647 {
2648 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2649 msg += MB_PTR2LEN(msg);
2650 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002651}
2652
2653/*
2654 * "term_start(command, options)" function
2655 */
2656 void
2657f_term_start(typval_T *argvars, typval_T *rettv)
2658{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002659 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002660
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002661 init_job_options(&opt);
2662 /* TODO: allow more job options */
2663 if (argvars[1].v_type != VAR_UNKNOWN
2664 && get_job_options(&argvars[1], &opt,
2665 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002666 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002667 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002668 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002669 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002670 return;
2671
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002672 if (opt.jo_vertical)
2673 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002674 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002675
2676 if (curbuf->b_term != NULL)
2677 rettv->vval.v_number = curbuf->b_fnum;
2678}
2679
2680/*
2681 * "term_wait" function
2682 */
2683 void
2684f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2685{
2686 buf_T *buf = term_get_buf(argvars);
2687
2688 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002689 {
2690 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002691 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002692 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002693 if (buf->b_term->tl_job == NULL)
2694 {
2695 ch_log(NULL, "term_wait(): no job to wait for");
2696 return;
2697 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002698
2699 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002700 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002701 {
2702 /* The job is dead, keep reading channel I/O until the channel is
2703 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002704 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002705 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2706 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002707 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002708 parse_queued_messages();
2709 ui_delay(10L, FALSE);
2710 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002711 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002712 parse_queued_messages();
2713 }
2714 else
2715 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002716 long wait = 10L;
2717
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002718 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002719 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002720
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002721 /* Wait for some time for any channel I/O. */
2722 if (argvars[1].v_type != VAR_UNKNOWN)
2723 wait = get_tv_number(&argvars[1]);
2724 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002725 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002726
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002727 /* Flushing messages on channels is hopefully sufficient.
2728 * TODO: is there a better way? */
2729 parse_queued_messages();
2730 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002731}
2732
Bram Moolenaara83e3962017-08-17 14:39:07 +02002733# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002734
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002735/**************************************
2736 * 2. MS-Windows implementation.
2737 */
2738
Bram Moolenaara83e3962017-08-17 14:39:07 +02002739# ifndef PROTO
2740
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002741#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2742#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2743
Bram Moolenaar8a773062017-07-24 22:29:21 +02002744void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002745void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002746void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002747BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2748void (*winpty_config_set_initial_size)(void*, int, int);
2749LPCWSTR (*winpty_conin_name)(void*);
2750LPCWSTR (*winpty_conout_name)(void*);
2751LPCWSTR (*winpty_conerr_name)(void*);
2752void (*winpty_free)(void*);
2753void (*winpty_config_free)(void*);
2754void (*winpty_spawn_config_free)(void*);
2755void (*winpty_error_free)(void*);
2756LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002757BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002758HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002759
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002760#define WINPTY_DLL "winpty.dll"
2761
2762static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002763# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002764
Bram Moolenaara83e3962017-08-17 14:39:07 +02002765 static int
2766dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002767{
2768 int i;
2769 static struct
2770 {
2771 char *name;
2772 FARPROC *ptr;
2773 } winpty_entry[] =
2774 {
2775 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2776 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2777 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2778 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2779 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2780 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2781 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2782 {"winpty_free", (FARPROC*)&winpty_free},
2783 {"winpty_open", (FARPROC*)&winpty_open},
2784 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2785 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2786 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2787 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002788 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002789 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002790 {NULL, NULL}
2791 };
2792
2793 /* No need to initialize twice. */
2794 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002795 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002796 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2797 * winpty.dll. */
2798 if (*p_winptydll != NUL)
2799 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2800 if (!hWinPtyDLL)
2801 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002802 if (!hWinPtyDLL)
2803 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002804 if (verbose)
2805 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2806 : (char_u *)WINPTY_DLL);
2807 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002808 }
2809 for (i = 0; winpty_entry[i].name != NULL
2810 && winpty_entry[i].ptr != NULL; ++i)
2811 {
2812 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2813 winpty_entry[i].name)) == NULL)
2814 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002815 if (verbose)
2816 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2817 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002818 }
2819 }
2820
Bram Moolenaara83e3962017-08-17 14:39:07 +02002821 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002822}
2823
2824/*
2825 * Create a new terminal of "rows" by "cols" cells.
2826 * Store a reference in "term".
2827 * Return OK or FAIL.
2828 */
2829 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002830term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002831{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002832 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002833 channel_T *channel = NULL;
2834 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002835 DWORD error;
2836 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2837 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002838 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002839 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002840 garray_T ga;
2841 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002842
Bram Moolenaara83e3962017-08-17 14:39:07 +02002843 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002844 return FAIL;
2845
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002846 if (argvar->v_type == VAR_STRING)
2847 cmd = argvar->vval.v_string;
2848 else
2849 {
2850 ga_init2(&ga, (int)sizeof(char*), 20);
2851 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2852 goto failed;
2853 cmd = ga.ga_data;
2854 }
2855
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002856 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002857 if (p == NULL)
2858 return FAIL;
2859
2860 job = job_alloc();
2861 if (job == NULL)
2862 goto failed;
2863
2864 channel = add_channel();
2865 if (channel == NULL)
2866 goto failed;
2867
2868 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2869 if (term->tl_winpty_config == NULL)
2870 goto failed;
2871
2872 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2873 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2874 if (term->tl_winpty == NULL)
2875 goto failed;
2876
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002877 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002878 spawn_config = winpty_spawn_config_new(
2879 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2880 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2881 NULL,
2882 p,
2883 NULL,
2884 NULL,
2885 &winpty_err);
2886 if (spawn_config == NULL)
2887 goto failed;
2888
2889 channel = add_channel();
2890 if (channel == NULL)
2891 goto failed;
2892
2893 job = job_alloc();
2894 if (job == NULL)
2895 goto failed;
2896
2897 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2898 &child_thread_handle, &error, &winpty_err))
2899 goto failed;
2900
2901 channel_set_pipes(channel,
2902 (sock_T) CreateFileW(
2903 winpty_conin_name(term->tl_winpty),
2904 GENERIC_WRITE, 0, NULL,
2905 OPEN_EXISTING, 0, NULL),
2906 (sock_T) CreateFileW(
2907 winpty_conout_name(term->tl_winpty),
2908 GENERIC_READ, 0, NULL,
2909 OPEN_EXISTING, 0, NULL),
2910 (sock_T) CreateFileW(
2911 winpty_conerr_name(term->tl_winpty),
2912 GENERIC_READ, 0, NULL,
2913 OPEN_EXISTING, 0, NULL));
2914
2915 jo = CreateJobObject(NULL, NULL);
2916 if (jo == NULL)
2917 goto failed;
2918
2919 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002920 {
2921 /* Failed, switch the way to terminate process with TerminateProcess. */
2922 CloseHandle(jo);
2923 jo = NULL;
2924 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002925
2926 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002927 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002928
2929 create_vterm(term, rows, cols);
2930
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002931 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002932 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002933
2934 job->jv_channel = channel;
2935 job->jv_proc_info.hProcess = child_process_handle;
2936 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2937 job->jv_job_object = jo;
2938 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002939 sprintf(buf, "winpty://%lu",
2940 GetProcessId(winpty_agent_process(term->tl_winpty)));
2941 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002942 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002943 term->tl_job = job;
2944
2945 return OK;
2946
2947failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002948 if (argvar->v_type == VAR_LIST)
2949 vim_free(ga.ga_data);
2950 if (p != NULL)
2951 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002952 if (spawn_config != NULL)
2953 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002954 if (channel != NULL)
2955 channel_clear(channel);
2956 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002957 {
2958 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002959 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002960 }
2961 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002962 if (jo != NULL)
2963 CloseHandle(jo);
2964 if (term->tl_winpty != NULL)
2965 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002966 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002967 if (term->tl_winpty_config != NULL)
2968 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002969 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002970 if (winpty_err != NULL)
2971 {
2972 char_u *msg = utf16_to_enc(
2973 (short_u *)winpty_error_msg(winpty_err), NULL);
2974
2975 EMSG(msg);
2976 winpty_error_free(winpty_err);
2977 }
2978 return FAIL;
2979}
2980
2981/*
2982 * Free the terminal emulator part of "term".
2983 */
2984 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002985term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002986{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002987 if (term->tl_winpty != NULL)
2988 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002989 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002990 if (term->tl_winpty_config != NULL)
2991 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002992 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002993 if (term->tl_vterm != NULL)
2994 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002995 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002996}
2997
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002998/*
2999 * Request size to terminal.
3000 */
3001 static void
3002term_report_winsize(term_T *term, int rows, int cols)
3003{
3004 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3005}
3006
Bram Moolenaara83e3962017-08-17 14:39:07 +02003007 int
3008terminal_enabled(void)
3009{
3010 return dyn_winpty_init(FALSE) == OK;
3011}
3012
3013
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003014# else
3015
3016/**************************************
3017 * 3. Unix-like implementation.
3018 */
3019
3020/*
3021 * Create a new terminal of "rows" by "cols" cells.
3022 * Start job for "cmd".
3023 * Store the pointers in "term".
3024 * Return OK or FAIL.
3025 */
3026 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003027term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003028{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003029 create_vterm(term, rows, cols);
3030
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003031 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003032 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003033 if (term->tl_job != NULL)
3034 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003035
Bram Moolenaar61a66052017-07-22 18:39:00 +02003036 return term->tl_job != NULL
3037 && term->tl_job->jv_channel != NULL
3038 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003039}
3040
3041/*
3042 * Free the terminal emulator part of "term".
3043 */
3044 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003045term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003046{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003047 if (term->tl_vterm != NULL)
3048 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003049 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003050}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003051
3052/*
3053 * Request size to terminal.
3054 */
3055 static void
3056term_report_winsize(term_T *term, int rows, int cols)
3057{
3058 /* Use an ioctl() to report the new window size to the job. */
3059 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3060 {
3061 int fd = -1;
3062 int part;
3063
3064 for (part = PART_OUT; part < PART_COUNT; ++part)
3065 {
3066 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3067 if (isatty(fd))
3068 break;
3069 }
3070 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003071 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003072 }
3073}
3074
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003075# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003076
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003077#endif /* FEAT_TERMINAL */