blob: 55575d14feb4cf52d09afdc52a8dc565ca0ce60b [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020022 * this library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020035 * When the job ends the text is put in a buffer. Redrawing then happens from
36 * that buffer, attributes come from the scrollback buffer tl_scrollback.
Bram Moolenaar679653e2017-08-13 14:13:19 +020037 * When the buffer is changed it is turned into a normal buffer, the attributes
38 * in tl_scrollback are no longer used.
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020039 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020040 * TODO:
Bram Moolenaar13ebb032017-08-26 22:02:51 +020041 * - ":term NONE" does not work in MS-Windows.
Bram Moolenaar0cbba822017-08-21 21:39:28 +020042 * - test for writing lines to terminal job does not work on MS-Windows
Bram Moolenaar94053a52017-08-01 21:44:33 +020043 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020044 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020045 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020046 * - support minimal size when 'termsize' is empty?
Bram Moolenaar285f2432017-08-23 23:10:21 +020047 * - GUI: when using tabs, focus in terminal, click on tab does not work.
48 * - GUI: when 'confirm' is set and trying to exit Vim, dialog offers to save
49 * changes to "!shell".
50 * (justrajdeep, 2017 Aug 22)
Bram Moolenaar58302322017-08-22 20:33:53 +020051 * - command argument with spaces doesn't work #1999
52 * :terminal ls dir\ with\ spaces
Bram Moolenaar4f44b882017-08-13 20:06:18 +020053 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020054 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
55 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
56 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
57 * Check that something is connected to the terminal.
58 * Test: "cat" reading from a file or buffer
Bram Moolenaar740c4332017-08-21 22:01:27 +020059 * "ls" writing stdout to a file or buffer
60 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020061 * - For the GUI fill termios with default values, perhaps like pangoterm:
62 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar423802d2017-07-30 16:52:24 +020063 * - if the job in the terminal does not support the mouse, we can use the
64 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
66 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020067 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020068 * - Copy text in the vterm to the Vim buffer once in a while, so that
69 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020070 */
71
72#include "vim.h"
73
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020074#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020075
Bram Moolenaard5310982017-08-05 15:16:32 +020076#ifndef MIN
77# define MIN(x,y) ((x) < (y) ? (x) : (y))
78#endif
79#ifndef MAX
80# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020081#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020083#include "libvterm/include/vterm.h"
84
Bram Moolenaar33a43be2017-08-06 21:36:22 +020085/* This is VTermScreenCell without the characters, thus much smaller. */
86typedef struct {
87 VTermScreenCellAttrs attrs;
88 char width;
89 VTermColor fg, bg;
90} cellattr_T;
91
Bram Moolenaard85f2712017-07-28 21:51:57 +020092typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020093 int sb_cols; /* can differ per line */
94 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020095} sb_line_T;
96
Bram Moolenaare4f25e42017-07-07 11:54:15 +020097/* typedef term_T in structs.h */
98struct terminal_S {
99 term_T *tl_next;
100
Bram Moolenaar423802d2017-07-30 16:52:24 +0200101 VTerm *tl_vterm;
102 job_T *tl_job;
103 buf_T *tl_buffer;
104
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200105 /* used when tl_job is NULL and only a pty was created */
106 int tl_tty_fd;
107 char_u *tl_tty_name;
108
Bram Moolenaar6d819742017-08-06 14:57:49 +0200109 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200110 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200111 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200112 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200113
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200114#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200115 void *tl_winpty_config;
116 void *tl_winpty;
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200117 char_u *tl_eof_chars;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200118#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200119
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200120 /* last known vterm size */
121 int tl_rows;
122 int tl_cols;
123 /* vterm size does not follow window size */
124 int tl_rows_fixed;
125 int tl_cols_fixed;
126
Bram Moolenaar21554412017-07-24 21:44:43 +0200127 char_u *tl_title; /* NULL or allocated */
128 char_u *tl_status_text; /* NULL or allocated */
129
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200130 /* Range of screen rows to update. Zero based. */
131 int tl_dirty_row_start; /* -1 if nothing dirty */
132 int tl_dirty_row_end; /* row below last one to update */
133
Bram Moolenaard85f2712017-07-28 21:51:57 +0200134 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200135 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200136
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200137 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200138 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200139 int tl_cursor_blink;
140 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
141 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200142
143 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200144};
145
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200146#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
147#define TMODE_LOOP 2 /* CTRL-W N used */
148
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200149/*
150 * List of all active terminals.
151 */
152static term_T *first_term = NULL;
153
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200154/* Terminal active in terminal_loop(). */
155static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200156
157#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
158#define KEY_BUF_LEN 200
159
160/*
161 * Functions with separate implementation for MS-Windows and Unix-like systems.
162 */
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200163static int term_and_job_init(term_T *term, typval_T *argvar, jobopt_T *opt);
164static int create_pty_only(term_T *term, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200165static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200166static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200167
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200168/* The characters that we know (or assume) that the terminal expects for the
169 * backspace and enter keys. */
170static int term_backspace_char = BS;
171static int term_enter_char = CAR;
172static int term_nl_does_cr = FALSE;
173
174
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200175/**************************************
176 * 1. Generic code for all systems.
177 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200178
179/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200180 * Determine the terminal size from 'termsize' and the current window.
181 * Assumes term->tl_rows and term->tl_cols are zero.
182 */
183 static void
184set_term_and_win_size(term_T *term)
185{
186 if (*curwin->w_p_tms != NUL)
187 {
188 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
189
190 term->tl_rows = atoi((char *)curwin->w_p_tms);
191 term->tl_cols = atoi((char *)p);
192 }
193 if (term->tl_rows == 0)
194 term->tl_rows = curwin->w_height;
195 else
196 {
197 win_setheight_win(term->tl_rows, curwin);
198 term->tl_rows_fixed = TRUE;
199 }
200 if (term->tl_cols == 0)
201 term->tl_cols = curwin->w_width;
202 else
203 {
204 win_setwidth_win(term->tl_cols, curwin);
205 term->tl_cols_fixed = TRUE;
206 }
207}
208
209/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200210 * Initialize job options for a terminal job.
211 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200212 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200213 static void
214init_job_options(jobopt_T *opt)
215{
216 clear_job_options(opt);
217
218 opt->jo_mode = MODE_RAW;
219 opt->jo_out_mode = MODE_RAW;
220 opt->jo_err_mode = MODE_RAW;
221 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
222
223 opt->jo_io[PART_OUT] = JIO_BUFFER;
224 opt->jo_io[PART_ERR] = JIO_BUFFER;
225 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
226
227 opt->jo_modifiable[PART_OUT] = 0;
228 opt->jo_modifiable[PART_ERR] = 0;
229 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
230
231 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
232}
233
234/*
235 * Set job options mandatory for a terminal job.
236 */
237 static void
238setup_job_options(jobopt_T *opt, int rows, int cols)
239{
240 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
241 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
242 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200243 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
244 opt->jo_term_rows = rows;
245 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
246 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200247}
248
249 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200250term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252 exarg_T split_ea;
253 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200255 buf_T *old_curbuf = NULL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200256 int res;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200257
258 if (check_restricted() || check_secure())
259 return;
260
261 term = (term_T *)alloc_clear(sizeof(term_T));
262 if (term == NULL)
263 return;
264 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200265 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200266 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200267 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200268 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200269
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200270 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200271 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200272 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200273 /* Create a new buffer in the current window. */
274 if (!can_abandon(curbuf, forceit))
275 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200276 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200277 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200278 return;
279 }
280 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
281 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200282 {
283 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200284 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200285 }
286 }
287 else if (opt->jo_hidden)
288 {
289 buf_T *buf;
290
291 /* Create a new buffer without a window. Make it the current buffer for
292 * a moment to be able to do the initialisations. */
293 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
294 BLN_NEW | BLN_LISTED);
295 if (buf == NULL || ml_open(buf) == FAIL)
296 {
297 vim_free(term);
298 return;
299 }
300 old_curbuf = curbuf;
301 --curbuf->b_nwindows;
302 curbuf = buf;
303 curwin->w_buffer = buf;
304 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200305 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200306 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200307 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200308 /* Open a new window or tab. */
309 split_ea.cmdidx = CMD_new;
310 split_ea.cmd = (char_u *)"new";
311 split_ea.arg = (char_u *)"";
312 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
313 {
314 split_ea.line2 = opt->jo_term_rows;
315 split_ea.addr_count = 1;
316 }
317 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
318 {
319 split_ea.line2 = opt->jo_term_cols;
320 split_ea.addr_count = 1;
321 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200322
Bram Moolenaarda43b612017-08-11 22:27:50 +0200323 ex_splitview(&split_ea);
324 if (curwin == old_curwin)
325 {
326 /* split failed */
327 vim_free(term);
328 return;
329 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200330 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200331 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200332 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200333
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200334 if (!opt->jo_hidden)
335 {
336 /* only one size was taken care of with :new, do the other one */
337 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
338 win_setheight(opt->jo_term_rows);
339 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
340 win_setwidth(opt->jo_term_cols);
341 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200342
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200343 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200344 term->tl_next = first_term;
345 first_term = term;
346
Bram Moolenaar78712a72017-08-05 14:50:12 +0200347 if (opt->jo_term_name != NULL)
348 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
349 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200350 {
351 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200352 size_t len;
353 char_u *cmd, *p;
354
355 if (argvar->v_type == VAR_STRING)
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200356 {
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200357 cmd = argvar->vval.v_string;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200358 if (cmd == NULL)
359 cmd = (char_u *)"";
360 else if (STRCMP(cmd, "NONE") == 0)
361 cmd = (char_u *)"pty";
362 }
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200363 else if (argvar->v_type != VAR_LIST
364 || argvar->vval.v_list == NULL
365 || argvar->vval.v_list->lv_len < 1)
366 cmd = (char_u*)"";
367 else
368 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
369
370 len = STRLEN(cmd) + 10;
371 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200372
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200373 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200374 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200375 /* Prepend a ! to the command name to avoid the buffer name equals
376 * the executable, otherwise ":w!" would overwrite it. */
377 if (i == 0)
378 vim_snprintf((char *)p, len, "!%s", cmd);
379 else
380 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200381 if (buflist_findname(p) == NULL)
382 {
383 curbuf->b_ffname = p;
384 break;
385 }
386 }
387 }
388 curbuf->b_fname = curbuf->b_ffname;
389
Bram Moolenaar37c45832017-08-12 16:01:04 +0200390 if (opt->jo_term_opencmd != NULL)
391 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
392
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200393# ifdef WIN3264
394 if (opt->jo_eof_chars != NULL)
395 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
396# endif
397
Bram Moolenaareb44a682017-08-03 22:44:55 +0200398 set_string_option_direct((char_u *)"buftype", -1,
399 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
400
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200401 /* Mark the buffer as not modifiable. It can only be made modifiable after
402 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200403 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200404
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200405 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200406 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200407
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200408 /* System dependent: setup the vterm and maybe start the job in it. */
409 if (argvar->v_type == VAR_STRING
410 && argvar->vval.v_string != NULL
411 && STRCMP(argvar->vval.v_string, "NONE") == 0)
412 res = create_pty_only(term, opt);
413 else
414 res = term_and_job_init(term, argvar, opt);
415
416 if (res == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200417 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200418 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200419 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200420 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200421
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200422 /* Make sure we don't get stuck on sending keys to the job, it leads to
423 * a deadlock if the job is waiting for Vim to read. */
424 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
425
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200426 if (old_curbuf != NULL)
427 {
428 --curbuf->b_nwindows;
429 curbuf = old_curbuf;
430 curwin->w_buffer = curbuf;
431 ++curbuf->b_nwindows;
432 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200433 }
434 else
435 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200436 buf_T *buf = curbuf;
437
Bram Moolenaard85f2712017-07-28 21:51:57 +0200438 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200439 if (old_curbuf != NULL)
440 {
441 --curbuf->b_nwindows;
442 curbuf = old_curbuf;
443 curwin->w_buffer = curbuf;
444 ++curbuf->b_nwindows;
445 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200446
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200447 /* Wiping out the buffer will also close the window and call
448 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200449 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200450 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200451}
452
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200453/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200454 * ":terminal": open a terminal window and execute a job in it.
455 */
456 void
457ex_terminal(exarg_T *eap)
458{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200459 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200460 jobopt_T opt;
461 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200462 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200463
464 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200465
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200466 cmd = eap->arg;
467 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
468 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200469 char_u *p, *ep;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200470
471 cmd += 2;
472 p = skiptowhite(cmd);
Bram Moolenaarb2412082017-08-20 18:09:14 +0200473 ep = vim_strchr(cmd, '=');
474 if (ep != NULL && ep < p)
475 p = ep;
476
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200477 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
478 opt.jo_term_finish = 'c';
479 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
480 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200481 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
482 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200483 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
484 opt.jo_hidden = 1;
Bram Moolenaarb2412082017-08-20 18:09:14 +0200485 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
486 && ep != NULL && isdigit(ep[1]))
487 {
488 opt.jo_set2 |= JO2_TERM_ROWS;
489 opt.jo_term_rows = atoi((char *)ep + 1);
490 p = skiptowhite(cmd);
491 }
492 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
493 && ep != NULL && isdigit(ep[1]))
494 {
495 opt.jo_set2 |= JO2_TERM_COLS;
496 opt.jo_term_cols = atoi((char *)ep + 1);
497 p = skiptowhite(cmd);
498 }
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200499 else if ((int)(p - cmd) == 3 && STRNICMP(cmd, "eof", 3) == 0
500 && ep != NULL)
501 {
502# ifdef WIN3264
503 char_u *buf = NULL;
504 char_u *keys;
505
506 p = skiptowhite(cmd);
507 *p = NUL;
508 keys = replace_termcodes(ep + 1, &buf, TRUE, TRUE, TRUE);
509 opt.jo_set2 |= JO2_EOF_CHARS;
510 opt.jo_eof_chars = vim_strsave(keys);
511 vim_free(buf);
512 *p = ' ';
513# else
514 p = skiptowhite(cmd);
515# endif
516 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200517 else
518 {
519 if (*p)
520 *p = NUL;
521 EMSG2(_("E181: Invalid attribute: %s"), cmd);
522 return;
523 }
524 cmd = skipwhite(p);
525 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200526 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200527 /* Make a copy, an autocommand may set 'shell'. */
528 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200529
Bram Moolenaarb2412082017-08-20 18:09:14 +0200530 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200531 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200532 /* Write lines from current buffer to the job. */
533 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
534 opt.jo_io[PART_IN] = JIO_BUFFER;
535 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
536 opt.jo_in_top = eap->line1;
537 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200538 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200539
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200540 argvar.v_type = VAR_STRING;
541 argvar.vval.v_string = cmd;
542 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200543 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200544}
545
546/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200547 * Free the scrollback buffer for "term".
548 */
549 static void
550free_scrollback(term_T *term)
551{
552 int i;
553
554 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
555 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
556 ga_clear(&term->tl_scrollback);
557}
558
559/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200560 * Free a terminal and everything it refers to.
561 * Kills the job if there is one.
562 * Called when wiping out a buffer.
563 */
564 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200565free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200566{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200567 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200568 term_T *tp;
569
570 if (term == NULL)
571 return;
572 if (first_term == term)
573 first_term = term->tl_next;
574 else
575 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
576 if (tp->tl_next == term)
577 {
578 tp->tl_next = term->tl_next;
579 break;
580 }
581
582 if (term->tl_job != NULL)
583 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200584 if (term->tl_job->jv_status != JOB_ENDED
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200585 && term->tl_job->jv_status != JOB_FINISHED
586 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200587 job_stop(term->tl_job, NULL, "kill");
588 job_unref(term->tl_job);
589 }
590
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200591 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200592
593 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200594 vim_free(term->tl_title);
595 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200596 vim_free(term->tl_opencmd);
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200597# ifdef WIN3264
598 vim_free(term->tl_eof_chars);
599# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200600 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200601 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200602 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200603 if (in_terminal_loop == term)
604 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200605}
606
607/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200608 * Write job output "msg[len]" to the vterm.
609 */
610 static void
611term_write_job_output(term_T *term, char_u *msg, size_t len)
612{
613 VTerm *vterm = term->tl_vterm;
614 char_u *p;
615 size_t done;
616 size_t len_now;
617
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200618 if (term_nl_does_cr)
619 vterm_input_write(vterm, (char *)msg, len);
620 else
621 /* need to convert NL to CR-NL */
622 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200623 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200624 for (p = msg + done; p < msg + len; )
625 {
626 if (*p == NL)
627 break;
628 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
629 }
630 len_now = p - msg - done;
631 vterm_input_write(vterm, (char *)msg + done, len_now);
632 if (p < msg + len && *p == NL)
633 {
634 vterm_input_write(vterm, "\r\n", 2);
635 ++len_now;
636 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200637 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200638
639 /* this invokes the damage callbacks */
640 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
641}
642
Bram Moolenaar1c844932017-07-24 23:36:41 +0200643 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200644update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200645{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200646 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200647 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200648 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200649 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200650 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200651 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200652 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200653 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200654#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200655 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200656 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200657#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200658 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200659}
660
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200661/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200662 * Invoked when "msg" output from a job was received. Write it to the terminal
663 * of "buffer".
664 */
665 void
666write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
667{
668 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200669 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200670
Bram Moolenaard85f2712017-07-28 21:51:57 +0200671 if (term->tl_vterm == NULL)
672 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200673 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200674 return;
675 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200676 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200677 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200678
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200679 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
680 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200681 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200682 {
683 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200684 ch_log(term->tl_job->jv_channel, "updating screen");
685 if (buffer == curbuf)
686 {
687 update_screen(0);
688 update_cursor(term, TRUE);
689 }
690 else
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200691 redraw_after_callback(TRUE);
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200692 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200693}
694
695/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200696 * Send a mouse position and click to the vterm
697 */
698 static int
699term_send_mouse(VTerm *vterm, int button, int pressed)
700{
701 VTermModifier mod = VTERM_MOD_NONE;
702
703 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
704 mouse_col - W_WINCOL(curwin), mod);
705 vterm_mouse_button(vterm, button, pressed, mod);
706 return TRUE;
707}
708
709/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200710 * Convert typed key "c" into bytes to send to the job.
711 * Return the number of bytes in "buf".
712 */
713 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200714term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200715{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200716 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200717 VTermKey key = VTERM_KEY_NONE;
718 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200719 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200720
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200721 switch (c)
722 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200723 case CAR: c = term_enter_char; break;
724 /* don't use VTERM_KEY_BACKSPACE, it always
725 * becomes 0x7f DEL */
726 case K_BS: c = term_backspace_char; break;
727
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200728 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200729 case K_DEL: key = VTERM_KEY_DEL; break;
730 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200731 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
732 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200733 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200734 case K_S_END: mod = VTERM_MOD_SHIFT;
735 key = VTERM_KEY_END; break;
736 case K_C_END: mod = VTERM_MOD_CTRL;
737 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200738 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
739 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
740 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
741 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
742 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
743 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
744 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
745 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
746 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
747 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
748 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
749 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
750 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200751 case K_S_HOME: mod = VTERM_MOD_SHIFT;
752 key = VTERM_KEY_HOME; break;
753 case K_C_HOME: mod = VTERM_MOD_CTRL;
754 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200755 case K_INS: key = VTERM_KEY_INS; break;
756 case K_K0: key = VTERM_KEY_KP_0; break;
757 case K_K1: key = VTERM_KEY_KP_1; break;
758 case K_K2: key = VTERM_KEY_KP_2; break;
759 case K_K3: key = VTERM_KEY_KP_3; break;
760 case K_K4: key = VTERM_KEY_KP_4; break;
761 case K_K5: key = VTERM_KEY_KP_5; break;
762 case K_K6: key = VTERM_KEY_KP_6; break;
763 case K_K7: key = VTERM_KEY_KP_7; break;
764 case K_K8: key = VTERM_KEY_KP_8; break;
765 case K_K9: key = VTERM_KEY_KP_9; break;
766 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
767 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
768 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
769 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
770 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
771 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
772 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
773 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
774 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
775 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
776 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
777 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
778 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200779 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
780 key = VTERM_KEY_LEFT; break;
781 case K_C_LEFT: mod = VTERM_MOD_CTRL;
782 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200783 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
784 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
785 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200786 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
787 key = VTERM_KEY_RIGHT; break;
788 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
789 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200790 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200791 case K_S_UP: mod = VTERM_MOD_SHIFT;
792 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200793 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200794
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200795 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
796 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
797 case K_MOUSELEFT: /* TODO */ return 0;
798 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200799
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200800 case K_LEFTMOUSE:
801 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
802 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
803 case K_LEFTRELEASE:
804 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
805 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
806 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
807 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
808 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
809 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
810 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
811 case K_X1MOUSE: /* TODO */ return 0;
812 case K_X1DRAG: /* TODO */ return 0;
813 case K_X1RELEASE: /* TODO */ return 0;
814 case K_X2MOUSE: /* TODO */ return 0;
815 case K_X2DRAG: /* TODO */ return 0;
816 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200817
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200818 case K_IGNORE: return 0;
819 case K_NOP: return 0;
820 case K_UNDO: return 0;
821 case K_HELP: return 0;
822 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
823 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
824 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
825 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
826 case K_SELECT: return 0;
827#ifdef FEAT_GUI
828 case K_VER_SCROLLBAR: return 0;
829 case K_HOR_SCROLLBAR: return 0;
830#endif
831#ifdef FEAT_GUI_TABLINE
832 case K_TABLINE: return 0;
833 case K_TABMENU: return 0;
834#endif
835#ifdef FEAT_NETBEANS_INTG
836 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
837#endif
838#ifdef FEAT_DND
839 case K_DROP: return 0;
840#endif
841#ifdef FEAT_AUTOCMD
842 case K_CURSORHOLD: return 0;
843#endif
844 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
845 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200846 }
847
848 /*
849 * Convert special keys to vterm keys:
850 * - Write keys to vterm: vterm_keyboard_key()
851 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200852 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200853 */
854 if (key != VTERM_KEY_NONE)
855 /* Special key, let vterm convert it. */
856 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200857 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200858 /* Normal character, let vterm convert it. */
859 vterm_keyboard_unichar(vterm, c, mod);
860
861 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200862 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200863}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200864
Bram Moolenaar938783d2017-07-16 20:13:26 +0200865/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200866 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200867 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200868 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200869term_job_running(term_T *term)
870{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200871 /* Also consider the job finished when the channel is closed, to avoid a
872 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200873 return term != NULL
874 && term->tl_job != NULL
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200875 && channel_is_open(term->tl_job->jv_channel)
876 && (term->tl_job->jv_status == JOB_STARTED
877 || term->tl_job->jv_channel->ch_keep_open);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200878}
879
880/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200881 * Add the last line of the scrollback buffer to the buffer in the window.
882 */
883 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200884add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200885{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200886 buf_T *buf = term->tl_buffer;
887 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
888 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200889
Bram Moolenaar58302322017-08-22 20:33:53 +0200890#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +0200891 if (!enc_utf8 && enc_codepage > 0)
892 {
893 WCHAR *ret = NULL;
894 int length = 0;
895
896 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
897 &ret, &length);
898 if (ret != NULL)
899 {
900 WideCharToMultiByte_alloc(enc_codepage, 0,
901 ret, length, (char **)&text, &len, 0, 0);
902 vim_free(ret);
903 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
904 vim_free(text);
905 }
906 }
907 else
908#endif
909 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200910 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200911 {
912 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200913 curbuf = buf;
914 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200915 curbuf = curwin->w_buffer;
916 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200917}
918
919/*
920 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200921 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200922 */
923 static void
924move_terminal_to_buffer(term_T *term)
925{
926 win_T *wp;
927 int len;
928 int lines_skipped = 0;
929 VTermPos pos;
930 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200931 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200932 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200933
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200934 if (term->tl_vterm == NULL)
935 return;
936 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200937 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
938 {
939 len = 0;
940 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
941 if (vterm_screen_get_cell(screen, pos, &cell) != 0
942 && cell.chars[0] != NUL)
943 len = pos.col + 1;
944
945 if (len == 0)
946 ++lines_skipped;
947 else
948 {
949 while (lines_skipped > 0)
950 {
951 /* Line was skipped, add an empty line. */
952 --lines_skipped;
953 if (ga_grow(&term->tl_scrollback, 1) == OK)
954 {
955 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
956 + term->tl_scrollback.ga_len;
957
958 line->sb_cols = 0;
959 line->sb_cells = NULL;
960 ++term->tl_scrollback.ga_len;
961
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200962 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200963 }
964 }
965
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200966 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200967 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
968 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200969 garray_T ga;
970 int width;
971 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200972 + term->tl_scrollback.ga_len;
973
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200974 ga_init2(&ga, 1, 100);
975 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200976 {
977 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200978 {
979 width = 1;
980 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
981 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +0200982 ga.ga_len += utf_char2bytes(' ',
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200983 (char_u *)ga.ga_data + ga.ga_len);
984 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200985 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200986 {
987 width = cell.width;
988
989 p[pos.col].width = cell.width;
990 p[pos.col].attrs = cell.attrs;
991 p[pos.col].fg = cell.fg;
992 p[pos.col].bg = cell.bg;
993
994 if (ga_grow(&ga, MB_MAXBYTES) == OK)
995 {
996 int i;
997 int c;
998
999 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +02001000 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001001 (char_u *)ga.ga_data + ga.ga_len);
1002 }
1003 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02001004 }
1005 line->sb_cols = len;
1006 line->sb_cells = p;
1007 ++term->tl_scrollback.ga_len;
1008
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001009 if (ga_grow(&ga, 1) == FAIL)
1010 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1011 else
1012 {
1013 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1014 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1015 }
1016 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001017 }
1018 else
1019 vim_free(p);
1020 }
1021 }
1022
1023 FOR_ALL_WINDOWS(wp)
1024 {
1025 if (wp->w_buffer == term->tl_buffer)
1026 {
1027 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1028 wp->w_cursor.col = 0;
1029 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +02001030 if (wp->w_cursor.lnum >= wp->w_height)
1031 {
1032 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
1033
1034 if (wp->w_topline < min_topline)
1035 wp->w_topline = min_topline;
1036 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02001037 redraw_win_later(wp, NOT_VALID);
1038 }
1039 }
1040}
1041
1042 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001043set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001044{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001045 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001046 vim_free(term->tl_status_text);
1047 term->tl_status_text = NULL;
1048 if (term->tl_buffer == curbuf)
1049 maketitle();
1050}
1051
1052/*
1053 * Called after the job if finished and Terminal mode is not active:
1054 * Move the vterm contents into the scrollback buffer and free the vterm.
1055 */
1056 static void
1057cleanup_vterm(term_T *term)
1058{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001059 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001060 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001061 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001062 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001063}
1064
1065/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001066 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001067 * Suspends updating the terminal window.
1068 */
1069 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001070term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001071{
1072 term_T *term = curbuf->b_term;
1073
1074 /* Append the current terminal contents to the buffer. */
1075 move_terminal_to_buffer(term);
1076
Bram Moolenaar6d819742017-08-06 14:57:49 +02001077 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001078
Bram Moolenaar6d819742017-08-06 14:57:49 +02001079 /* Move the window cursor to the position of the cursor in the
1080 * terminal. */
1081 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1082 + term->tl_cursor_pos.row + 1;
1083 check_cursor();
1084 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001085
Bram Moolenaar6d819742017-08-06 14:57:49 +02001086 /* Display the same lines as in the terminal. */
1087 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001088}
1089
1090/*
1091 * Returns TRUE if the current window contains a terminal and we are in
1092 * Terminal-Normal mode.
1093 */
1094 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001095term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001096{
1097 term_T *term = curbuf->b_term;
1098
Bram Moolenaar6d819742017-08-06 14:57:49 +02001099 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001100}
1101
1102/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001103 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001104 * Restores updating the terminal window.
1105 */
1106 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001107term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001108{
1109 term_T *term = curbuf->b_term;
1110 sb_line_T *line;
1111 garray_T *gap;
1112
1113 /* Remove the terminal contents from the scrollback and the buffer. */
1114 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001115 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1116 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001117 {
1118 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1119 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1120 vim_free(line->sb_cells);
1121 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001122 }
1123 check_cursor();
1124
Bram Moolenaar6d819742017-08-06 14:57:49 +02001125 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001126
1127 if (term->tl_channel_closed)
1128 cleanup_vterm(term);
1129 redraw_buf_and_status_later(curbuf, NOT_VALID);
1130}
1131
1132/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001133 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001134 * Note: while waiting a terminal may be closed and freed if the channel is
1135 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001136 * TODO: use terminal mode mappings.
1137 */
1138 static int
1139term_vgetc()
1140{
1141 int c;
1142
1143 ++no_mapping;
1144 ++allow_keys;
1145 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001146#ifdef WIN3264
1147 ctrl_break_was_pressed = FALSE;
1148#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001149 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001150 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001151 --no_mapping;
1152 --allow_keys;
1153 return c;
1154}
1155
1156/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001157 * Get the part that is connected to the tty. Normally this is PART_IN, but
1158 * when writing buffer lines to the job it can be another. This makes it
1159 * possible to do "1,5term vim -".
1160 */
1161 static ch_part_T
1162get_tty_part(term_T *term)
1163{
1164#ifdef UNIX
1165 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1166 int i;
1167
1168 for (i = 0; i < 3; ++i)
1169 {
1170 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1171
1172 if (isatty(fd))
1173 return parts[i];
1174 }
1175#endif
1176 return PART_IN;
1177}
1178
1179/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001180 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001181 * Return FAIL when the key needs to be handled in Normal mode.
1182 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001183 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001184 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001185send_keys_to_term(term_T *term, int c, int typed)
1186{
1187 char msg[KEY_BUF_LEN];
1188 size_t len;
1189 static int mouse_was_outside = FALSE;
1190 int dragging_outside = FALSE;
1191
1192 /* Catch keys that need to be handled as in Normal mode. */
1193 switch (c)
1194 {
1195 case NUL:
1196 case K_ZERO:
1197 if (typed)
1198 stuffcharReadbuff(c);
1199 return FAIL;
1200
1201 case K_IGNORE:
1202 return FAIL;
1203
1204 case K_LEFTDRAG:
1205 case K_MIDDLEDRAG:
1206 case K_RIGHTDRAG:
1207 case K_X1DRAG:
1208 case K_X2DRAG:
1209 dragging_outside = mouse_was_outside;
1210 /* FALLTHROUGH */
1211 case K_LEFTMOUSE:
1212 case K_LEFTMOUSE_NM:
1213 case K_LEFTRELEASE:
1214 case K_LEFTRELEASE_NM:
1215 case K_MIDDLEMOUSE:
1216 case K_MIDDLERELEASE:
1217 case K_RIGHTMOUSE:
1218 case K_RIGHTRELEASE:
1219 case K_X1MOUSE:
1220 case K_X1RELEASE:
1221 case K_X2MOUSE:
1222 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001223
1224 case K_MOUSEUP:
1225 case K_MOUSEDOWN:
1226 case K_MOUSELEFT:
1227 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001228 if (mouse_row < W_WINROW(curwin)
1229 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1230 || mouse_col < W_WINCOL(curwin)
1231 || mouse_col >= W_ENDCOL(curwin)
1232 || dragging_outside)
1233 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001234 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001235 if (typed)
1236 {
1237 stuffcharReadbuff(c);
1238 mouse_was_outside = TRUE;
1239 }
1240 return FAIL;
1241 }
1242 }
1243 if (typed)
1244 mouse_was_outside = FALSE;
1245
1246 /* Convert the typed key to a sequence of bytes for the job. */
1247 len = term_convert_key(term, c, msg);
1248 if (len > 0)
1249 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001250 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1251 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001252
1253 return OK;
1254}
1255
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001256 static void
1257position_cursor(win_T *wp, VTermPos *pos)
1258{
1259 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1260 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1261 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1262}
1263
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001264/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001265 * Handle CTRL-W "": send register contents to the job.
1266 */
1267 static void
1268term_paste_register(int prev_c UNUSED)
1269{
1270 int c;
1271 list_T *l;
1272 listitem_T *item;
1273 long reglen = 0;
1274 int type;
1275
1276#ifdef FEAT_CMDL_INFO
1277 if (add_to_showcmd(prev_c))
1278 if (add_to_showcmd('"'))
1279 out_flush();
1280#endif
1281 c = term_vgetc();
1282#ifdef FEAT_CMDL_INFO
1283 clear_showcmd();
1284#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001285 if (!term_use_loop())
1286 /* job finished while waiting for a character */
1287 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001288
1289 /* CTRL-W "= prompt for expression to evaluate. */
1290 if (c == '=' && get_expr_register() != '=')
1291 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001292 if (!term_use_loop())
1293 /* job finished while waiting for a character */
1294 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001295
1296 l = (list_T *)get_reg_contents(c, GREG_LIST);
1297 if (l != NULL)
1298 {
1299 type = get_reg_type(c, &reglen);
1300 for (item = l->lv_first; item != NULL; item = item->li_next)
1301 {
1302 char_u *s = get_tv_string(&item->li_tv);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001303#ifdef WIN3264
1304 char_u *tmp = s;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001305
Bram Moolenaar285f2432017-08-23 23:10:21 +02001306 if (!enc_utf8 && enc_codepage > 0)
1307 {
1308 WCHAR *ret = NULL;
1309 int length = 0;
1310
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001311 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
1312 (int)STRLEN(s), &ret, &length);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001313 if (ret != NULL)
1314 {
1315 WideCharToMultiByte_alloc(CP_UTF8, 0,
1316 ret, length, (char **)&s, &length, 0, 0);
1317 vim_free(ret);
1318 }
1319 }
1320#endif
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001321 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001322 s, (int)STRLEN(s), NULL);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001323#ifdef WIN3264
1324 if (tmp != s)
1325 vim_free(s);
1326#endif
1327
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001328 if (item->li_next != NULL || type == MLINE)
1329 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1330 (char_u *)"\r", 1, NULL);
1331 }
1332 list_free(l);
1333 }
1334}
1335
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001336#if defined(FEAT_GUI) || defined(PROTO)
1337/*
1338 * Return TRUE when the cursor of the terminal should be displayed.
1339 */
1340 int
1341use_terminal_cursor()
1342{
1343 return in_terminal_loop != NULL;
1344}
1345
1346 cursorentry_T *
1347term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1348{
1349 term_T *term = in_terminal_loop;
1350 static cursorentry_T entry;
1351
1352 vim_memset(&entry, 0, sizeof(entry));
1353 entry.shape = entry.mshape =
1354 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1355 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1356 SHAPE_BLOCK;
1357 entry.percentage = 20;
1358 if (term->tl_cursor_blink)
1359 {
1360 entry.blinkwait = 700;
1361 entry.blinkon = 400;
Bram Moolenaar58302322017-08-22 20:33:53 +02001362 entry.blinkoff = 250;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001363 }
1364 *fg = gui.back_pixel;
1365 if (term->tl_cursor_color == NULL)
1366 *bg = gui.norm_pixel;
1367 else
1368 *bg = color_name2handle(term->tl_cursor_color);
1369 entry.name = "n";
1370 entry.used_for = SHAPE_CURSOR;
1371
1372 return &entry;
1373}
1374#endif
1375
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001376static int did_change_cursor = FALSE;
1377
1378 static void
1379may_set_cursor_props(term_T *term)
1380{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001381#ifdef FEAT_GUI
1382 /* For the GUI the cursor properties are obtained with
1383 * term_get_cursor_shape(). */
1384 if (gui.in_use)
1385 return;
1386#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001387 if (in_terminal_loop == term)
1388 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001389 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001390 if (term->tl_cursor_color != NULL)
1391 term_cursor_color(term->tl_cursor_color);
1392 else
1393 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001394 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1395 }
1396}
1397
1398 static void
1399may_restore_cursor_props(void)
1400{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001401#ifdef FEAT_GUI
1402 if (gui.in_use)
1403 return;
1404#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001405 if (did_change_cursor)
1406 {
1407 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001408 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001409 /* this will restore the initial cursor style, if possible */
1410 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001411 }
1412}
1413
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001414/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001415 * Returns TRUE if the current window contains a terminal and we are sending
1416 * keys to the job.
1417 */
1418 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001419term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001420{
1421 term_T *term = curbuf->b_term;
1422
1423 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001424 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001425 && term->tl_vterm != NULL
1426 && term_job_running(term);
1427}
1428
1429/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001430 * Wait for input and send it to the job.
1431 * Return when the start of a CTRL-W command is typed or anything else that
1432 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001433 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1434 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001435 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001436 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001437terminal_loop(void)
1438{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001439 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001440 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001441 int ret;
1442
Bram Moolenaar679653e2017-08-13 14:13:19 +02001443 /* Remember the terminal we are sending keys to. However, the terminal
1444 * might be closed while waiting for a character, e.g. typing "exit" in a
1445 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1446 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001447 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001448
1449 if (*curwin->w_p_tk != NUL)
1450 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001451 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001452 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001453
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001454#ifdef UNIX
1455 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001456 int part = get_tty_part(curbuf->b_term);
1457 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001458
1459 if (isatty(fd))
1460 {
1461 ttyinfo_T info;
1462
1463 /* Get the current backspace and enter characters of the pty. */
1464 if (get_tty_info(fd, &info) == OK)
1465 {
1466 term_backspace_char = info.backspace;
1467 term_enter_char = info.enter;
1468 term_nl_does_cr = info.nl_does_cr;
1469 }
1470 }
1471 }
1472#endif
1473
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001474 for (;;)
1475 {
1476 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001477 /* Repeat redrawing in case a message is received while redrawing. */
1478 while (curwin->w_redr_type != 0)
1479 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001480 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001481
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001482 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001483 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001484 /* job finished while waiting for a character */
1485 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001486 if (c == K_IGNORE)
1487 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001488
Bram Moolenaarfae42832017-08-01 22:24:26 +02001489#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001490 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001491 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001492 if (ctrl_break_was_pressed)
1493 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001494#endif
1495
Bram Moolenaar69198192017-08-05 14:10:48 +02001496 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001497 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001498 int prev_c = c;
1499
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001500#ifdef FEAT_CMDL_INFO
1501 if (add_to_showcmd(c))
1502 out_flush();
1503#endif
1504 c = term_vgetc();
1505#ifdef FEAT_CMDL_INFO
1506 clear_showcmd();
1507#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001508 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001509 /* job finished while waiting for a character */
1510 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001511
Bram Moolenaar69198192017-08-05 14:10:48 +02001512 if (prev_c == Ctrl_BSL)
1513 {
1514 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001515 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001516 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1517 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001518 ret = FAIL;
1519 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001520 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001521 /* Send both keys to the terminal. */
1522 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1523 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001524 else if (c == Ctrl_C)
1525 {
1526 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1527 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1528 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001529 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001530 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001531 /* "CTRL-W .": send CTRL-W to the job */
1532 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001533 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001534 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001535 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001536 /* CTRL-W N : go to Terminal-Normal mode. */
1537 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001538 ret = FAIL;
1539 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001540 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001541 else if (c == '"')
1542 {
1543 term_paste_register(prev_c);
1544 continue;
1545 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001546 else if (termkey == 0 || c != termkey)
1547 {
1548 stuffcharReadbuff(Ctrl_W);
1549 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001550 ret = OK;
1551 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001552 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001553 }
Bram Moolenaar58302322017-08-22 20:33:53 +02001554# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02001555 if (!enc_utf8 && has_mbyte && c >= 0x80)
1556 {
1557 WCHAR wc;
1558 char_u mb[3];
1559
1560 mb[0] = (unsigned)c >> 8;
1561 mb[1] = c;
1562 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1563 c = wc;
1564 }
1565# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001566 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001567 {
1568 ret = OK;
1569 goto theend;
1570 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001571 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001572 ret = FAIL;
1573
1574theend:
1575 in_terminal_loop = NULL;
1576 may_restore_cursor_props();
1577 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001578}
1579
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001580/*
1581 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001582 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001583 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001584 */
1585 void
1586term_job_ended(job_T *job)
1587{
1588 term_T *term;
1589 int did_one = FALSE;
1590
1591 for (term = first_term; term != NULL; term = term->tl_next)
1592 if (term->tl_job == job)
1593 {
1594 vim_free(term->tl_title);
1595 term->tl_title = NULL;
1596 vim_free(term->tl_status_text);
1597 term->tl_status_text = NULL;
1598 redraw_buf_and_status_later(term->tl_buffer, VALID);
1599 did_one = TRUE;
1600 }
1601 if (did_one)
1602 redraw_statuslines();
1603 if (curbuf->b_term != NULL)
1604 {
1605 if (curbuf->b_term->tl_job == job)
1606 maketitle();
1607 update_cursor(curbuf->b_term, TRUE);
1608 }
1609}
1610
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001611 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001612may_toggle_cursor(term_T *term)
1613{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001614 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001615 {
1616 if (term->tl_cursor_visible)
1617 cursor_on();
1618 else
1619 cursor_off();
1620 }
1621}
1622
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001623/*
1624 * Reverse engineer the RGB value into a cterm color index.
1625 * First color is 1. Return 0 if no match found.
1626 */
1627 static int
1628color2index(VTermColor *color, int fg, int *boldp)
1629{
1630 int red = color->red;
1631 int blue = color->blue;
1632 int green = color->green;
1633
1634 /* The argument for lookup_color() is for the color_names[] table. */
1635 if (red == 0)
1636 {
1637 if (green == 0)
1638 {
1639 if (blue == 0)
1640 return lookup_color(0, fg, boldp) + 1; /* black */
1641 if (blue == 224)
1642 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1643 }
1644 else if (green == 224)
1645 {
1646 if (blue == 0)
1647 return lookup_color(2, fg, boldp) + 1; /* dark green */
1648 if (blue == 224)
1649 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1650 }
1651 }
1652 else if (red == 224)
1653 {
1654 if (green == 0)
1655 {
1656 if (blue == 0)
1657 return lookup_color(4, fg, boldp) + 1; /* dark red */
1658 if (blue == 224)
1659 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1660 }
1661 else if (green == 224)
1662 {
1663 if (blue == 0)
1664 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1665 if (blue == 224)
1666 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1667 }
1668 }
1669 else if (red == 128)
1670 {
1671 if (green == 128 && blue == 128)
1672 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1673 }
1674 else if (red == 255)
1675 {
1676 if (green == 64)
1677 {
1678 if (blue == 64)
1679 return lookup_color(20, fg, boldp) + 1; /* light red */
1680 if (blue == 255)
1681 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1682 }
1683 else if (green == 255)
1684 {
1685 if (blue == 64)
1686 return lookup_color(24, fg, boldp) + 1; /* yellow */
1687 if (blue == 255)
1688 return lookup_color(26, fg, boldp) + 1; /* white */
1689 }
1690 }
1691 else if (red == 64)
1692 {
1693 if (green == 64)
1694 {
1695 if (blue == 255)
1696 return lookup_color(14, fg, boldp) + 1; /* light blue */
1697 }
1698 else if (green == 255)
1699 {
1700 if (blue == 64)
1701 return lookup_color(16, fg, boldp) + 1; /* light green */
1702 if (blue == 255)
1703 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1704 }
1705 }
1706 if (t_colors >= 256)
1707 {
1708 if (red == blue && red == green)
1709 {
1710 /* 24-color greyscale */
1711 static int cutoff[23] = {
1712 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1713 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1714 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1715 int i;
1716
1717 for (i = 0; i < 23; ++i)
1718 if (red < cutoff[i])
1719 return i + 233;
1720 return 256;
1721 }
1722
1723 /* 216-color cube */
1724 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001725 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001726 + (blue + 25) / 0x33;
1727 }
1728 return 0;
1729}
1730
1731/*
1732 * Convert the attributes of a vterm cell into an attribute index.
1733 */
1734 static int
1735cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1736{
1737 int attr = 0;
1738
1739 if (cellattrs.bold)
1740 attr |= HL_BOLD;
1741 if (cellattrs.underline)
1742 attr |= HL_UNDERLINE;
1743 if (cellattrs.italic)
1744 attr |= HL_ITALIC;
1745 if (cellattrs.strike)
1746 attr |= HL_STANDOUT;
1747 if (cellattrs.reverse)
1748 attr |= HL_INVERSE;
1749
1750#ifdef FEAT_GUI
1751 if (gui.in_use)
1752 {
1753 guicolor_T fg, bg;
1754
1755 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1756 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1757 return get_gui_attr_idx(attr, fg, bg);
1758 }
1759 else
1760#endif
1761#ifdef FEAT_TERMGUICOLORS
1762 if (p_tgc)
1763 {
1764 guicolor_T fg, bg;
1765
1766 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1767 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1768
1769 return get_tgc_attr_idx(attr, fg, bg);
1770 }
1771 else
1772#endif
1773 {
1774 int bold = MAYBE;
1775 int fg = color2index(&cellfg, TRUE, &bold);
1776 int bg = color2index(&cellbg, FALSE, &bold);
1777
1778 /* with 8 colors set the bold attribute to get a bright foreground */
1779 if (bold == TRUE)
1780 attr |= HL_BOLD;
1781 return get_cterm_attr_idx(attr, fg, bg);
1782 }
1783 return 0;
1784}
1785
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001786 static int
1787handle_damage(VTermRect rect, void *user)
1788{
1789 term_T *term = (term_T *)user;
1790
1791 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1792 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1793 redraw_buf_later(term->tl_buffer, NOT_VALID);
1794 return 1;
1795}
1796
1797 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001798handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001799{
1800 term_T *term = (term_T *)user;
1801
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001802 /* Scrolling up is done much more efficiently by deleting lines instead of
1803 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001804 if (dest.start_col == src.start_col
1805 && dest.end_col == src.end_col
1806 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001807 {
1808 win_T *wp;
1809 VTermColor fg, bg;
1810 VTermScreenCellAttrs attr;
1811 int clear_attr;
1812
1813 /* Set the color to clear lines with. */
1814 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1815 &fg, &bg);
1816 vim_memset(&attr, 0, sizeof(attr));
1817 clear_attr = cell2attr(attr, fg, bg);
1818
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001819 FOR_ALL_WINDOWS(wp)
1820 {
1821 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001822 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001823 src.start_row - dest.start_row, FALSE, FALSE,
1824 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001825 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001826 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001827 redraw_buf_later(term->tl_buffer, NOT_VALID);
1828 return 1;
1829}
1830
1831 static int
1832handle_movecursor(
1833 VTermPos pos,
1834 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001835 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001836 void *user)
1837{
1838 term_T *term = (term_T *)user;
1839 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001840
1841 term->tl_cursor_pos = pos;
1842 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001843
1844 FOR_ALL_WINDOWS(wp)
1845 {
1846 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001847 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001848 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001849 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001850 {
1851 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001852 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001853 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001854
1855 return 1;
1856}
1857
Bram Moolenaar21554412017-07-24 21:44:43 +02001858 static int
1859handle_settermprop(
1860 VTermProp prop,
1861 VTermValue *value,
1862 void *user)
1863{
1864 term_T *term = (term_T *)user;
1865
1866 switch (prop)
1867 {
1868 case VTERM_PROP_TITLE:
1869 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001870 /* a blank title isn't useful, make it empty, so that "running" is
1871 * displayed */
1872 if (*skipwhite((char_u *)value->string) == NUL)
1873 term->tl_title = NULL;
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001874#ifdef WIN3264
1875 else if (!enc_utf8 && enc_codepage > 0)
1876 {
1877 WCHAR *ret = NULL;
1878 int length = 0;
1879
1880 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001881 (char*)value->string, (int)STRLEN(value->string),
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001882 &ret, &length);
1883 if (ret != NULL)
1884 {
1885 WideCharToMultiByte_alloc(enc_codepage, 0,
1886 ret, length, (char**)&term->tl_title,
1887 &length, 0, 0);
1888 vim_free(ret);
1889 }
1890 }
1891#endif
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001892 else
1893 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001894 vim_free(term->tl_status_text);
1895 term->tl_status_text = NULL;
1896 if (term == curbuf->b_term)
1897 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001898 break;
1899
1900 case VTERM_PROP_CURSORVISIBLE:
1901 term->tl_cursor_visible = value->boolean;
1902 may_toggle_cursor(term);
1903 out_flush();
1904 break;
1905
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001906 case VTERM_PROP_CURSORBLINK:
1907 term->tl_cursor_blink = value->boolean;
1908 may_set_cursor_props(term);
1909 break;
1910
1911 case VTERM_PROP_CURSORSHAPE:
1912 term->tl_cursor_shape = value->number;
1913 may_set_cursor_props(term);
1914 break;
1915
1916 case VTERM_PROP_CURSORCOLOR:
1917 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001918 if (*value->string == NUL)
1919 term->tl_cursor_color = NULL;
1920 else
1921 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001922 may_set_cursor_props(term);
1923 break;
1924
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001925 case VTERM_PROP_ALTSCREEN:
1926 /* TODO: do anything else? */
1927 term->tl_using_altscreen = value->boolean;
1928 break;
1929
Bram Moolenaar21554412017-07-24 21:44:43 +02001930 default:
1931 break;
1932 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001933 /* Always return 1, otherwise vterm doesn't store the value internally. */
1934 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001935}
1936
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001937/*
1938 * The job running in the terminal resized the terminal.
1939 */
1940 static int
1941handle_resize(int rows, int cols, void *user)
1942{
1943 term_T *term = (term_T *)user;
1944 win_T *wp;
1945
1946 term->tl_rows = rows;
1947 term->tl_cols = cols;
1948 FOR_ALL_WINDOWS(wp)
1949 {
1950 if (wp->w_buffer == term->tl_buffer)
1951 {
1952 win_setheight_win(rows, wp);
1953 win_setwidth_win(cols, wp);
1954 }
1955 }
1956
1957 redraw_buf_later(term->tl_buffer, NOT_VALID);
1958 return 1;
1959}
1960
Bram Moolenaard85f2712017-07-28 21:51:57 +02001961/*
1962 * Handle a line that is pushed off the top of the screen.
1963 */
1964 static int
1965handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1966{
1967 term_T *term = (term_T *)user;
1968
1969 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001970 if (ga_grow(&term->tl_scrollback, 1) == OK)
1971 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001972 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001973 int len = 0;
1974 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001975 int c;
1976 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001977 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001978 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001979
1980 /* do not store empty cells at the end */
1981 for (i = 0; i < cols; ++i)
1982 if (cells[i].chars[0] != 0)
1983 len = i + 1;
1984
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001985 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001986 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001987 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001988 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001989 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001990 for (col = 0; col < len; col += cells[col].width)
1991 {
1992 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1993 {
1994 ga.ga_len = 0;
1995 break;
1996 }
1997 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +02001998 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001999 (char_u *)ga.ga_data + ga.ga_len);
2000 p[col].width = cells[col].width;
2001 p[col].attrs = cells[col].attrs;
2002 p[col].fg = cells[col].fg;
2003 p[col].bg = cells[col].bg;
2004 }
2005 }
2006 if (ga_grow(&ga, 1) == FAIL)
2007 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
2008 else
2009 {
2010 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
2011 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
2012 }
2013 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02002014
2015 line = (sb_line_T *)term->tl_scrollback.ga_data
2016 + term->tl_scrollback.ga_len;
2017 line->sb_cols = len;
2018 line->sb_cells = p;
2019 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002020 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002021 }
2022 return 0; /* ignored */
2023}
2024
Bram Moolenaar21554412017-07-24 21:44:43 +02002025static VTermScreenCallbacks screen_callbacks = {
2026 handle_damage, /* damage */
2027 handle_moverect, /* moverect */
2028 handle_movecursor, /* movecursor */
2029 handle_settermprop, /* settermprop */
2030 NULL, /* bell */
2031 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002032 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02002033 NULL /* sb_popline */
2034};
2035
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002036/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02002037 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002038 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02002039 */
2040 void
2041term_channel_closed(channel_T *ch)
2042{
2043 term_T *term;
2044 int did_one = FALSE;
2045
2046 for (term = first_term; term != NULL; term = term->tl_next)
2047 if (term->tl_job == ch->ch_job)
2048 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002049 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002050 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002051
Bram Moolenaard85f2712017-07-28 21:51:57 +02002052 vim_free(term->tl_title);
2053 term->tl_title = NULL;
2054 vim_free(term->tl_status_text);
2055 term->tl_status_text = NULL;
2056
Bram Moolenaar423802d2017-07-30 16:52:24 +02002057 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02002058 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002059 {
2060 int fnum = term->tl_buffer->b_fnum;
2061
Bram Moolenaar423802d2017-07-30 16:52:24 +02002062 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002063
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002064 if (term->tl_finish == 'c')
2065 {
2066 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002067 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002068 curbuf = term->tl_buffer;
2069 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
2070 break;
2071 }
2072 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
2073 {
2074 char buf[50];
2075
2076 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002077 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02002078 vim_snprintf(buf, sizeof(buf),
2079 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02002080 ? "botright sbuf %d"
2081 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002082 do_cmdline_cmd((char_u *)buf);
2083 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002084 else
2085 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002086 }
2087
Bram Moolenaard85f2712017-07-28 21:51:57 +02002088 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002089 }
2090 if (did_one)
2091 {
2092 redraw_statuslines();
2093
2094 /* Need to break out of vgetc(). */
2095 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002096 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002097
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002098 term = curbuf->b_term;
2099 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002100 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002101 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002102 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002103 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002104 }
2105 }
2106}
2107
2108/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002109 * Called to update a window that contains an active terminal.
2110 * Returns FAIL when there is no terminal running in this window or in
2111 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002112 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002113 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002114term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002115{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002116 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002117 VTerm *vterm;
2118 VTermScreen *screen;
2119 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002120 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002121
Bram Moolenaar6d819742017-08-06 14:57:49 +02002122 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002123 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002124
Bram Moolenaard85f2712017-07-28 21:51:57 +02002125 vterm = term->tl_vterm;
2126 screen = vterm_obtain_screen(vterm);
2127 state = vterm_obtain_state(vterm);
2128
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002129 /*
2130 * If the window was resized a redraw will be triggered and we get here.
2131 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2132 */
2133 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2134 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002135 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002136 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2137 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2138 win_T *twp;
2139
2140 FOR_ALL_WINDOWS(twp)
2141 {
2142 /* When more than one window shows the same terminal, use the
2143 * smallest size. */
2144 if (twp->w_buffer == term->tl_buffer)
2145 {
2146 if (!term->tl_rows_fixed && rows > twp->w_height)
2147 rows = twp->w_height;
2148 if (!term->tl_cols_fixed && cols > twp->w_width)
2149 cols = twp->w_width;
2150 }
2151 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002152
2153 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002154 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002155 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002156 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002157 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002158
2159 /* The cursor may have been moved when resizing. */
2160 vterm_state_get_cursorpos(state, &pos);
2161 position_cursor(wp, &pos);
2162
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002163 /* TODO: Only redraw what changed. */
2164 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002165 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002166 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002167 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002168
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002169 if (pos.row < term->tl_rows)
2170 {
2171 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002172 {
2173 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002174 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002175
Bram Moolenaareeac6772017-07-23 15:48:37 +02002176 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2177 vim_memset(&cell, 0, sizeof(cell));
2178
2179 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002180 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002181 if (c == NUL)
2182 {
2183 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002184 if (enc_utf8)
2185 ScreenLinesUC[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002186 }
2187 else
2188 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002189 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002190 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002191 if (c >= 0x80)
2192 {
2193 ScreenLines[off] = ' ';
2194 ScreenLinesUC[off] = c;
2195 }
2196 else
2197 {
2198 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002199 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002200 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002201 }
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002202#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02002203 else if (has_mbyte && c >= 0x80)
2204 {
2205 char_u mb[MB_MAXBYTES+1];
2206 WCHAR wc = c;
2207
2208 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2209 (char*)mb, 2, 0, 0) > 1)
2210 {
2211 ScreenLines[off] = mb[0];
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002212 ScreenLines[off + 1] = mb[1];
Bram Moolenaar740c4332017-08-21 22:01:27 +02002213 cell.width = mb_ptr2cells(mb);
2214 }
2215 else
2216 ScreenLines[off] = c;
2217 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002218#endif
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002219 else
Bram Moolenaar740c4332017-08-21 22:01:27 +02002220 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002221 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002222 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002223
2224 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002225 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002226 if (cell.width == 2)
2227 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002228 if (enc_utf8)
2229 ScreenLinesUC[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002230
2231 /* don't set the second byte to NUL for a DBCS encoding, it
2232 * has been set above */
2233 if (enc_utf8 || !has_mbyte)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002234 ScreenLines[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002235
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002236 ++pos.col;
2237 ++off;
2238 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002239 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002240 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002241 else
2242 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002243
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002244 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2245 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002246 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002247
2248 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002249}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002250
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002251/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002252 * Return TRUE if "wp" is a terminal window where the job has finished.
2253 */
2254 int
2255term_is_finished(buf_T *buf)
2256{
2257 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2258}
2259
2260/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002261 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002262 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002263 */
2264 int
2265term_show_buffer(buf_T *buf)
2266{
2267 term_T *term = buf->b_term;
2268
Bram Moolenaar6d819742017-08-06 14:57:49 +02002269 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002270}
2271
2272/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002273 * The current buffer is going to be changed. If there is terminal
2274 * highlighting remove it now.
2275 */
2276 void
2277term_change_in_curbuf(void)
2278{
2279 term_T *term = curbuf->b_term;
2280
2281 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2282 {
2283 free_scrollback(term);
2284 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002285
2286 /* The buffer is now like a normal buffer, it cannot be easily
2287 * abandoned when changed. */
2288 set_string_option_direct((char_u *)"buftype", -1,
2289 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002290 }
2291}
2292
2293/*
2294 * Get the screen attribute for a position in the buffer.
2295 */
2296 int
2297term_get_attr(buf_T *buf, linenr_T lnum, int col)
2298{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002299 term_T *term = buf->b_term;
2300 sb_line_T *line;
2301 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002302
Bram Moolenaar70229f92017-07-29 16:01:53 +02002303 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002304 return 0;
2305 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2306 if (col >= line->sb_cols)
2307 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002308 cellattr = line->sb_cells + col;
2309 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002310}
2311
2312/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002313 * Create a new vterm and initialize it.
2314 */
2315 static void
2316create_vterm(term_T *term, int rows, int cols)
2317{
2318 VTerm *vterm;
2319 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002320 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002321
2322 vterm = vterm_new(rows, cols);
2323 term->tl_vterm = vterm;
2324 screen = vterm_obtain_screen(vterm);
2325 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2326 /* TODO: depends on 'encoding'. */
2327 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002328
2329 /* Vterm uses a default black background. Set it to white when
2330 * 'background' is "light". */
2331 if (*p_bg == 'l')
2332 {
2333 VTermColor fg, bg;
2334
2335 fg.red = fg.green = fg.blue = 0;
2336 bg.red = bg.green = bg.blue = 255;
2337 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2338 }
2339
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002340 /* Required to initialize most things. */
2341 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002342
2343 /* Allow using alternate screen. */
2344 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002345
Bram Moolenaar58302322017-08-22 20:33:53 +02002346 /* For unix do not use a blinking cursor. In an xterm this causes the
2347 * cursor to blink if it's blinking in the xterm.
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002348 * For Windows we respect the system wide setting. */
Bram Moolenaar58302322017-08-22 20:33:53 +02002349#ifdef WIN3264
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002350 if (GetCaretBlinkTime() == INFINITE)
2351 value.boolean = 0;
2352 else
2353 value.boolean = 1;
Bram Moolenaar58302322017-08-22 20:33:53 +02002354#else
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002355 value.boolean = 0;
Bram Moolenaar58302322017-08-22 20:33:53 +02002356#endif
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002357 vterm_state_set_termprop(vterm_obtain_state(vterm),
2358 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002359}
2360
Bram Moolenaar21554412017-07-24 21:44:43 +02002361/*
2362 * Return the text to show for the buffer name and status.
2363 */
2364 char_u *
2365term_get_status_text(term_T *term)
2366{
2367 if (term->tl_status_text == NULL)
2368 {
2369 char_u *txt;
2370 size_t len;
2371
Bram Moolenaar6d819742017-08-06 14:57:49 +02002372 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002373 {
2374 if (term_job_running(term))
2375 txt = (char_u *)_("Terminal");
2376 else
2377 txt = (char_u *)_("Terminal-finished");
2378 }
2379 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002380 txt = term->tl_title;
2381 else if (term_job_running(term))
2382 txt = (char_u *)_("running");
2383 else
2384 txt = (char_u *)_("finished");
2385 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002386 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002387 if (term->tl_status_text != NULL)
2388 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2389 term->tl_buffer->b_fname, txt);
2390 }
2391 return term->tl_status_text;
2392}
2393
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002394/*
2395 * Mark references in jobs of terminals.
2396 */
2397 int
2398set_ref_in_term(int copyID)
2399{
2400 int abort = FALSE;
2401 term_T *term;
2402 typval_T tv;
2403
2404 for (term = first_term; term != NULL; term = term->tl_next)
2405 if (term->tl_job != NULL)
2406 {
2407 tv.v_type = VAR_JOB;
2408 tv.vval.v_job = term->tl_job;
2409 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2410 }
2411 return abort;
2412}
2413
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002414/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002415 * Get the buffer from the first argument in "argvars".
2416 * Returns NULL when the buffer is not for a terminal window.
2417 */
2418 static buf_T *
2419term_get_buf(typval_T *argvars)
2420{
2421 buf_T *buf;
2422
2423 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2424 ++emsg_off;
2425 buf = get_buf_tv(&argvars[0], FALSE);
2426 --emsg_off;
2427 if (buf == NULL || buf->b_term == NULL)
2428 return NULL;
2429 return buf;
2430}
2431
2432/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002433 * "term_getaltscreen(buf)" function
2434 */
2435 void
2436f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2437{
2438 buf_T *buf = term_get_buf(argvars);
2439
2440 if (buf == NULL)
2441 return;
2442 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2443}
2444
2445/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002446 * "term_getattr(attr, name)" function
2447 */
2448 void
2449f_term_getattr(typval_T *argvars, typval_T *rettv)
2450{
2451 int attr;
2452 size_t i;
2453 char_u *name;
2454
2455 static struct {
2456 char *name;
2457 int attr;
2458 } attrs[] = {
2459 {"bold", HL_BOLD},
2460 {"italic", HL_ITALIC},
2461 {"underline", HL_UNDERLINE},
2462 {"strike", HL_STANDOUT},
2463 {"reverse", HL_INVERSE},
2464 };
2465
2466 attr = get_tv_number(&argvars[0]);
2467 name = get_tv_string_chk(&argvars[1]);
2468 if (name == NULL)
2469 return;
2470
2471 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2472 if (STRCMP(name, attrs[i].name) == 0)
2473 {
2474 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2475 break;
2476 }
2477}
2478
2479/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002480 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002481 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002482 void
2483f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002484{
Bram Moolenaar97870002017-07-30 18:28:38 +02002485 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002486 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002487 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002488 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002489
Bram Moolenaar97870002017-07-30 18:28:38 +02002490 if (rettv_list_alloc(rettv) == FAIL)
2491 return;
2492 if (buf == NULL)
2493 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002494 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002495
2496 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002497 list_append_number(l, term->tl_cursor_pos.row + 1);
2498 list_append_number(l, term->tl_cursor_pos.col + 1);
2499
2500 d = dict_alloc();
2501 if (d != NULL)
2502 {
2503 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
Bram Moolenaar4db25542017-08-28 22:43:05 +02002504 dict_add_nr_str(d, "blink", blink_state_is_inverted()
2505 ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002506 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2507 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2508 ? (char_u *)"" : term->tl_cursor_color);
2509 list_append_dict(l, d);
2510 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002511}
2512
2513/*
2514 * "term_getjob(buf)" function
2515 */
2516 void
2517f_term_getjob(typval_T *argvars, typval_T *rettv)
2518{
2519 buf_T *buf = term_get_buf(argvars);
2520
2521 rettv->v_type = VAR_JOB;
2522 rettv->vval.v_job = NULL;
2523 if (buf == NULL)
2524 return;
2525
2526 rettv->vval.v_job = buf->b_term->tl_job;
2527 if (rettv->vval.v_job != NULL)
2528 ++rettv->vval.v_job->jv_refcount;
2529}
2530
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002531 static int
2532get_row_number(typval_T *tv, term_T *term)
2533{
2534 if (tv->v_type == VAR_STRING
2535 && tv->vval.v_string != NULL
2536 && STRCMP(tv->vval.v_string, ".") == 0)
2537 return term->tl_cursor_pos.row;
2538 return (int)get_tv_number(tv) - 1;
2539}
2540
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002541/*
2542 * "term_getline(buf, row)" function
2543 */
2544 void
2545f_term_getline(typval_T *argvars, typval_T *rettv)
2546{
2547 buf_T *buf = term_get_buf(argvars);
2548 term_T *term;
2549 int row;
2550
2551 rettv->v_type = VAR_STRING;
2552 if (buf == NULL)
2553 return;
2554 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002555 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002556
2557 if (term->tl_vterm == NULL)
2558 {
2559 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2560
2561 /* vterm is finished, get the text from the buffer */
2562 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2563 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2564 }
2565 else
2566 {
2567 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2568 VTermRect rect;
2569 int len;
2570 char_u *p;
2571
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002572 if (row < 0 || row >= term->tl_rows)
2573 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002574 len = term->tl_cols * MB_MAXBYTES + 1;
2575 p = alloc(len);
2576 if (p == NULL)
2577 return;
2578 rettv->vval.v_string = p;
2579
2580 rect.start_col = 0;
2581 rect.end_col = term->tl_cols;
2582 rect.start_row = row;
2583 rect.end_row = row + 1;
2584 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2585 }
2586}
2587
2588/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002589 * "term_getscrolled(buf)" function
2590 */
2591 void
2592f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2593{
2594 buf_T *buf = term_get_buf(argvars);
2595
2596 if (buf == NULL)
2597 return;
2598 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2599}
2600
2601/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002602 * "term_getsize(buf)" function
2603 */
2604 void
2605f_term_getsize(typval_T *argvars, typval_T *rettv)
2606{
2607 buf_T *buf = term_get_buf(argvars);
2608 list_T *l;
2609
2610 if (rettv_list_alloc(rettv) == FAIL)
2611 return;
2612 if (buf == NULL)
2613 return;
2614
2615 l = rettv->vval.v_list;
2616 list_append_number(l, buf->b_term->tl_rows);
2617 list_append_number(l, buf->b_term->tl_cols);
2618}
2619
2620/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002621 * "term_getstatus(buf)" function
2622 */
2623 void
2624f_term_getstatus(typval_T *argvars, typval_T *rettv)
2625{
2626 buf_T *buf = term_get_buf(argvars);
2627 term_T *term;
2628 char_u val[100];
2629
2630 rettv->v_type = VAR_STRING;
2631 if (buf == NULL)
2632 return;
2633 term = buf->b_term;
2634
2635 if (term_job_running(term))
2636 STRCPY(val, "running");
2637 else
2638 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002639 if (term->tl_normal_mode)
2640 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002641 rettv->vval.v_string = vim_strsave(val);
2642}
2643
2644/*
2645 * "term_gettitle(buf)" function
2646 */
2647 void
2648f_term_gettitle(typval_T *argvars, typval_T *rettv)
2649{
2650 buf_T *buf = term_get_buf(argvars);
2651
2652 rettv->v_type = VAR_STRING;
2653 if (buf == NULL)
2654 return;
2655
2656 if (buf->b_term->tl_title != NULL)
2657 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2658}
2659
2660/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002661 * "term_gettty(buf)" function
2662 */
2663 void
2664f_term_gettty(typval_T *argvars, typval_T *rettv)
2665{
2666 buf_T *buf = term_get_buf(argvars);
2667 char_u *p;
2668
2669 rettv->v_type = VAR_STRING;
2670 if (buf == NULL)
2671 return;
2672 if (buf->b_term->tl_job != NULL)
2673 p = buf->b_term->tl_job->jv_tty_name;
2674 else
2675 p = buf->b_term->tl_tty_name;
2676 if (p != NULL)
2677 rettv->vval.v_string = vim_strsave(p);
2678}
2679
2680/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002681 * "term_list()" function
2682 */
2683 void
2684f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2685{
2686 term_T *tp;
2687 list_T *l;
2688
2689 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2690 return;
2691
2692 l = rettv->vval.v_list;
2693 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2694 if (tp != NULL && tp->tl_buffer != NULL)
2695 if (list_append_number(l,
2696 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2697 return;
2698}
2699
2700/*
2701 * "term_scrape(buf, row)" function
2702 */
2703 void
2704f_term_scrape(typval_T *argvars, typval_T *rettv)
2705{
2706 buf_T *buf = term_get_buf(argvars);
2707 VTermScreen *screen = NULL;
2708 VTermPos pos;
2709 list_T *l;
2710 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002711 char_u *p;
2712 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002713
2714 if (rettv_list_alloc(rettv) == FAIL)
2715 return;
2716 if (buf == NULL)
2717 return;
2718 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002719
2720 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002721 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002722
2723 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002724 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002725 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002726 p = NULL;
2727 line = NULL;
2728 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002729 else
2730 {
2731 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2732
2733 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2734 return;
2735 p = ml_get_buf(buf, lnum + 1, FALSE);
2736 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2737 }
2738
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002739 for (pos.col = 0; pos.col < term->tl_cols; )
2740 {
2741 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002742 int width;
2743 VTermScreenCellAttrs attrs;
2744 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002745 char_u rgb[8];
2746 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2747 int off = 0;
2748 int i;
2749
2750 if (screen == NULL)
2751 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002752 cellattr_T *cellattr;
2753 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002754
2755 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002756 if (pos.col >= line->sb_cols)
2757 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002758 cellattr = line->sb_cells + pos.col;
2759 width = cellattr->width;
2760 attrs = cellattr->attrs;
2761 fg = cellattr->fg;
2762 bg = cellattr->bg;
2763 len = MB_PTR2LEN(p);
2764 mch_memmove(mbs, p, len);
2765 mbs[len] = NUL;
2766 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002767 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002768 else
2769 {
2770 VTermScreenCell cell;
2771 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2772 break;
2773 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2774 {
2775 if (cell.chars[i] == 0)
2776 break;
2777 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2778 }
2779 mbs[off] = NUL;
2780 width = cell.width;
2781 attrs = cell.attrs;
2782 fg = cell.fg;
2783 bg = cell.bg;
2784 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002785 dcell = dict_alloc();
2786 list_append_dict(l, dcell);
2787
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002788 dict_add_nr_str(dcell, "chars", 0, mbs);
2789
2790 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002791 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002792 dict_add_nr_str(dcell, "fg", 0, rgb);
2793 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002794 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002795 dict_add_nr_str(dcell, "bg", 0, rgb);
2796
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002797 dict_add_nr_str(dcell, "attr",
2798 cell2attr(attrs, fg, bg), NULL);
2799 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002800
2801 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002802 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002803 ++pos.col;
2804 }
2805}
2806
2807/*
2808 * "term_sendkeys(buf, keys)" function
2809 */
2810 void
2811f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2812{
2813 buf_T *buf = term_get_buf(argvars);
2814 char_u *msg;
2815 term_T *term;
2816
2817 rettv->v_type = VAR_UNKNOWN;
2818 if (buf == NULL)
2819 return;
2820
2821 msg = get_tv_string_chk(&argvars[1]);
2822 if (msg == NULL)
2823 return;
2824 term = buf->b_term;
2825 if (term->tl_vterm == NULL)
2826 return;
2827
2828 while (*msg != NUL)
2829 {
2830 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2831 msg += MB_PTR2LEN(msg);
2832 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002833}
2834
2835/*
2836 * "term_start(command, options)" function
2837 */
2838 void
2839f_term_start(typval_T *argvars, typval_T *rettv)
2840{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002841 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002842
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002843 init_job_options(&opt);
2844 /* TODO: allow more job options */
2845 if (argvars[1].v_type != VAR_UNKNOWN
2846 && get_job_options(&argvars[1], &opt,
2847 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002848 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
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
2890 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002891 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002892 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2893 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002894 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002895 parse_queued_messages();
2896 ui_delay(10L, FALSE);
2897 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002898 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002899 parse_queued_messages();
2900 }
2901 else
2902 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002903 long wait = 10L;
2904
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002905 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002906 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002907
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002908 /* Wait for some time for any channel I/O. */
2909 if (argvars[1].v_type != VAR_UNKNOWN)
2910 wait = get_tv_number(&argvars[1]);
2911 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002912 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002913
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002914 /* Flushing messages on channels is hopefully sufficient.
2915 * TODO: is there a better way? */
2916 parse_queued_messages();
2917 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002918}
2919
Bram Moolenaara83e3962017-08-17 14:39:07 +02002920# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002921
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002922/**************************************
2923 * 2. MS-Windows implementation.
2924 */
2925
Bram Moolenaara83e3962017-08-17 14:39:07 +02002926# ifndef PROTO
2927
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002928#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2929#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2930
Bram Moolenaar8a773062017-07-24 22:29:21 +02002931void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002932void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002933void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002934BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2935void (*winpty_config_set_initial_size)(void*, int, int);
2936LPCWSTR (*winpty_conin_name)(void*);
2937LPCWSTR (*winpty_conout_name)(void*);
2938LPCWSTR (*winpty_conerr_name)(void*);
2939void (*winpty_free)(void*);
2940void (*winpty_config_free)(void*);
2941void (*winpty_spawn_config_free)(void*);
2942void (*winpty_error_free)(void*);
2943LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002944BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002945HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002946
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002947#define WINPTY_DLL "winpty.dll"
2948
2949static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002950# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002951
Bram Moolenaara83e3962017-08-17 14:39:07 +02002952 static int
2953dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002954{
2955 int i;
2956 static struct
2957 {
2958 char *name;
2959 FARPROC *ptr;
2960 } winpty_entry[] =
2961 {
2962 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2963 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2964 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2965 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2966 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2967 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2968 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2969 {"winpty_free", (FARPROC*)&winpty_free},
2970 {"winpty_open", (FARPROC*)&winpty_open},
2971 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2972 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2973 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2974 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002975 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002976 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002977 {NULL, NULL}
2978 };
2979
2980 /* No need to initialize twice. */
2981 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002982 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002983 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2984 * winpty.dll. */
2985 if (*p_winptydll != NUL)
2986 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2987 if (!hWinPtyDLL)
2988 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002989 if (!hWinPtyDLL)
2990 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002991 if (verbose)
2992 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2993 : (char_u *)WINPTY_DLL);
2994 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002995 }
2996 for (i = 0; winpty_entry[i].name != NULL
2997 && winpty_entry[i].ptr != NULL; ++i)
2998 {
2999 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
3000 winpty_entry[i].name)) == NULL)
3001 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02003002 if (verbose)
3003 EMSG2(_(e_loadfunc), winpty_entry[i].name);
3004 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003005 }
3006 }
3007
Bram Moolenaara83e3962017-08-17 14:39:07 +02003008 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003009}
3010
3011/*
3012 * Create a new terminal of "rows" by "cols" cells.
3013 * Store a reference in "term".
3014 * Return OK or FAIL.
3015 */
3016 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003017term_and_job_init(
3018 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02003019 typval_T *argvar,
3020 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003021{
Bram Moolenaar5983d502017-08-20 19:22:56 +02003022 WCHAR *cmd_wchar = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003023 channel_T *channel = NULL;
3024 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003025 DWORD error;
Bram Moolenaar5983d502017-08-20 19:22:56 +02003026 HANDLE jo = NULL;
3027 HANDLE child_process_handle;
3028 HANDLE child_thread_handle;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003029 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003030 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003031 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003032 garray_T ga;
3033 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003034
Bram Moolenaara83e3962017-08-17 14:39:07 +02003035 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003036 return FAIL;
3037
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003038 if (argvar->v_type == VAR_STRING)
3039 cmd = argvar->vval.v_string;
3040 else
3041 {
3042 ga_init2(&ga, (int)sizeof(char*), 20);
3043 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
3044 goto failed;
3045 cmd = ga.ga_data;
3046 }
3047
Bram Moolenaar5983d502017-08-20 19:22:56 +02003048 cmd_wchar = enc_to_utf16(cmd, NULL);
3049 if (cmd_wchar == NULL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003050 return FAIL;
3051
3052 job = job_alloc();
3053 if (job == NULL)
3054 goto failed;
3055
3056 channel = add_channel();
3057 if (channel == NULL)
3058 goto failed;
3059
3060 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
3061 if (term->tl_winpty_config == NULL)
3062 goto failed;
3063
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003064 winpty_config_set_initial_size(term->tl_winpty_config,
3065 term->tl_cols, term->tl_rows);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003066 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
3067 if (term->tl_winpty == NULL)
3068 goto failed;
3069
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003070 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003071 spawn_config = winpty_spawn_config_new(
3072 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
3073 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
3074 NULL,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003075 cmd_wchar,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003076 NULL,
3077 NULL,
3078 &winpty_err);
3079 if (spawn_config == NULL)
3080 goto failed;
3081
3082 channel = add_channel();
3083 if (channel == NULL)
3084 goto failed;
3085
3086 job = job_alloc();
3087 if (job == NULL)
3088 goto failed;
3089
Bram Moolenaar5983d502017-08-20 19:22:56 +02003090 if (opt->jo_set & JO_IN_BUF)
3091 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
3092
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003093 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
3094 &child_thread_handle, &error, &winpty_err))
3095 goto failed;
3096
3097 channel_set_pipes(channel,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003098 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003099 winpty_conin_name(term->tl_winpty),
3100 GENERIC_WRITE, 0, NULL,
3101 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003102 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003103 winpty_conout_name(term->tl_winpty),
3104 GENERIC_READ, 0, NULL,
3105 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003106 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003107 winpty_conerr_name(term->tl_winpty),
3108 GENERIC_READ, 0, NULL,
3109 OPEN_EXISTING, 0, NULL));
3110
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02003111 /* Write lines with CR instead of NL. */
3112 channel->ch_write_text_mode = TRUE;
3113
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003114 jo = CreateJobObject(NULL, NULL);
3115 if (jo == NULL)
3116 goto failed;
3117
3118 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003119 {
3120 /* Failed, switch the way to terminate process with TerminateProcess. */
3121 CloseHandle(jo);
3122 jo = NULL;
3123 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003124
3125 winpty_spawn_config_free(spawn_config);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003126 vim_free(cmd_wchar);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003127
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003128 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003129
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02003130 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02003131 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003132
3133 job->jv_channel = channel;
3134 job->jv_proc_info.hProcess = child_process_handle;
3135 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
3136 job->jv_job_object = jo;
3137 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003138 sprintf(buf, "winpty://%lu",
3139 GetProcessId(winpty_agent_process(term->tl_winpty)));
3140 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003141 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003142 term->tl_job = job;
3143
3144 return OK;
3145
3146failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003147 if (argvar->v_type == VAR_LIST)
3148 vim_free(ga.ga_data);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003149 if (cmd_wchar != NULL)
3150 vim_free(cmd_wchar);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003151 if (spawn_config != NULL)
3152 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003153 if (channel != NULL)
3154 channel_clear(channel);
3155 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003156 {
3157 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003158 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003159 }
3160 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003161 if (jo != NULL)
3162 CloseHandle(jo);
3163 if (term->tl_winpty != NULL)
3164 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003165 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003166 if (term->tl_winpty_config != NULL)
3167 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003168 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003169 if (winpty_err != NULL)
3170 {
3171 char_u *msg = utf16_to_enc(
3172 (short_u *)winpty_error_msg(winpty_err), NULL);
3173
3174 EMSG(msg);
3175 winpty_error_free(winpty_err);
3176 }
3177 return FAIL;
3178}
3179
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003180 static int
3181create_pty_only(term_T *term, jobopt_T *opt)
3182{
3183 /* TODO: implement this */
3184 return FAIL;
3185}
3186
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003187/*
3188 * Free the terminal emulator part of "term".
3189 */
3190 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003191term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003192{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003193 if (term->tl_winpty != NULL)
3194 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003195 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003196 if (term->tl_winpty_config != NULL)
3197 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003198 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003199 if (term->tl_vterm != NULL)
3200 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003201 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003202}
3203
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003204/*
3205 * Request size to terminal.
3206 */
3207 static void
3208term_report_winsize(term_T *term, int rows, int cols)
3209{
3210 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3211}
3212
Bram Moolenaara83e3962017-08-17 14:39:07 +02003213 int
3214terminal_enabled(void)
3215{
3216 return dyn_winpty_init(FALSE) == OK;
3217}
3218
Bram Moolenaar3346cc42017-09-02 14:54:21 +02003219/*
3220 * Called when a channel has sent all the lines to a terminal.
3221 * Send a CTRL-D to mark the end of the text.
3222 */
3223 void
3224term_send_eof(channel_T *ch)
3225{
3226 term_T *term;
3227
3228 for (term = first_term; term != NULL; term = term->tl_next)
3229 if (term->tl_job == ch->ch_job)
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02003230 {
3231 if (term->tl_eof_chars != NULL)
3232 channel_send(ch, PART_IN, term->tl_eof_chars,
3233 (int)STRLEN(term->tl_eof_chars), NULL);
3234 else
3235 /* Default: CTRL-D */
3236 channel_send(ch, PART_IN, (char_u *)"\004", 1, NULL);
3237 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
3238 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02003239}
Bram Moolenaara83e3962017-08-17 14:39:07 +02003240
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003241# else
3242
3243/**************************************
3244 * 3. Unix-like implementation.
3245 */
3246
3247/*
3248 * Create a new terminal of "rows" by "cols" cells.
3249 * Start job for "cmd".
3250 * Store the pointers in "term".
3251 * Return OK or FAIL.
3252 */
3253 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003254term_and_job_init(
3255 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02003256 typval_T *argvar,
3257 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003258{
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003259 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003260
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003261 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003262 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003263 if (term->tl_job != NULL)
3264 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003265
Bram Moolenaar61a66052017-07-22 18:39:00 +02003266 return term->tl_job != NULL
3267 && term->tl_job->jv_channel != NULL
3268 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003269}
3270
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003271 static int
3272create_pty_only(term_T *term, jobopt_T *opt)
3273{
3274 int ret;
3275
3276 create_vterm(term, term->tl_rows, term->tl_cols);
3277
3278 term->tl_job = job_alloc();
3279 if (term->tl_job == NULL)
3280 return FAIL;
3281 ++term->tl_job->jv_refcount;
3282
3283 /* behave like the job is already finished */
3284 term->tl_job->jv_status = JOB_FINISHED;
3285
3286 ret = mch_create_pty_channel(term->tl_job, opt);
3287
3288 return ret;
3289}
3290
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003291/*
3292 * Free the terminal emulator part of "term".
3293 */
3294 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003295term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003296{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003297 if (term->tl_vterm != NULL)
3298 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003299 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003300}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003301
3302/*
3303 * Request size to terminal.
3304 */
3305 static void
3306term_report_winsize(term_T *term, int rows, int cols)
3307{
3308 /* Use an ioctl() to report the new window size to the job. */
3309 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3310 {
3311 int fd = -1;
3312 int part;
3313
3314 for (part = PART_OUT; part < PART_COUNT; ++part)
3315 {
3316 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3317 if (isatty(fd))
3318 break;
3319 }
3320 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003321 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003322 }
3323}
3324
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003325# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003326
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003327#endif /* FEAT_TERMINAL */