blob: 3ceb360fbccb2044f8b60ab5b9c7557de255ed53 [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 Moolenaar6756c702017-09-05 23:01:12 +020041 * - ":term NONE" does not work on MS-Windows.
42 * https://github.com/vim/vim/pull/2056
43 * - Redirecting output does not work on MS-Windows.
Bram Moolenaar94053a52017-08-01 21:44:33 +020044 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020045 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020046 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020047 * - support minimal size when 'termsize' is empty?
Bram Moolenaar285f2432017-08-23 23:10:21 +020048 * - GUI: when using tabs, focus in terminal, click on tab does not work.
49 * - GUI: when 'confirm' is set and trying to exit Vim, dialog offers to save
50 * changes to "!shell".
51 * (justrajdeep, 2017 Aug 22)
Bram Moolenaar4f44b882017-08-13 20:06:18 +020052 * - For the GUI fill termios with default values, perhaps like pangoterm:
53 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar423802d2017-07-30 16:52:24 +020054 * - if the job in the terminal does not support the mouse, we can use the
55 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020056 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
57 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020058 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020059 * - Copy text in the vterm to the Vim buffer once in a while, so that
60 * completion works.
Bram Moolenaar6756c702017-09-05 23:01:12 +020061 * - add an optional limit for the scrollback size. When reaching it remove
62 * 10% at the start.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020063 */
64
65#include "vim.h"
66
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020067#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020068
Bram Moolenaard5310982017-08-05 15:16:32 +020069#ifndef MIN
70# define MIN(x,y) ((x) < (y) ? (x) : (y))
71#endif
72#ifndef MAX
73# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020074#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020075
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020076#include "libvterm/include/vterm.h"
77
Bram Moolenaar33a43be2017-08-06 21:36:22 +020078/* This is VTermScreenCell without the characters, thus much smaller. */
79typedef struct {
80 VTermScreenCellAttrs attrs;
81 char width;
82 VTermColor fg, bg;
83} cellattr_T;
84
Bram Moolenaard85f2712017-07-28 21:51:57 +020085typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020086 int sb_cols; /* can differ per line */
87 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020088} sb_line_T;
89
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090/* typedef term_T in structs.h */
91struct terminal_S {
92 term_T *tl_next;
93
Bram Moolenaar423802d2017-07-30 16:52:24 +020094 VTerm *tl_vterm;
95 job_T *tl_job;
96 buf_T *tl_buffer;
97
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020098 /* used when tl_job is NULL and only a pty was created */
99 int tl_tty_fd;
100 char_u *tl_tty_name;
101
Bram Moolenaar6d819742017-08-06 14:57:49 +0200102 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200103 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200104 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200105 char_u *tl_opencmd;
Bram Moolenaardada6d22017-09-02 17:18:35 +0200106 char_u *tl_eof_chars;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200107
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200108#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200109 void *tl_winpty_config;
110 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200111#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200112
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200113 /* last known vterm size */
114 int tl_rows;
115 int tl_cols;
116 /* vterm size does not follow window size */
117 int tl_rows_fixed;
118 int tl_cols_fixed;
119
Bram Moolenaar21554412017-07-24 21:44:43 +0200120 char_u *tl_title; /* NULL or allocated */
121 char_u *tl_status_text; /* NULL or allocated */
122
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200123 /* Range of screen rows to update. Zero based. */
124 int tl_dirty_row_start; /* -1 if nothing dirty */
125 int tl_dirty_row_end; /* row below last one to update */
126
Bram Moolenaard85f2712017-07-28 21:51:57 +0200127 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200128 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200129
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200130 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200131 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200132 int tl_cursor_blink;
133 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
134 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200135
136 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200137};
138
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200139#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
140#define TMODE_LOOP 2 /* CTRL-W N used */
141
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200142/*
143 * List of all active terminals.
144 */
145static term_T *first_term = NULL;
146
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200147/* Terminal active in terminal_loop(). */
148static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200149
150#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
151#define KEY_BUF_LEN 200
152
153/*
154 * Functions with separate implementation for MS-Windows and Unix-like systems.
155 */
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200156static int term_and_job_init(term_T *term, typval_T *argvar, jobopt_T *opt);
157static int create_pty_only(term_T *term, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200158static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200159static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200160
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200161/* The characters that we know (or assume) that the terminal expects for the
162 * backspace and enter keys. */
163static int term_backspace_char = BS;
164static int term_enter_char = CAR;
165static int term_nl_does_cr = FALSE;
166
167
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200168/**************************************
169 * 1. Generic code for all systems.
170 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200171
172/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200173 * Determine the terminal size from 'termsize' and the current window.
174 * Assumes term->tl_rows and term->tl_cols are zero.
175 */
176 static void
177set_term_and_win_size(term_T *term)
178{
179 if (*curwin->w_p_tms != NUL)
180 {
181 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
182
183 term->tl_rows = atoi((char *)curwin->w_p_tms);
184 term->tl_cols = atoi((char *)p);
185 }
186 if (term->tl_rows == 0)
187 term->tl_rows = curwin->w_height;
188 else
189 {
190 win_setheight_win(term->tl_rows, curwin);
191 term->tl_rows_fixed = TRUE;
192 }
193 if (term->tl_cols == 0)
194 term->tl_cols = curwin->w_width;
195 else
196 {
197 win_setwidth_win(term->tl_cols, curwin);
198 term->tl_cols_fixed = TRUE;
199 }
200}
201
202/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200203 * Initialize job options for a terminal job.
204 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200205 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200206 static void
207init_job_options(jobopt_T *opt)
208{
209 clear_job_options(opt);
210
211 opt->jo_mode = MODE_RAW;
212 opt->jo_out_mode = MODE_RAW;
213 opt->jo_err_mode = MODE_RAW;
214 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200215}
216
217/*
218 * Set job options mandatory for a terminal job.
219 */
220 static void
221setup_job_options(jobopt_T *opt, int rows, int cols)
222{
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200223 if (!(opt->jo_set & JO_OUT_IO))
224 {
225 /* Connect stdout to the terminal. */
226 opt->jo_io[PART_OUT] = JIO_BUFFER;
227 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
228 opt->jo_modifiable[PART_OUT] = 0;
229 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
230 }
231
232 if (!(opt->jo_set & JO_ERR_IO))
233 {
234 /* Connect stderr to the terminal. */
235 opt->jo_io[PART_ERR] = JIO_BUFFER;
236 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
237 opt->jo_modifiable[PART_ERR] = 0;
238 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
239 }
240
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200241 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200242 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
243 opt->jo_term_rows = rows;
244 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
245 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200246}
247
248 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200249term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251 exarg_T split_ea;
252 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200253 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200254 buf_T *old_curbuf = NULL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200255 int res;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200256
257 if (check_restricted() || check_secure())
258 return;
259
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200260 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
261 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
262 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
263 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF)))
264 {
265 EMSG(_(e_invarg));
266 return;
267 }
268
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200269 term = (term_T *)alloc_clear(sizeof(term_T));
270 if (term == NULL)
271 return;
272 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200273 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200274 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200275 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200276 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200277
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200278 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200279 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200280 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200281 /* Create a new buffer in the current window. */
282 if (!can_abandon(curbuf, forceit))
283 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200284 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200285 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200286 return;
287 }
288 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
289 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200290 {
291 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200292 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200293 }
294 }
295 else if (opt->jo_hidden)
296 {
297 buf_T *buf;
298
299 /* Create a new buffer without a window. Make it the current buffer for
300 * a moment to be able to do the initialisations. */
301 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
302 BLN_NEW | BLN_LISTED);
303 if (buf == NULL || ml_open(buf) == FAIL)
304 {
305 vim_free(term);
306 return;
307 }
308 old_curbuf = curbuf;
309 --curbuf->b_nwindows;
310 curbuf = buf;
311 curwin->w_buffer = buf;
312 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200313 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200314 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200315 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200316 /* Open a new window or tab. */
317 split_ea.cmdidx = CMD_new;
318 split_ea.cmd = (char_u *)"new";
319 split_ea.arg = (char_u *)"";
320 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
321 {
322 split_ea.line2 = opt->jo_term_rows;
323 split_ea.addr_count = 1;
324 }
325 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
326 {
327 split_ea.line2 = opt->jo_term_cols;
328 split_ea.addr_count = 1;
329 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200330
Bram Moolenaarda43b612017-08-11 22:27:50 +0200331 ex_splitview(&split_ea);
332 if (curwin == old_curwin)
333 {
334 /* split failed */
335 vim_free(term);
336 return;
337 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200338 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200339 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200340 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200341
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200342 if (!opt->jo_hidden)
343 {
344 /* only one size was taken care of with :new, do the other one */
345 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
346 win_setheight(opt->jo_term_rows);
347 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
348 win_setwidth(opt->jo_term_cols);
349 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200350
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200351 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200352 term->tl_next = first_term;
353 first_term = term;
354
Bram Moolenaar78712a72017-08-05 14:50:12 +0200355 if (opt->jo_term_name != NULL)
356 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
357 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200358 {
359 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200360 size_t len;
361 char_u *cmd, *p;
362
363 if (argvar->v_type == VAR_STRING)
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200364 {
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200365 cmd = argvar->vval.v_string;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200366 if (cmd == NULL)
367 cmd = (char_u *)"";
368 else if (STRCMP(cmd, "NONE") == 0)
369 cmd = (char_u *)"pty";
370 }
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200371 else if (argvar->v_type != VAR_LIST
372 || argvar->vval.v_list == NULL
Bram Moolenaar6756c702017-09-05 23:01:12 +0200373 || argvar->vval.v_list->lv_len < 1
374 || (cmd = get_tv_string_chk(
375 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200376 cmd = (char_u*)"";
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200377
378 len = STRLEN(cmd) + 10;
379 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200380
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200381 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200382 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200383 /* Prepend a ! to the command name to avoid the buffer name equals
384 * the executable, otherwise ":w!" would overwrite it. */
385 if (i == 0)
386 vim_snprintf((char *)p, len, "!%s", cmd);
387 else
388 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200389 if (buflist_findname(p) == NULL)
390 {
391 curbuf->b_ffname = p;
392 break;
393 }
394 }
395 }
396 curbuf->b_fname = curbuf->b_ffname;
397
Bram Moolenaar37c45832017-08-12 16:01:04 +0200398 if (opt->jo_term_opencmd != NULL)
399 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
400
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200401 if (opt->jo_eof_chars != NULL)
402 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200403
Bram Moolenaareb44a682017-08-03 22:44:55 +0200404 set_string_option_direct((char_u *)"buftype", -1,
405 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
406
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200407 /* Mark the buffer as not modifiable. It can only be made modifiable after
408 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200409 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200410
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200411 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200412 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200413
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200414 /* System dependent: setup the vterm and maybe start the job in it. */
415 if (argvar->v_type == VAR_STRING
416 && argvar->vval.v_string != NULL
417 && STRCMP(argvar->vval.v_string, "NONE") == 0)
418 res = create_pty_only(term, opt);
419 else
420 res = term_and_job_init(term, argvar, opt);
421
422 if (res == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200423 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200424 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200425 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200426 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200427
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200428 /* Make sure we don't get stuck on sending keys to the job, it leads to
429 * a deadlock if the job is waiting for Vim to read. */
430 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
431
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200432 if (old_curbuf != NULL)
433 {
434 --curbuf->b_nwindows;
435 curbuf = old_curbuf;
436 curwin->w_buffer = curbuf;
437 ++curbuf->b_nwindows;
438 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200439 }
440 else
441 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200442 buf_T *buf = curbuf;
443
Bram Moolenaard85f2712017-07-28 21:51:57 +0200444 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200445 if (old_curbuf != NULL)
446 {
447 --curbuf->b_nwindows;
448 curbuf = old_curbuf;
449 curwin->w_buffer = curbuf;
450 ++curbuf->b_nwindows;
451 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200452
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200453 /* Wiping out the buffer will also close the window and call
454 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200455 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200456 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200457}
458
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200459/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200460 * ":terminal": open a terminal window and execute a job in it.
461 */
462 void
463ex_terminal(exarg_T *eap)
464{
Bram Moolenaar28550b72017-09-05 23:31:01 +0200465 typval_T argvar[2];
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200466 jobopt_T opt;
467 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200468 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200469
470 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200471
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200472 cmd = eap->arg;
473 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
474 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200475 char_u *p, *ep;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200476
477 cmd += 2;
478 p = skiptowhite(cmd);
Bram Moolenaarb2412082017-08-20 18:09:14 +0200479 ep = vim_strchr(cmd, '=');
480 if (ep != NULL && ep < p)
481 p = ep;
482
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200483 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
484 opt.jo_term_finish = 'c';
485 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
486 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200487 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
488 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200489 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
490 opt.jo_hidden = 1;
Bram Moolenaarb2412082017-08-20 18:09:14 +0200491 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
492 && ep != NULL && isdigit(ep[1]))
493 {
494 opt.jo_set2 |= JO2_TERM_ROWS;
495 opt.jo_term_rows = atoi((char *)ep + 1);
496 p = skiptowhite(cmd);
497 }
498 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
499 && ep != NULL && isdigit(ep[1]))
500 {
501 opt.jo_set2 |= JO2_TERM_COLS;
502 opt.jo_term_cols = atoi((char *)ep + 1);
503 p = skiptowhite(cmd);
504 }
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200505 else if ((int)(p - cmd) == 3 && STRNICMP(cmd, "eof", 3) == 0
506 && ep != NULL)
507 {
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200508 char_u *buf = NULL;
509 char_u *keys;
510
511 p = skiptowhite(cmd);
512 *p = NUL;
513 keys = replace_termcodes(ep + 1, &buf, TRUE, TRUE, TRUE);
514 opt.jo_set2 |= JO2_EOF_CHARS;
515 opt.jo_eof_chars = vim_strsave(keys);
516 vim_free(buf);
517 *p = ' ';
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200518 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200519 else
520 {
521 if (*p)
522 *p = NUL;
523 EMSG2(_("E181: Invalid attribute: %s"), cmd);
524 return;
525 }
526 cmd = skipwhite(p);
527 }
Bram Moolenaar28550b72017-09-05 23:31:01 +0200528 if (*cmd == NUL)
529 /* Make a copy of 'shell', an autocommand may change the option. */
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200530 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200531
Bram Moolenaarb2412082017-08-20 18:09:14 +0200532 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200533 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200534 /* Write lines from current buffer to the job. */
535 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
536 opt.jo_io[PART_IN] = JIO_BUFFER;
537 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
538 opt.jo_in_top = eap->line1;
539 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200540 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200541
Bram Moolenaar28550b72017-09-05 23:31:01 +0200542 argvar[0].v_type = VAR_STRING;
543 argvar[0].vval.v_string = cmd;
544 argvar[1].v_type = VAR_UNKNOWN;
545 term_start(argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200546 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200547}
548
549/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200550 * Free the scrollback buffer for "term".
551 */
552 static void
553free_scrollback(term_T *term)
554{
555 int i;
556
557 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
558 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
559 ga_clear(&term->tl_scrollback);
560}
561
562/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200563 * Free a terminal and everything it refers to.
564 * Kills the job if there is one.
565 * Called when wiping out a buffer.
566 */
567 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200568free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200569{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200570 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200571 term_T *tp;
572
573 if (term == NULL)
574 return;
575 if (first_term == term)
576 first_term = term->tl_next;
577 else
578 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
579 if (tp->tl_next == term)
580 {
581 tp->tl_next = term->tl_next;
582 break;
583 }
584
585 if (term->tl_job != NULL)
586 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200587 if (term->tl_job->jv_status != JOB_ENDED
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200588 && term->tl_job->jv_status != JOB_FINISHED
589 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200590 job_stop(term->tl_job, NULL, "kill");
591 job_unref(term->tl_job);
592 }
593
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200594 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200595
596 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200597 vim_free(term->tl_title);
598 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200599 vim_free(term->tl_opencmd);
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200600 vim_free(term->tl_eof_chars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200601 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200602 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200603 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200604 if (in_terminal_loop == term)
605 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200606}
607
608/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200609 * Write job output "msg[len]" to the vterm.
610 */
611 static void
612term_write_job_output(term_T *term, char_u *msg, size_t len)
613{
614 VTerm *vterm = term->tl_vterm;
615 char_u *p;
616 size_t done;
617 size_t len_now;
618
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200619 if (term_nl_does_cr)
620 vterm_input_write(vterm, (char *)msg, len);
621 else
622 /* need to convert NL to CR-NL */
623 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200624 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200625 for (p = msg + done; p < msg + len; )
626 {
627 if (*p == NL)
628 break;
629 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
630 }
631 len_now = p - msg - done;
632 vterm_input_write(vterm, (char *)msg + done, len_now);
633 if (p < msg + len && *p == NL)
634 {
635 vterm_input_write(vterm, "\r\n", 2);
636 ++len_now;
637 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200638 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200639
640 /* this invokes the damage callbacks */
641 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
642}
643
Bram Moolenaar1c844932017-07-24 23:36:41 +0200644 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200645update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200646{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200647 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200648 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200649 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200650 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200651 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200652 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200653 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200654 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200655#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200656 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200657 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200658#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200659 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200660}
661
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200662/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200663 * Invoked when "msg" output from a job was received. Write it to the terminal
664 * of "buffer".
665 */
666 void
667write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
668{
669 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200670 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200671
Bram Moolenaard85f2712017-07-28 21:51:57 +0200672 if (term->tl_vterm == NULL)
673 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200674 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200675 return;
676 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200677 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200678 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200679
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200680 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
681 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200682 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200683 {
684 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200685 ch_log(term->tl_job->jv_channel, "updating screen");
686 if (buffer == curbuf)
687 {
688 update_screen(0);
689 update_cursor(term, TRUE);
690 }
691 else
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200692 redraw_after_callback(TRUE);
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200693 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200694}
695
696/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200697 * Send a mouse position and click to the vterm
698 */
699 static int
700term_send_mouse(VTerm *vterm, int button, int pressed)
701{
702 VTermModifier mod = VTERM_MOD_NONE;
703
704 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
705 mouse_col - W_WINCOL(curwin), mod);
706 vterm_mouse_button(vterm, button, pressed, mod);
707 return TRUE;
708}
709
710/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200711 * Convert typed key "c" into bytes to send to the job.
712 * Return the number of bytes in "buf".
713 */
714 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200715term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200716{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200717 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200718 VTermKey key = VTERM_KEY_NONE;
719 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200720 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200721
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200722 switch (c)
723 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200724 case CAR: c = term_enter_char; break;
725 /* don't use VTERM_KEY_BACKSPACE, it always
726 * becomes 0x7f DEL */
727 case K_BS: c = term_backspace_char; break;
728
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200729 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200730 case K_DEL: key = VTERM_KEY_DEL; break;
731 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200732 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
733 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200734 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200735 case K_S_END: mod = VTERM_MOD_SHIFT;
736 key = VTERM_KEY_END; break;
737 case K_C_END: mod = VTERM_MOD_CTRL;
738 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200739 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
740 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
741 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
742 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
743 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
744 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
745 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
746 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
747 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
748 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
749 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
750 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
751 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200752 case K_S_HOME: mod = VTERM_MOD_SHIFT;
753 key = VTERM_KEY_HOME; break;
754 case K_C_HOME: mod = VTERM_MOD_CTRL;
755 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200756 case K_INS: key = VTERM_KEY_INS; break;
757 case K_K0: key = VTERM_KEY_KP_0; break;
758 case K_K1: key = VTERM_KEY_KP_1; break;
759 case K_K2: key = VTERM_KEY_KP_2; break;
760 case K_K3: key = VTERM_KEY_KP_3; break;
761 case K_K4: key = VTERM_KEY_KP_4; break;
762 case K_K5: key = VTERM_KEY_KP_5; break;
763 case K_K6: key = VTERM_KEY_KP_6; break;
764 case K_K7: key = VTERM_KEY_KP_7; break;
765 case K_K8: key = VTERM_KEY_KP_8; break;
766 case K_K9: key = VTERM_KEY_KP_9; break;
767 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
768 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
769 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
770 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
771 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
772 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
773 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
774 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
775 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
776 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
777 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
778 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
779 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200780 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
781 key = VTERM_KEY_LEFT; break;
782 case K_C_LEFT: mod = VTERM_MOD_CTRL;
783 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200784 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
785 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
786 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200787 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
788 key = VTERM_KEY_RIGHT; break;
789 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
790 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200791 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200792 case K_S_UP: mod = VTERM_MOD_SHIFT;
793 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200794 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200795
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200796 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
797 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
798 case K_MOUSELEFT: /* TODO */ return 0;
799 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200800
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200801 case K_LEFTMOUSE:
802 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
803 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
804 case K_LEFTRELEASE:
805 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
806 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
807 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
808 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
809 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
810 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
811 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
812 case K_X1MOUSE: /* TODO */ return 0;
813 case K_X1DRAG: /* TODO */ return 0;
814 case K_X1RELEASE: /* TODO */ return 0;
815 case K_X2MOUSE: /* TODO */ return 0;
816 case K_X2DRAG: /* TODO */ return 0;
817 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200818
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200819 case K_IGNORE: return 0;
820 case K_NOP: return 0;
821 case K_UNDO: return 0;
822 case K_HELP: return 0;
823 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
824 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
825 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
826 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
827 case K_SELECT: return 0;
828#ifdef FEAT_GUI
829 case K_VER_SCROLLBAR: return 0;
830 case K_HOR_SCROLLBAR: return 0;
831#endif
832#ifdef FEAT_GUI_TABLINE
833 case K_TABLINE: return 0;
834 case K_TABMENU: return 0;
835#endif
836#ifdef FEAT_NETBEANS_INTG
837 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
838#endif
839#ifdef FEAT_DND
840 case K_DROP: return 0;
841#endif
842#ifdef FEAT_AUTOCMD
843 case K_CURSORHOLD: return 0;
844#endif
845 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
846 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200847 }
848
849 /*
850 * Convert special keys to vterm keys:
851 * - Write keys to vterm: vterm_keyboard_key()
852 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200853 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200854 */
855 if (key != VTERM_KEY_NONE)
856 /* Special key, let vterm convert it. */
857 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200858 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200859 /* Normal character, let vterm convert it. */
860 vterm_keyboard_unichar(vterm, c, mod);
861
862 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200863 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200864}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200865
Bram Moolenaar938783d2017-07-16 20:13:26 +0200866/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200867 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200868 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200869 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200870term_job_running(term_T *term)
871{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200872 /* Also consider the job finished when the channel is closed, to avoid a
873 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200874 return term != NULL
875 && term->tl_job != NULL
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200876 && channel_is_open(term->tl_job->jv_channel)
877 && (term->tl_job->jv_status == JOB_STARTED
878 || term->tl_job->jv_channel->ch_keep_open);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200879}
880
881/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200882 * Add the last line of the scrollback buffer to the buffer in the window.
883 */
884 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200885add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200886{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200887 buf_T *buf = term->tl_buffer;
888 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
889 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200890
Bram Moolenaar58302322017-08-22 20:33:53 +0200891#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +0200892 if (!enc_utf8 && enc_codepage > 0)
893 {
894 WCHAR *ret = NULL;
895 int length = 0;
896
897 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
898 &ret, &length);
899 if (ret != NULL)
900 {
901 WideCharToMultiByte_alloc(enc_codepage, 0,
902 ret, length, (char **)&text, &len, 0, 0);
903 vim_free(ret);
904 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
905 vim_free(text);
906 }
907 }
908 else
909#endif
910 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200911 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200912 {
913 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200914 curbuf = buf;
915 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200916 curbuf = curwin->w_buffer;
917 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200918}
919
920/*
921 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200922 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200923 */
924 static void
925move_terminal_to_buffer(term_T *term)
926{
927 win_T *wp;
928 int len;
929 int lines_skipped = 0;
930 VTermPos pos;
931 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200932 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200933 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200934
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200935 if (term->tl_vterm == NULL)
936 return;
937 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200938 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
939 {
940 len = 0;
941 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
942 if (vterm_screen_get_cell(screen, pos, &cell) != 0
943 && cell.chars[0] != NUL)
944 len = pos.col + 1;
945
946 if (len == 0)
947 ++lines_skipped;
948 else
949 {
950 while (lines_skipped > 0)
951 {
952 /* Line was skipped, add an empty line. */
953 --lines_skipped;
954 if (ga_grow(&term->tl_scrollback, 1) == OK)
955 {
956 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
957 + term->tl_scrollback.ga_len;
958
959 line->sb_cols = 0;
960 line->sb_cells = NULL;
961 ++term->tl_scrollback.ga_len;
962
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200963 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200964 }
965 }
966
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200967 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200968 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
969 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200970 garray_T ga;
971 int width;
972 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200973 + term->tl_scrollback.ga_len;
974
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200975 ga_init2(&ga, 1, 100);
976 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200977 {
978 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200979 {
980 width = 1;
981 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
982 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +0200983 ga.ga_len += utf_char2bytes(' ',
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200984 (char_u *)ga.ga_data + ga.ga_len);
985 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200986 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200987 {
988 width = cell.width;
989
990 p[pos.col].width = cell.width;
991 p[pos.col].attrs = cell.attrs;
992 p[pos.col].fg = cell.fg;
993 p[pos.col].bg = cell.bg;
994
995 if (ga_grow(&ga, MB_MAXBYTES) == OK)
996 {
997 int i;
998 int c;
999
1000 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +02001001 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001002 (char_u *)ga.ga_data + ga.ga_len);
1003 }
1004 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02001005 }
1006 line->sb_cols = len;
1007 line->sb_cells = p;
1008 ++term->tl_scrollback.ga_len;
1009
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001010 if (ga_grow(&ga, 1) == FAIL)
1011 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1012 else
1013 {
1014 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1015 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1016 }
1017 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001018 }
1019 else
1020 vim_free(p);
1021 }
1022 }
1023
1024 FOR_ALL_WINDOWS(wp)
1025 {
1026 if (wp->w_buffer == term->tl_buffer)
1027 {
1028 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1029 wp->w_cursor.col = 0;
1030 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +02001031 if (wp->w_cursor.lnum >= wp->w_height)
1032 {
1033 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
1034
1035 if (wp->w_topline < min_topline)
1036 wp->w_topline = min_topline;
1037 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02001038 redraw_win_later(wp, NOT_VALID);
1039 }
1040 }
1041}
1042
1043 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001044set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001045{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001046 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001047 vim_free(term->tl_status_text);
1048 term->tl_status_text = NULL;
1049 if (term->tl_buffer == curbuf)
1050 maketitle();
1051}
1052
1053/*
1054 * Called after the job if finished and Terminal mode is not active:
1055 * Move the vterm contents into the scrollback buffer and free the vterm.
1056 */
1057 static void
1058cleanup_vterm(term_T *term)
1059{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001060 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001061 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001062 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001063 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001064}
1065
1066/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001067 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001068 * Suspends updating the terminal window.
1069 */
1070 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001071term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001072{
1073 term_T *term = curbuf->b_term;
1074
1075 /* Append the current terminal contents to the buffer. */
1076 move_terminal_to_buffer(term);
1077
Bram Moolenaar6d819742017-08-06 14:57:49 +02001078 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001079
Bram Moolenaar6d819742017-08-06 14:57:49 +02001080 /* Move the window cursor to the position of the cursor in the
1081 * terminal. */
1082 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1083 + term->tl_cursor_pos.row + 1;
1084 check_cursor();
1085 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001086
Bram Moolenaar6d819742017-08-06 14:57:49 +02001087 /* Display the same lines as in the terminal. */
1088 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001089}
1090
1091/*
1092 * Returns TRUE if the current window contains a terminal and we are in
1093 * Terminal-Normal mode.
1094 */
1095 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001096term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001097{
1098 term_T *term = curbuf->b_term;
1099
Bram Moolenaar6d819742017-08-06 14:57:49 +02001100 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001101}
1102
1103/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001104 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001105 * Restores updating the terminal window.
1106 */
1107 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001108term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001109{
1110 term_T *term = curbuf->b_term;
1111 sb_line_T *line;
1112 garray_T *gap;
1113
1114 /* Remove the terminal contents from the scrollback and the buffer. */
1115 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001116 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1117 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001118 {
1119 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1120 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1121 vim_free(line->sb_cells);
1122 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001123 }
1124 check_cursor();
1125
Bram Moolenaar6d819742017-08-06 14:57:49 +02001126 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001127
1128 if (term->tl_channel_closed)
1129 cleanup_vterm(term);
1130 redraw_buf_and_status_later(curbuf, NOT_VALID);
1131}
1132
1133/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001134 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001135 * Note: while waiting a terminal may be closed and freed if the channel is
1136 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001137 * TODO: use terminal mode mappings.
1138 */
1139 static int
1140term_vgetc()
1141{
1142 int c;
1143
1144 ++no_mapping;
1145 ++allow_keys;
1146 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001147#ifdef WIN3264
1148 ctrl_break_was_pressed = FALSE;
1149#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001150 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001151 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001152 --no_mapping;
1153 --allow_keys;
1154 return c;
1155}
1156
1157/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001158 * Get the part that is connected to the tty. Normally this is PART_IN, but
1159 * when writing buffer lines to the job it can be another. This makes it
1160 * possible to do "1,5term vim -".
1161 */
1162 static ch_part_T
1163get_tty_part(term_T *term)
1164{
1165#ifdef UNIX
1166 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1167 int i;
1168
1169 for (i = 0; i < 3; ++i)
1170 {
1171 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1172
1173 if (isatty(fd))
1174 return parts[i];
1175 }
1176#endif
1177 return PART_IN;
1178}
1179
1180/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001181 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001182 * Return FAIL when the key needs to be handled in Normal mode.
1183 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001184 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001185 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001186send_keys_to_term(term_T *term, int c, int typed)
1187{
1188 char msg[KEY_BUF_LEN];
1189 size_t len;
1190 static int mouse_was_outside = FALSE;
1191 int dragging_outside = FALSE;
1192
1193 /* Catch keys that need to be handled as in Normal mode. */
1194 switch (c)
1195 {
1196 case NUL:
1197 case K_ZERO:
1198 if (typed)
1199 stuffcharReadbuff(c);
1200 return FAIL;
1201
1202 case K_IGNORE:
1203 return FAIL;
1204
1205 case K_LEFTDRAG:
1206 case K_MIDDLEDRAG:
1207 case K_RIGHTDRAG:
1208 case K_X1DRAG:
1209 case K_X2DRAG:
1210 dragging_outside = mouse_was_outside;
1211 /* FALLTHROUGH */
1212 case K_LEFTMOUSE:
1213 case K_LEFTMOUSE_NM:
1214 case K_LEFTRELEASE:
1215 case K_LEFTRELEASE_NM:
1216 case K_MIDDLEMOUSE:
1217 case K_MIDDLERELEASE:
1218 case K_RIGHTMOUSE:
1219 case K_RIGHTRELEASE:
1220 case K_X1MOUSE:
1221 case K_X1RELEASE:
1222 case K_X2MOUSE:
1223 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001224
1225 case K_MOUSEUP:
1226 case K_MOUSEDOWN:
1227 case K_MOUSELEFT:
1228 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001229 if (mouse_row < W_WINROW(curwin)
1230 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1231 || mouse_col < W_WINCOL(curwin)
1232 || mouse_col >= W_ENDCOL(curwin)
1233 || dragging_outside)
1234 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001235 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001236 if (typed)
1237 {
1238 stuffcharReadbuff(c);
1239 mouse_was_outside = TRUE;
1240 }
1241 return FAIL;
1242 }
1243 }
1244 if (typed)
1245 mouse_was_outside = FALSE;
1246
1247 /* Convert the typed key to a sequence of bytes for the job. */
1248 len = term_convert_key(term, c, msg);
1249 if (len > 0)
1250 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001251 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1252 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001253
1254 return OK;
1255}
1256
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001257 static void
1258position_cursor(win_T *wp, VTermPos *pos)
1259{
1260 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1261 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1262 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1263}
1264
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001265/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001266 * Handle CTRL-W "": send register contents to the job.
1267 */
1268 static void
1269term_paste_register(int prev_c UNUSED)
1270{
1271 int c;
1272 list_T *l;
1273 listitem_T *item;
1274 long reglen = 0;
1275 int type;
1276
1277#ifdef FEAT_CMDL_INFO
1278 if (add_to_showcmd(prev_c))
1279 if (add_to_showcmd('"'))
1280 out_flush();
1281#endif
1282 c = term_vgetc();
1283#ifdef FEAT_CMDL_INFO
1284 clear_showcmd();
1285#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001286 if (!term_use_loop())
1287 /* job finished while waiting for a character */
1288 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001289
1290 /* CTRL-W "= prompt for expression to evaluate. */
1291 if (c == '=' && get_expr_register() != '=')
1292 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001293 if (!term_use_loop())
1294 /* job finished while waiting for a character */
1295 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001296
1297 l = (list_T *)get_reg_contents(c, GREG_LIST);
1298 if (l != NULL)
1299 {
1300 type = get_reg_type(c, &reglen);
1301 for (item = l->lv_first; item != NULL; item = item->li_next)
1302 {
1303 char_u *s = get_tv_string(&item->li_tv);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001304#ifdef WIN3264
1305 char_u *tmp = s;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001306
Bram Moolenaar285f2432017-08-23 23:10:21 +02001307 if (!enc_utf8 && enc_codepage > 0)
1308 {
1309 WCHAR *ret = NULL;
1310 int length = 0;
1311
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001312 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
1313 (int)STRLEN(s), &ret, &length);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001314 if (ret != NULL)
1315 {
1316 WideCharToMultiByte_alloc(CP_UTF8, 0,
1317 ret, length, (char **)&s, &length, 0, 0);
1318 vim_free(ret);
1319 }
1320 }
1321#endif
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001322 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001323 s, (int)STRLEN(s), NULL);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001324#ifdef WIN3264
1325 if (tmp != s)
1326 vim_free(s);
1327#endif
1328
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001329 if (item->li_next != NULL || type == MLINE)
1330 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1331 (char_u *)"\r", 1, NULL);
1332 }
1333 list_free(l);
1334 }
1335}
1336
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001337#if defined(FEAT_GUI) || defined(PROTO)
1338/*
1339 * Return TRUE when the cursor of the terminal should be displayed.
1340 */
1341 int
1342use_terminal_cursor()
1343{
1344 return in_terminal_loop != NULL;
1345}
1346
1347 cursorentry_T *
1348term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1349{
1350 term_T *term = in_terminal_loop;
1351 static cursorentry_T entry;
1352
1353 vim_memset(&entry, 0, sizeof(entry));
1354 entry.shape = entry.mshape =
1355 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1356 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1357 SHAPE_BLOCK;
1358 entry.percentage = 20;
1359 if (term->tl_cursor_blink)
1360 {
1361 entry.blinkwait = 700;
1362 entry.blinkon = 400;
Bram Moolenaar58302322017-08-22 20:33:53 +02001363 entry.blinkoff = 250;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001364 }
1365 *fg = gui.back_pixel;
1366 if (term->tl_cursor_color == NULL)
1367 *bg = gui.norm_pixel;
1368 else
1369 *bg = color_name2handle(term->tl_cursor_color);
1370 entry.name = "n";
1371 entry.used_for = SHAPE_CURSOR;
1372
1373 return &entry;
1374}
1375#endif
1376
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001377static int did_change_cursor = FALSE;
1378
1379 static void
1380may_set_cursor_props(term_T *term)
1381{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001382#ifdef FEAT_GUI
1383 /* For the GUI the cursor properties are obtained with
1384 * term_get_cursor_shape(). */
1385 if (gui.in_use)
1386 return;
1387#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001388 if (in_terminal_loop == term)
1389 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001390 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001391 if (term->tl_cursor_color != NULL)
1392 term_cursor_color(term->tl_cursor_color);
1393 else
1394 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001395 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1396 }
1397}
1398
1399 static void
1400may_restore_cursor_props(void)
1401{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001402#ifdef FEAT_GUI
1403 if (gui.in_use)
1404 return;
1405#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001406 if (did_change_cursor)
1407 {
1408 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001409 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001410 /* this will restore the initial cursor style, if possible */
1411 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001412 }
1413}
1414
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001415/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001416 * Returns TRUE if the current window contains a terminal and we are sending
1417 * keys to the job.
1418 */
1419 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001420term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001421{
1422 term_T *term = curbuf->b_term;
1423
1424 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001425 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001426 && term->tl_vterm != NULL
1427 && term_job_running(term);
1428}
1429
1430/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001431 * Wait for input and send it to the job.
1432 * Return when the start of a CTRL-W command is typed or anything else that
1433 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001434 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1435 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001436 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001437 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001438terminal_loop(void)
1439{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001440 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001441 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001442 int ret;
1443
Bram Moolenaar679653e2017-08-13 14:13:19 +02001444 /* Remember the terminal we are sending keys to. However, the terminal
1445 * might be closed while waiting for a character, e.g. typing "exit" in a
1446 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1447 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001448 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001449
1450 if (*curwin->w_p_tk != NUL)
1451 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001452 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001453 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001454
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001455#ifdef UNIX
1456 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001457 int part = get_tty_part(curbuf->b_term);
1458 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001459
1460 if (isatty(fd))
1461 {
1462 ttyinfo_T info;
1463
1464 /* Get the current backspace and enter characters of the pty. */
1465 if (get_tty_info(fd, &info) == OK)
1466 {
1467 term_backspace_char = info.backspace;
1468 term_enter_char = info.enter;
1469 term_nl_does_cr = info.nl_does_cr;
1470 }
1471 }
1472 }
1473#endif
1474
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001475 for (;;)
1476 {
1477 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001478 /* Repeat redrawing in case a message is received while redrawing. */
1479 while (curwin->w_redr_type != 0)
1480 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001481 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001482
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001483 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001484 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001485 /* job finished while waiting for a character */
1486 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001487 if (c == K_IGNORE)
1488 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001489
Bram Moolenaarfae42832017-08-01 22:24:26 +02001490#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001491 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001492 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001493 if (ctrl_break_was_pressed)
1494 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001495#endif
1496
Bram Moolenaar69198192017-08-05 14:10:48 +02001497 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001498 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001499 int prev_c = c;
1500
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001501#ifdef FEAT_CMDL_INFO
1502 if (add_to_showcmd(c))
1503 out_flush();
1504#endif
1505 c = term_vgetc();
1506#ifdef FEAT_CMDL_INFO
1507 clear_showcmd();
1508#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001509 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001510 /* job finished while waiting for a character */
1511 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001512
Bram Moolenaar69198192017-08-05 14:10:48 +02001513 if (prev_c == Ctrl_BSL)
1514 {
1515 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001516 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001517 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1518 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001519 ret = FAIL;
1520 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001521 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001522 /* Send both keys to the terminal. */
1523 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1524 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001525 else if (c == Ctrl_C)
1526 {
1527 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1528 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1529 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001530 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001531 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001532 /* "CTRL-W .": send CTRL-W to the job */
1533 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001534 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001535 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001536 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001537 /* CTRL-W N : go to Terminal-Normal mode. */
1538 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001539 ret = FAIL;
1540 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001541 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001542 else if (c == '"')
1543 {
1544 term_paste_register(prev_c);
1545 continue;
1546 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001547 else if (termkey == 0 || c != termkey)
1548 {
1549 stuffcharReadbuff(Ctrl_W);
1550 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001551 ret = OK;
1552 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001553 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001554 }
Bram Moolenaar58302322017-08-22 20:33:53 +02001555# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02001556 if (!enc_utf8 && has_mbyte && c >= 0x80)
1557 {
1558 WCHAR wc;
1559 char_u mb[3];
1560
1561 mb[0] = (unsigned)c >> 8;
1562 mb[1] = c;
1563 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1564 c = wc;
1565 }
1566# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001567 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001568 {
1569 ret = OK;
1570 goto theend;
1571 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001572 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001573 ret = FAIL;
1574
1575theend:
1576 in_terminal_loop = NULL;
1577 may_restore_cursor_props();
1578 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001579}
1580
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001581/*
1582 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001583 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001584 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001585 */
1586 void
1587term_job_ended(job_T *job)
1588{
1589 term_T *term;
1590 int did_one = FALSE;
1591
1592 for (term = first_term; term != NULL; term = term->tl_next)
1593 if (term->tl_job == job)
1594 {
1595 vim_free(term->tl_title);
1596 term->tl_title = NULL;
1597 vim_free(term->tl_status_text);
1598 term->tl_status_text = NULL;
1599 redraw_buf_and_status_later(term->tl_buffer, VALID);
1600 did_one = TRUE;
1601 }
1602 if (did_one)
1603 redraw_statuslines();
1604 if (curbuf->b_term != NULL)
1605 {
1606 if (curbuf->b_term->tl_job == job)
1607 maketitle();
1608 update_cursor(curbuf->b_term, TRUE);
1609 }
1610}
1611
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001612 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001613may_toggle_cursor(term_T *term)
1614{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001615 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001616 {
1617 if (term->tl_cursor_visible)
1618 cursor_on();
1619 else
1620 cursor_off();
1621 }
1622}
1623
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001624/*
1625 * Reverse engineer the RGB value into a cterm color index.
1626 * First color is 1. Return 0 if no match found.
1627 */
1628 static int
1629color2index(VTermColor *color, int fg, int *boldp)
1630{
1631 int red = color->red;
1632 int blue = color->blue;
1633 int green = color->green;
1634
1635 /* The argument for lookup_color() is for the color_names[] table. */
1636 if (red == 0)
1637 {
1638 if (green == 0)
1639 {
1640 if (blue == 0)
1641 return lookup_color(0, fg, boldp) + 1; /* black */
1642 if (blue == 224)
1643 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1644 }
1645 else if (green == 224)
1646 {
1647 if (blue == 0)
1648 return lookup_color(2, fg, boldp) + 1; /* dark green */
1649 if (blue == 224)
1650 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1651 }
1652 }
1653 else if (red == 224)
1654 {
1655 if (green == 0)
1656 {
1657 if (blue == 0)
1658 return lookup_color(4, fg, boldp) + 1; /* dark red */
1659 if (blue == 224)
1660 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1661 }
1662 else if (green == 224)
1663 {
1664 if (blue == 0)
1665 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1666 if (blue == 224)
1667 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1668 }
1669 }
1670 else if (red == 128)
1671 {
1672 if (green == 128 && blue == 128)
1673 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1674 }
1675 else if (red == 255)
1676 {
1677 if (green == 64)
1678 {
1679 if (blue == 64)
1680 return lookup_color(20, fg, boldp) + 1; /* light red */
1681 if (blue == 255)
1682 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1683 }
1684 else if (green == 255)
1685 {
1686 if (blue == 64)
1687 return lookup_color(24, fg, boldp) + 1; /* yellow */
1688 if (blue == 255)
1689 return lookup_color(26, fg, boldp) + 1; /* white */
1690 }
1691 }
1692 else if (red == 64)
1693 {
1694 if (green == 64)
1695 {
1696 if (blue == 255)
1697 return lookup_color(14, fg, boldp) + 1; /* light blue */
1698 }
1699 else if (green == 255)
1700 {
1701 if (blue == 64)
1702 return lookup_color(16, fg, boldp) + 1; /* light green */
1703 if (blue == 255)
1704 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1705 }
1706 }
1707 if (t_colors >= 256)
1708 {
1709 if (red == blue && red == green)
1710 {
1711 /* 24-color greyscale */
1712 static int cutoff[23] = {
1713 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1714 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1715 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1716 int i;
1717
1718 for (i = 0; i < 23; ++i)
1719 if (red < cutoff[i])
1720 return i + 233;
1721 return 256;
1722 }
1723
1724 /* 216-color cube */
1725 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001726 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001727 + (blue + 25) / 0x33;
1728 }
1729 return 0;
1730}
1731
1732/*
1733 * Convert the attributes of a vterm cell into an attribute index.
1734 */
1735 static int
1736cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1737{
1738 int attr = 0;
1739
1740 if (cellattrs.bold)
1741 attr |= HL_BOLD;
1742 if (cellattrs.underline)
1743 attr |= HL_UNDERLINE;
1744 if (cellattrs.italic)
1745 attr |= HL_ITALIC;
1746 if (cellattrs.strike)
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001747 attr |= HL_STRIKETHROUGH;
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001748 if (cellattrs.reverse)
1749 attr |= HL_INVERSE;
1750
1751#ifdef FEAT_GUI
1752 if (gui.in_use)
1753 {
1754 guicolor_T fg, bg;
1755
1756 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1757 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1758 return get_gui_attr_idx(attr, fg, bg);
1759 }
1760 else
1761#endif
1762#ifdef FEAT_TERMGUICOLORS
1763 if (p_tgc)
1764 {
1765 guicolor_T fg, bg;
1766
1767 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1768 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1769
1770 return get_tgc_attr_idx(attr, fg, bg);
1771 }
1772 else
1773#endif
1774 {
1775 int bold = MAYBE;
1776 int fg = color2index(&cellfg, TRUE, &bold);
1777 int bg = color2index(&cellbg, FALSE, &bold);
1778
1779 /* with 8 colors set the bold attribute to get a bright foreground */
1780 if (bold == TRUE)
1781 attr |= HL_BOLD;
1782 return get_cterm_attr_idx(attr, fg, bg);
1783 }
1784 return 0;
1785}
1786
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001787 static int
1788handle_damage(VTermRect rect, void *user)
1789{
1790 term_T *term = (term_T *)user;
1791
1792 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1793 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1794 redraw_buf_later(term->tl_buffer, NOT_VALID);
1795 return 1;
1796}
1797
1798 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001799handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001800{
1801 term_T *term = (term_T *)user;
1802
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001803 /* Scrolling up is done much more efficiently by deleting lines instead of
1804 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001805 if (dest.start_col == src.start_col
1806 && dest.end_col == src.end_col
1807 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001808 {
1809 win_T *wp;
1810 VTermColor fg, bg;
1811 VTermScreenCellAttrs attr;
1812 int clear_attr;
1813
1814 /* Set the color to clear lines with. */
1815 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1816 &fg, &bg);
1817 vim_memset(&attr, 0, sizeof(attr));
1818 clear_attr = cell2attr(attr, fg, bg);
1819
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001820 FOR_ALL_WINDOWS(wp)
1821 {
1822 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001823 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001824 src.start_row - dest.start_row, FALSE, FALSE,
1825 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001826 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001827 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001828 redraw_buf_later(term->tl_buffer, NOT_VALID);
1829 return 1;
1830}
1831
1832 static int
1833handle_movecursor(
1834 VTermPos pos,
1835 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001836 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001837 void *user)
1838{
1839 term_T *term = (term_T *)user;
1840 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001841
1842 term->tl_cursor_pos = pos;
1843 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001844
1845 FOR_ALL_WINDOWS(wp)
1846 {
1847 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001848 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001849 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001850 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001851 {
1852 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001853 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001854 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001855
1856 return 1;
1857}
1858
Bram Moolenaar21554412017-07-24 21:44:43 +02001859 static int
1860handle_settermprop(
1861 VTermProp prop,
1862 VTermValue *value,
1863 void *user)
1864{
1865 term_T *term = (term_T *)user;
1866
1867 switch (prop)
1868 {
1869 case VTERM_PROP_TITLE:
1870 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001871 /* a blank title isn't useful, make it empty, so that "running" is
1872 * displayed */
1873 if (*skipwhite((char_u *)value->string) == NUL)
1874 term->tl_title = NULL;
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001875#ifdef WIN3264
1876 else if (!enc_utf8 && enc_codepage > 0)
1877 {
1878 WCHAR *ret = NULL;
1879 int length = 0;
1880
1881 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001882 (char*)value->string, (int)STRLEN(value->string),
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001883 &ret, &length);
1884 if (ret != NULL)
1885 {
1886 WideCharToMultiByte_alloc(enc_codepage, 0,
1887 ret, length, (char**)&term->tl_title,
1888 &length, 0, 0);
1889 vim_free(ret);
1890 }
1891 }
1892#endif
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001893 else
1894 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001895 vim_free(term->tl_status_text);
1896 term->tl_status_text = NULL;
1897 if (term == curbuf->b_term)
1898 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001899 break;
1900
1901 case VTERM_PROP_CURSORVISIBLE:
1902 term->tl_cursor_visible = value->boolean;
1903 may_toggle_cursor(term);
1904 out_flush();
1905 break;
1906
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001907 case VTERM_PROP_CURSORBLINK:
1908 term->tl_cursor_blink = value->boolean;
1909 may_set_cursor_props(term);
1910 break;
1911
1912 case VTERM_PROP_CURSORSHAPE:
1913 term->tl_cursor_shape = value->number;
1914 may_set_cursor_props(term);
1915 break;
1916
1917 case VTERM_PROP_CURSORCOLOR:
1918 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001919 if (*value->string == NUL)
1920 term->tl_cursor_color = NULL;
1921 else
1922 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001923 may_set_cursor_props(term);
1924 break;
1925
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001926 case VTERM_PROP_ALTSCREEN:
1927 /* TODO: do anything else? */
1928 term->tl_using_altscreen = value->boolean;
1929 break;
1930
Bram Moolenaar21554412017-07-24 21:44:43 +02001931 default:
1932 break;
1933 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001934 /* Always return 1, otherwise vterm doesn't store the value internally. */
1935 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001936}
1937
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001938/*
1939 * The job running in the terminal resized the terminal.
1940 */
1941 static int
1942handle_resize(int rows, int cols, void *user)
1943{
1944 term_T *term = (term_T *)user;
1945 win_T *wp;
1946
1947 term->tl_rows = rows;
1948 term->tl_cols = cols;
1949 FOR_ALL_WINDOWS(wp)
1950 {
1951 if (wp->w_buffer == term->tl_buffer)
1952 {
1953 win_setheight_win(rows, wp);
1954 win_setwidth_win(cols, wp);
1955 }
1956 }
1957
1958 redraw_buf_later(term->tl_buffer, NOT_VALID);
1959 return 1;
1960}
1961
Bram Moolenaard85f2712017-07-28 21:51:57 +02001962/*
1963 * Handle a line that is pushed off the top of the screen.
1964 */
1965 static int
1966handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1967{
1968 term_T *term = (term_T *)user;
1969
1970 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001971 if (ga_grow(&term->tl_scrollback, 1) == OK)
1972 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001973 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001974 int len = 0;
1975 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001976 int c;
1977 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001978 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001979 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001980
1981 /* do not store empty cells at the end */
1982 for (i = 0; i < cols; ++i)
1983 if (cells[i].chars[0] != 0)
1984 len = i + 1;
1985
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001986 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001987 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001988 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001989 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001990 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001991 for (col = 0; col < len; col += cells[col].width)
1992 {
1993 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1994 {
1995 ga.ga_len = 0;
1996 break;
1997 }
1998 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +02001999 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002000 (char_u *)ga.ga_data + ga.ga_len);
2001 p[col].width = cells[col].width;
2002 p[col].attrs = cells[col].attrs;
2003 p[col].fg = cells[col].fg;
2004 p[col].bg = cells[col].bg;
2005 }
2006 }
2007 if (ga_grow(&ga, 1) == FAIL)
2008 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
2009 else
2010 {
2011 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
2012 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
2013 }
2014 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02002015
2016 line = (sb_line_T *)term->tl_scrollback.ga_data
2017 + term->tl_scrollback.ga_len;
2018 line->sb_cols = len;
2019 line->sb_cells = p;
2020 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002021 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002022 }
2023 return 0; /* ignored */
2024}
2025
Bram Moolenaar21554412017-07-24 21:44:43 +02002026static VTermScreenCallbacks screen_callbacks = {
2027 handle_damage, /* damage */
2028 handle_moverect, /* moverect */
2029 handle_movecursor, /* movecursor */
2030 handle_settermprop, /* settermprop */
2031 NULL, /* bell */
2032 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002033 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02002034 NULL /* sb_popline */
2035};
2036
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002037/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02002038 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002039 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02002040 */
2041 void
2042term_channel_closed(channel_T *ch)
2043{
2044 term_T *term;
2045 int did_one = FALSE;
2046
2047 for (term = first_term; term != NULL; term = term->tl_next)
2048 if (term->tl_job == ch->ch_job)
2049 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002050 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002051 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002052
Bram Moolenaard85f2712017-07-28 21:51:57 +02002053 vim_free(term->tl_title);
2054 term->tl_title = NULL;
2055 vim_free(term->tl_status_text);
2056 term->tl_status_text = NULL;
2057
Bram Moolenaar423802d2017-07-30 16:52:24 +02002058 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02002059 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002060 {
2061 int fnum = term->tl_buffer->b_fnum;
2062
Bram Moolenaar423802d2017-07-30 16:52:24 +02002063 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002064
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002065 if (term->tl_finish == 'c')
2066 {
2067 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002068 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002069 curbuf = term->tl_buffer;
2070 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
2071 break;
2072 }
2073 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
2074 {
2075 char buf[50];
2076
2077 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002078 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02002079 vim_snprintf(buf, sizeof(buf),
2080 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02002081 ? "botright sbuf %d"
2082 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002083 do_cmdline_cmd((char_u *)buf);
2084 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002085 else
2086 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002087 }
2088
Bram Moolenaard85f2712017-07-28 21:51:57 +02002089 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002090 }
2091 if (did_one)
2092 {
2093 redraw_statuslines();
2094
2095 /* Need to break out of vgetc(). */
2096 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002097 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002098
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002099 term = curbuf->b_term;
2100 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002101 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002102 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002103 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002104 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002105 }
2106 }
2107}
2108
2109/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002110 * Called to update a window that contains an active terminal.
2111 * Returns FAIL when there is no terminal running in this window or in
2112 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002113 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002114 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002115term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002116{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002117 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002118 VTerm *vterm;
2119 VTermScreen *screen;
2120 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002121 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002122
Bram Moolenaar6d819742017-08-06 14:57:49 +02002123 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002124 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002125
Bram Moolenaard85f2712017-07-28 21:51:57 +02002126 vterm = term->tl_vterm;
2127 screen = vterm_obtain_screen(vterm);
2128 state = vterm_obtain_state(vterm);
2129
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002130 /*
2131 * If the window was resized a redraw will be triggered and we get here.
2132 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2133 */
2134 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2135 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002136 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002137 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2138 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2139 win_T *twp;
2140
2141 FOR_ALL_WINDOWS(twp)
2142 {
2143 /* When more than one window shows the same terminal, use the
2144 * smallest size. */
2145 if (twp->w_buffer == term->tl_buffer)
2146 {
2147 if (!term->tl_rows_fixed && rows > twp->w_height)
2148 rows = twp->w_height;
2149 if (!term->tl_cols_fixed && cols > twp->w_width)
2150 cols = twp->w_width;
2151 }
2152 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002153
2154 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002155 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002156 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002157 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002158 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002159
2160 /* The cursor may have been moved when resizing. */
2161 vterm_state_get_cursorpos(state, &pos);
2162 position_cursor(wp, &pos);
2163
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002164 /* TODO: Only redraw what changed. */
2165 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002166 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002167 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002168 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002169
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002170 if (pos.row < term->tl_rows)
2171 {
2172 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002173 {
2174 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002175 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002176
Bram Moolenaareeac6772017-07-23 15:48:37 +02002177 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2178 vim_memset(&cell, 0, sizeof(cell));
2179
2180 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002181 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002182 if (c == NUL)
2183 {
2184 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002185 if (enc_utf8)
2186 ScreenLinesUC[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002187 }
2188 else
2189 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002190 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002191 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002192 if (c >= 0x80)
2193 {
2194 ScreenLines[off] = ' ';
2195 ScreenLinesUC[off] = c;
2196 }
2197 else
2198 {
2199 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002200 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002201 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002202 }
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002203#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02002204 else if (has_mbyte && c >= 0x80)
2205 {
2206 char_u mb[MB_MAXBYTES+1];
2207 WCHAR wc = c;
2208
2209 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2210 (char*)mb, 2, 0, 0) > 1)
2211 {
2212 ScreenLines[off] = mb[0];
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002213 ScreenLines[off + 1] = mb[1];
Bram Moolenaar740c4332017-08-21 22:01:27 +02002214 cell.width = mb_ptr2cells(mb);
2215 }
2216 else
2217 ScreenLines[off] = c;
2218 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002219#endif
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002220 else
Bram Moolenaar740c4332017-08-21 22:01:27 +02002221 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002222 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002223 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002224
2225 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002226 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002227 if (cell.width == 2)
2228 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002229 if (enc_utf8)
2230 ScreenLinesUC[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002231
2232 /* don't set the second byte to NUL for a DBCS encoding, it
2233 * has been set above */
2234 if (enc_utf8 || !has_mbyte)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002235 ScreenLines[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002236
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002237 ++pos.col;
2238 ++off;
2239 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002240 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002241 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002242 else
2243 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002244
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002245 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2246 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002247 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002248
2249 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002250}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002251
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002252/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002253 * Return TRUE if "wp" is a terminal window where the job has finished.
2254 */
2255 int
2256term_is_finished(buf_T *buf)
2257{
2258 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2259}
2260
2261/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002262 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002263 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002264 */
2265 int
2266term_show_buffer(buf_T *buf)
2267{
2268 term_T *term = buf->b_term;
2269
Bram Moolenaar6d819742017-08-06 14:57:49 +02002270 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002271}
2272
2273/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002274 * The current buffer is going to be changed. If there is terminal
2275 * highlighting remove it now.
2276 */
2277 void
2278term_change_in_curbuf(void)
2279{
2280 term_T *term = curbuf->b_term;
2281
2282 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2283 {
2284 free_scrollback(term);
2285 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002286
2287 /* The buffer is now like a normal buffer, it cannot be easily
2288 * abandoned when changed. */
2289 set_string_option_direct((char_u *)"buftype", -1,
2290 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002291 }
2292}
2293
2294/*
2295 * Get the screen attribute for a position in the buffer.
2296 */
2297 int
2298term_get_attr(buf_T *buf, linenr_T lnum, int col)
2299{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002300 term_T *term = buf->b_term;
2301 sb_line_T *line;
2302 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002303
Bram Moolenaar70229f92017-07-29 16:01:53 +02002304 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002305 return 0;
2306 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2307 if (col >= line->sb_cols)
2308 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002309 cellattr = line->sb_cells + col;
2310 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002311}
2312
2313/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002314 * Create a new vterm and initialize it.
2315 */
2316 static void
2317create_vterm(term_T *term, int rows, int cols)
2318{
2319 VTerm *vterm;
2320 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002321 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002322
2323 vterm = vterm_new(rows, cols);
2324 term->tl_vterm = vterm;
2325 screen = vterm_obtain_screen(vterm);
2326 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2327 /* TODO: depends on 'encoding'. */
2328 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002329
2330 /* Vterm uses a default black background. Set it to white when
2331 * 'background' is "light". */
2332 if (*p_bg == 'l')
2333 {
2334 VTermColor fg, bg;
2335
2336 fg.red = fg.green = fg.blue = 0;
2337 bg.red = bg.green = bg.blue = 255;
2338 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2339 }
2340
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002341 /* Required to initialize most things. */
2342 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002343
2344 /* Allow using alternate screen. */
2345 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002346
Bram Moolenaar58302322017-08-22 20:33:53 +02002347 /* For unix do not use a blinking cursor. In an xterm this causes the
2348 * cursor to blink if it's blinking in the xterm.
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002349 * For Windows we respect the system wide setting. */
Bram Moolenaar58302322017-08-22 20:33:53 +02002350#ifdef WIN3264
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002351 if (GetCaretBlinkTime() == INFINITE)
2352 value.boolean = 0;
2353 else
2354 value.boolean = 1;
Bram Moolenaar58302322017-08-22 20:33:53 +02002355#else
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002356 value.boolean = 0;
Bram Moolenaar58302322017-08-22 20:33:53 +02002357#endif
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002358 vterm_state_set_termprop(vterm_obtain_state(vterm),
2359 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002360}
2361
Bram Moolenaar21554412017-07-24 21:44:43 +02002362/*
2363 * Return the text to show for the buffer name and status.
2364 */
2365 char_u *
2366term_get_status_text(term_T *term)
2367{
2368 if (term->tl_status_text == NULL)
2369 {
2370 char_u *txt;
2371 size_t len;
2372
Bram Moolenaar6d819742017-08-06 14:57:49 +02002373 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002374 {
2375 if (term_job_running(term))
2376 txt = (char_u *)_("Terminal");
2377 else
2378 txt = (char_u *)_("Terminal-finished");
2379 }
2380 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002381 txt = term->tl_title;
2382 else if (term_job_running(term))
2383 txt = (char_u *)_("running");
2384 else
2385 txt = (char_u *)_("finished");
2386 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002387 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002388 if (term->tl_status_text != NULL)
2389 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2390 term->tl_buffer->b_fname, txt);
2391 }
2392 return term->tl_status_text;
2393}
2394
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002395/*
2396 * Mark references in jobs of terminals.
2397 */
2398 int
2399set_ref_in_term(int copyID)
2400{
2401 int abort = FALSE;
2402 term_T *term;
2403 typval_T tv;
2404
2405 for (term = first_term; term != NULL; term = term->tl_next)
2406 if (term->tl_job != NULL)
2407 {
2408 tv.v_type = VAR_JOB;
2409 tv.vval.v_job = term->tl_job;
2410 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2411 }
2412 return abort;
2413}
2414
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002415/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002416 * Get the buffer from the first argument in "argvars".
2417 * Returns NULL when the buffer is not for a terminal window.
2418 */
2419 static buf_T *
2420term_get_buf(typval_T *argvars)
2421{
2422 buf_T *buf;
2423
2424 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2425 ++emsg_off;
2426 buf = get_buf_tv(&argvars[0], FALSE);
2427 --emsg_off;
2428 if (buf == NULL || buf->b_term == NULL)
2429 return NULL;
2430 return buf;
2431}
2432
2433/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002434 * "term_getaltscreen(buf)" function
2435 */
2436 void
2437f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2438{
2439 buf_T *buf = term_get_buf(argvars);
2440
2441 if (buf == NULL)
2442 return;
2443 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2444}
2445
2446/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002447 * "term_getattr(attr, name)" function
2448 */
2449 void
2450f_term_getattr(typval_T *argvars, typval_T *rettv)
2451{
2452 int attr;
2453 size_t i;
2454 char_u *name;
2455
2456 static struct {
2457 char *name;
2458 int attr;
2459 } attrs[] = {
2460 {"bold", HL_BOLD},
2461 {"italic", HL_ITALIC},
2462 {"underline", HL_UNDERLINE},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002463 {"strike", HL_STRIKETHROUGH},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002464 {"reverse", HL_INVERSE},
2465 };
2466
2467 attr = get_tv_number(&argvars[0]);
2468 name = get_tv_string_chk(&argvars[1]);
2469 if (name == NULL)
2470 return;
2471
2472 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2473 if (STRCMP(name, attrs[i].name) == 0)
2474 {
2475 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2476 break;
2477 }
2478}
2479
2480/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002481 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002482 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002483 void
2484f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002485{
Bram Moolenaar97870002017-07-30 18:28:38 +02002486 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002487 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002488 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002489 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002490
Bram Moolenaar97870002017-07-30 18:28:38 +02002491 if (rettv_list_alloc(rettv) == FAIL)
2492 return;
2493 if (buf == NULL)
2494 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002495 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002496
2497 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002498 list_append_number(l, term->tl_cursor_pos.row + 1);
2499 list_append_number(l, term->tl_cursor_pos.col + 1);
2500
2501 d = dict_alloc();
2502 if (d != NULL)
2503 {
2504 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
Bram Moolenaar4db25542017-08-28 22:43:05 +02002505 dict_add_nr_str(d, "blink", blink_state_is_inverted()
2506 ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002507 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2508 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2509 ? (char_u *)"" : term->tl_cursor_color);
2510 list_append_dict(l, d);
2511 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002512}
2513
2514/*
2515 * "term_getjob(buf)" function
2516 */
2517 void
2518f_term_getjob(typval_T *argvars, typval_T *rettv)
2519{
2520 buf_T *buf = term_get_buf(argvars);
2521
2522 rettv->v_type = VAR_JOB;
2523 rettv->vval.v_job = NULL;
2524 if (buf == NULL)
2525 return;
2526
2527 rettv->vval.v_job = buf->b_term->tl_job;
2528 if (rettv->vval.v_job != NULL)
2529 ++rettv->vval.v_job->jv_refcount;
2530}
2531
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002532 static int
2533get_row_number(typval_T *tv, term_T *term)
2534{
2535 if (tv->v_type == VAR_STRING
2536 && tv->vval.v_string != NULL
2537 && STRCMP(tv->vval.v_string, ".") == 0)
2538 return term->tl_cursor_pos.row;
2539 return (int)get_tv_number(tv) - 1;
2540}
2541
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002542/*
2543 * "term_getline(buf, row)" function
2544 */
2545 void
2546f_term_getline(typval_T *argvars, typval_T *rettv)
2547{
2548 buf_T *buf = term_get_buf(argvars);
2549 term_T *term;
2550 int row;
2551
2552 rettv->v_type = VAR_STRING;
2553 if (buf == NULL)
2554 return;
2555 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002556 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002557
2558 if (term->tl_vterm == NULL)
2559 {
2560 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2561
2562 /* vterm is finished, get the text from the buffer */
2563 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2564 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2565 }
2566 else
2567 {
2568 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2569 VTermRect rect;
2570 int len;
2571 char_u *p;
2572
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002573 if (row < 0 || row >= term->tl_rows)
2574 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002575 len = term->tl_cols * MB_MAXBYTES + 1;
2576 p = alloc(len);
2577 if (p == NULL)
2578 return;
2579 rettv->vval.v_string = p;
2580
2581 rect.start_col = 0;
2582 rect.end_col = term->tl_cols;
2583 rect.start_row = row;
2584 rect.end_row = row + 1;
2585 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2586 }
2587}
2588
2589/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002590 * "term_getscrolled(buf)" function
2591 */
2592 void
2593f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2594{
2595 buf_T *buf = term_get_buf(argvars);
2596
2597 if (buf == NULL)
2598 return;
2599 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2600}
2601
2602/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002603 * "term_getsize(buf)" function
2604 */
2605 void
2606f_term_getsize(typval_T *argvars, typval_T *rettv)
2607{
2608 buf_T *buf = term_get_buf(argvars);
2609 list_T *l;
2610
2611 if (rettv_list_alloc(rettv) == FAIL)
2612 return;
2613 if (buf == NULL)
2614 return;
2615
2616 l = rettv->vval.v_list;
2617 list_append_number(l, buf->b_term->tl_rows);
2618 list_append_number(l, buf->b_term->tl_cols);
2619}
2620
2621/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002622 * "term_getstatus(buf)" function
2623 */
2624 void
2625f_term_getstatus(typval_T *argvars, typval_T *rettv)
2626{
2627 buf_T *buf = term_get_buf(argvars);
2628 term_T *term;
2629 char_u val[100];
2630
2631 rettv->v_type = VAR_STRING;
2632 if (buf == NULL)
2633 return;
2634 term = buf->b_term;
2635
2636 if (term_job_running(term))
2637 STRCPY(val, "running");
2638 else
2639 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002640 if (term->tl_normal_mode)
2641 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002642 rettv->vval.v_string = vim_strsave(val);
2643}
2644
2645/*
2646 * "term_gettitle(buf)" function
2647 */
2648 void
2649f_term_gettitle(typval_T *argvars, typval_T *rettv)
2650{
2651 buf_T *buf = term_get_buf(argvars);
2652
2653 rettv->v_type = VAR_STRING;
2654 if (buf == NULL)
2655 return;
2656
2657 if (buf->b_term->tl_title != NULL)
2658 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2659}
2660
2661/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002662 * "term_gettty(buf)" function
2663 */
2664 void
2665f_term_gettty(typval_T *argvars, typval_T *rettv)
2666{
2667 buf_T *buf = term_get_buf(argvars);
2668 char_u *p;
2669
2670 rettv->v_type = VAR_STRING;
2671 if (buf == NULL)
2672 return;
2673 if (buf->b_term->tl_job != NULL)
2674 p = buf->b_term->tl_job->jv_tty_name;
2675 else
2676 p = buf->b_term->tl_tty_name;
2677 if (p != NULL)
2678 rettv->vval.v_string = vim_strsave(p);
2679}
2680
2681/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002682 * "term_list()" function
2683 */
2684 void
2685f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2686{
2687 term_T *tp;
2688 list_T *l;
2689
2690 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2691 return;
2692
2693 l = rettv->vval.v_list;
2694 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2695 if (tp != NULL && tp->tl_buffer != NULL)
2696 if (list_append_number(l,
2697 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2698 return;
2699}
2700
2701/*
2702 * "term_scrape(buf, row)" function
2703 */
2704 void
2705f_term_scrape(typval_T *argvars, typval_T *rettv)
2706{
2707 buf_T *buf = term_get_buf(argvars);
2708 VTermScreen *screen = NULL;
2709 VTermPos pos;
2710 list_T *l;
2711 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002712 char_u *p;
2713 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002714
2715 if (rettv_list_alloc(rettv) == FAIL)
2716 return;
2717 if (buf == NULL)
2718 return;
2719 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002720
2721 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002722 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002723
2724 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002725 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002726 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002727 p = NULL;
2728 line = NULL;
2729 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002730 else
2731 {
2732 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2733
2734 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2735 return;
2736 p = ml_get_buf(buf, lnum + 1, FALSE);
2737 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2738 }
2739
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002740 for (pos.col = 0; pos.col < term->tl_cols; )
2741 {
2742 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002743 int width;
2744 VTermScreenCellAttrs attrs;
2745 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002746 char_u rgb[8];
2747 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2748 int off = 0;
2749 int i;
2750
2751 if (screen == NULL)
2752 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002753 cellattr_T *cellattr;
2754 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002755
2756 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002757 if (pos.col >= line->sb_cols)
2758 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002759 cellattr = line->sb_cells + pos.col;
2760 width = cellattr->width;
2761 attrs = cellattr->attrs;
2762 fg = cellattr->fg;
2763 bg = cellattr->bg;
2764 len = MB_PTR2LEN(p);
2765 mch_memmove(mbs, p, len);
2766 mbs[len] = NUL;
2767 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002768 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002769 else
2770 {
2771 VTermScreenCell cell;
2772 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2773 break;
2774 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2775 {
2776 if (cell.chars[i] == 0)
2777 break;
2778 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2779 }
2780 mbs[off] = NUL;
2781 width = cell.width;
2782 attrs = cell.attrs;
2783 fg = cell.fg;
2784 bg = cell.bg;
2785 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002786 dcell = dict_alloc();
2787 list_append_dict(l, dcell);
2788
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002789 dict_add_nr_str(dcell, "chars", 0, mbs);
2790
2791 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002792 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002793 dict_add_nr_str(dcell, "fg", 0, rgb);
2794 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002795 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002796 dict_add_nr_str(dcell, "bg", 0, rgb);
2797
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002798 dict_add_nr_str(dcell, "attr",
2799 cell2attr(attrs, fg, bg), NULL);
2800 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002801
2802 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002803 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002804 ++pos.col;
2805 }
2806}
2807
2808/*
2809 * "term_sendkeys(buf, keys)" function
2810 */
2811 void
2812f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2813{
2814 buf_T *buf = term_get_buf(argvars);
2815 char_u *msg;
2816 term_T *term;
2817
2818 rettv->v_type = VAR_UNKNOWN;
2819 if (buf == NULL)
2820 return;
2821
2822 msg = get_tv_string_chk(&argvars[1]);
2823 if (msg == NULL)
2824 return;
2825 term = buf->b_term;
2826 if (term->tl_vterm == NULL)
2827 return;
2828
2829 while (*msg != NUL)
2830 {
2831 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2832 msg += MB_PTR2LEN(msg);
2833 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002834}
2835
2836/*
2837 * "term_start(command, options)" function
2838 */
2839 void
2840f_term_start(typval_T *argvars, typval_T *rettv)
2841{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002842 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002843
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002844 init_job_options(&opt);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002845 if (argvars[1].v_type != VAR_UNKNOWN
2846 && get_job_options(&argvars[1], &opt,
2847 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02002848 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002849 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002850 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar3346cc42017-09-02 14:54:21 +02002851 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002852 return;
2853
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002854 if (opt.jo_vertical)
2855 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002856 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002857
2858 if (curbuf->b_term != NULL)
2859 rettv->vval.v_number = curbuf->b_fnum;
2860}
2861
2862/*
2863 * "term_wait" function
2864 */
2865 void
2866f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2867{
2868 buf_T *buf = term_get_buf(argvars);
2869
2870 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002871 {
2872 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002873 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002874 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002875 if (buf->b_term->tl_job == NULL)
2876 {
2877 ch_log(NULL, "term_wait(): no job to wait for");
2878 return;
2879 }
Bram Moolenaar13ebb032017-08-26 22:02:51 +02002880 if (buf->b_term->tl_job->jv_channel == NULL)
2881 /* channel is closed, nothing to do */
2882 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002883
2884 /* Get the job status, this will detect a job that finished. */
Bram Moolenaar13ebb032017-08-26 22:02:51 +02002885 if ((buf->b_term->tl_job->jv_channel == NULL
2886 || !buf->b_term->tl_job->jv_channel->ch_keep_open)
2887 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002888 {
2889 /* The job is dead, keep reading channel I/O until the channel is
Bram Moolenaar28550b72017-09-05 23:31:01 +02002890 * closed. buf->b_term may become NULL if the terminal was closed while
2891 * waiting. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002892 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002893 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2894 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002895 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002896 parse_queued_messages();
2897 ui_delay(10L, FALSE);
2898 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002899 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002900 parse_queued_messages();
2901 }
2902 else
2903 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002904 long wait = 10L;
2905
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002906 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002907 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002908
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002909 /* Wait for some time for any channel I/O. */
2910 if (argvars[1].v_type != VAR_UNKNOWN)
2911 wait = get_tv_number(&argvars[1]);
2912 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002913 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002914
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002915 /* Flushing messages on channels is hopefully sufficient.
2916 * TODO: is there a better way? */
2917 parse_queued_messages();
2918 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002919}
2920
Bram Moolenaardada6d22017-09-02 17:18:35 +02002921/*
2922 * Called when a channel has sent all the lines to a terminal.
2923 * Send a CTRL-D to mark the end of the text.
2924 */
2925 void
2926term_send_eof(channel_T *ch)
2927{
2928 term_T *term;
2929
2930 for (term = first_term; term != NULL; term = term->tl_next)
2931 if (term->tl_job == ch->ch_job)
2932 {
2933 if (term->tl_eof_chars != NULL)
2934 {
2935 channel_send(ch, PART_IN, term->tl_eof_chars,
2936 (int)STRLEN(term->tl_eof_chars), NULL);
2937 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
2938 }
2939# ifdef WIN3264
2940 else
2941 /* Default: CTRL-D */
2942 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
2943# endif
2944 }
2945}
2946
Bram Moolenaara83e3962017-08-17 14:39:07 +02002947# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002948
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002949/**************************************
2950 * 2. MS-Windows implementation.
2951 */
2952
Bram Moolenaara83e3962017-08-17 14:39:07 +02002953# ifndef PROTO
2954
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002955#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2956#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2957
Bram Moolenaar8a773062017-07-24 22:29:21 +02002958void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002959void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002960void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002961BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2962void (*winpty_config_set_initial_size)(void*, int, int);
2963LPCWSTR (*winpty_conin_name)(void*);
2964LPCWSTR (*winpty_conout_name)(void*);
2965LPCWSTR (*winpty_conerr_name)(void*);
2966void (*winpty_free)(void*);
2967void (*winpty_config_free)(void*);
2968void (*winpty_spawn_config_free)(void*);
2969void (*winpty_error_free)(void*);
2970LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002971BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002972HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002973
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002974#define WINPTY_DLL "winpty.dll"
2975
2976static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002977# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002978
Bram Moolenaara83e3962017-08-17 14:39:07 +02002979 static int
2980dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002981{
2982 int i;
2983 static struct
2984 {
2985 char *name;
2986 FARPROC *ptr;
2987 } winpty_entry[] =
2988 {
2989 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2990 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2991 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2992 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2993 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2994 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2995 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2996 {"winpty_free", (FARPROC*)&winpty_free},
2997 {"winpty_open", (FARPROC*)&winpty_open},
2998 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2999 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
3000 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
3001 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003002 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003003 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003004 {NULL, NULL}
3005 };
3006
3007 /* No need to initialize twice. */
3008 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02003009 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02003010 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
3011 * winpty.dll. */
3012 if (*p_winptydll != NUL)
3013 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
3014 if (!hWinPtyDLL)
3015 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003016 if (!hWinPtyDLL)
3017 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02003018 if (verbose)
3019 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
3020 : (char_u *)WINPTY_DLL);
3021 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003022 }
3023 for (i = 0; winpty_entry[i].name != NULL
3024 && winpty_entry[i].ptr != NULL; ++i)
3025 {
3026 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
3027 winpty_entry[i].name)) == NULL)
3028 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02003029 if (verbose)
3030 EMSG2(_(e_loadfunc), winpty_entry[i].name);
3031 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003032 }
3033 }
3034
Bram Moolenaara83e3962017-08-17 14:39:07 +02003035 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003036}
3037
3038/*
3039 * Create a new terminal of "rows" by "cols" cells.
3040 * Store a reference in "term".
3041 * Return OK or FAIL.
3042 */
3043 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003044term_and_job_init(
3045 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02003046 typval_T *argvar,
3047 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003048{
Bram Moolenaar5983d502017-08-20 19:22:56 +02003049 WCHAR *cmd_wchar = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003050 channel_T *channel = NULL;
3051 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003052 DWORD error;
Bram Moolenaar5983d502017-08-20 19:22:56 +02003053 HANDLE jo = NULL;
3054 HANDLE child_process_handle;
3055 HANDLE child_thread_handle;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003056 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003057 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003058 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003059 garray_T ga;
3060 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003061
Bram Moolenaara83e3962017-08-17 14:39:07 +02003062 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003063 return FAIL;
3064
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003065 if (argvar->v_type == VAR_STRING)
3066 cmd = argvar->vval.v_string;
3067 else
3068 {
3069 ga_init2(&ga, (int)sizeof(char*), 20);
3070 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
3071 goto failed;
3072 cmd = ga.ga_data;
3073 }
3074
Bram Moolenaar5983d502017-08-20 19:22:56 +02003075 cmd_wchar = enc_to_utf16(cmd, NULL);
3076 if (cmd_wchar == NULL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003077 return FAIL;
3078
3079 job = job_alloc();
3080 if (job == NULL)
3081 goto failed;
3082
3083 channel = add_channel();
3084 if (channel == NULL)
3085 goto failed;
3086
3087 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
3088 if (term->tl_winpty_config == NULL)
3089 goto failed;
3090
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003091 winpty_config_set_initial_size(term->tl_winpty_config,
3092 term->tl_cols, term->tl_rows);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003093 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
3094 if (term->tl_winpty == NULL)
3095 goto failed;
3096
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003097 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003098 spawn_config = winpty_spawn_config_new(
3099 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
3100 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
3101 NULL,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003102 cmd_wchar,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003103 NULL,
3104 NULL,
3105 &winpty_err);
3106 if (spawn_config == NULL)
3107 goto failed;
3108
3109 channel = add_channel();
3110 if (channel == NULL)
3111 goto failed;
3112
3113 job = job_alloc();
3114 if (job == NULL)
3115 goto failed;
3116
Bram Moolenaar5983d502017-08-20 19:22:56 +02003117 if (opt->jo_set & JO_IN_BUF)
3118 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
3119
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003120 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
3121 &child_thread_handle, &error, &winpty_err))
3122 goto failed;
3123
3124 channel_set_pipes(channel,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003125 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003126 winpty_conin_name(term->tl_winpty),
3127 GENERIC_WRITE, 0, NULL,
3128 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003129 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003130 winpty_conout_name(term->tl_winpty),
3131 GENERIC_READ, 0, NULL,
3132 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003133 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003134 winpty_conerr_name(term->tl_winpty),
3135 GENERIC_READ, 0, NULL,
3136 OPEN_EXISTING, 0, NULL));
3137
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02003138 /* Write lines with CR instead of NL. */
3139 channel->ch_write_text_mode = TRUE;
3140
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003141 jo = CreateJobObject(NULL, NULL);
3142 if (jo == NULL)
3143 goto failed;
3144
3145 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003146 {
3147 /* Failed, switch the way to terminate process with TerminateProcess. */
3148 CloseHandle(jo);
3149 jo = NULL;
3150 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003151
3152 winpty_spawn_config_free(spawn_config);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003153 vim_free(cmd_wchar);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003154
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003155 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003156
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02003157 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02003158 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003159
3160 job->jv_channel = channel;
3161 job->jv_proc_info.hProcess = child_process_handle;
3162 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
3163 job->jv_job_object = jo;
3164 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003165 sprintf(buf, "winpty://%lu",
3166 GetProcessId(winpty_agent_process(term->tl_winpty)));
3167 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003168 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003169 term->tl_job = job;
3170
3171 return OK;
3172
3173failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003174 if (argvar->v_type == VAR_LIST)
3175 vim_free(ga.ga_data);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003176 if (cmd_wchar != NULL)
3177 vim_free(cmd_wchar);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003178 if (spawn_config != NULL)
3179 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003180 if (channel != NULL)
3181 channel_clear(channel);
3182 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003183 {
3184 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003185 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003186 }
3187 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003188 if (jo != NULL)
3189 CloseHandle(jo);
3190 if (term->tl_winpty != NULL)
3191 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003192 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003193 if (term->tl_winpty_config != NULL)
3194 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003195 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003196 if (winpty_err != NULL)
3197 {
3198 char_u *msg = utf16_to_enc(
3199 (short_u *)winpty_error_msg(winpty_err), NULL);
3200
3201 EMSG(msg);
3202 winpty_error_free(winpty_err);
3203 }
3204 return FAIL;
3205}
3206
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003207 static int
3208create_pty_only(term_T *term, jobopt_T *opt)
3209{
3210 /* TODO: implement this */
3211 return FAIL;
3212}
3213
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003214/*
3215 * Free the terminal emulator part of "term".
3216 */
3217 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003218term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003219{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003220 if (term->tl_winpty != NULL)
3221 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003222 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003223 if (term->tl_winpty_config != NULL)
3224 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003225 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003226 if (term->tl_vterm != NULL)
3227 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003228 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003229}
3230
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003231/*
3232 * Request size to terminal.
3233 */
3234 static void
3235term_report_winsize(term_T *term, int rows, int cols)
3236{
3237 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3238}
3239
Bram Moolenaara83e3962017-08-17 14:39:07 +02003240 int
3241terminal_enabled(void)
3242{
3243 return dyn_winpty_init(FALSE) == OK;
3244}
3245
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003246# else
3247
3248/**************************************
3249 * 3. Unix-like implementation.
3250 */
3251
3252/*
3253 * Create a new terminal of "rows" by "cols" cells.
3254 * Start job for "cmd".
3255 * Store the pointers in "term".
3256 * Return OK or FAIL.
3257 */
3258 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003259term_and_job_init(
3260 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02003261 typval_T *argvar,
3262 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003263{
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003264 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003265
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003266 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003267 if (term->tl_job != NULL)
3268 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003269
Bram Moolenaar61a66052017-07-22 18:39:00 +02003270 return term->tl_job != NULL
3271 && term->tl_job->jv_channel != NULL
3272 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003273}
3274
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003275 static int
3276create_pty_only(term_T *term, jobopt_T *opt)
3277{
3278 int ret;
3279
3280 create_vterm(term, term->tl_rows, term->tl_cols);
3281
3282 term->tl_job = job_alloc();
3283 if (term->tl_job == NULL)
3284 return FAIL;
3285 ++term->tl_job->jv_refcount;
3286
3287 /* behave like the job is already finished */
3288 term->tl_job->jv_status = JOB_FINISHED;
3289
3290 ret = mch_create_pty_channel(term->tl_job, opt);
3291
3292 return ret;
3293}
3294
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003295/*
3296 * Free the terminal emulator part of "term".
3297 */
3298 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003299term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003300{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003301 if (term->tl_vterm != NULL)
3302 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003303 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003304}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003305
3306/*
3307 * Request size to terminal.
3308 */
3309 static void
3310term_report_winsize(term_T *term, int rows, int cols)
3311{
3312 /* Use an ioctl() to report the new window size to the job. */
3313 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3314 {
3315 int fd = -1;
3316 int part;
3317
3318 for (part = PART_OUT; part < PART_COUNT; ++part)
3319 {
3320 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3321 if (isatty(fd))
3322 break;
3323 }
3324 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003325 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003326 }
3327}
3328
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003329# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003330
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003331#endif /* FEAT_TERMINAL */