blob: 8d15d3a99162400dc27df3ce6cfa71b64b9f1659 [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 Moolenaar9e13aa72017-08-16 23:14:08 +020041 * - make [range]terminal pipe [range] lines to the terminal
Bram Moolenaar94053a52017-08-01 21:44:33 +020042 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020043 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020044 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020045 * - support minimal size when 'termsize' is empty?
Bram Moolenaar4f44b882017-08-13 20:06:18 +020046 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020047 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
48 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
49 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
50 * Check that something is connected to the terminal.
51 * Test: "cat" reading from a file or buffer
52 * "ls" writing stdout to a file or buffer
53 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020054 * - For the GUI fill termios with default values, perhaps like pangoterm:
55 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020056 * - support ":term NONE" to open a terminal with a pty but not running a job
57 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020058 * - if the job in the terminal does not support the mouse, we can use the
59 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020060 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
61 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020062 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020063 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020064 * - Copy text in the vterm to the Vim buffer once in a while, so that
65 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020066 */
67
68#include "vim.h"
69
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020070#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020071
Bram Moolenaard5310982017-08-05 15:16:32 +020072#ifndef MIN
73# define MIN(x,y) ((x) < (y) ? (x) : (y))
74#endif
75#ifndef MAX
76# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020077#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079#include "libvterm/include/vterm.h"
80
Bram Moolenaar33a43be2017-08-06 21:36:22 +020081/* This is VTermScreenCell without the characters, thus much smaller. */
82typedef struct {
83 VTermScreenCellAttrs attrs;
84 char width;
85 VTermColor fg, bg;
86} cellattr_T;
87
Bram Moolenaard85f2712017-07-28 21:51:57 +020088typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020089 int sb_cols; /* can differ per line */
90 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020091} sb_line_T;
92
Bram Moolenaare4f25e42017-07-07 11:54:15 +020093/* typedef term_T in structs.h */
94struct terminal_S {
95 term_T *tl_next;
96
Bram Moolenaar423802d2017-07-30 16:52:24 +020097 VTerm *tl_vterm;
98 job_T *tl_job;
99 buf_T *tl_buffer;
100
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200101 /* used when tl_job is NULL and only a pty was created */
102 int tl_tty_fd;
103 char_u *tl_tty_name;
104
Bram Moolenaar6d819742017-08-06 14:57:49 +0200105 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200106 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200107 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200108 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200109
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200110#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200111 void *tl_winpty_config;
112 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200113#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200114
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200115 /* last known vterm size */
116 int tl_rows;
117 int tl_cols;
118 /* vterm size does not follow window size */
119 int tl_rows_fixed;
120 int tl_cols_fixed;
121
Bram Moolenaar21554412017-07-24 21:44:43 +0200122 char_u *tl_title; /* NULL or allocated */
123 char_u *tl_status_text; /* NULL or allocated */
124
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125 /* Range of screen rows to update. Zero based. */
126 int tl_dirty_row_start; /* -1 if nothing dirty */
127 int tl_dirty_row_end; /* row below last one to update */
128
Bram Moolenaard85f2712017-07-28 21:51:57 +0200129 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200130 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200131
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200132 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200133 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200134 int tl_cursor_blink;
135 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
136 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200137
138 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200139};
140
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200141#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
142#define TMODE_LOOP 2 /* CTRL-W N used */
143
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200144/*
145 * List of all active terminals.
146 */
147static term_T *first_term = NULL;
148
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200149/* Terminal active in terminal_loop(). */
150static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200151
152#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
153#define KEY_BUF_LEN 200
154
155/*
156 * Functions with separate implementation for MS-Windows and Unix-like systems.
157 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200158static int term_and_job_init(term_T *term, int rows, int cols,
159 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200160static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200161static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200162
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200163/* The characters that we know (or assume) that the terminal expects for the
164 * backspace and enter keys. */
165static int term_backspace_char = BS;
166static int term_enter_char = CAR;
167static int term_nl_does_cr = FALSE;
168
169
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200170/**************************************
171 * 1. Generic code for all systems.
172 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200173
174/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200175 * Determine the terminal size from 'termsize' and the current window.
176 * Assumes term->tl_rows and term->tl_cols are zero.
177 */
178 static void
179set_term_and_win_size(term_T *term)
180{
181 if (*curwin->w_p_tms != NUL)
182 {
183 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
184
185 term->tl_rows = atoi((char *)curwin->w_p_tms);
186 term->tl_cols = atoi((char *)p);
187 }
188 if (term->tl_rows == 0)
189 term->tl_rows = curwin->w_height;
190 else
191 {
192 win_setheight_win(term->tl_rows, curwin);
193 term->tl_rows_fixed = TRUE;
194 }
195 if (term->tl_cols == 0)
196 term->tl_cols = curwin->w_width;
197 else
198 {
199 win_setwidth_win(term->tl_cols, curwin);
200 term->tl_cols_fixed = TRUE;
201 }
202}
203
204/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200205 * Initialize job options for a terminal job.
206 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200207 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200208 static void
209init_job_options(jobopt_T *opt)
210{
211 clear_job_options(opt);
212
213 opt->jo_mode = MODE_RAW;
214 opt->jo_out_mode = MODE_RAW;
215 opt->jo_err_mode = MODE_RAW;
216 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
217
218 opt->jo_io[PART_OUT] = JIO_BUFFER;
219 opt->jo_io[PART_ERR] = JIO_BUFFER;
220 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
221
222 opt->jo_modifiable[PART_OUT] = 0;
223 opt->jo_modifiable[PART_ERR] = 0;
224 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
225
226 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
227}
228
229/*
230 * Set job options mandatory for a terminal job.
231 */
232 static void
233setup_job_options(jobopt_T *opt, int rows, int cols)
234{
235 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
236 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
237 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200238 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
239 opt->jo_term_rows = rows;
240 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
241 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200242}
243
244 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200245term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247 exarg_T split_ea;
248 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200250 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251
252 if (check_restricted() || check_secure())
253 return;
254
255 term = (term_T *)alloc_clear(sizeof(term_T));
256 if (term == NULL)
257 return;
258 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200259 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200260 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200261 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200262 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200265 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200266 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200267 /* Create a new buffer in the current window. */
268 if (!can_abandon(curbuf, forceit))
269 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200270 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200271 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200272 return;
273 }
274 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
275 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200276 {
277 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200278 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200279 }
280 }
281 else if (opt->jo_hidden)
282 {
283 buf_T *buf;
284
285 /* Create a new buffer without a window. Make it the current buffer for
286 * a moment to be able to do the initialisations. */
287 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
288 BLN_NEW | BLN_LISTED);
289 if (buf == NULL || ml_open(buf) == FAIL)
290 {
291 vim_free(term);
292 return;
293 }
294 old_curbuf = curbuf;
295 --curbuf->b_nwindows;
296 curbuf = buf;
297 curwin->w_buffer = buf;
298 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200299 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200300 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200301 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200302 /* Open a new window or tab. */
303 split_ea.cmdidx = CMD_new;
304 split_ea.cmd = (char_u *)"new";
305 split_ea.arg = (char_u *)"";
306 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
307 {
308 split_ea.line2 = opt->jo_term_rows;
309 split_ea.addr_count = 1;
310 }
311 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
312 {
313 split_ea.line2 = opt->jo_term_cols;
314 split_ea.addr_count = 1;
315 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200316
Bram Moolenaarda43b612017-08-11 22:27:50 +0200317 ex_splitview(&split_ea);
318 if (curwin == old_curwin)
319 {
320 /* split failed */
321 vim_free(term);
322 return;
323 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200324 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200325 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200326 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200327
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200328 if (!opt->jo_hidden)
329 {
330 /* only one size was taken care of with :new, do the other one */
331 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
332 win_setheight(opt->jo_term_rows);
333 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
334 win_setwidth(opt->jo_term_cols);
335 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200336
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200337 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200338 term->tl_next = first_term;
339 first_term = term;
340
Bram Moolenaar78712a72017-08-05 14:50:12 +0200341 if (opt->jo_term_name != NULL)
342 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
343 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200344 {
345 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200346 size_t len;
347 char_u *cmd, *p;
348
349 if (argvar->v_type == VAR_STRING)
350 cmd = argvar->vval.v_string;
351 else if (argvar->v_type != VAR_LIST
352 || argvar->vval.v_list == NULL
353 || argvar->vval.v_list->lv_len < 1)
354 cmd = (char_u*)"";
355 else
356 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
357
358 len = STRLEN(cmd) + 10;
359 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200360
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200361 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200362 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200363 /* Prepend a ! to the command name to avoid the buffer name equals
364 * the executable, otherwise ":w!" would overwrite it. */
365 if (i == 0)
366 vim_snprintf((char *)p, len, "!%s", cmd);
367 else
368 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200369 if (buflist_findname(p) == NULL)
370 {
371 curbuf->b_ffname = p;
372 break;
373 }
374 }
375 }
376 curbuf->b_fname = curbuf->b_ffname;
377
Bram Moolenaar37c45832017-08-12 16:01:04 +0200378 if (opt->jo_term_opencmd != NULL)
379 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
380
Bram Moolenaareb44a682017-08-03 22:44:55 +0200381 set_string_option_direct((char_u *)"buftype", -1,
382 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
383
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200384 /* Mark the buffer as not modifiable. It can only be made modifiable after
385 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200386 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200387
388 /* Set 'bufhidden' to "hide": allow closing the window. */
389 set_string_option_direct((char_u *)"bufhidden", -1,
390 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200391
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200392 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200393 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200394
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200395 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200396 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt)
397 == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200398 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200399 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200400 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200401 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200402
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200403 /* Make sure we don't get stuck on sending keys to the job, it leads to
404 * a deadlock if the job is waiting for Vim to read. */
405 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
406
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200407 if (old_curbuf != NULL)
408 {
409 --curbuf->b_nwindows;
410 curbuf = old_curbuf;
411 curwin->w_buffer = curbuf;
412 ++curbuf->b_nwindows;
413 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200414 }
415 else
416 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200417 buf_T *buf = curbuf;
418
Bram Moolenaard85f2712017-07-28 21:51:57 +0200419 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200420 if (old_curbuf != NULL)
421 {
422 --curbuf->b_nwindows;
423 curbuf = old_curbuf;
424 curwin->w_buffer = curbuf;
425 ++curbuf->b_nwindows;
426 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200427
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200428 /* Wiping out the buffer will also close the window and call
429 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200430 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200431 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200432}
433
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200434/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200435 * ":terminal": open a terminal window and execute a job in it.
436 */
437 void
438ex_terminal(exarg_T *eap)
439{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200440 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200441 jobopt_T opt;
442 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200443 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200444
445 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200446
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200447 cmd = eap->arg;
448 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
449 {
450 char_u *p;
451
452 cmd += 2;
453 p = skiptowhite(cmd);
454 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
455 opt.jo_term_finish = 'c';
456 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
457 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200458 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
459 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200460 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
461 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200462 else
463 {
464 if (*p)
465 *p = NUL;
466 EMSG2(_("E181: Invalid attribute: %s"), cmd);
467 return;
468 }
469 cmd = skipwhite(p);
470 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200471 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200472 /* Make a copy, an autocommand may set 'shell'. */
473 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200474
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200475 if (eap->addr_count == 2)
476 {
477 opt.jo_term_rows = eap->line1;
478 opt.jo_term_cols = eap->line2;
479 }
480 else if (eap->addr_count == 1)
481 {
482 if (cmdmod.split & WSP_VERT)
483 opt.jo_term_cols = eap->line2;
484 else
485 opt.jo_term_rows = eap->line2;
486 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200487
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200488 argvar.v_type = VAR_STRING;
489 argvar.vval.v_string = cmd;
490 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200491 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200492}
493
494/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200495 * Free the scrollback buffer for "term".
496 */
497 static void
498free_scrollback(term_T *term)
499{
500 int i;
501
502 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
503 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
504 ga_clear(&term->tl_scrollback);
505}
506
507/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200508 * Free a terminal and everything it refers to.
509 * Kills the job if there is one.
510 * Called when wiping out a buffer.
511 */
512 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200513free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200514{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200515 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200516 term_T *tp;
517
518 if (term == NULL)
519 return;
520 if (first_term == term)
521 first_term = term->tl_next;
522 else
523 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
524 if (tp->tl_next == term)
525 {
526 tp->tl_next = term->tl_next;
527 break;
528 }
529
530 if (term->tl_job != NULL)
531 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200532 if (term->tl_job->jv_status != JOB_ENDED
533 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200534 job_stop(term->tl_job, NULL, "kill");
535 job_unref(term->tl_job);
536 }
537
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200538 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200539
540 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200541 vim_free(term->tl_title);
542 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200543 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200544 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200545 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200546 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200547 if (in_terminal_loop == term)
548 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200549}
550
551/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200552 * Write job output "msg[len]" to the vterm.
553 */
554 static void
555term_write_job_output(term_T *term, char_u *msg, size_t len)
556{
557 VTerm *vterm = term->tl_vterm;
558 char_u *p;
559 size_t done;
560 size_t len_now;
561
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200562 if (term_nl_does_cr)
563 vterm_input_write(vterm, (char *)msg, len);
564 else
565 /* need to convert NL to CR-NL */
566 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200567 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200568 for (p = msg + done; p < msg + len; )
569 {
570 if (*p == NL)
571 break;
572 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
573 }
574 len_now = p - msg - done;
575 vterm_input_write(vterm, (char *)msg + done, len_now);
576 if (p < msg + len && *p == NL)
577 {
578 vterm_input_write(vterm, "\r\n", 2);
579 ++len_now;
580 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200581 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200582
583 /* this invokes the damage callbacks */
584 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
585}
586
Bram Moolenaar1c844932017-07-24 23:36:41 +0200587 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200588update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200589{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200590 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200591 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200592 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200593 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200594 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200595 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200596 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200597 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200598#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200599 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200600 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200601#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200602 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200603}
604
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200605/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200606 * Invoked when "msg" output from a job was received. Write it to the terminal
607 * of "buffer".
608 */
609 void
610write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
611{
612 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200613 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200614
Bram Moolenaard85f2712017-07-28 21:51:57 +0200615 if (term->tl_vterm == NULL)
616 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200617 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200618 return;
619 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200620 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200621 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200622
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200623 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
624 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200625 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200626 {
627 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200628 ch_log(term->tl_job->jv_channel, "updating screen");
629 if (buffer == curbuf)
630 {
631 update_screen(0);
632 update_cursor(term, TRUE);
633 }
634 else
635 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200636 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200637}
638
639/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200640 * Send a mouse position and click to the vterm
641 */
642 static int
643term_send_mouse(VTerm *vterm, int button, int pressed)
644{
645 VTermModifier mod = VTERM_MOD_NONE;
646
647 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
648 mouse_col - W_WINCOL(curwin), mod);
649 vterm_mouse_button(vterm, button, pressed, mod);
650 return TRUE;
651}
652
653/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200654 * Convert typed key "c" into bytes to send to the job.
655 * Return the number of bytes in "buf".
656 */
657 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200658term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200659{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200660 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200661 VTermKey key = VTERM_KEY_NONE;
662 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200663 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200664
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200665 switch (c)
666 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200667 case CAR: c = term_enter_char; break;
668 /* don't use VTERM_KEY_BACKSPACE, it always
669 * becomes 0x7f DEL */
670 case K_BS: c = term_backspace_char; break;
671
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200672 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200673 case K_DEL: key = VTERM_KEY_DEL; break;
674 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200675 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
676 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200677 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200678 case K_S_END: mod = VTERM_MOD_SHIFT;
679 key = VTERM_KEY_END; break;
680 case K_C_END: mod = VTERM_MOD_CTRL;
681 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200682 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
683 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
684 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
685 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
686 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
687 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
688 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
689 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
690 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
691 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
692 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
693 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
694 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200695 case K_S_HOME: mod = VTERM_MOD_SHIFT;
696 key = VTERM_KEY_HOME; break;
697 case K_C_HOME: mod = VTERM_MOD_CTRL;
698 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200699 case K_INS: key = VTERM_KEY_INS; break;
700 case K_K0: key = VTERM_KEY_KP_0; break;
701 case K_K1: key = VTERM_KEY_KP_1; break;
702 case K_K2: key = VTERM_KEY_KP_2; break;
703 case K_K3: key = VTERM_KEY_KP_3; break;
704 case K_K4: key = VTERM_KEY_KP_4; break;
705 case K_K5: key = VTERM_KEY_KP_5; break;
706 case K_K6: key = VTERM_KEY_KP_6; break;
707 case K_K7: key = VTERM_KEY_KP_7; break;
708 case K_K8: key = VTERM_KEY_KP_8; break;
709 case K_K9: key = VTERM_KEY_KP_9; break;
710 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
711 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
712 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
713 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
714 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
715 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
716 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
717 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
718 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
719 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
720 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
721 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
722 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200723 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
724 key = VTERM_KEY_LEFT; break;
725 case K_C_LEFT: mod = VTERM_MOD_CTRL;
726 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200727 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
728 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
729 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200730 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
731 key = VTERM_KEY_RIGHT; break;
732 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
733 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200734 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200735 case K_S_UP: mod = VTERM_MOD_SHIFT;
736 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200737 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200738
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200739 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
740 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
741 case K_MOUSELEFT: /* TODO */ return 0;
742 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200743
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200744 case K_LEFTMOUSE:
745 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
746 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
747 case K_LEFTRELEASE:
748 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
749 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
750 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
751 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
752 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
753 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
754 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
755 case K_X1MOUSE: /* TODO */ return 0;
756 case K_X1DRAG: /* TODO */ return 0;
757 case K_X1RELEASE: /* TODO */ return 0;
758 case K_X2MOUSE: /* TODO */ return 0;
759 case K_X2DRAG: /* TODO */ return 0;
760 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200761
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200762 case K_IGNORE: return 0;
763 case K_NOP: return 0;
764 case K_UNDO: return 0;
765 case K_HELP: return 0;
766 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
767 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
768 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
769 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
770 case K_SELECT: return 0;
771#ifdef FEAT_GUI
772 case K_VER_SCROLLBAR: return 0;
773 case K_HOR_SCROLLBAR: return 0;
774#endif
775#ifdef FEAT_GUI_TABLINE
776 case K_TABLINE: return 0;
777 case K_TABMENU: return 0;
778#endif
779#ifdef FEAT_NETBEANS_INTG
780 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
781#endif
782#ifdef FEAT_DND
783 case K_DROP: return 0;
784#endif
785#ifdef FEAT_AUTOCMD
786 case K_CURSORHOLD: return 0;
787#endif
788 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
789 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200790 }
791
792 /*
793 * Convert special keys to vterm keys:
794 * - Write keys to vterm: vterm_keyboard_key()
795 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200796 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200797 */
798 if (key != VTERM_KEY_NONE)
799 /* Special key, let vterm convert it. */
800 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200801 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200802 /* Normal character, let vterm convert it. */
803 vterm_keyboard_unichar(vterm, c, mod);
804
805 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200806 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200807}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200808
Bram Moolenaar938783d2017-07-16 20:13:26 +0200809/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200810 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200811 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200812 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200813term_job_running(term_T *term)
814{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200815 /* Also consider the job finished when the channel is closed, to avoid a
816 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200817 return term != NULL
818 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200819 && term->tl_job->jv_status == JOB_STARTED
820 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200821}
822
823/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200824 * Add the last line of the scrollback buffer to the buffer in the window.
825 */
826 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200827add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200828{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200829 buf_T *buf = term->tl_buffer;
830 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
831 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200832
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200833 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200834 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200835 {
836 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200837 curbuf = buf;
838 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200839 curbuf = curwin->w_buffer;
840 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200841}
842
843/*
844 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200845 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200846 */
847 static void
848move_terminal_to_buffer(term_T *term)
849{
850 win_T *wp;
851 int len;
852 int lines_skipped = 0;
853 VTermPos pos;
854 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200855 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200856 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200857
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200858 if (term->tl_vterm == NULL)
859 return;
860 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200861 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
862 {
863 len = 0;
864 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
865 if (vterm_screen_get_cell(screen, pos, &cell) != 0
866 && cell.chars[0] != NUL)
867 len = pos.col + 1;
868
869 if (len == 0)
870 ++lines_skipped;
871 else
872 {
873 while (lines_skipped > 0)
874 {
875 /* Line was skipped, add an empty line. */
876 --lines_skipped;
877 if (ga_grow(&term->tl_scrollback, 1) == OK)
878 {
879 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
880 + term->tl_scrollback.ga_len;
881
882 line->sb_cols = 0;
883 line->sb_cells = NULL;
884 ++term->tl_scrollback.ga_len;
885
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200886 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200887 }
888 }
889
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200890 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200891 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
892 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200893 garray_T ga;
894 int width;
895 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200896 + term->tl_scrollback.ga_len;
897
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200898 ga_init2(&ga, 1, 100);
899 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200900 {
901 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200902 {
903 width = 1;
904 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
905 if (ga_grow(&ga, 1) == OK)
906 ga.ga_len += mb_char2bytes(' ',
907 (char_u *)ga.ga_data + ga.ga_len);
908 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200909 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200910 {
911 width = cell.width;
912
913 p[pos.col].width = cell.width;
914 p[pos.col].attrs = cell.attrs;
915 p[pos.col].fg = cell.fg;
916 p[pos.col].bg = cell.bg;
917
918 if (ga_grow(&ga, MB_MAXBYTES) == OK)
919 {
920 int i;
921 int c;
922
923 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
924 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
925 (char_u *)ga.ga_data + ga.ga_len);
926 }
927 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200928 }
929 line->sb_cols = len;
930 line->sb_cells = p;
931 ++term->tl_scrollback.ga_len;
932
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200933 if (ga_grow(&ga, 1) == FAIL)
934 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
935 else
936 {
937 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
938 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
939 }
940 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200941 }
942 else
943 vim_free(p);
944 }
945 }
946
947 FOR_ALL_WINDOWS(wp)
948 {
949 if (wp->w_buffer == term->tl_buffer)
950 {
951 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
952 wp->w_cursor.col = 0;
953 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200954 if (wp->w_cursor.lnum >= wp->w_height)
955 {
956 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
957
958 if (wp->w_topline < min_topline)
959 wp->w_topline = min_topline;
960 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200961 redraw_win_later(wp, NOT_VALID);
962 }
963 }
964}
965
966 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200967set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200968{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200969 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200970 vim_free(term->tl_status_text);
971 term->tl_status_text = NULL;
972 if (term->tl_buffer == curbuf)
973 maketitle();
974}
975
976/*
977 * Called after the job if finished and Terminal mode is not active:
978 * Move the vterm contents into the scrollback buffer and free the vterm.
979 */
980 static void
981cleanup_vterm(term_T *term)
982{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200983 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200984 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200985 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200986 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200987}
988
989/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200990 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200991 * Suspends updating the terminal window.
992 */
993 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200994term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200995{
996 term_T *term = curbuf->b_term;
997
998 /* Append the current terminal contents to the buffer. */
999 move_terminal_to_buffer(term);
1000
Bram Moolenaar6d819742017-08-06 14:57:49 +02001001 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001002
Bram Moolenaar6d819742017-08-06 14:57:49 +02001003 /* Move the window cursor to the position of the cursor in the
1004 * terminal. */
1005 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1006 + term->tl_cursor_pos.row + 1;
1007 check_cursor();
1008 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001009
Bram Moolenaar6d819742017-08-06 14:57:49 +02001010 /* Display the same lines as in the terminal. */
1011 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001012}
1013
1014/*
1015 * Returns TRUE if the current window contains a terminal and we are in
1016 * Terminal-Normal mode.
1017 */
1018 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001019term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001020{
1021 term_T *term = curbuf->b_term;
1022
Bram Moolenaar6d819742017-08-06 14:57:49 +02001023 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001024}
1025
1026/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001027 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001028 * Restores updating the terminal window.
1029 */
1030 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001031term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001032{
1033 term_T *term = curbuf->b_term;
1034 sb_line_T *line;
1035 garray_T *gap;
1036
1037 /* Remove the terminal contents from the scrollback and the buffer. */
1038 gap = &term->tl_scrollback;
1039 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
1040 {
1041 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1042 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1043 vim_free(line->sb_cells);
1044 --gap->ga_len;
1045 if (gap->ga_len == 0)
1046 break;
1047 }
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;
1290 ui_cursor_shape_forced(TRUE);
1291 term_cursor_color((char_u *)"");
1292 term_cursor_blink(FALSE);
1293 }
1294}
1295
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001296/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001297 * Returns TRUE if the current window contains a terminal and we are sending
1298 * keys to the job.
1299 */
1300 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001301term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001302{
1303 term_T *term = curbuf->b_term;
1304
1305 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001306 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001307 && term->tl_vterm != NULL
1308 && term_job_running(term);
1309}
1310
1311/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001312 * Wait for input and send it to the job.
1313 * Return when the start of a CTRL-W command is typed or anything else that
1314 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001315 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1316 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001317 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001318 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001319terminal_loop(void)
1320{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001321 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001322 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001323 int ret;
1324
Bram Moolenaar679653e2017-08-13 14:13:19 +02001325 /* Remember the terminal we are sending keys to. However, the terminal
1326 * might be closed while waiting for a character, e.g. typing "exit" in a
1327 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1328 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001329 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001330
1331 if (*curwin->w_p_tk != NUL)
1332 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001333 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001334 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001335
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001336#ifdef UNIX
1337 {
1338 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[PART_IN].ch_fd;
1339
1340 if (isatty(fd))
1341 {
1342 ttyinfo_T info;
1343
1344 /* Get the current backspace and enter characters of the pty. */
1345 if (get_tty_info(fd, &info) == OK)
1346 {
1347 term_backspace_char = info.backspace;
1348 term_enter_char = info.enter;
1349 term_nl_does_cr = info.nl_does_cr;
1350 }
1351 }
1352 }
1353#endif
1354
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001355 for (;;)
1356 {
1357 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001358 /* Repeat redrawing in case a message is received while redrawing. */
1359 while (curwin->w_redr_type != 0)
1360 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001361 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001362
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001363 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001364 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001365 /* job finished while waiting for a character */
1366 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001367 if (c == K_IGNORE)
1368 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001369
Bram Moolenaarfae42832017-08-01 22:24:26 +02001370#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001371 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001372 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001373 if (ctrl_break_was_pressed)
1374 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001375#endif
1376
Bram Moolenaar69198192017-08-05 14:10:48 +02001377 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001378 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001379 int prev_c = c;
1380
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001381#ifdef FEAT_CMDL_INFO
1382 if (add_to_showcmd(c))
1383 out_flush();
1384#endif
1385 c = term_vgetc();
1386#ifdef FEAT_CMDL_INFO
1387 clear_showcmd();
1388#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001389 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001390 /* job finished while waiting for a character */
1391 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001392
Bram Moolenaar69198192017-08-05 14:10:48 +02001393 if (prev_c == Ctrl_BSL)
1394 {
1395 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001396 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001397 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1398 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001399 ret = FAIL;
1400 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001401 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001402 /* Send both keys to the terminal. */
1403 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1404 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001405 else if (c == Ctrl_C)
1406 {
1407 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1408 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1409 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001410 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001411 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001412 /* "CTRL-W .": send CTRL-W to the job */
1413 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001414 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001415 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001416 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001417 /* CTRL-W N : go to Terminal-Normal mode. */
1418 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001419 ret = FAIL;
1420 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001421 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001422 else if (c == '"')
1423 {
1424 term_paste_register(prev_c);
1425 continue;
1426 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001427 else if (termkey == 0 || c != termkey)
1428 {
1429 stuffcharReadbuff(Ctrl_W);
1430 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001431 ret = OK;
1432 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001433 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001434 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001435 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001436 {
1437 ret = OK;
1438 goto theend;
1439 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001440 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001441 ret = FAIL;
1442
1443theend:
1444 in_terminal_loop = NULL;
1445 may_restore_cursor_props();
1446 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001447}
1448
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001449/*
1450 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001451 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001452 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001453 */
1454 void
1455term_job_ended(job_T *job)
1456{
1457 term_T *term;
1458 int did_one = FALSE;
1459
1460 for (term = first_term; term != NULL; term = term->tl_next)
1461 if (term->tl_job == job)
1462 {
1463 vim_free(term->tl_title);
1464 term->tl_title = NULL;
1465 vim_free(term->tl_status_text);
1466 term->tl_status_text = NULL;
1467 redraw_buf_and_status_later(term->tl_buffer, VALID);
1468 did_one = TRUE;
1469 }
1470 if (did_one)
1471 redraw_statuslines();
1472 if (curbuf->b_term != NULL)
1473 {
1474 if (curbuf->b_term->tl_job == job)
1475 maketitle();
1476 update_cursor(curbuf->b_term, TRUE);
1477 }
1478}
1479
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001480 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001481may_toggle_cursor(term_T *term)
1482{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001483 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001484 {
1485 if (term->tl_cursor_visible)
1486 cursor_on();
1487 else
1488 cursor_off();
1489 }
1490}
1491
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001492/*
1493 * Reverse engineer the RGB value into a cterm color index.
1494 * First color is 1. Return 0 if no match found.
1495 */
1496 static int
1497color2index(VTermColor *color, int fg, int *boldp)
1498{
1499 int red = color->red;
1500 int blue = color->blue;
1501 int green = color->green;
1502
1503 /* The argument for lookup_color() is for the color_names[] table. */
1504 if (red == 0)
1505 {
1506 if (green == 0)
1507 {
1508 if (blue == 0)
1509 return lookup_color(0, fg, boldp) + 1; /* black */
1510 if (blue == 224)
1511 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1512 }
1513 else if (green == 224)
1514 {
1515 if (blue == 0)
1516 return lookup_color(2, fg, boldp) + 1; /* dark green */
1517 if (blue == 224)
1518 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1519 }
1520 }
1521 else if (red == 224)
1522 {
1523 if (green == 0)
1524 {
1525 if (blue == 0)
1526 return lookup_color(4, fg, boldp) + 1; /* dark red */
1527 if (blue == 224)
1528 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1529 }
1530 else if (green == 224)
1531 {
1532 if (blue == 0)
1533 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1534 if (blue == 224)
1535 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1536 }
1537 }
1538 else if (red == 128)
1539 {
1540 if (green == 128 && blue == 128)
1541 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1542 }
1543 else if (red == 255)
1544 {
1545 if (green == 64)
1546 {
1547 if (blue == 64)
1548 return lookup_color(20, fg, boldp) + 1; /* light red */
1549 if (blue == 255)
1550 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1551 }
1552 else if (green == 255)
1553 {
1554 if (blue == 64)
1555 return lookup_color(24, fg, boldp) + 1; /* yellow */
1556 if (blue == 255)
1557 return lookup_color(26, fg, boldp) + 1; /* white */
1558 }
1559 }
1560 else if (red == 64)
1561 {
1562 if (green == 64)
1563 {
1564 if (blue == 255)
1565 return lookup_color(14, fg, boldp) + 1; /* light blue */
1566 }
1567 else if (green == 255)
1568 {
1569 if (blue == 64)
1570 return lookup_color(16, fg, boldp) + 1; /* light green */
1571 if (blue == 255)
1572 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1573 }
1574 }
1575 if (t_colors >= 256)
1576 {
1577 if (red == blue && red == green)
1578 {
1579 /* 24-color greyscale */
1580 static int cutoff[23] = {
1581 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1582 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1583 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1584 int i;
1585
1586 for (i = 0; i < 23; ++i)
1587 if (red < cutoff[i])
1588 return i + 233;
1589 return 256;
1590 }
1591
1592 /* 216-color cube */
1593 return 17 + ((red + 25) / 0x33) * 36
1594 + ((green + 25) / 0x33) * 6
1595 + (blue + 25) / 0x33;
1596 }
1597 return 0;
1598}
1599
1600/*
1601 * Convert the attributes of a vterm cell into an attribute index.
1602 */
1603 static int
1604cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1605{
1606 int attr = 0;
1607
1608 if (cellattrs.bold)
1609 attr |= HL_BOLD;
1610 if (cellattrs.underline)
1611 attr |= HL_UNDERLINE;
1612 if (cellattrs.italic)
1613 attr |= HL_ITALIC;
1614 if (cellattrs.strike)
1615 attr |= HL_STANDOUT;
1616 if (cellattrs.reverse)
1617 attr |= HL_INVERSE;
1618
1619#ifdef FEAT_GUI
1620 if (gui.in_use)
1621 {
1622 guicolor_T fg, bg;
1623
1624 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1625 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1626 return get_gui_attr_idx(attr, fg, bg);
1627 }
1628 else
1629#endif
1630#ifdef FEAT_TERMGUICOLORS
1631 if (p_tgc)
1632 {
1633 guicolor_T fg, bg;
1634
1635 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1636 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1637
1638 return get_tgc_attr_idx(attr, fg, bg);
1639 }
1640 else
1641#endif
1642 {
1643 int bold = MAYBE;
1644 int fg = color2index(&cellfg, TRUE, &bold);
1645 int bg = color2index(&cellbg, FALSE, &bold);
1646
1647 /* with 8 colors set the bold attribute to get a bright foreground */
1648 if (bold == TRUE)
1649 attr |= HL_BOLD;
1650 return get_cterm_attr_idx(attr, fg, bg);
1651 }
1652 return 0;
1653}
1654
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001655 static int
1656handle_damage(VTermRect rect, void *user)
1657{
1658 term_T *term = (term_T *)user;
1659
1660 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1661 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1662 redraw_buf_later(term->tl_buffer, NOT_VALID);
1663 return 1;
1664}
1665
1666 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001667handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001668{
1669 term_T *term = (term_T *)user;
1670
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001671 /* Scrolling up is done much more efficiently by deleting lines instead of
1672 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001673 if (dest.start_col == src.start_col
1674 && dest.end_col == src.end_col
1675 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001676 {
1677 win_T *wp;
1678 VTermColor fg, bg;
1679 VTermScreenCellAttrs attr;
1680 int clear_attr;
1681
1682 /* Set the color to clear lines with. */
1683 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1684 &fg, &bg);
1685 vim_memset(&attr, 0, sizeof(attr));
1686 clear_attr = cell2attr(attr, fg, bg);
1687
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001688 FOR_ALL_WINDOWS(wp)
1689 {
1690 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001691 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001692 src.start_row - dest.start_row, FALSE, FALSE,
1693 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001694 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001695 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001696 redraw_buf_later(term->tl_buffer, NOT_VALID);
1697 return 1;
1698}
1699
1700 static int
1701handle_movecursor(
1702 VTermPos pos,
1703 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001704 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001705 void *user)
1706{
1707 term_T *term = (term_T *)user;
1708 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001709
1710 term->tl_cursor_pos = pos;
1711 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001712
1713 FOR_ALL_WINDOWS(wp)
1714 {
1715 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001716 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001717 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001718 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001719 {
1720 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001721 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001722 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001723
1724 return 1;
1725}
1726
Bram Moolenaar21554412017-07-24 21:44:43 +02001727 static int
1728handle_settermprop(
1729 VTermProp prop,
1730 VTermValue *value,
1731 void *user)
1732{
1733 term_T *term = (term_T *)user;
1734
1735 switch (prop)
1736 {
1737 case VTERM_PROP_TITLE:
1738 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001739 /* a blank title isn't useful, make it empty, so that "running" is
1740 * displayed */
1741 if (*skipwhite((char_u *)value->string) == NUL)
1742 term->tl_title = NULL;
1743 else
1744 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001745 vim_free(term->tl_status_text);
1746 term->tl_status_text = NULL;
1747 if (term == curbuf->b_term)
1748 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001749 break;
1750
1751 case VTERM_PROP_CURSORVISIBLE:
1752 term->tl_cursor_visible = value->boolean;
1753 may_toggle_cursor(term);
1754 out_flush();
1755 break;
1756
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001757 case VTERM_PROP_CURSORBLINK:
1758 term->tl_cursor_blink = value->boolean;
1759 may_set_cursor_props(term);
1760 break;
1761
1762 case VTERM_PROP_CURSORSHAPE:
1763 term->tl_cursor_shape = value->number;
1764 may_set_cursor_props(term);
1765 break;
1766
1767 case VTERM_PROP_CURSORCOLOR:
1768 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001769 if (*value->string == NUL)
1770 term->tl_cursor_color = NULL;
1771 else
1772 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001773 may_set_cursor_props(term);
1774 break;
1775
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001776 case VTERM_PROP_ALTSCREEN:
1777 /* TODO: do anything else? */
1778 term->tl_using_altscreen = value->boolean;
1779 break;
1780
Bram Moolenaar21554412017-07-24 21:44:43 +02001781 default:
1782 break;
1783 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001784 /* Always return 1, otherwise vterm doesn't store the value internally. */
1785 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001786}
1787
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001788/*
1789 * The job running in the terminal resized the terminal.
1790 */
1791 static int
1792handle_resize(int rows, int cols, void *user)
1793{
1794 term_T *term = (term_T *)user;
1795 win_T *wp;
1796
1797 term->tl_rows = rows;
1798 term->tl_cols = cols;
1799 FOR_ALL_WINDOWS(wp)
1800 {
1801 if (wp->w_buffer == term->tl_buffer)
1802 {
1803 win_setheight_win(rows, wp);
1804 win_setwidth_win(cols, wp);
1805 }
1806 }
1807
1808 redraw_buf_later(term->tl_buffer, NOT_VALID);
1809 return 1;
1810}
1811
Bram Moolenaard85f2712017-07-28 21:51:57 +02001812/*
1813 * Handle a line that is pushed off the top of the screen.
1814 */
1815 static int
1816handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1817{
1818 term_T *term = (term_T *)user;
1819
1820 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001821 if (ga_grow(&term->tl_scrollback, 1) == OK)
1822 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001823 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001824 int len = 0;
1825 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001826 int c;
1827 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001828 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001829 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001830
1831 /* do not store empty cells at the end */
1832 for (i = 0; i < cols; ++i)
1833 if (cells[i].chars[0] != 0)
1834 len = i + 1;
1835
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001836 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001837 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001838 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001839 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001840 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001841 for (col = 0; col < len; col += cells[col].width)
1842 {
1843 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1844 {
1845 ga.ga_len = 0;
1846 break;
1847 }
1848 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1849 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1850 (char_u *)ga.ga_data + ga.ga_len);
1851 p[col].width = cells[col].width;
1852 p[col].attrs = cells[col].attrs;
1853 p[col].fg = cells[col].fg;
1854 p[col].bg = cells[col].bg;
1855 }
1856 }
1857 if (ga_grow(&ga, 1) == FAIL)
1858 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1859 else
1860 {
1861 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1862 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1863 }
1864 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001865
1866 line = (sb_line_T *)term->tl_scrollback.ga_data
1867 + term->tl_scrollback.ga_len;
1868 line->sb_cols = len;
1869 line->sb_cells = p;
1870 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001871 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001872 }
1873 return 0; /* ignored */
1874}
1875
Bram Moolenaar21554412017-07-24 21:44:43 +02001876static VTermScreenCallbacks screen_callbacks = {
1877 handle_damage, /* damage */
1878 handle_moverect, /* moverect */
1879 handle_movecursor, /* movecursor */
1880 handle_settermprop, /* settermprop */
1881 NULL, /* bell */
1882 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001883 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001884 NULL /* sb_popline */
1885};
1886
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001887/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001888 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001889 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001890 */
1891 void
1892term_channel_closed(channel_T *ch)
1893{
1894 term_T *term;
1895 int did_one = FALSE;
1896
1897 for (term = first_term; term != NULL; term = term->tl_next)
1898 if (term->tl_job == ch->ch_job)
1899 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001900 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001901 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001902
Bram Moolenaard85f2712017-07-28 21:51:57 +02001903 vim_free(term->tl_title);
1904 term->tl_title = NULL;
1905 vim_free(term->tl_status_text);
1906 term->tl_status_text = NULL;
1907
Bram Moolenaar423802d2017-07-30 16:52:24 +02001908 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001909 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001910 {
1911 int fnum = term->tl_buffer->b_fnum;
1912
Bram Moolenaar423802d2017-07-30 16:52:24 +02001913 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001914
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001915 if (term->tl_finish == 'c')
1916 {
1917 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001918 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001919 curbuf = term->tl_buffer;
1920 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1921 break;
1922 }
1923 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1924 {
1925 char buf[50];
1926
1927 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001928 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001929 vim_snprintf(buf, sizeof(buf),
1930 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001931 ? "botright sbuf %d"
1932 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001933 do_cmdline_cmd((char_u *)buf);
1934 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001935 else
1936 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001937 }
1938
Bram Moolenaard85f2712017-07-28 21:51:57 +02001939 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001940 }
1941 if (did_one)
1942 {
1943 redraw_statuslines();
1944
1945 /* Need to break out of vgetc(). */
1946 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001947 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001948
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001949 term = curbuf->b_term;
1950 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001951 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001952 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001953 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001954 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001955 }
1956 }
1957}
1958
1959/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001960 * Called to update a window that contains an active terminal.
1961 * Returns FAIL when there is no terminal running in this window or in
1962 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001963 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001964 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001965term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001966{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001967 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001968 VTerm *vterm;
1969 VTermScreen *screen;
1970 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001971 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001972
Bram Moolenaar6d819742017-08-06 14:57:49 +02001973 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001974 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001975
Bram Moolenaard85f2712017-07-28 21:51:57 +02001976 vterm = term->tl_vterm;
1977 screen = vterm_obtain_screen(vterm);
1978 state = vterm_obtain_state(vterm);
1979
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001980 /*
1981 * If the window was resized a redraw will be triggered and we get here.
1982 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1983 */
1984 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1985 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001986 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001987 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1988 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1989 win_T *twp;
1990
1991 FOR_ALL_WINDOWS(twp)
1992 {
1993 /* When more than one window shows the same terminal, use the
1994 * smallest size. */
1995 if (twp->w_buffer == term->tl_buffer)
1996 {
1997 if (!term->tl_rows_fixed && rows > twp->w_height)
1998 rows = twp->w_height;
1999 if (!term->tl_cols_fixed && cols > twp->w_width)
2000 cols = twp->w_width;
2001 }
2002 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002003
2004 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002005 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002006 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002007 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002008 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002009
2010 /* The cursor may have been moved when resizing. */
2011 vterm_state_get_cursorpos(state, &pos);
2012 position_cursor(wp, &pos);
2013
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002014 /* TODO: Only redraw what changed. */
2015 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002016 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002017 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002018 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002019
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002020 if (pos.row < term->tl_rows)
2021 {
2022 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002023 {
2024 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002025 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002026
Bram Moolenaareeac6772017-07-23 15:48:37 +02002027 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2028 vim_memset(&cell, 0, sizeof(cell));
2029
2030 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002031 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002032 if (c == NUL)
2033 {
2034 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002035#if defined(FEAT_MBYTE)
2036 if (enc_utf8)
2037 ScreenLinesUC[off] = NUL;
2038#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002039 }
2040 else
2041 {
2042#if defined(FEAT_MBYTE)
2043 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002044 {
2045 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002046 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002047 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002048 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002049 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002050 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002051 if (enc_utf8)
2052 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002053 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002054#else
2055 ScreenLines[off] = c;
2056#endif
2057 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002058 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002059
2060 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002061 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002062 if (cell.width == 2)
2063 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002064 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002065#if defined(FEAT_MBYTE)
2066 if (enc_utf8)
2067 ScreenLinesUC[off] = NUL;
2068#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002069 ++pos.col;
2070 ++off;
2071 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002072 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002073 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002074 else
2075 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002076
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002077 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2078 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002079 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002080
2081 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002082}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002083
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002084/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002085 * Return TRUE if "wp" is a terminal window where the job has finished.
2086 */
2087 int
2088term_is_finished(buf_T *buf)
2089{
2090 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2091}
2092
2093/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002094 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002095 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002096 */
2097 int
2098term_show_buffer(buf_T *buf)
2099{
2100 term_T *term = buf->b_term;
2101
Bram Moolenaar6d819742017-08-06 14:57:49 +02002102 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002103}
2104
2105/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002106 * The current buffer is going to be changed. If there is terminal
2107 * highlighting remove it now.
2108 */
2109 void
2110term_change_in_curbuf(void)
2111{
2112 term_T *term = curbuf->b_term;
2113
2114 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2115 {
2116 free_scrollback(term);
2117 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002118
2119 /* The buffer is now like a normal buffer, it cannot be easily
2120 * abandoned when changed. */
2121 set_string_option_direct((char_u *)"buftype", -1,
2122 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002123 }
2124}
2125
2126/*
2127 * Get the screen attribute for a position in the buffer.
2128 */
2129 int
2130term_get_attr(buf_T *buf, linenr_T lnum, int col)
2131{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002132 term_T *term = buf->b_term;
2133 sb_line_T *line;
2134 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002135
Bram Moolenaar70229f92017-07-29 16:01:53 +02002136 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002137 return 0;
2138 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2139 if (col >= line->sb_cols)
2140 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002141 cellattr = line->sb_cells + col;
2142 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002143}
2144
2145/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002146 * Create a new vterm and initialize it.
2147 */
2148 static void
2149create_vterm(term_T *term, int rows, int cols)
2150{
2151 VTerm *vterm;
2152 VTermScreen *screen;
2153
2154 vterm = vterm_new(rows, cols);
2155 term->tl_vterm = vterm;
2156 screen = vterm_obtain_screen(vterm);
2157 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2158 /* TODO: depends on 'encoding'. */
2159 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002160
2161 /* Vterm uses a default black background. Set it to white when
2162 * 'background' is "light". */
2163 if (*p_bg == 'l')
2164 {
2165 VTermColor fg, bg;
2166
2167 fg.red = fg.green = fg.blue = 0;
2168 bg.red = bg.green = bg.blue = 255;
2169 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2170 }
2171
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002172 /* Required to initialize most things. */
2173 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002174
2175 /* Allow using alternate screen. */
2176 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002177}
2178
Bram Moolenaar21554412017-07-24 21:44:43 +02002179/*
2180 * Return the text to show for the buffer name and status.
2181 */
2182 char_u *
2183term_get_status_text(term_T *term)
2184{
2185 if (term->tl_status_text == NULL)
2186 {
2187 char_u *txt;
2188 size_t len;
2189
Bram Moolenaar6d819742017-08-06 14:57:49 +02002190 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002191 {
2192 if (term_job_running(term))
2193 txt = (char_u *)_("Terminal");
2194 else
2195 txt = (char_u *)_("Terminal-finished");
2196 }
2197 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002198 txt = term->tl_title;
2199 else if (term_job_running(term))
2200 txt = (char_u *)_("running");
2201 else
2202 txt = (char_u *)_("finished");
2203 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002204 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002205 if (term->tl_status_text != NULL)
2206 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2207 term->tl_buffer->b_fname, txt);
2208 }
2209 return term->tl_status_text;
2210}
2211
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002212/*
2213 * Mark references in jobs of terminals.
2214 */
2215 int
2216set_ref_in_term(int copyID)
2217{
2218 int abort = FALSE;
2219 term_T *term;
2220 typval_T tv;
2221
2222 for (term = first_term; term != NULL; term = term->tl_next)
2223 if (term->tl_job != NULL)
2224 {
2225 tv.v_type = VAR_JOB;
2226 tv.vval.v_job = term->tl_job;
2227 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2228 }
2229 return abort;
2230}
2231
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002232/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002233 * Get the buffer from the first argument in "argvars".
2234 * Returns NULL when the buffer is not for a terminal window.
2235 */
2236 static buf_T *
2237term_get_buf(typval_T *argvars)
2238{
2239 buf_T *buf;
2240
2241 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2242 ++emsg_off;
2243 buf = get_buf_tv(&argvars[0], FALSE);
2244 --emsg_off;
2245 if (buf == NULL || buf->b_term == NULL)
2246 return NULL;
2247 return buf;
2248}
2249
2250/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002251 * "term_getaltscreen(buf)" function
2252 */
2253 void
2254f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2255{
2256 buf_T *buf = term_get_buf(argvars);
2257
2258 if (buf == NULL)
2259 return;
2260 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2261}
2262
2263/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002264 * "term_getattr(attr, name)" function
2265 */
2266 void
2267f_term_getattr(typval_T *argvars, typval_T *rettv)
2268{
2269 int attr;
2270 size_t i;
2271 char_u *name;
2272
2273 static struct {
2274 char *name;
2275 int attr;
2276 } attrs[] = {
2277 {"bold", HL_BOLD},
2278 {"italic", HL_ITALIC},
2279 {"underline", HL_UNDERLINE},
2280 {"strike", HL_STANDOUT},
2281 {"reverse", HL_INVERSE},
2282 };
2283
2284 attr = get_tv_number(&argvars[0]);
2285 name = get_tv_string_chk(&argvars[1]);
2286 if (name == NULL)
2287 return;
2288
2289 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2290 if (STRCMP(name, attrs[i].name) == 0)
2291 {
2292 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2293 break;
2294 }
2295}
2296
2297/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002298 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002299 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002300 void
2301f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002302{
Bram Moolenaar97870002017-07-30 18:28:38 +02002303 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002304 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002305 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002306 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002307
Bram Moolenaar97870002017-07-30 18:28:38 +02002308 if (rettv_list_alloc(rettv) == FAIL)
2309 return;
2310 if (buf == NULL)
2311 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002312 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002313
2314 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002315 list_append_number(l, term->tl_cursor_pos.row + 1);
2316 list_append_number(l, term->tl_cursor_pos.col + 1);
2317
2318 d = dict_alloc();
2319 if (d != NULL)
2320 {
2321 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2322 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2323 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2324 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2325 ? (char_u *)"" : term->tl_cursor_color);
2326 list_append_dict(l, d);
2327 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002328}
2329
2330/*
2331 * "term_getjob(buf)" function
2332 */
2333 void
2334f_term_getjob(typval_T *argvars, typval_T *rettv)
2335{
2336 buf_T *buf = term_get_buf(argvars);
2337
2338 rettv->v_type = VAR_JOB;
2339 rettv->vval.v_job = NULL;
2340 if (buf == NULL)
2341 return;
2342
2343 rettv->vval.v_job = buf->b_term->tl_job;
2344 if (rettv->vval.v_job != NULL)
2345 ++rettv->vval.v_job->jv_refcount;
2346}
2347
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002348 static int
2349get_row_number(typval_T *tv, term_T *term)
2350{
2351 if (tv->v_type == VAR_STRING
2352 && tv->vval.v_string != NULL
2353 && STRCMP(tv->vval.v_string, ".") == 0)
2354 return term->tl_cursor_pos.row;
2355 return (int)get_tv_number(tv) - 1;
2356}
2357
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002358/*
2359 * "term_getline(buf, row)" function
2360 */
2361 void
2362f_term_getline(typval_T *argvars, typval_T *rettv)
2363{
2364 buf_T *buf = term_get_buf(argvars);
2365 term_T *term;
2366 int row;
2367
2368 rettv->v_type = VAR_STRING;
2369 if (buf == NULL)
2370 return;
2371 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002372 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002373
2374 if (term->tl_vterm == NULL)
2375 {
2376 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2377
2378 /* vterm is finished, get the text from the buffer */
2379 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2380 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2381 }
2382 else
2383 {
2384 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2385 VTermRect rect;
2386 int len;
2387 char_u *p;
2388
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002389 if (row < 0 || row >= term->tl_rows)
2390 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002391 len = term->tl_cols * MB_MAXBYTES + 1;
2392 p = alloc(len);
2393 if (p == NULL)
2394 return;
2395 rettv->vval.v_string = p;
2396
2397 rect.start_col = 0;
2398 rect.end_col = term->tl_cols;
2399 rect.start_row = row;
2400 rect.end_row = row + 1;
2401 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2402 }
2403}
2404
2405/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002406 * "term_getscrolled(buf)" function
2407 */
2408 void
2409f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2410{
2411 buf_T *buf = term_get_buf(argvars);
2412
2413 if (buf == NULL)
2414 return;
2415 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2416}
2417
2418/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002419 * "term_getsize(buf)" function
2420 */
2421 void
2422f_term_getsize(typval_T *argvars, typval_T *rettv)
2423{
2424 buf_T *buf = term_get_buf(argvars);
2425 list_T *l;
2426
2427 if (rettv_list_alloc(rettv) == FAIL)
2428 return;
2429 if (buf == NULL)
2430 return;
2431
2432 l = rettv->vval.v_list;
2433 list_append_number(l, buf->b_term->tl_rows);
2434 list_append_number(l, buf->b_term->tl_cols);
2435}
2436
2437/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002438 * "term_getstatus(buf)" function
2439 */
2440 void
2441f_term_getstatus(typval_T *argvars, typval_T *rettv)
2442{
2443 buf_T *buf = term_get_buf(argvars);
2444 term_T *term;
2445 char_u val[100];
2446
2447 rettv->v_type = VAR_STRING;
2448 if (buf == NULL)
2449 return;
2450 term = buf->b_term;
2451
2452 if (term_job_running(term))
2453 STRCPY(val, "running");
2454 else
2455 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002456 if (term->tl_normal_mode)
2457 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002458 rettv->vval.v_string = vim_strsave(val);
2459}
2460
2461/*
2462 * "term_gettitle(buf)" function
2463 */
2464 void
2465f_term_gettitle(typval_T *argvars, typval_T *rettv)
2466{
2467 buf_T *buf = term_get_buf(argvars);
2468
2469 rettv->v_type = VAR_STRING;
2470 if (buf == NULL)
2471 return;
2472
2473 if (buf->b_term->tl_title != NULL)
2474 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2475}
2476
2477/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002478 * "term_gettty(buf)" function
2479 */
2480 void
2481f_term_gettty(typval_T *argvars, typval_T *rettv)
2482{
2483 buf_T *buf = term_get_buf(argvars);
2484 char_u *p;
2485
2486 rettv->v_type = VAR_STRING;
2487 if (buf == NULL)
2488 return;
2489 if (buf->b_term->tl_job != NULL)
2490 p = buf->b_term->tl_job->jv_tty_name;
2491 else
2492 p = buf->b_term->tl_tty_name;
2493 if (p != NULL)
2494 rettv->vval.v_string = vim_strsave(p);
2495}
2496
2497/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002498 * "term_list()" function
2499 */
2500 void
2501f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2502{
2503 term_T *tp;
2504 list_T *l;
2505
2506 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2507 return;
2508
2509 l = rettv->vval.v_list;
2510 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2511 if (tp != NULL && tp->tl_buffer != NULL)
2512 if (list_append_number(l,
2513 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2514 return;
2515}
2516
2517/*
2518 * "term_scrape(buf, row)" function
2519 */
2520 void
2521f_term_scrape(typval_T *argvars, typval_T *rettv)
2522{
2523 buf_T *buf = term_get_buf(argvars);
2524 VTermScreen *screen = NULL;
2525 VTermPos pos;
2526 list_T *l;
2527 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002528 char_u *p;
2529 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002530
2531 if (rettv_list_alloc(rettv) == FAIL)
2532 return;
2533 if (buf == NULL)
2534 return;
2535 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002536
2537 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002538 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002539
2540 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002541 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002542 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002543 p = NULL;
2544 line = NULL;
2545 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002546 else
2547 {
2548 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2549
2550 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2551 return;
2552 p = ml_get_buf(buf, lnum + 1, FALSE);
2553 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2554 }
2555
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002556 for (pos.col = 0; pos.col < term->tl_cols; )
2557 {
2558 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002559 int width;
2560 VTermScreenCellAttrs attrs;
2561 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002562 char_u rgb[8];
2563 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2564 int off = 0;
2565 int i;
2566
2567 if (screen == NULL)
2568 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002569 cellattr_T *cellattr;
2570 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002571
2572 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002573 if (pos.col >= line->sb_cols)
2574 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002575 cellattr = line->sb_cells + pos.col;
2576 width = cellattr->width;
2577 attrs = cellattr->attrs;
2578 fg = cellattr->fg;
2579 bg = cellattr->bg;
2580 len = MB_PTR2LEN(p);
2581 mch_memmove(mbs, p, len);
2582 mbs[len] = NUL;
2583 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002584 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002585 else
2586 {
2587 VTermScreenCell cell;
2588 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2589 break;
2590 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2591 {
2592 if (cell.chars[i] == 0)
2593 break;
2594 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2595 }
2596 mbs[off] = NUL;
2597 width = cell.width;
2598 attrs = cell.attrs;
2599 fg = cell.fg;
2600 bg = cell.bg;
2601 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002602 dcell = dict_alloc();
2603 list_append_dict(l, dcell);
2604
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002605 dict_add_nr_str(dcell, "chars", 0, mbs);
2606
2607 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002608 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002609 dict_add_nr_str(dcell, "fg", 0, rgb);
2610 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002611 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002612 dict_add_nr_str(dcell, "bg", 0, rgb);
2613
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002614 dict_add_nr_str(dcell, "attr",
2615 cell2attr(attrs, fg, bg), NULL);
2616 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002617
2618 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002619 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002620 ++pos.col;
2621 }
2622}
2623
2624/*
2625 * "term_sendkeys(buf, keys)" function
2626 */
2627 void
2628f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2629{
2630 buf_T *buf = term_get_buf(argvars);
2631 char_u *msg;
2632 term_T *term;
2633
2634 rettv->v_type = VAR_UNKNOWN;
2635 if (buf == NULL)
2636 return;
2637
2638 msg = get_tv_string_chk(&argvars[1]);
2639 if (msg == NULL)
2640 return;
2641 term = buf->b_term;
2642 if (term->tl_vterm == NULL)
2643 return;
2644
2645 while (*msg != NUL)
2646 {
2647 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2648 msg += MB_PTR2LEN(msg);
2649 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002650}
2651
2652/*
2653 * "term_start(command, options)" function
2654 */
2655 void
2656f_term_start(typval_T *argvars, typval_T *rettv)
2657{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002658 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002659
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002660 init_job_options(&opt);
2661 /* TODO: allow more job options */
2662 if (argvars[1].v_type != VAR_UNKNOWN
2663 && get_job_options(&argvars[1], &opt,
2664 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002665 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002666 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002667 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002668 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002669 return;
2670
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002671 if (opt.jo_vertical)
2672 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002673 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002674
2675 if (curbuf->b_term != NULL)
2676 rettv->vval.v_number = curbuf->b_fnum;
2677}
2678
2679/*
2680 * "term_wait" function
2681 */
2682 void
2683f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2684{
2685 buf_T *buf = term_get_buf(argvars);
2686
2687 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002688 {
2689 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002690 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002691 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002692 if (buf->b_term->tl_job == NULL)
2693 {
2694 ch_log(NULL, "term_wait(): no job to wait for");
2695 return;
2696 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002697
2698 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002699 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002700 {
2701 /* The job is dead, keep reading channel I/O until the channel is
2702 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002703 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002704 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2705 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002706 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002707 parse_queued_messages();
2708 ui_delay(10L, FALSE);
2709 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002710 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002711 parse_queued_messages();
2712 }
2713 else
2714 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002715 long wait = 10L;
2716
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002717 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002718 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002719
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002720 /* Wait for some time for any channel I/O. */
2721 if (argvars[1].v_type != VAR_UNKNOWN)
2722 wait = get_tv_number(&argvars[1]);
2723 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002724 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002725
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002726 /* Flushing messages on channels is hopefully sufficient.
2727 * TODO: is there a better way? */
2728 parse_queued_messages();
2729 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002730}
2731
Bram Moolenaara83e3962017-08-17 14:39:07 +02002732# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002733
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002734/**************************************
2735 * 2. MS-Windows implementation.
2736 */
2737
Bram Moolenaara83e3962017-08-17 14:39:07 +02002738# ifndef PROTO
2739
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002740#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2741#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2742
Bram Moolenaar8a773062017-07-24 22:29:21 +02002743void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002744void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002745void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002746BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2747void (*winpty_config_set_initial_size)(void*, int, int);
2748LPCWSTR (*winpty_conin_name)(void*);
2749LPCWSTR (*winpty_conout_name)(void*);
2750LPCWSTR (*winpty_conerr_name)(void*);
2751void (*winpty_free)(void*);
2752void (*winpty_config_free)(void*);
2753void (*winpty_spawn_config_free)(void*);
2754void (*winpty_error_free)(void*);
2755LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002756BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002757HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002758
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002759#define WINPTY_DLL "winpty.dll"
2760
2761static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002762# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002763
Bram Moolenaara83e3962017-08-17 14:39:07 +02002764 static int
2765dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002766{
2767 int i;
2768 static struct
2769 {
2770 char *name;
2771 FARPROC *ptr;
2772 } winpty_entry[] =
2773 {
2774 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2775 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2776 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2777 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2778 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2779 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2780 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2781 {"winpty_free", (FARPROC*)&winpty_free},
2782 {"winpty_open", (FARPROC*)&winpty_open},
2783 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2784 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2785 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2786 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002787 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002788 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002789 {NULL, NULL}
2790 };
2791
2792 /* No need to initialize twice. */
2793 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002794 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002795 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2796 * winpty.dll. */
2797 if (*p_winptydll != NUL)
2798 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2799 if (!hWinPtyDLL)
2800 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002801 if (!hWinPtyDLL)
2802 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002803 if (verbose)
2804 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2805 : (char_u *)WINPTY_DLL);
2806 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002807 }
2808 for (i = 0; winpty_entry[i].name != NULL
2809 && winpty_entry[i].ptr != NULL; ++i)
2810 {
2811 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2812 winpty_entry[i].name)) == NULL)
2813 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002814 if (verbose)
2815 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2816 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002817 }
2818 }
2819
Bram Moolenaara83e3962017-08-17 14:39:07 +02002820 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002821}
2822
2823/*
2824 * Create a new terminal of "rows" by "cols" cells.
2825 * Store a reference in "term".
2826 * Return OK or FAIL.
2827 */
2828 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002829term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002830{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002831 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002832 channel_T *channel = NULL;
2833 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002834 DWORD error;
2835 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2836 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002837 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002838 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002839 garray_T ga;
2840 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002841
Bram Moolenaara83e3962017-08-17 14:39:07 +02002842 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002843 return FAIL;
2844
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002845 if (argvar->v_type == VAR_STRING)
2846 cmd = argvar->vval.v_string;
2847 else
2848 {
2849 ga_init2(&ga, (int)sizeof(char*), 20);
2850 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2851 goto failed;
2852 cmd = ga.ga_data;
2853 }
2854
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002855 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002856 if (p == NULL)
2857 return FAIL;
2858
2859 job = job_alloc();
2860 if (job == NULL)
2861 goto failed;
2862
2863 channel = add_channel();
2864 if (channel == NULL)
2865 goto failed;
2866
2867 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2868 if (term->tl_winpty_config == NULL)
2869 goto failed;
2870
2871 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2872 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2873 if (term->tl_winpty == NULL)
2874 goto failed;
2875
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002876 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002877 spawn_config = winpty_spawn_config_new(
2878 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2879 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2880 NULL,
2881 p,
2882 NULL,
2883 NULL,
2884 &winpty_err);
2885 if (spawn_config == NULL)
2886 goto failed;
2887
2888 channel = add_channel();
2889 if (channel == NULL)
2890 goto failed;
2891
2892 job = job_alloc();
2893 if (job == NULL)
2894 goto failed;
2895
2896 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2897 &child_thread_handle, &error, &winpty_err))
2898 goto failed;
2899
2900 channel_set_pipes(channel,
2901 (sock_T) CreateFileW(
2902 winpty_conin_name(term->tl_winpty),
2903 GENERIC_WRITE, 0, NULL,
2904 OPEN_EXISTING, 0, NULL),
2905 (sock_T) CreateFileW(
2906 winpty_conout_name(term->tl_winpty),
2907 GENERIC_READ, 0, NULL,
2908 OPEN_EXISTING, 0, NULL),
2909 (sock_T) CreateFileW(
2910 winpty_conerr_name(term->tl_winpty),
2911 GENERIC_READ, 0, NULL,
2912 OPEN_EXISTING, 0, NULL));
2913
2914 jo = CreateJobObject(NULL, NULL);
2915 if (jo == NULL)
2916 goto failed;
2917
2918 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002919 {
2920 /* Failed, switch the way to terminate process with TerminateProcess. */
2921 CloseHandle(jo);
2922 jo = NULL;
2923 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002924
2925 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002926 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002927
2928 create_vterm(term, rows, cols);
2929
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002930 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002931 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002932
2933 job->jv_channel = channel;
2934 job->jv_proc_info.hProcess = child_process_handle;
2935 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2936 job->jv_job_object = jo;
2937 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002938 sprintf(buf, "winpty://%lu",
2939 GetProcessId(winpty_agent_process(term->tl_winpty)));
2940 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002941 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002942 term->tl_job = job;
2943
2944 return OK;
2945
2946failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002947 if (argvar->v_type == VAR_LIST)
2948 vim_free(ga.ga_data);
2949 if (p != NULL)
2950 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002951 if (spawn_config != NULL)
2952 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002953 if (channel != NULL)
2954 channel_clear(channel);
2955 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002956 {
2957 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002958 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002959 }
2960 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002961 if (jo != NULL)
2962 CloseHandle(jo);
2963 if (term->tl_winpty != NULL)
2964 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002965 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002966 if (term->tl_winpty_config != NULL)
2967 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002968 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002969 if (winpty_err != NULL)
2970 {
2971 char_u *msg = utf16_to_enc(
2972 (short_u *)winpty_error_msg(winpty_err), NULL);
2973
2974 EMSG(msg);
2975 winpty_error_free(winpty_err);
2976 }
2977 return FAIL;
2978}
2979
2980/*
2981 * Free the terminal emulator part of "term".
2982 */
2983 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002984term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002985{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002986 if (term->tl_winpty != NULL)
2987 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002988 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002989 if (term->tl_winpty_config != NULL)
2990 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002991 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002992 if (term->tl_vterm != NULL)
2993 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002994 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002995}
2996
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002997/*
2998 * Request size to terminal.
2999 */
3000 static void
3001term_report_winsize(term_T *term, int rows, int cols)
3002{
3003 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3004}
3005
Bram Moolenaara83e3962017-08-17 14:39:07 +02003006 int
3007terminal_enabled(void)
3008{
3009 return dyn_winpty_init(FALSE) == OK;
3010}
3011
3012
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003013# else
3014
3015/**************************************
3016 * 3. Unix-like implementation.
3017 */
3018
3019/*
3020 * Create a new terminal of "rows" by "cols" cells.
3021 * Start job for "cmd".
3022 * Store the pointers in "term".
3023 * Return OK or FAIL.
3024 */
3025 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003026term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003027{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003028 create_vterm(term, rows, cols);
3029
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003030 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003031 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003032 if (term->tl_job != NULL)
3033 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003034
Bram Moolenaar61a66052017-07-22 18:39:00 +02003035 return term->tl_job != NULL
3036 && term->tl_job->jv_channel != NULL
3037 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003038}
3039
3040/*
3041 * Free the terminal emulator part of "term".
3042 */
3043 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003044term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003045{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003046 if (term->tl_vterm != NULL)
3047 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003048 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003049}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003050
3051/*
3052 * Request size to terminal.
3053 */
3054 static void
3055term_report_winsize(term_T *term, int rows, int cols)
3056{
3057 /* Use an ioctl() to report the new window size to the job. */
3058 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3059 {
3060 int fd = -1;
3061 int part;
3062
3063 for (part = PART_OUT; part < PART_COUNT; ++part)
3064 {
3065 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3066 if (isatty(fd))
3067 break;
3068 }
3069 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003070 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003071 }
3072}
3073
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003074# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003075
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003076#endif /* FEAT_TERMINAL */