blob: faa74f7f47ca9f6b95f2226ddda3218f9e23dc4e [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 Moolenaar13ebb032017-08-26 22:02:51 +020041 * - ":term NONE" does not work in MS-Windows.
Bram Moolenaar0cbba822017-08-21 21:39:28 +020042 * - test for writing lines to terminal job does not work on MS-Windows
Bram Moolenaar94053a52017-08-01 21:44:33 +020043 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020044 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020045 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020046 * - support minimal size when 'termsize' is empty?
Bram Moolenaar285f2432017-08-23 23:10:21 +020047 * - GUI: when using tabs, focus in terminal, click on tab does not work.
48 * - GUI: when 'confirm' is set and trying to exit Vim, dialog offers to save
49 * changes to "!shell".
50 * (justrajdeep, 2017 Aug 22)
Bram Moolenaar58302322017-08-22 20:33:53 +020051 * - command argument with spaces doesn't work #1999
52 * :terminal ls dir\ with\ spaces
Bram Moolenaar4f44b882017-08-13 20:06:18 +020053 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020054 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
55 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
56 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
57 * Check that something is connected to the terminal.
58 * Test: "cat" reading from a file or buffer
Bram Moolenaar740c4332017-08-21 22:01:27 +020059 * "ls" writing stdout to a file or buffer
60 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020061 * - For the GUI fill termios with default values, perhaps like pangoterm:
62 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar423802d2017-07-30 16:52:24 +020063 * - if the job in the terminal does not support the mouse, we can use the
64 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
66 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020067 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020068 * - Copy text in the vterm to the Vim buffer once in a while, so that
69 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020070 */
71
72#include "vim.h"
73
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020074#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020075
Bram Moolenaard5310982017-08-05 15:16:32 +020076#ifndef MIN
77# define MIN(x,y) ((x) < (y) ? (x) : (y))
78#endif
79#ifndef MAX
80# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020081#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020083#include "libvterm/include/vterm.h"
84
Bram Moolenaar33a43be2017-08-06 21:36:22 +020085/* This is VTermScreenCell without the characters, thus much smaller. */
86typedef struct {
87 VTermScreenCellAttrs attrs;
88 char width;
89 VTermColor fg, bg;
90} cellattr_T;
91
Bram Moolenaard85f2712017-07-28 21:51:57 +020092typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020093 int sb_cols; /* can differ per line */
94 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020095} sb_line_T;
96
Bram Moolenaare4f25e42017-07-07 11:54:15 +020097/* typedef term_T in structs.h */
98struct terminal_S {
99 term_T *tl_next;
100
Bram Moolenaar423802d2017-07-30 16:52:24 +0200101 VTerm *tl_vterm;
102 job_T *tl_job;
103 buf_T *tl_buffer;
104
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200105 /* used when tl_job is NULL and only a pty was created */
106 int tl_tty_fd;
107 char_u *tl_tty_name;
108
Bram Moolenaar6d819742017-08-06 14:57:49 +0200109 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200110 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200111 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200112 char_u *tl_opencmd;
Bram Moolenaardada6d22017-09-02 17:18:35 +0200113 char_u *tl_eof_chars;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200114
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200115#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200116 void *tl_winpty_config;
117 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200118#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200119
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200120 /* last known vterm size */
121 int tl_rows;
122 int tl_cols;
123 /* vterm size does not follow window size */
124 int tl_rows_fixed;
125 int tl_cols_fixed;
126
Bram Moolenaar21554412017-07-24 21:44:43 +0200127 char_u *tl_title; /* NULL or allocated */
128 char_u *tl_status_text; /* NULL or allocated */
129
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200130 /* Range of screen rows to update. Zero based. */
131 int tl_dirty_row_start; /* -1 if nothing dirty */
132 int tl_dirty_row_end; /* row below last one to update */
133
Bram Moolenaard85f2712017-07-28 21:51:57 +0200134 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200135 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200136
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200137 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200138 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200139 int tl_cursor_blink;
140 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
141 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200142
143 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200144};
145
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200146#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
147#define TMODE_LOOP 2 /* CTRL-W N used */
148
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200149/*
150 * List of all active terminals.
151 */
152static term_T *first_term = NULL;
153
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200154/* Terminal active in terminal_loop(). */
155static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200156
157#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
158#define KEY_BUF_LEN 200
159
160/*
161 * Functions with separate implementation for MS-Windows and Unix-like systems.
162 */
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200163static int term_and_job_init(term_T *term, typval_T *argvar, jobopt_T *opt);
164static int create_pty_only(term_T *term, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200165static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200166static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200167
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200168/* The characters that we know (or assume) that the terminal expects for the
169 * backspace and enter keys. */
170static int term_backspace_char = BS;
171static int term_enter_char = CAR;
172static int term_nl_does_cr = FALSE;
173
174
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200175/**************************************
176 * 1. Generic code for all systems.
177 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200178
179/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200180 * Determine the terminal size from 'termsize' and the current window.
181 * Assumes term->tl_rows and term->tl_cols are zero.
182 */
183 static void
184set_term_and_win_size(term_T *term)
185{
186 if (*curwin->w_p_tms != NUL)
187 {
188 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
189
190 term->tl_rows = atoi((char *)curwin->w_p_tms);
191 term->tl_cols = atoi((char *)p);
192 }
193 if (term->tl_rows == 0)
194 term->tl_rows = curwin->w_height;
195 else
196 {
197 win_setheight_win(term->tl_rows, curwin);
198 term->tl_rows_fixed = TRUE;
199 }
200 if (term->tl_cols == 0)
201 term->tl_cols = curwin->w_width;
202 else
203 {
204 win_setwidth_win(term->tl_cols, curwin);
205 term->tl_cols_fixed = TRUE;
206 }
207}
208
209/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200210 * Initialize job options for a terminal job.
211 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200212 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200213 static void
214init_job_options(jobopt_T *opt)
215{
216 clear_job_options(opt);
217
218 opt->jo_mode = MODE_RAW;
219 opt->jo_out_mode = MODE_RAW;
220 opt->jo_err_mode = MODE_RAW;
221 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
222
223 opt->jo_io[PART_OUT] = JIO_BUFFER;
224 opt->jo_io[PART_ERR] = JIO_BUFFER;
225 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
226
227 opt->jo_modifiable[PART_OUT] = 0;
228 opt->jo_modifiable[PART_ERR] = 0;
229 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
230
231 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
232}
233
234/*
235 * Set job options mandatory for a terminal job.
236 */
237 static void
238setup_job_options(jobopt_T *opt, int rows, int cols)
239{
240 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
241 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
242 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200243 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
244 opt->jo_term_rows = rows;
245 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
246 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200247}
248
249 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200250term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252 exarg_T split_ea;
253 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200255 buf_T *old_curbuf = NULL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200256 int res;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200257
258 if (check_restricted() || check_secure())
259 return;
260
261 term = (term_T *)alloc_clear(sizeof(term_T));
262 if (term == NULL)
263 return;
264 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200265 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200266 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200267 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200268 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200269
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200270 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200271 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200272 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200273 /* Create a new buffer in the current window. */
274 if (!can_abandon(curbuf, forceit))
275 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200276 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200277 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200278 return;
279 }
280 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
281 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200282 {
283 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200284 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200285 }
286 }
287 else if (opt->jo_hidden)
288 {
289 buf_T *buf;
290
291 /* Create a new buffer without a window. Make it the current buffer for
292 * a moment to be able to do the initialisations. */
293 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
294 BLN_NEW | BLN_LISTED);
295 if (buf == NULL || ml_open(buf) == FAIL)
296 {
297 vim_free(term);
298 return;
299 }
300 old_curbuf = curbuf;
301 --curbuf->b_nwindows;
302 curbuf = buf;
303 curwin->w_buffer = buf;
304 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200305 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200306 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200307 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200308 /* Open a new window or tab. */
309 split_ea.cmdidx = CMD_new;
310 split_ea.cmd = (char_u *)"new";
311 split_ea.arg = (char_u *)"";
312 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
313 {
314 split_ea.line2 = opt->jo_term_rows;
315 split_ea.addr_count = 1;
316 }
317 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
318 {
319 split_ea.line2 = opt->jo_term_cols;
320 split_ea.addr_count = 1;
321 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200322
Bram Moolenaarda43b612017-08-11 22:27:50 +0200323 ex_splitview(&split_ea);
324 if (curwin == old_curwin)
325 {
326 /* split failed */
327 vim_free(term);
328 return;
329 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200330 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200331 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200332 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200333
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200334 if (!opt->jo_hidden)
335 {
336 /* only one size was taken care of with :new, do the other one */
337 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
338 win_setheight(opt->jo_term_rows);
339 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
340 win_setwidth(opt->jo_term_cols);
341 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200342
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200343 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200344 term->tl_next = first_term;
345 first_term = term;
346
Bram Moolenaar78712a72017-08-05 14:50:12 +0200347 if (opt->jo_term_name != NULL)
348 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
349 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200350 {
351 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200352 size_t len;
353 char_u *cmd, *p;
354
355 if (argvar->v_type == VAR_STRING)
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200356 {
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200357 cmd = argvar->vval.v_string;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200358 if (cmd == NULL)
359 cmd = (char_u *)"";
360 else if (STRCMP(cmd, "NONE") == 0)
361 cmd = (char_u *)"pty";
362 }
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200363 else if (argvar->v_type != VAR_LIST
364 || argvar->vval.v_list == NULL
365 || argvar->vval.v_list->lv_len < 1)
366 cmd = (char_u*)"";
367 else
368 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
369
370 len = STRLEN(cmd) + 10;
371 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200372
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200373 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200374 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200375 /* Prepend a ! to the command name to avoid the buffer name equals
376 * the executable, otherwise ":w!" would overwrite it. */
377 if (i == 0)
378 vim_snprintf((char *)p, len, "!%s", cmd);
379 else
380 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200381 if (buflist_findname(p) == NULL)
382 {
383 curbuf->b_ffname = p;
384 break;
385 }
386 }
387 }
388 curbuf->b_fname = curbuf->b_ffname;
389
Bram Moolenaar37c45832017-08-12 16:01:04 +0200390 if (opt->jo_term_opencmd != NULL)
391 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
392
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200393 if (opt->jo_eof_chars != NULL)
394 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200395
Bram Moolenaareb44a682017-08-03 22:44:55 +0200396 set_string_option_direct((char_u *)"buftype", -1,
397 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
398
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200399 /* Mark the buffer as not modifiable. It can only be made modifiable after
400 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200401 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200402
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200403 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200404 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200405
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200406 /* System dependent: setup the vterm and maybe start the job in it. */
407 if (argvar->v_type == VAR_STRING
408 && argvar->vval.v_string != NULL
409 && STRCMP(argvar->vval.v_string, "NONE") == 0)
410 res = create_pty_only(term, opt);
411 else
412 res = term_and_job_init(term, argvar, opt);
413
414 if (res == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200415 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200416 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200417 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200418 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200419
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200420 /* Make sure we don't get stuck on sending keys to the job, it leads to
421 * a deadlock if the job is waiting for Vim to read. */
422 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
423
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200424 if (old_curbuf != NULL)
425 {
426 --curbuf->b_nwindows;
427 curbuf = old_curbuf;
428 curwin->w_buffer = curbuf;
429 ++curbuf->b_nwindows;
430 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200431 }
432 else
433 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200434 buf_T *buf = curbuf;
435
Bram Moolenaard85f2712017-07-28 21:51:57 +0200436 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200437 if (old_curbuf != NULL)
438 {
439 --curbuf->b_nwindows;
440 curbuf = old_curbuf;
441 curwin->w_buffer = curbuf;
442 ++curbuf->b_nwindows;
443 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200444
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200445 /* Wiping out the buffer will also close the window and call
446 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200447 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200448 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200449}
450
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200451/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200452 * ":terminal": open a terminal window and execute a job in it.
453 */
454 void
455ex_terminal(exarg_T *eap)
456{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200457 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200458 jobopt_T opt;
459 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200460 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200461
462 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200463
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200464 cmd = eap->arg;
465 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
466 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200467 char_u *p, *ep;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200468
469 cmd += 2;
470 p = skiptowhite(cmd);
Bram Moolenaarb2412082017-08-20 18:09:14 +0200471 ep = vim_strchr(cmd, '=');
472 if (ep != NULL && ep < p)
473 p = ep;
474
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200475 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
476 opt.jo_term_finish = 'c';
477 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
478 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200479 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
480 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200481 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
482 opt.jo_hidden = 1;
Bram Moolenaarb2412082017-08-20 18:09:14 +0200483 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
484 && ep != NULL && isdigit(ep[1]))
485 {
486 opt.jo_set2 |= JO2_TERM_ROWS;
487 opt.jo_term_rows = atoi((char *)ep + 1);
488 p = skiptowhite(cmd);
489 }
490 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
491 && ep != NULL && isdigit(ep[1]))
492 {
493 opt.jo_set2 |= JO2_TERM_COLS;
494 opt.jo_term_cols = atoi((char *)ep + 1);
495 p = skiptowhite(cmd);
496 }
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200497 else if ((int)(p - cmd) == 3 && STRNICMP(cmd, "eof", 3) == 0
498 && ep != NULL)
499 {
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200500 char_u *buf = NULL;
501 char_u *keys;
502
503 p = skiptowhite(cmd);
504 *p = NUL;
505 keys = replace_termcodes(ep + 1, &buf, TRUE, TRUE, TRUE);
506 opt.jo_set2 |= JO2_EOF_CHARS;
507 opt.jo_eof_chars = vim_strsave(keys);
508 vim_free(buf);
509 *p = ' ';
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200510 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200511 else
512 {
513 if (*p)
514 *p = NUL;
515 EMSG2(_("E181: Invalid attribute: %s"), cmd);
516 return;
517 }
518 cmd = skipwhite(p);
519 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200520 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200521 /* Make a copy, an autocommand may set 'shell'. */
522 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200523
Bram Moolenaarb2412082017-08-20 18:09:14 +0200524 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200525 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200526 /* Write lines from current buffer to the job. */
527 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
528 opt.jo_io[PART_IN] = JIO_BUFFER;
529 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
530 opt.jo_in_top = eap->line1;
531 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200532 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200533
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200534 argvar.v_type = VAR_STRING;
535 argvar.vval.v_string = cmd;
536 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200537 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200538}
539
540/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200541 * Free the scrollback buffer for "term".
542 */
543 static void
544free_scrollback(term_T *term)
545{
546 int i;
547
548 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
549 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
550 ga_clear(&term->tl_scrollback);
551}
552
553/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200554 * Free a terminal and everything it refers to.
555 * Kills the job if there is one.
556 * Called when wiping out a buffer.
557 */
558 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200559free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200560{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200561 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200562 term_T *tp;
563
564 if (term == NULL)
565 return;
566 if (first_term == term)
567 first_term = term->tl_next;
568 else
569 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
570 if (tp->tl_next == term)
571 {
572 tp->tl_next = term->tl_next;
573 break;
574 }
575
576 if (term->tl_job != NULL)
577 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200578 if (term->tl_job->jv_status != JOB_ENDED
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200579 && term->tl_job->jv_status != JOB_FINISHED
580 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200581 job_stop(term->tl_job, NULL, "kill");
582 job_unref(term->tl_job);
583 }
584
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200585 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200586
587 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200588 vim_free(term->tl_title);
589 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200590 vim_free(term->tl_opencmd);
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200591 vim_free(term->tl_eof_chars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200592 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200593 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200594 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200595 if (in_terminal_loop == term)
596 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200597}
598
599/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200600 * Write job output "msg[len]" to the vterm.
601 */
602 static void
603term_write_job_output(term_T *term, char_u *msg, size_t len)
604{
605 VTerm *vterm = term->tl_vterm;
606 char_u *p;
607 size_t done;
608 size_t len_now;
609
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200610 if (term_nl_does_cr)
611 vterm_input_write(vterm, (char *)msg, len);
612 else
613 /* need to convert NL to CR-NL */
614 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200615 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200616 for (p = msg + done; p < msg + len; )
617 {
618 if (*p == NL)
619 break;
620 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
621 }
622 len_now = p - msg - done;
623 vterm_input_write(vterm, (char *)msg + done, len_now);
624 if (p < msg + len && *p == NL)
625 {
626 vterm_input_write(vterm, "\r\n", 2);
627 ++len_now;
628 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200629 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200630
631 /* this invokes the damage callbacks */
632 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
633}
634
Bram Moolenaar1c844932017-07-24 23:36:41 +0200635 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200636update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200637{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200638 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200639 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200640 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200641 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200642 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200643 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200644 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200645 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200646#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200647 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200648 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200649#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200650 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200651}
652
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200653/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200654 * Invoked when "msg" output from a job was received. Write it to the terminal
655 * of "buffer".
656 */
657 void
658write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
659{
660 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200661 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200662
Bram Moolenaard85f2712017-07-28 21:51:57 +0200663 if (term->tl_vterm == NULL)
664 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200665 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200666 return;
667 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200668 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200669 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200670
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200671 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
672 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200673 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200674 {
675 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200676 ch_log(term->tl_job->jv_channel, "updating screen");
677 if (buffer == curbuf)
678 {
679 update_screen(0);
680 update_cursor(term, TRUE);
681 }
682 else
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200683 redraw_after_callback(TRUE);
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200684 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200685}
686
687/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200688 * Send a mouse position and click to the vterm
689 */
690 static int
691term_send_mouse(VTerm *vterm, int button, int pressed)
692{
693 VTermModifier mod = VTERM_MOD_NONE;
694
695 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
696 mouse_col - W_WINCOL(curwin), mod);
697 vterm_mouse_button(vterm, button, pressed, mod);
698 return TRUE;
699}
700
701/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200702 * Convert typed key "c" into bytes to send to the job.
703 * Return the number of bytes in "buf".
704 */
705 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200706term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200707{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200708 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200709 VTermKey key = VTERM_KEY_NONE;
710 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200711 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200712
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200713 switch (c)
714 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200715 case CAR: c = term_enter_char; break;
716 /* don't use VTERM_KEY_BACKSPACE, it always
717 * becomes 0x7f DEL */
718 case K_BS: c = term_backspace_char; break;
719
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200720 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200721 case K_DEL: key = VTERM_KEY_DEL; break;
722 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200723 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
724 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200725 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200726 case K_S_END: mod = VTERM_MOD_SHIFT;
727 key = VTERM_KEY_END; break;
728 case K_C_END: mod = VTERM_MOD_CTRL;
729 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200730 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
731 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
732 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
733 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
734 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
735 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
736 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
737 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
738 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
739 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
740 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
741 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
742 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200743 case K_S_HOME: mod = VTERM_MOD_SHIFT;
744 key = VTERM_KEY_HOME; break;
745 case K_C_HOME: mod = VTERM_MOD_CTRL;
746 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200747 case K_INS: key = VTERM_KEY_INS; break;
748 case K_K0: key = VTERM_KEY_KP_0; break;
749 case K_K1: key = VTERM_KEY_KP_1; break;
750 case K_K2: key = VTERM_KEY_KP_2; break;
751 case K_K3: key = VTERM_KEY_KP_3; break;
752 case K_K4: key = VTERM_KEY_KP_4; break;
753 case K_K5: key = VTERM_KEY_KP_5; break;
754 case K_K6: key = VTERM_KEY_KP_6; break;
755 case K_K7: key = VTERM_KEY_KP_7; break;
756 case K_K8: key = VTERM_KEY_KP_8; break;
757 case K_K9: key = VTERM_KEY_KP_9; break;
758 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
759 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
760 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
761 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
762 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
763 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
764 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
765 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
766 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
767 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
768 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
769 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
770 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200771 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
772 key = VTERM_KEY_LEFT; break;
773 case K_C_LEFT: mod = VTERM_MOD_CTRL;
774 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200775 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
776 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
777 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200778 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
779 key = VTERM_KEY_RIGHT; break;
780 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
781 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200782 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200783 case K_S_UP: mod = VTERM_MOD_SHIFT;
784 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200785 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200786
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200787 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
788 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
789 case K_MOUSELEFT: /* TODO */ return 0;
790 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200791
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200792 case K_LEFTMOUSE:
793 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
794 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
795 case K_LEFTRELEASE:
796 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
797 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
798 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
799 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
800 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
801 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
802 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
803 case K_X1MOUSE: /* TODO */ return 0;
804 case K_X1DRAG: /* TODO */ return 0;
805 case K_X1RELEASE: /* TODO */ return 0;
806 case K_X2MOUSE: /* TODO */ return 0;
807 case K_X2DRAG: /* TODO */ return 0;
808 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200809
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200810 case K_IGNORE: return 0;
811 case K_NOP: return 0;
812 case K_UNDO: return 0;
813 case K_HELP: return 0;
814 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
815 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
816 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
817 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
818 case K_SELECT: return 0;
819#ifdef FEAT_GUI
820 case K_VER_SCROLLBAR: return 0;
821 case K_HOR_SCROLLBAR: return 0;
822#endif
823#ifdef FEAT_GUI_TABLINE
824 case K_TABLINE: return 0;
825 case K_TABMENU: return 0;
826#endif
827#ifdef FEAT_NETBEANS_INTG
828 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
829#endif
830#ifdef FEAT_DND
831 case K_DROP: return 0;
832#endif
833#ifdef FEAT_AUTOCMD
834 case K_CURSORHOLD: return 0;
835#endif
836 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
837 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200838 }
839
840 /*
841 * Convert special keys to vterm keys:
842 * - Write keys to vterm: vterm_keyboard_key()
843 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200844 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200845 */
846 if (key != VTERM_KEY_NONE)
847 /* Special key, let vterm convert it. */
848 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200849 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200850 /* Normal character, let vterm convert it. */
851 vterm_keyboard_unichar(vterm, c, mod);
852
853 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200854 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200855}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200856
Bram Moolenaar938783d2017-07-16 20:13:26 +0200857/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200858 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200859 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200860 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200861term_job_running(term_T *term)
862{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200863 /* Also consider the job finished when the channel is closed, to avoid a
864 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200865 return term != NULL
866 && term->tl_job != NULL
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200867 && channel_is_open(term->tl_job->jv_channel)
868 && (term->tl_job->jv_status == JOB_STARTED
869 || term->tl_job->jv_channel->ch_keep_open);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200870}
871
872/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200873 * Add the last line of the scrollback buffer to the buffer in the window.
874 */
875 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200876add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200877{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200878 buf_T *buf = term->tl_buffer;
879 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
880 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200881
Bram Moolenaar58302322017-08-22 20:33:53 +0200882#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +0200883 if (!enc_utf8 && enc_codepage > 0)
884 {
885 WCHAR *ret = NULL;
886 int length = 0;
887
888 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
889 &ret, &length);
890 if (ret != NULL)
891 {
892 WideCharToMultiByte_alloc(enc_codepage, 0,
893 ret, length, (char **)&text, &len, 0, 0);
894 vim_free(ret);
895 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
896 vim_free(text);
897 }
898 }
899 else
900#endif
901 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200902 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200903 {
904 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200905 curbuf = buf;
906 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200907 curbuf = curwin->w_buffer;
908 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200909}
910
911/*
912 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200913 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200914 */
915 static void
916move_terminal_to_buffer(term_T *term)
917{
918 win_T *wp;
919 int len;
920 int lines_skipped = 0;
921 VTermPos pos;
922 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200923 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200924 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200925
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200926 if (term->tl_vterm == NULL)
927 return;
928 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200929 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
930 {
931 len = 0;
932 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
933 if (vterm_screen_get_cell(screen, pos, &cell) != 0
934 && cell.chars[0] != NUL)
935 len = pos.col + 1;
936
937 if (len == 0)
938 ++lines_skipped;
939 else
940 {
941 while (lines_skipped > 0)
942 {
943 /* Line was skipped, add an empty line. */
944 --lines_skipped;
945 if (ga_grow(&term->tl_scrollback, 1) == OK)
946 {
947 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
948 + term->tl_scrollback.ga_len;
949
950 line->sb_cols = 0;
951 line->sb_cells = NULL;
952 ++term->tl_scrollback.ga_len;
953
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200954 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200955 }
956 }
957
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200958 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200959 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
960 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200961 garray_T ga;
962 int width;
963 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200964 + term->tl_scrollback.ga_len;
965
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200966 ga_init2(&ga, 1, 100);
967 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200968 {
969 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200970 {
971 width = 1;
972 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
973 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +0200974 ga.ga_len += utf_char2bytes(' ',
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200975 (char_u *)ga.ga_data + ga.ga_len);
976 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200977 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200978 {
979 width = cell.width;
980
981 p[pos.col].width = cell.width;
982 p[pos.col].attrs = cell.attrs;
983 p[pos.col].fg = cell.fg;
984 p[pos.col].bg = cell.bg;
985
986 if (ga_grow(&ga, MB_MAXBYTES) == OK)
987 {
988 int i;
989 int c;
990
991 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +0200992 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200993 (char_u *)ga.ga_data + ga.ga_len);
994 }
995 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200996 }
997 line->sb_cols = len;
998 line->sb_cells = p;
999 ++term->tl_scrollback.ga_len;
1000
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001001 if (ga_grow(&ga, 1) == FAIL)
1002 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1003 else
1004 {
1005 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1006 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1007 }
1008 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001009 }
1010 else
1011 vim_free(p);
1012 }
1013 }
1014
1015 FOR_ALL_WINDOWS(wp)
1016 {
1017 if (wp->w_buffer == term->tl_buffer)
1018 {
1019 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1020 wp->w_cursor.col = 0;
1021 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +02001022 if (wp->w_cursor.lnum >= wp->w_height)
1023 {
1024 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
1025
1026 if (wp->w_topline < min_topline)
1027 wp->w_topline = min_topline;
1028 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02001029 redraw_win_later(wp, NOT_VALID);
1030 }
1031 }
1032}
1033
1034 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001035set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001036{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001037 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001038 vim_free(term->tl_status_text);
1039 term->tl_status_text = NULL;
1040 if (term->tl_buffer == curbuf)
1041 maketitle();
1042}
1043
1044/*
1045 * Called after the job if finished and Terminal mode is not active:
1046 * Move the vterm contents into the scrollback buffer and free the vterm.
1047 */
1048 static void
1049cleanup_vterm(term_T *term)
1050{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001051 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001052 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001053 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001054 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001055}
1056
1057/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001058 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001059 * Suspends updating the terminal window.
1060 */
1061 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001062term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001063{
1064 term_T *term = curbuf->b_term;
1065
1066 /* Append the current terminal contents to the buffer. */
1067 move_terminal_to_buffer(term);
1068
Bram Moolenaar6d819742017-08-06 14:57:49 +02001069 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001070
Bram Moolenaar6d819742017-08-06 14:57:49 +02001071 /* Move the window cursor to the position of the cursor in the
1072 * terminal. */
1073 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1074 + term->tl_cursor_pos.row + 1;
1075 check_cursor();
1076 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001077
Bram Moolenaar6d819742017-08-06 14:57:49 +02001078 /* Display the same lines as in the terminal. */
1079 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001080}
1081
1082/*
1083 * Returns TRUE if the current window contains a terminal and we are in
1084 * Terminal-Normal mode.
1085 */
1086 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001087term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001088{
1089 term_T *term = curbuf->b_term;
1090
Bram Moolenaar6d819742017-08-06 14:57:49 +02001091 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001092}
1093
1094/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001095 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001096 * Restores updating the terminal window.
1097 */
1098 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001099term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001100{
1101 term_T *term = curbuf->b_term;
1102 sb_line_T *line;
1103 garray_T *gap;
1104
1105 /* Remove the terminal contents from the scrollback and the buffer. */
1106 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001107 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1108 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001109 {
1110 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1111 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1112 vim_free(line->sb_cells);
1113 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001114 }
1115 check_cursor();
1116
Bram Moolenaar6d819742017-08-06 14:57:49 +02001117 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001118
1119 if (term->tl_channel_closed)
1120 cleanup_vterm(term);
1121 redraw_buf_and_status_later(curbuf, NOT_VALID);
1122}
1123
1124/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001125 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001126 * Note: while waiting a terminal may be closed and freed if the channel is
1127 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001128 * TODO: use terminal mode mappings.
1129 */
1130 static int
1131term_vgetc()
1132{
1133 int c;
1134
1135 ++no_mapping;
1136 ++allow_keys;
1137 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001138#ifdef WIN3264
1139 ctrl_break_was_pressed = FALSE;
1140#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001141 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001142 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001143 --no_mapping;
1144 --allow_keys;
1145 return c;
1146}
1147
1148/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001149 * Get the part that is connected to the tty. Normally this is PART_IN, but
1150 * when writing buffer lines to the job it can be another. This makes it
1151 * possible to do "1,5term vim -".
1152 */
1153 static ch_part_T
1154get_tty_part(term_T *term)
1155{
1156#ifdef UNIX
1157 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1158 int i;
1159
1160 for (i = 0; i < 3; ++i)
1161 {
1162 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1163
1164 if (isatty(fd))
1165 return parts[i];
1166 }
1167#endif
1168 return PART_IN;
1169}
1170
1171/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001172 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001173 * Return FAIL when the key needs to be handled in Normal mode.
1174 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001175 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001176 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001177send_keys_to_term(term_T *term, int c, int typed)
1178{
1179 char msg[KEY_BUF_LEN];
1180 size_t len;
1181 static int mouse_was_outside = FALSE;
1182 int dragging_outside = FALSE;
1183
1184 /* Catch keys that need to be handled as in Normal mode. */
1185 switch (c)
1186 {
1187 case NUL:
1188 case K_ZERO:
1189 if (typed)
1190 stuffcharReadbuff(c);
1191 return FAIL;
1192
1193 case K_IGNORE:
1194 return FAIL;
1195
1196 case K_LEFTDRAG:
1197 case K_MIDDLEDRAG:
1198 case K_RIGHTDRAG:
1199 case K_X1DRAG:
1200 case K_X2DRAG:
1201 dragging_outside = mouse_was_outside;
1202 /* FALLTHROUGH */
1203 case K_LEFTMOUSE:
1204 case K_LEFTMOUSE_NM:
1205 case K_LEFTRELEASE:
1206 case K_LEFTRELEASE_NM:
1207 case K_MIDDLEMOUSE:
1208 case K_MIDDLERELEASE:
1209 case K_RIGHTMOUSE:
1210 case K_RIGHTRELEASE:
1211 case K_X1MOUSE:
1212 case K_X1RELEASE:
1213 case K_X2MOUSE:
1214 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001215
1216 case K_MOUSEUP:
1217 case K_MOUSEDOWN:
1218 case K_MOUSELEFT:
1219 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001220 if (mouse_row < W_WINROW(curwin)
1221 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1222 || mouse_col < W_WINCOL(curwin)
1223 || mouse_col >= W_ENDCOL(curwin)
1224 || dragging_outside)
1225 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001226 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001227 if (typed)
1228 {
1229 stuffcharReadbuff(c);
1230 mouse_was_outside = TRUE;
1231 }
1232 return FAIL;
1233 }
1234 }
1235 if (typed)
1236 mouse_was_outside = FALSE;
1237
1238 /* Convert the typed key to a sequence of bytes for the job. */
1239 len = term_convert_key(term, c, msg);
1240 if (len > 0)
1241 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001242 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1243 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001244
1245 return OK;
1246}
1247
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001248 static void
1249position_cursor(win_T *wp, VTermPos *pos)
1250{
1251 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1252 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1253 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1254}
1255
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001256/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001257 * Handle CTRL-W "": send register contents to the job.
1258 */
1259 static void
1260term_paste_register(int prev_c UNUSED)
1261{
1262 int c;
1263 list_T *l;
1264 listitem_T *item;
1265 long reglen = 0;
1266 int type;
1267
1268#ifdef FEAT_CMDL_INFO
1269 if (add_to_showcmd(prev_c))
1270 if (add_to_showcmd('"'))
1271 out_flush();
1272#endif
1273 c = term_vgetc();
1274#ifdef FEAT_CMDL_INFO
1275 clear_showcmd();
1276#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001277 if (!term_use_loop())
1278 /* job finished while waiting for a character */
1279 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001280
1281 /* CTRL-W "= prompt for expression to evaluate. */
1282 if (c == '=' && get_expr_register() != '=')
1283 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001284 if (!term_use_loop())
1285 /* job finished while waiting for a character */
1286 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001287
1288 l = (list_T *)get_reg_contents(c, GREG_LIST);
1289 if (l != NULL)
1290 {
1291 type = get_reg_type(c, &reglen);
1292 for (item = l->lv_first; item != NULL; item = item->li_next)
1293 {
1294 char_u *s = get_tv_string(&item->li_tv);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001295#ifdef WIN3264
1296 char_u *tmp = s;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001297
Bram Moolenaar285f2432017-08-23 23:10:21 +02001298 if (!enc_utf8 && enc_codepage > 0)
1299 {
1300 WCHAR *ret = NULL;
1301 int length = 0;
1302
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001303 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
1304 (int)STRLEN(s), &ret, &length);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001305 if (ret != NULL)
1306 {
1307 WideCharToMultiByte_alloc(CP_UTF8, 0,
1308 ret, length, (char **)&s, &length, 0, 0);
1309 vim_free(ret);
1310 }
1311 }
1312#endif
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001313 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001314 s, (int)STRLEN(s), NULL);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001315#ifdef WIN3264
1316 if (tmp != s)
1317 vim_free(s);
1318#endif
1319
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001320 if (item->li_next != NULL || type == MLINE)
1321 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1322 (char_u *)"\r", 1, NULL);
1323 }
1324 list_free(l);
1325 }
1326}
1327
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001328#if defined(FEAT_GUI) || defined(PROTO)
1329/*
1330 * Return TRUE when the cursor of the terminal should be displayed.
1331 */
1332 int
1333use_terminal_cursor()
1334{
1335 return in_terminal_loop != NULL;
1336}
1337
1338 cursorentry_T *
1339term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1340{
1341 term_T *term = in_terminal_loop;
1342 static cursorentry_T entry;
1343
1344 vim_memset(&entry, 0, sizeof(entry));
1345 entry.shape = entry.mshape =
1346 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1347 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1348 SHAPE_BLOCK;
1349 entry.percentage = 20;
1350 if (term->tl_cursor_blink)
1351 {
1352 entry.blinkwait = 700;
1353 entry.blinkon = 400;
Bram Moolenaar58302322017-08-22 20:33:53 +02001354 entry.blinkoff = 250;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001355 }
1356 *fg = gui.back_pixel;
1357 if (term->tl_cursor_color == NULL)
1358 *bg = gui.norm_pixel;
1359 else
1360 *bg = color_name2handle(term->tl_cursor_color);
1361 entry.name = "n";
1362 entry.used_for = SHAPE_CURSOR;
1363
1364 return &entry;
1365}
1366#endif
1367
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001368static int did_change_cursor = FALSE;
1369
1370 static void
1371may_set_cursor_props(term_T *term)
1372{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001373#ifdef FEAT_GUI
1374 /* For the GUI the cursor properties are obtained with
1375 * term_get_cursor_shape(). */
1376 if (gui.in_use)
1377 return;
1378#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001379 if (in_terminal_loop == term)
1380 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001381 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001382 if (term->tl_cursor_color != NULL)
1383 term_cursor_color(term->tl_cursor_color);
1384 else
1385 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001386 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1387 }
1388}
1389
1390 static void
1391may_restore_cursor_props(void)
1392{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001393#ifdef FEAT_GUI
1394 if (gui.in_use)
1395 return;
1396#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001397 if (did_change_cursor)
1398 {
1399 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001400 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001401 /* this will restore the initial cursor style, if possible */
1402 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001403 }
1404}
1405
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001406/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001407 * Returns TRUE if the current window contains a terminal and we are sending
1408 * keys to the job.
1409 */
1410 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001411term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001412{
1413 term_T *term = curbuf->b_term;
1414
1415 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001416 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001417 && term->tl_vterm != NULL
1418 && term_job_running(term);
1419}
1420
1421/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001422 * Wait for input and send it to the job.
1423 * Return when the start of a CTRL-W command is typed or anything else that
1424 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001425 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1426 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001427 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001428 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001429terminal_loop(void)
1430{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001431 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001432 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001433 int ret;
1434
Bram Moolenaar679653e2017-08-13 14:13:19 +02001435 /* Remember the terminal we are sending keys to. However, the terminal
1436 * might be closed while waiting for a character, e.g. typing "exit" in a
1437 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1438 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001439 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001440
1441 if (*curwin->w_p_tk != NUL)
1442 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001443 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001444 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001445
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001446#ifdef UNIX
1447 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001448 int part = get_tty_part(curbuf->b_term);
1449 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001450
1451 if (isatty(fd))
1452 {
1453 ttyinfo_T info;
1454
1455 /* Get the current backspace and enter characters of the pty. */
1456 if (get_tty_info(fd, &info) == OK)
1457 {
1458 term_backspace_char = info.backspace;
1459 term_enter_char = info.enter;
1460 term_nl_does_cr = info.nl_does_cr;
1461 }
1462 }
1463 }
1464#endif
1465
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001466 for (;;)
1467 {
1468 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001469 /* Repeat redrawing in case a message is received while redrawing. */
1470 while (curwin->w_redr_type != 0)
1471 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001472 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001473
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001474 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001475 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001476 /* job finished while waiting for a character */
1477 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001478 if (c == K_IGNORE)
1479 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001480
Bram Moolenaarfae42832017-08-01 22:24:26 +02001481#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001482 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001483 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001484 if (ctrl_break_was_pressed)
1485 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001486#endif
1487
Bram Moolenaar69198192017-08-05 14:10:48 +02001488 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001489 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001490 int prev_c = c;
1491
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001492#ifdef FEAT_CMDL_INFO
1493 if (add_to_showcmd(c))
1494 out_flush();
1495#endif
1496 c = term_vgetc();
1497#ifdef FEAT_CMDL_INFO
1498 clear_showcmd();
1499#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001500 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001501 /* job finished while waiting for a character */
1502 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001503
Bram Moolenaar69198192017-08-05 14:10:48 +02001504 if (prev_c == Ctrl_BSL)
1505 {
1506 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001507 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001508 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1509 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001510 ret = FAIL;
1511 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001512 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001513 /* Send both keys to the terminal. */
1514 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1515 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001516 else if (c == Ctrl_C)
1517 {
1518 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1519 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1520 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001521 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001522 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001523 /* "CTRL-W .": send CTRL-W to the job */
1524 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001525 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001526 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001527 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001528 /* CTRL-W N : go to Terminal-Normal mode. */
1529 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001530 ret = FAIL;
1531 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001532 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001533 else if (c == '"')
1534 {
1535 term_paste_register(prev_c);
1536 continue;
1537 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001538 else if (termkey == 0 || c != termkey)
1539 {
1540 stuffcharReadbuff(Ctrl_W);
1541 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001542 ret = OK;
1543 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001544 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001545 }
Bram Moolenaar58302322017-08-22 20:33:53 +02001546# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02001547 if (!enc_utf8 && has_mbyte && c >= 0x80)
1548 {
1549 WCHAR wc;
1550 char_u mb[3];
1551
1552 mb[0] = (unsigned)c >> 8;
1553 mb[1] = c;
1554 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1555 c = wc;
1556 }
1557# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001558 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001559 {
1560 ret = OK;
1561 goto theend;
1562 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001563 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001564 ret = FAIL;
1565
1566theend:
1567 in_terminal_loop = NULL;
1568 may_restore_cursor_props();
1569 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001570}
1571
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001572/*
1573 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001574 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001575 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001576 */
1577 void
1578term_job_ended(job_T *job)
1579{
1580 term_T *term;
1581 int did_one = FALSE;
1582
1583 for (term = first_term; term != NULL; term = term->tl_next)
1584 if (term->tl_job == job)
1585 {
1586 vim_free(term->tl_title);
1587 term->tl_title = NULL;
1588 vim_free(term->tl_status_text);
1589 term->tl_status_text = NULL;
1590 redraw_buf_and_status_later(term->tl_buffer, VALID);
1591 did_one = TRUE;
1592 }
1593 if (did_one)
1594 redraw_statuslines();
1595 if (curbuf->b_term != NULL)
1596 {
1597 if (curbuf->b_term->tl_job == job)
1598 maketitle();
1599 update_cursor(curbuf->b_term, TRUE);
1600 }
1601}
1602
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001603 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001604may_toggle_cursor(term_T *term)
1605{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001606 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001607 {
1608 if (term->tl_cursor_visible)
1609 cursor_on();
1610 else
1611 cursor_off();
1612 }
1613}
1614
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001615/*
1616 * Reverse engineer the RGB value into a cterm color index.
1617 * First color is 1. Return 0 if no match found.
1618 */
1619 static int
1620color2index(VTermColor *color, int fg, int *boldp)
1621{
1622 int red = color->red;
1623 int blue = color->blue;
1624 int green = color->green;
1625
1626 /* The argument for lookup_color() is for the color_names[] table. */
1627 if (red == 0)
1628 {
1629 if (green == 0)
1630 {
1631 if (blue == 0)
1632 return lookup_color(0, fg, boldp) + 1; /* black */
1633 if (blue == 224)
1634 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1635 }
1636 else if (green == 224)
1637 {
1638 if (blue == 0)
1639 return lookup_color(2, fg, boldp) + 1; /* dark green */
1640 if (blue == 224)
1641 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1642 }
1643 }
1644 else if (red == 224)
1645 {
1646 if (green == 0)
1647 {
1648 if (blue == 0)
1649 return lookup_color(4, fg, boldp) + 1; /* dark red */
1650 if (blue == 224)
1651 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1652 }
1653 else if (green == 224)
1654 {
1655 if (blue == 0)
1656 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1657 if (blue == 224)
1658 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1659 }
1660 }
1661 else if (red == 128)
1662 {
1663 if (green == 128 && blue == 128)
1664 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1665 }
1666 else if (red == 255)
1667 {
1668 if (green == 64)
1669 {
1670 if (blue == 64)
1671 return lookup_color(20, fg, boldp) + 1; /* light red */
1672 if (blue == 255)
1673 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1674 }
1675 else if (green == 255)
1676 {
1677 if (blue == 64)
1678 return lookup_color(24, fg, boldp) + 1; /* yellow */
1679 if (blue == 255)
1680 return lookup_color(26, fg, boldp) + 1; /* white */
1681 }
1682 }
1683 else if (red == 64)
1684 {
1685 if (green == 64)
1686 {
1687 if (blue == 255)
1688 return lookup_color(14, fg, boldp) + 1; /* light blue */
1689 }
1690 else if (green == 255)
1691 {
1692 if (blue == 64)
1693 return lookup_color(16, fg, boldp) + 1; /* light green */
1694 if (blue == 255)
1695 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1696 }
1697 }
1698 if (t_colors >= 256)
1699 {
1700 if (red == blue && red == green)
1701 {
1702 /* 24-color greyscale */
1703 static int cutoff[23] = {
1704 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1705 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1706 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1707 int i;
1708
1709 for (i = 0; i < 23; ++i)
1710 if (red < cutoff[i])
1711 return i + 233;
1712 return 256;
1713 }
1714
1715 /* 216-color cube */
1716 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001717 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001718 + (blue + 25) / 0x33;
1719 }
1720 return 0;
1721}
1722
1723/*
1724 * Convert the attributes of a vterm cell into an attribute index.
1725 */
1726 static int
1727cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1728{
1729 int attr = 0;
1730
1731 if (cellattrs.bold)
1732 attr |= HL_BOLD;
1733 if (cellattrs.underline)
1734 attr |= HL_UNDERLINE;
1735 if (cellattrs.italic)
1736 attr |= HL_ITALIC;
1737 if (cellattrs.strike)
1738 attr |= HL_STANDOUT;
1739 if (cellattrs.reverse)
1740 attr |= HL_INVERSE;
1741
1742#ifdef FEAT_GUI
1743 if (gui.in_use)
1744 {
1745 guicolor_T fg, bg;
1746
1747 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1748 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1749 return get_gui_attr_idx(attr, fg, bg);
1750 }
1751 else
1752#endif
1753#ifdef FEAT_TERMGUICOLORS
1754 if (p_tgc)
1755 {
1756 guicolor_T fg, bg;
1757
1758 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1759 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1760
1761 return get_tgc_attr_idx(attr, fg, bg);
1762 }
1763 else
1764#endif
1765 {
1766 int bold = MAYBE;
1767 int fg = color2index(&cellfg, TRUE, &bold);
1768 int bg = color2index(&cellbg, FALSE, &bold);
1769
1770 /* with 8 colors set the bold attribute to get a bright foreground */
1771 if (bold == TRUE)
1772 attr |= HL_BOLD;
1773 return get_cterm_attr_idx(attr, fg, bg);
1774 }
1775 return 0;
1776}
1777
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001778 static int
1779handle_damage(VTermRect rect, void *user)
1780{
1781 term_T *term = (term_T *)user;
1782
1783 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1784 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1785 redraw_buf_later(term->tl_buffer, NOT_VALID);
1786 return 1;
1787}
1788
1789 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001790handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001791{
1792 term_T *term = (term_T *)user;
1793
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001794 /* Scrolling up is done much more efficiently by deleting lines instead of
1795 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001796 if (dest.start_col == src.start_col
1797 && dest.end_col == src.end_col
1798 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001799 {
1800 win_T *wp;
1801 VTermColor fg, bg;
1802 VTermScreenCellAttrs attr;
1803 int clear_attr;
1804
1805 /* Set the color to clear lines with. */
1806 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1807 &fg, &bg);
1808 vim_memset(&attr, 0, sizeof(attr));
1809 clear_attr = cell2attr(attr, fg, bg);
1810
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001811 FOR_ALL_WINDOWS(wp)
1812 {
1813 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001814 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001815 src.start_row - dest.start_row, FALSE, FALSE,
1816 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001817 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001818 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001819 redraw_buf_later(term->tl_buffer, NOT_VALID);
1820 return 1;
1821}
1822
1823 static int
1824handle_movecursor(
1825 VTermPos pos,
1826 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001827 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001828 void *user)
1829{
1830 term_T *term = (term_T *)user;
1831 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001832
1833 term->tl_cursor_pos = pos;
1834 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001835
1836 FOR_ALL_WINDOWS(wp)
1837 {
1838 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001839 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001840 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001841 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001842 {
1843 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001844 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001845 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001846
1847 return 1;
1848}
1849
Bram Moolenaar21554412017-07-24 21:44:43 +02001850 static int
1851handle_settermprop(
1852 VTermProp prop,
1853 VTermValue *value,
1854 void *user)
1855{
1856 term_T *term = (term_T *)user;
1857
1858 switch (prop)
1859 {
1860 case VTERM_PROP_TITLE:
1861 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001862 /* a blank title isn't useful, make it empty, so that "running" is
1863 * displayed */
1864 if (*skipwhite((char_u *)value->string) == NUL)
1865 term->tl_title = NULL;
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001866#ifdef WIN3264
1867 else if (!enc_utf8 && enc_codepage > 0)
1868 {
1869 WCHAR *ret = NULL;
1870 int length = 0;
1871
1872 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001873 (char*)value->string, (int)STRLEN(value->string),
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001874 &ret, &length);
1875 if (ret != NULL)
1876 {
1877 WideCharToMultiByte_alloc(enc_codepage, 0,
1878 ret, length, (char**)&term->tl_title,
1879 &length, 0, 0);
1880 vim_free(ret);
1881 }
1882 }
1883#endif
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001884 else
1885 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001886 vim_free(term->tl_status_text);
1887 term->tl_status_text = NULL;
1888 if (term == curbuf->b_term)
1889 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001890 break;
1891
1892 case VTERM_PROP_CURSORVISIBLE:
1893 term->tl_cursor_visible = value->boolean;
1894 may_toggle_cursor(term);
1895 out_flush();
1896 break;
1897
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001898 case VTERM_PROP_CURSORBLINK:
1899 term->tl_cursor_blink = value->boolean;
1900 may_set_cursor_props(term);
1901 break;
1902
1903 case VTERM_PROP_CURSORSHAPE:
1904 term->tl_cursor_shape = value->number;
1905 may_set_cursor_props(term);
1906 break;
1907
1908 case VTERM_PROP_CURSORCOLOR:
1909 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001910 if (*value->string == NUL)
1911 term->tl_cursor_color = NULL;
1912 else
1913 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001914 may_set_cursor_props(term);
1915 break;
1916
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001917 case VTERM_PROP_ALTSCREEN:
1918 /* TODO: do anything else? */
1919 term->tl_using_altscreen = value->boolean;
1920 break;
1921
Bram Moolenaar21554412017-07-24 21:44:43 +02001922 default:
1923 break;
1924 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001925 /* Always return 1, otherwise vterm doesn't store the value internally. */
1926 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001927}
1928
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001929/*
1930 * The job running in the terminal resized the terminal.
1931 */
1932 static int
1933handle_resize(int rows, int cols, void *user)
1934{
1935 term_T *term = (term_T *)user;
1936 win_T *wp;
1937
1938 term->tl_rows = rows;
1939 term->tl_cols = cols;
1940 FOR_ALL_WINDOWS(wp)
1941 {
1942 if (wp->w_buffer == term->tl_buffer)
1943 {
1944 win_setheight_win(rows, wp);
1945 win_setwidth_win(cols, wp);
1946 }
1947 }
1948
1949 redraw_buf_later(term->tl_buffer, NOT_VALID);
1950 return 1;
1951}
1952
Bram Moolenaard85f2712017-07-28 21:51:57 +02001953/*
1954 * Handle a line that is pushed off the top of the screen.
1955 */
1956 static int
1957handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1958{
1959 term_T *term = (term_T *)user;
1960
1961 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001962 if (ga_grow(&term->tl_scrollback, 1) == OK)
1963 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001964 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001965 int len = 0;
1966 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001967 int c;
1968 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001969 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001970 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001971
1972 /* do not store empty cells at the end */
1973 for (i = 0; i < cols; ++i)
1974 if (cells[i].chars[0] != 0)
1975 len = i + 1;
1976
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001977 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001978 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001979 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001980 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001981 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001982 for (col = 0; col < len; col += cells[col].width)
1983 {
1984 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1985 {
1986 ga.ga_len = 0;
1987 break;
1988 }
1989 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +02001990 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001991 (char_u *)ga.ga_data + ga.ga_len);
1992 p[col].width = cells[col].width;
1993 p[col].attrs = cells[col].attrs;
1994 p[col].fg = cells[col].fg;
1995 p[col].bg = cells[col].bg;
1996 }
1997 }
1998 if (ga_grow(&ga, 1) == FAIL)
1999 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
2000 else
2001 {
2002 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
2003 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
2004 }
2005 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02002006
2007 line = (sb_line_T *)term->tl_scrollback.ga_data
2008 + term->tl_scrollback.ga_len;
2009 line->sb_cols = len;
2010 line->sb_cells = p;
2011 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002012 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002013 }
2014 return 0; /* ignored */
2015}
2016
Bram Moolenaar21554412017-07-24 21:44:43 +02002017static VTermScreenCallbacks screen_callbacks = {
2018 handle_damage, /* damage */
2019 handle_moverect, /* moverect */
2020 handle_movecursor, /* movecursor */
2021 handle_settermprop, /* settermprop */
2022 NULL, /* bell */
2023 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002024 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02002025 NULL /* sb_popline */
2026};
2027
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002028/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02002029 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002030 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02002031 */
2032 void
2033term_channel_closed(channel_T *ch)
2034{
2035 term_T *term;
2036 int did_one = FALSE;
2037
2038 for (term = first_term; term != NULL; term = term->tl_next)
2039 if (term->tl_job == ch->ch_job)
2040 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002041 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002042 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002043
Bram Moolenaard85f2712017-07-28 21:51:57 +02002044 vim_free(term->tl_title);
2045 term->tl_title = NULL;
2046 vim_free(term->tl_status_text);
2047 term->tl_status_text = NULL;
2048
Bram Moolenaar423802d2017-07-30 16:52:24 +02002049 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02002050 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002051 {
2052 int fnum = term->tl_buffer->b_fnum;
2053
Bram Moolenaar423802d2017-07-30 16:52:24 +02002054 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002055
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002056 if (term->tl_finish == 'c')
2057 {
2058 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002059 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002060 curbuf = term->tl_buffer;
2061 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
2062 break;
2063 }
2064 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
2065 {
2066 char buf[50];
2067
2068 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002069 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02002070 vim_snprintf(buf, sizeof(buf),
2071 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02002072 ? "botright sbuf %d"
2073 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002074 do_cmdline_cmd((char_u *)buf);
2075 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002076 else
2077 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002078 }
2079
Bram Moolenaard85f2712017-07-28 21:51:57 +02002080 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002081 }
2082 if (did_one)
2083 {
2084 redraw_statuslines();
2085
2086 /* Need to break out of vgetc(). */
2087 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002088 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002089
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002090 term = curbuf->b_term;
2091 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002092 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002093 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002094 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002095 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002096 }
2097 }
2098}
2099
2100/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002101 * Called to update a window that contains an active terminal.
2102 * Returns FAIL when there is no terminal running in this window or in
2103 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002104 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002105 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002106term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002107{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002108 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002109 VTerm *vterm;
2110 VTermScreen *screen;
2111 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002112 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002113
Bram Moolenaar6d819742017-08-06 14:57:49 +02002114 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002115 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002116
Bram Moolenaard85f2712017-07-28 21:51:57 +02002117 vterm = term->tl_vterm;
2118 screen = vterm_obtain_screen(vterm);
2119 state = vterm_obtain_state(vterm);
2120
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002121 /*
2122 * If the window was resized a redraw will be triggered and we get here.
2123 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2124 */
2125 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2126 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002127 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002128 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2129 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2130 win_T *twp;
2131
2132 FOR_ALL_WINDOWS(twp)
2133 {
2134 /* When more than one window shows the same terminal, use the
2135 * smallest size. */
2136 if (twp->w_buffer == term->tl_buffer)
2137 {
2138 if (!term->tl_rows_fixed && rows > twp->w_height)
2139 rows = twp->w_height;
2140 if (!term->tl_cols_fixed && cols > twp->w_width)
2141 cols = twp->w_width;
2142 }
2143 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002144
2145 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002146 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002147 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002148 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002149 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002150
2151 /* The cursor may have been moved when resizing. */
2152 vterm_state_get_cursorpos(state, &pos);
2153 position_cursor(wp, &pos);
2154
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002155 /* TODO: Only redraw what changed. */
2156 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002157 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002158 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002159 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002160
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002161 if (pos.row < term->tl_rows)
2162 {
2163 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002164 {
2165 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002166 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002167
Bram Moolenaareeac6772017-07-23 15:48:37 +02002168 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2169 vim_memset(&cell, 0, sizeof(cell));
2170
2171 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002172 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002173 if (c == NUL)
2174 {
2175 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002176 if (enc_utf8)
2177 ScreenLinesUC[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002178 }
2179 else
2180 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002181 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002182 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002183 if (c >= 0x80)
2184 {
2185 ScreenLines[off] = ' ';
2186 ScreenLinesUC[off] = c;
2187 }
2188 else
2189 {
2190 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002191 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002192 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002193 }
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002194#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02002195 else if (has_mbyte && c >= 0x80)
2196 {
2197 char_u mb[MB_MAXBYTES+1];
2198 WCHAR wc = c;
2199
2200 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2201 (char*)mb, 2, 0, 0) > 1)
2202 {
2203 ScreenLines[off] = mb[0];
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002204 ScreenLines[off + 1] = mb[1];
Bram Moolenaar740c4332017-08-21 22:01:27 +02002205 cell.width = mb_ptr2cells(mb);
2206 }
2207 else
2208 ScreenLines[off] = c;
2209 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002210#endif
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002211 else
Bram Moolenaar740c4332017-08-21 22:01:27 +02002212 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002213 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002214 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002215
2216 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002217 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002218 if (cell.width == 2)
2219 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002220 if (enc_utf8)
2221 ScreenLinesUC[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002222
2223 /* don't set the second byte to NUL for a DBCS encoding, it
2224 * has been set above */
2225 if (enc_utf8 || !has_mbyte)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002226 ScreenLines[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002227
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002228 ++pos.col;
2229 ++off;
2230 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002231 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002232 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002233 else
2234 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002235
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002236 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2237 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002238 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002239
2240 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002241}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002242
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002243/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002244 * Return TRUE if "wp" is a terminal window where the job has finished.
2245 */
2246 int
2247term_is_finished(buf_T *buf)
2248{
2249 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2250}
2251
2252/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002253 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002254 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002255 */
2256 int
2257term_show_buffer(buf_T *buf)
2258{
2259 term_T *term = buf->b_term;
2260
Bram Moolenaar6d819742017-08-06 14:57:49 +02002261 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002262}
2263
2264/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002265 * The current buffer is going to be changed. If there is terminal
2266 * highlighting remove it now.
2267 */
2268 void
2269term_change_in_curbuf(void)
2270{
2271 term_T *term = curbuf->b_term;
2272
2273 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2274 {
2275 free_scrollback(term);
2276 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002277
2278 /* The buffer is now like a normal buffer, it cannot be easily
2279 * abandoned when changed. */
2280 set_string_option_direct((char_u *)"buftype", -1,
2281 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002282 }
2283}
2284
2285/*
2286 * Get the screen attribute for a position in the buffer.
2287 */
2288 int
2289term_get_attr(buf_T *buf, linenr_T lnum, int col)
2290{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002291 term_T *term = buf->b_term;
2292 sb_line_T *line;
2293 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002294
Bram Moolenaar70229f92017-07-29 16:01:53 +02002295 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002296 return 0;
2297 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2298 if (col >= line->sb_cols)
2299 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002300 cellattr = line->sb_cells + col;
2301 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002302}
2303
2304/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002305 * Create a new vterm and initialize it.
2306 */
2307 static void
2308create_vterm(term_T *term, int rows, int cols)
2309{
2310 VTerm *vterm;
2311 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002312 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002313
2314 vterm = vterm_new(rows, cols);
2315 term->tl_vterm = vterm;
2316 screen = vterm_obtain_screen(vterm);
2317 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2318 /* TODO: depends on 'encoding'. */
2319 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002320
2321 /* Vterm uses a default black background. Set it to white when
2322 * 'background' is "light". */
2323 if (*p_bg == 'l')
2324 {
2325 VTermColor fg, bg;
2326
2327 fg.red = fg.green = fg.blue = 0;
2328 bg.red = bg.green = bg.blue = 255;
2329 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2330 }
2331
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002332 /* Required to initialize most things. */
2333 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002334
2335 /* Allow using alternate screen. */
2336 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002337
Bram Moolenaar58302322017-08-22 20:33:53 +02002338 /* For unix do not use a blinking cursor. In an xterm this causes the
2339 * cursor to blink if it's blinking in the xterm.
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002340 * For Windows we respect the system wide setting. */
Bram Moolenaar58302322017-08-22 20:33:53 +02002341#ifdef WIN3264
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002342 if (GetCaretBlinkTime() == INFINITE)
2343 value.boolean = 0;
2344 else
2345 value.boolean = 1;
Bram Moolenaar58302322017-08-22 20:33:53 +02002346#else
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002347 value.boolean = 0;
Bram Moolenaar58302322017-08-22 20:33:53 +02002348#endif
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002349 vterm_state_set_termprop(vterm_obtain_state(vterm),
2350 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002351}
2352
Bram Moolenaar21554412017-07-24 21:44:43 +02002353/*
2354 * Return the text to show for the buffer name and status.
2355 */
2356 char_u *
2357term_get_status_text(term_T *term)
2358{
2359 if (term->tl_status_text == NULL)
2360 {
2361 char_u *txt;
2362 size_t len;
2363
Bram Moolenaar6d819742017-08-06 14:57:49 +02002364 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002365 {
2366 if (term_job_running(term))
2367 txt = (char_u *)_("Terminal");
2368 else
2369 txt = (char_u *)_("Terminal-finished");
2370 }
2371 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002372 txt = term->tl_title;
2373 else if (term_job_running(term))
2374 txt = (char_u *)_("running");
2375 else
2376 txt = (char_u *)_("finished");
2377 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002378 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002379 if (term->tl_status_text != NULL)
2380 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2381 term->tl_buffer->b_fname, txt);
2382 }
2383 return term->tl_status_text;
2384}
2385
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002386/*
2387 * Mark references in jobs of terminals.
2388 */
2389 int
2390set_ref_in_term(int copyID)
2391{
2392 int abort = FALSE;
2393 term_T *term;
2394 typval_T tv;
2395
2396 for (term = first_term; term != NULL; term = term->tl_next)
2397 if (term->tl_job != NULL)
2398 {
2399 tv.v_type = VAR_JOB;
2400 tv.vval.v_job = term->tl_job;
2401 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2402 }
2403 return abort;
2404}
2405
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002406/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002407 * Get the buffer from the first argument in "argvars".
2408 * Returns NULL when the buffer is not for a terminal window.
2409 */
2410 static buf_T *
2411term_get_buf(typval_T *argvars)
2412{
2413 buf_T *buf;
2414
2415 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2416 ++emsg_off;
2417 buf = get_buf_tv(&argvars[0], FALSE);
2418 --emsg_off;
2419 if (buf == NULL || buf->b_term == NULL)
2420 return NULL;
2421 return buf;
2422}
2423
2424/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002425 * "term_getaltscreen(buf)" function
2426 */
2427 void
2428f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2429{
2430 buf_T *buf = term_get_buf(argvars);
2431
2432 if (buf == NULL)
2433 return;
2434 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2435}
2436
2437/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002438 * "term_getattr(attr, name)" function
2439 */
2440 void
2441f_term_getattr(typval_T *argvars, typval_T *rettv)
2442{
2443 int attr;
2444 size_t i;
2445 char_u *name;
2446
2447 static struct {
2448 char *name;
2449 int attr;
2450 } attrs[] = {
2451 {"bold", HL_BOLD},
2452 {"italic", HL_ITALIC},
2453 {"underline", HL_UNDERLINE},
2454 {"strike", HL_STANDOUT},
2455 {"reverse", HL_INVERSE},
2456 };
2457
2458 attr = get_tv_number(&argvars[0]);
2459 name = get_tv_string_chk(&argvars[1]);
2460 if (name == NULL)
2461 return;
2462
2463 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2464 if (STRCMP(name, attrs[i].name) == 0)
2465 {
2466 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2467 break;
2468 }
2469}
2470
2471/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002472 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002473 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002474 void
2475f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002476{
Bram Moolenaar97870002017-07-30 18:28:38 +02002477 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002478 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002479 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002480 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002481
Bram Moolenaar97870002017-07-30 18:28:38 +02002482 if (rettv_list_alloc(rettv) == FAIL)
2483 return;
2484 if (buf == NULL)
2485 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002486 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002487
2488 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002489 list_append_number(l, term->tl_cursor_pos.row + 1);
2490 list_append_number(l, term->tl_cursor_pos.col + 1);
2491
2492 d = dict_alloc();
2493 if (d != NULL)
2494 {
2495 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
Bram Moolenaar4db25542017-08-28 22:43:05 +02002496 dict_add_nr_str(d, "blink", blink_state_is_inverted()
2497 ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002498 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2499 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2500 ? (char_u *)"" : term->tl_cursor_color);
2501 list_append_dict(l, d);
2502 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002503}
2504
2505/*
2506 * "term_getjob(buf)" function
2507 */
2508 void
2509f_term_getjob(typval_T *argvars, typval_T *rettv)
2510{
2511 buf_T *buf = term_get_buf(argvars);
2512
2513 rettv->v_type = VAR_JOB;
2514 rettv->vval.v_job = NULL;
2515 if (buf == NULL)
2516 return;
2517
2518 rettv->vval.v_job = buf->b_term->tl_job;
2519 if (rettv->vval.v_job != NULL)
2520 ++rettv->vval.v_job->jv_refcount;
2521}
2522
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002523 static int
2524get_row_number(typval_T *tv, term_T *term)
2525{
2526 if (tv->v_type == VAR_STRING
2527 && tv->vval.v_string != NULL
2528 && STRCMP(tv->vval.v_string, ".") == 0)
2529 return term->tl_cursor_pos.row;
2530 return (int)get_tv_number(tv) - 1;
2531}
2532
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002533/*
2534 * "term_getline(buf, row)" function
2535 */
2536 void
2537f_term_getline(typval_T *argvars, typval_T *rettv)
2538{
2539 buf_T *buf = term_get_buf(argvars);
2540 term_T *term;
2541 int row;
2542
2543 rettv->v_type = VAR_STRING;
2544 if (buf == NULL)
2545 return;
2546 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002547 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002548
2549 if (term->tl_vterm == NULL)
2550 {
2551 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2552
2553 /* vterm is finished, get the text from the buffer */
2554 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2555 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2556 }
2557 else
2558 {
2559 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2560 VTermRect rect;
2561 int len;
2562 char_u *p;
2563
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002564 if (row < 0 || row >= term->tl_rows)
2565 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002566 len = term->tl_cols * MB_MAXBYTES + 1;
2567 p = alloc(len);
2568 if (p == NULL)
2569 return;
2570 rettv->vval.v_string = p;
2571
2572 rect.start_col = 0;
2573 rect.end_col = term->tl_cols;
2574 rect.start_row = row;
2575 rect.end_row = row + 1;
2576 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2577 }
2578}
2579
2580/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002581 * "term_getscrolled(buf)" function
2582 */
2583 void
2584f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2585{
2586 buf_T *buf = term_get_buf(argvars);
2587
2588 if (buf == NULL)
2589 return;
2590 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2591}
2592
2593/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002594 * "term_getsize(buf)" function
2595 */
2596 void
2597f_term_getsize(typval_T *argvars, typval_T *rettv)
2598{
2599 buf_T *buf = term_get_buf(argvars);
2600 list_T *l;
2601
2602 if (rettv_list_alloc(rettv) == FAIL)
2603 return;
2604 if (buf == NULL)
2605 return;
2606
2607 l = rettv->vval.v_list;
2608 list_append_number(l, buf->b_term->tl_rows);
2609 list_append_number(l, buf->b_term->tl_cols);
2610}
2611
2612/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002613 * "term_getstatus(buf)" function
2614 */
2615 void
2616f_term_getstatus(typval_T *argvars, typval_T *rettv)
2617{
2618 buf_T *buf = term_get_buf(argvars);
2619 term_T *term;
2620 char_u val[100];
2621
2622 rettv->v_type = VAR_STRING;
2623 if (buf == NULL)
2624 return;
2625 term = buf->b_term;
2626
2627 if (term_job_running(term))
2628 STRCPY(val, "running");
2629 else
2630 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002631 if (term->tl_normal_mode)
2632 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002633 rettv->vval.v_string = vim_strsave(val);
2634}
2635
2636/*
2637 * "term_gettitle(buf)" function
2638 */
2639 void
2640f_term_gettitle(typval_T *argvars, typval_T *rettv)
2641{
2642 buf_T *buf = term_get_buf(argvars);
2643
2644 rettv->v_type = VAR_STRING;
2645 if (buf == NULL)
2646 return;
2647
2648 if (buf->b_term->tl_title != NULL)
2649 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2650}
2651
2652/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002653 * "term_gettty(buf)" function
2654 */
2655 void
2656f_term_gettty(typval_T *argvars, typval_T *rettv)
2657{
2658 buf_T *buf = term_get_buf(argvars);
2659 char_u *p;
2660
2661 rettv->v_type = VAR_STRING;
2662 if (buf == NULL)
2663 return;
2664 if (buf->b_term->tl_job != NULL)
2665 p = buf->b_term->tl_job->jv_tty_name;
2666 else
2667 p = buf->b_term->tl_tty_name;
2668 if (p != NULL)
2669 rettv->vval.v_string = vim_strsave(p);
2670}
2671
2672/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002673 * "term_list()" function
2674 */
2675 void
2676f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2677{
2678 term_T *tp;
2679 list_T *l;
2680
2681 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2682 return;
2683
2684 l = rettv->vval.v_list;
2685 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2686 if (tp != NULL && tp->tl_buffer != NULL)
2687 if (list_append_number(l,
2688 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2689 return;
2690}
2691
2692/*
2693 * "term_scrape(buf, row)" function
2694 */
2695 void
2696f_term_scrape(typval_T *argvars, typval_T *rettv)
2697{
2698 buf_T *buf = term_get_buf(argvars);
2699 VTermScreen *screen = NULL;
2700 VTermPos pos;
2701 list_T *l;
2702 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002703 char_u *p;
2704 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002705
2706 if (rettv_list_alloc(rettv) == FAIL)
2707 return;
2708 if (buf == NULL)
2709 return;
2710 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002711
2712 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002713 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002714
2715 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002716 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002717 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002718 p = NULL;
2719 line = NULL;
2720 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002721 else
2722 {
2723 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2724
2725 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2726 return;
2727 p = ml_get_buf(buf, lnum + 1, FALSE);
2728 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2729 }
2730
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002731 for (pos.col = 0; pos.col < term->tl_cols; )
2732 {
2733 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002734 int width;
2735 VTermScreenCellAttrs attrs;
2736 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002737 char_u rgb[8];
2738 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2739 int off = 0;
2740 int i;
2741
2742 if (screen == NULL)
2743 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002744 cellattr_T *cellattr;
2745 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002746
2747 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002748 if (pos.col >= line->sb_cols)
2749 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002750 cellattr = line->sb_cells + pos.col;
2751 width = cellattr->width;
2752 attrs = cellattr->attrs;
2753 fg = cellattr->fg;
2754 bg = cellattr->bg;
2755 len = MB_PTR2LEN(p);
2756 mch_memmove(mbs, p, len);
2757 mbs[len] = NUL;
2758 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002759 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002760 else
2761 {
2762 VTermScreenCell cell;
2763 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2764 break;
2765 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2766 {
2767 if (cell.chars[i] == 0)
2768 break;
2769 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2770 }
2771 mbs[off] = NUL;
2772 width = cell.width;
2773 attrs = cell.attrs;
2774 fg = cell.fg;
2775 bg = cell.bg;
2776 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002777 dcell = dict_alloc();
2778 list_append_dict(l, dcell);
2779
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002780 dict_add_nr_str(dcell, "chars", 0, mbs);
2781
2782 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002783 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002784 dict_add_nr_str(dcell, "fg", 0, rgb);
2785 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002786 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002787 dict_add_nr_str(dcell, "bg", 0, rgb);
2788
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002789 dict_add_nr_str(dcell, "attr",
2790 cell2attr(attrs, fg, bg), NULL);
2791 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002792
2793 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002794 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002795 ++pos.col;
2796 }
2797}
2798
2799/*
2800 * "term_sendkeys(buf, keys)" function
2801 */
2802 void
2803f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2804{
2805 buf_T *buf = term_get_buf(argvars);
2806 char_u *msg;
2807 term_T *term;
2808
2809 rettv->v_type = VAR_UNKNOWN;
2810 if (buf == NULL)
2811 return;
2812
2813 msg = get_tv_string_chk(&argvars[1]);
2814 if (msg == NULL)
2815 return;
2816 term = buf->b_term;
2817 if (term->tl_vterm == NULL)
2818 return;
2819
2820 while (*msg != NUL)
2821 {
2822 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2823 msg += MB_PTR2LEN(msg);
2824 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002825}
2826
2827/*
2828 * "term_start(command, options)" function
2829 */
2830 void
2831f_term_start(typval_T *argvars, typval_T *rettv)
2832{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002833 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002834
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002835 init_job_options(&opt);
2836 /* TODO: allow more job options */
2837 if (argvars[1].v_type != VAR_UNKNOWN
2838 && get_job_options(&argvars[1], &opt,
2839 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002840 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002841 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002842 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar3346cc42017-09-02 14:54:21 +02002843 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002844 return;
2845
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002846 if (opt.jo_vertical)
2847 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002848 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002849
2850 if (curbuf->b_term != NULL)
2851 rettv->vval.v_number = curbuf->b_fnum;
2852}
2853
2854/*
2855 * "term_wait" function
2856 */
2857 void
2858f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2859{
2860 buf_T *buf = term_get_buf(argvars);
2861
2862 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002863 {
2864 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002865 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002866 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002867 if (buf->b_term->tl_job == NULL)
2868 {
2869 ch_log(NULL, "term_wait(): no job to wait for");
2870 return;
2871 }
Bram Moolenaar13ebb032017-08-26 22:02:51 +02002872 if (buf->b_term->tl_job->jv_channel == NULL)
2873 /* channel is closed, nothing to do */
2874 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002875
2876 /* Get the job status, this will detect a job that finished. */
Bram Moolenaar13ebb032017-08-26 22:02:51 +02002877 if ((buf->b_term->tl_job->jv_channel == NULL
2878 || !buf->b_term->tl_job->jv_channel->ch_keep_open)
2879 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002880 {
2881 /* The job is dead, keep reading channel I/O until the channel is
2882 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002883 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002884 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2885 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002886 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002887 parse_queued_messages();
2888 ui_delay(10L, FALSE);
2889 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002890 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002891 parse_queued_messages();
2892 }
2893 else
2894 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002895 long wait = 10L;
2896
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002897 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002898 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002899
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002900 /* Wait for some time for any channel I/O. */
2901 if (argvars[1].v_type != VAR_UNKNOWN)
2902 wait = get_tv_number(&argvars[1]);
2903 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002904 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002905
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002906 /* Flushing messages on channels is hopefully sufficient.
2907 * TODO: is there a better way? */
2908 parse_queued_messages();
2909 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002910}
2911
Bram Moolenaardada6d22017-09-02 17:18:35 +02002912/*
2913 * Called when a channel has sent all the lines to a terminal.
2914 * Send a CTRL-D to mark the end of the text.
2915 */
2916 void
2917term_send_eof(channel_T *ch)
2918{
2919 term_T *term;
2920
2921 for (term = first_term; term != NULL; term = term->tl_next)
2922 if (term->tl_job == ch->ch_job)
2923 {
2924 if (term->tl_eof_chars != NULL)
2925 {
2926 channel_send(ch, PART_IN, term->tl_eof_chars,
2927 (int)STRLEN(term->tl_eof_chars), NULL);
2928 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
2929 }
2930# ifdef WIN3264
2931 else
2932 /* Default: CTRL-D */
2933 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
2934# endif
2935 }
2936}
2937
Bram Moolenaara83e3962017-08-17 14:39:07 +02002938# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002939
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002940/**************************************
2941 * 2. MS-Windows implementation.
2942 */
2943
Bram Moolenaara83e3962017-08-17 14:39:07 +02002944# ifndef PROTO
2945
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002946#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2947#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2948
Bram Moolenaar8a773062017-07-24 22:29:21 +02002949void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002950void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002951void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002952BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2953void (*winpty_config_set_initial_size)(void*, int, int);
2954LPCWSTR (*winpty_conin_name)(void*);
2955LPCWSTR (*winpty_conout_name)(void*);
2956LPCWSTR (*winpty_conerr_name)(void*);
2957void (*winpty_free)(void*);
2958void (*winpty_config_free)(void*);
2959void (*winpty_spawn_config_free)(void*);
2960void (*winpty_error_free)(void*);
2961LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002962BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002963HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002964
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002965#define WINPTY_DLL "winpty.dll"
2966
2967static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002968# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002969
Bram Moolenaara83e3962017-08-17 14:39:07 +02002970 static int
2971dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002972{
2973 int i;
2974 static struct
2975 {
2976 char *name;
2977 FARPROC *ptr;
2978 } winpty_entry[] =
2979 {
2980 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2981 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2982 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2983 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2984 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2985 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2986 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2987 {"winpty_free", (FARPROC*)&winpty_free},
2988 {"winpty_open", (FARPROC*)&winpty_open},
2989 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2990 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2991 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2992 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002993 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002994 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002995 {NULL, NULL}
2996 };
2997
2998 /* No need to initialize twice. */
2999 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02003000 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02003001 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
3002 * winpty.dll. */
3003 if (*p_winptydll != NUL)
3004 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
3005 if (!hWinPtyDLL)
3006 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003007 if (!hWinPtyDLL)
3008 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02003009 if (verbose)
3010 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
3011 : (char_u *)WINPTY_DLL);
3012 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003013 }
3014 for (i = 0; winpty_entry[i].name != NULL
3015 && winpty_entry[i].ptr != NULL; ++i)
3016 {
3017 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
3018 winpty_entry[i].name)) == NULL)
3019 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02003020 if (verbose)
3021 EMSG2(_(e_loadfunc), winpty_entry[i].name);
3022 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003023 }
3024 }
3025
Bram Moolenaara83e3962017-08-17 14:39:07 +02003026 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003027}
3028
3029/*
3030 * Create a new terminal of "rows" by "cols" cells.
3031 * Store a reference in "term".
3032 * Return OK or FAIL.
3033 */
3034 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003035term_and_job_init(
3036 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02003037 typval_T *argvar,
3038 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003039{
Bram Moolenaar5983d502017-08-20 19:22:56 +02003040 WCHAR *cmd_wchar = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003041 channel_T *channel = NULL;
3042 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003043 DWORD error;
Bram Moolenaar5983d502017-08-20 19:22:56 +02003044 HANDLE jo = NULL;
3045 HANDLE child_process_handle;
3046 HANDLE child_thread_handle;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003047 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003048 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003049 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003050 garray_T ga;
3051 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003052
Bram Moolenaara83e3962017-08-17 14:39:07 +02003053 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003054 return FAIL;
3055
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003056 if (argvar->v_type == VAR_STRING)
3057 cmd = argvar->vval.v_string;
3058 else
3059 {
3060 ga_init2(&ga, (int)sizeof(char*), 20);
3061 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
3062 goto failed;
3063 cmd = ga.ga_data;
3064 }
3065
Bram Moolenaar5983d502017-08-20 19:22:56 +02003066 cmd_wchar = enc_to_utf16(cmd, NULL);
3067 if (cmd_wchar == NULL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003068 return FAIL;
3069
3070 job = job_alloc();
3071 if (job == NULL)
3072 goto failed;
3073
3074 channel = add_channel();
3075 if (channel == NULL)
3076 goto failed;
3077
3078 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
3079 if (term->tl_winpty_config == NULL)
3080 goto failed;
3081
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003082 winpty_config_set_initial_size(term->tl_winpty_config,
3083 term->tl_cols, term->tl_rows);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003084 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
3085 if (term->tl_winpty == NULL)
3086 goto failed;
3087
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003088 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003089 spawn_config = winpty_spawn_config_new(
3090 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
3091 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
3092 NULL,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003093 cmd_wchar,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003094 NULL,
3095 NULL,
3096 &winpty_err);
3097 if (spawn_config == NULL)
3098 goto failed;
3099
3100 channel = add_channel();
3101 if (channel == NULL)
3102 goto failed;
3103
3104 job = job_alloc();
3105 if (job == NULL)
3106 goto failed;
3107
Bram Moolenaar5983d502017-08-20 19:22:56 +02003108 if (opt->jo_set & JO_IN_BUF)
3109 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
3110
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003111 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
3112 &child_thread_handle, &error, &winpty_err))
3113 goto failed;
3114
3115 channel_set_pipes(channel,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003116 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003117 winpty_conin_name(term->tl_winpty),
3118 GENERIC_WRITE, 0, NULL,
3119 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003120 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003121 winpty_conout_name(term->tl_winpty),
3122 GENERIC_READ, 0, NULL,
3123 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003124 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003125 winpty_conerr_name(term->tl_winpty),
3126 GENERIC_READ, 0, NULL,
3127 OPEN_EXISTING, 0, NULL));
3128
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02003129 /* Write lines with CR instead of NL. */
3130 channel->ch_write_text_mode = TRUE;
3131
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003132 jo = CreateJobObject(NULL, NULL);
3133 if (jo == NULL)
3134 goto failed;
3135
3136 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003137 {
3138 /* Failed, switch the way to terminate process with TerminateProcess. */
3139 CloseHandle(jo);
3140 jo = NULL;
3141 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003142
3143 winpty_spawn_config_free(spawn_config);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003144 vim_free(cmd_wchar);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003145
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003146 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003147
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02003148 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02003149 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003150
3151 job->jv_channel = channel;
3152 job->jv_proc_info.hProcess = child_process_handle;
3153 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
3154 job->jv_job_object = jo;
3155 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003156 sprintf(buf, "winpty://%lu",
3157 GetProcessId(winpty_agent_process(term->tl_winpty)));
3158 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003159 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003160 term->tl_job = job;
3161
3162 return OK;
3163
3164failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003165 if (argvar->v_type == VAR_LIST)
3166 vim_free(ga.ga_data);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003167 if (cmd_wchar != NULL)
3168 vim_free(cmd_wchar);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003169 if (spawn_config != NULL)
3170 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003171 if (channel != NULL)
3172 channel_clear(channel);
3173 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003174 {
3175 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003176 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003177 }
3178 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003179 if (jo != NULL)
3180 CloseHandle(jo);
3181 if (term->tl_winpty != NULL)
3182 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003183 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003184 if (term->tl_winpty_config != NULL)
3185 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003186 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003187 if (winpty_err != NULL)
3188 {
3189 char_u *msg = utf16_to_enc(
3190 (short_u *)winpty_error_msg(winpty_err), NULL);
3191
3192 EMSG(msg);
3193 winpty_error_free(winpty_err);
3194 }
3195 return FAIL;
3196}
3197
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003198 static int
3199create_pty_only(term_T *term, jobopt_T *opt)
3200{
3201 /* TODO: implement this */
3202 return FAIL;
3203}
3204
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003205/*
3206 * Free the terminal emulator part of "term".
3207 */
3208 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003209term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003210{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003211 if (term->tl_winpty != NULL)
3212 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003213 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003214 if (term->tl_winpty_config != NULL)
3215 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003216 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003217 if (term->tl_vterm != NULL)
3218 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003219 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003220}
3221
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003222/*
3223 * Request size to terminal.
3224 */
3225 static void
3226term_report_winsize(term_T *term, int rows, int cols)
3227{
3228 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3229}
3230
Bram Moolenaara83e3962017-08-17 14:39:07 +02003231 int
3232terminal_enabled(void)
3233{
3234 return dyn_winpty_init(FALSE) == OK;
3235}
3236
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003237# else
3238
3239/**************************************
3240 * 3. Unix-like implementation.
3241 */
3242
3243/*
3244 * Create a new terminal of "rows" by "cols" cells.
3245 * Start job for "cmd".
3246 * Store the pointers in "term".
3247 * Return OK or FAIL.
3248 */
3249 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003250term_and_job_init(
3251 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02003252 typval_T *argvar,
3253 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003254{
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003255 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003256
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003257 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003258 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003259 if (term->tl_job != NULL)
3260 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003261
Bram Moolenaar61a66052017-07-22 18:39:00 +02003262 return term->tl_job != NULL
3263 && term->tl_job->jv_channel != NULL
3264 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003265}
3266
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003267 static int
3268create_pty_only(term_T *term, jobopt_T *opt)
3269{
3270 int ret;
3271
3272 create_vterm(term, term->tl_rows, term->tl_cols);
3273
3274 term->tl_job = job_alloc();
3275 if (term->tl_job == NULL)
3276 return FAIL;
3277 ++term->tl_job->jv_refcount;
3278
3279 /* behave like the job is already finished */
3280 term->tl_job->jv_status = JOB_FINISHED;
3281
3282 ret = mch_create_pty_channel(term->tl_job, opt);
3283
3284 return ret;
3285}
3286
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003287/*
3288 * Free the terminal emulator part of "term".
3289 */
3290 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003291term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003292{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003293 if (term->tl_vterm != NULL)
3294 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003295 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003296}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003297
3298/*
3299 * Request size to terminal.
3300 */
3301 static void
3302term_report_winsize(term_T *term, int rows, int cols)
3303{
3304 /* Use an ioctl() to report the new window size to the job. */
3305 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3306 {
3307 int fd = -1;
3308 int part;
3309
3310 for (part = PART_OUT; part < PART_COUNT; ++part)
3311 {
3312 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3313 if (isatty(fd))
3314 break;
3315 }
3316 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003317 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003318 }
3319}
3320
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003321# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003322
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003323#endif /* FEAT_TERMINAL */