blob: 0a3e1b71f0ac23dc4b02119c7901237f17577753 [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 Moolenaardd693ce2017-08-10 23:15:19 +0200499 else
500 {
501 if (*p)
502 *p = NUL;
503 EMSG2(_("E181: Invalid attribute: %s"), cmd);
504 return;
505 }
506 cmd = skipwhite(p);
507 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200508 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200509 /* Make a copy, an autocommand may set 'shell'. */
510 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200511
Bram Moolenaarb2412082017-08-20 18:09:14 +0200512 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200513 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200514 /* Write lines from current buffer to the job. */
515 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
516 opt.jo_io[PART_IN] = JIO_BUFFER;
517 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
518 opt.jo_in_top = eap->line1;
519 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200520 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200521
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200522 argvar.v_type = VAR_STRING;
523 argvar.vval.v_string = cmd;
524 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200525 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200526}
527
528/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200529 * Free the scrollback buffer for "term".
530 */
531 static void
532free_scrollback(term_T *term)
533{
534 int i;
535
536 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
537 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
538 ga_clear(&term->tl_scrollback);
539}
540
541/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200542 * Free a terminal and everything it refers to.
543 * Kills the job if there is one.
544 * Called when wiping out a buffer.
545 */
546 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200547free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200548{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200549 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200550 term_T *tp;
551
552 if (term == NULL)
553 return;
554 if (first_term == term)
555 first_term = term->tl_next;
556 else
557 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
558 if (tp->tl_next == term)
559 {
560 tp->tl_next = term->tl_next;
561 break;
562 }
563
564 if (term->tl_job != NULL)
565 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200566 if (term->tl_job->jv_status != JOB_ENDED
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200567 && term->tl_job->jv_status != JOB_FINISHED
568 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200569 job_stop(term->tl_job, NULL, "kill");
570 job_unref(term->tl_job);
571 }
572
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200573 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200574
575 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200576 vim_free(term->tl_title);
577 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200578 vim_free(term->tl_opencmd);
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200579# ifdef WIN3264
580 vim_free(term->tl_eof_chars);
581# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200582 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200583 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200584 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200585 if (in_terminal_loop == term)
586 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200587}
588
589/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200590 * Write job output "msg[len]" to the vterm.
591 */
592 static void
593term_write_job_output(term_T *term, char_u *msg, size_t len)
594{
595 VTerm *vterm = term->tl_vterm;
596 char_u *p;
597 size_t done;
598 size_t len_now;
599
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200600 if (term_nl_does_cr)
601 vterm_input_write(vterm, (char *)msg, len);
602 else
603 /* need to convert NL to CR-NL */
604 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200605 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200606 for (p = msg + done; p < msg + len; )
607 {
608 if (*p == NL)
609 break;
610 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
611 }
612 len_now = p - msg - done;
613 vterm_input_write(vterm, (char *)msg + done, len_now);
614 if (p < msg + len && *p == NL)
615 {
616 vterm_input_write(vterm, "\r\n", 2);
617 ++len_now;
618 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200619 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200620
621 /* this invokes the damage callbacks */
622 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
623}
624
Bram Moolenaar1c844932017-07-24 23:36:41 +0200625 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200626update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200627{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200628 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200629 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200630 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200631 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200632 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200633 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200634 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200635 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200636#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200637 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200638 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200639#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200640 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200641}
642
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200643/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200644 * Invoked when "msg" output from a job was received. Write it to the terminal
645 * of "buffer".
646 */
647 void
648write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
649{
650 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200651 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200652
Bram Moolenaard85f2712017-07-28 21:51:57 +0200653 if (term->tl_vterm == NULL)
654 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200655 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200656 return;
657 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200658 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200659 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200660
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200661 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
662 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200663 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200664 {
665 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200666 ch_log(term->tl_job->jv_channel, "updating screen");
667 if (buffer == curbuf)
668 {
669 update_screen(0);
670 update_cursor(term, TRUE);
671 }
672 else
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200673 redraw_after_callback(TRUE);
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200674 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200675}
676
677/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200678 * Send a mouse position and click to the vterm
679 */
680 static int
681term_send_mouse(VTerm *vterm, int button, int pressed)
682{
683 VTermModifier mod = VTERM_MOD_NONE;
684
685 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
686 mouse_col - W_WINCOL(curwin), mod);
687 vterm_mouse_button(vterm, button, pressed, mod);
688 return TRUE;
689}
690
691/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200692 * Convert typed key "c" into bytes to send to the job.
693 * Return the number of bytes in "buf".
694 */
695 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200696term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200697{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200698 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200699 VTermKey key = VTERM_KEY_NONE;
700 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200701 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200702
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200703 switch (c)
704 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200705 case CAR: c = term_enter_char; break;
706 /* don't use VTERM_KEY_BACKSPACE, it always
707 * becomes 0x7f DEL */
708 case K_BS: c = term_backspace_char; break;
709
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200710 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200711 case K_DEL: key = VTERM_KEY_DEL; break;
712 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200713 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
714 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200715 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200716 case K_S_END: mod = VTERM_MOD_SHIFT;
717 key = VTERM_KEY_END; break;
718 case K_C_END: mod = VTERM_MOD_CTRL;
719 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200720 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
721 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
722 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
723 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
724 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
725 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
726 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
727 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
728 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
729 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
730 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
731 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
732 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200733 case K_S_HOME: mod = VTERM_MOD_SHIFT;
734 key = VTERM_KEY_HOME; break;
735 case K_C_HOME: mod = VTERM_MOD_CTRL;
736 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200737 case K_INS: key = VTERM_KEY_INS; break;
738 case K_K0: key = VTERM_KEY_KP_0; break;
739 case K_K1: key = VTERM_KEY_KP_1; break;
740 case K_K2: key = VTERM_KEY_KP_2; break;
741 case K_K3: key = VTERM_KEY_KP_3; break;
742 case K_K4: key = VTERM_KEY_KP_4; break;
743 case K_K5: key = VTERM_KEY_KP_5; break;
744 case K_K6: key = VTERM_KEY_KP_6; break;
745 case K_K7: key = VTERM_KEY_KP_7; break;
746 case K_K8: key = VTERM_KEY_KP_8; break;
747 case K_K9: key = VTERM_KEY_KP_9; break;
748 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
749 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
750 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
751 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
752 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
753 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
754 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
755 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
756 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
757 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
758 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
759 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
760 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200761 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
762 key = VTERM_KEY_LEFT; break;
763 case K_C_LEFT: mod = VTERM_MOD_CTRL;
764 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200765 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
766 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
767 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200768 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
769 key = VTERM_KEY_RIGHT; break;
770 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
771 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200772 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200773 case K_S_UP: mod = VTERM_MOD_SHIFT;
774 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200775 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200776
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200777 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
778 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
779 case K_MOUSELEFT: /* TODO */ return 0;
780 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200781
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200782 case K_LEFTMOUSE:
783 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
784 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
785 case K_LEFTRELEASE:
786 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
787 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
788 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
789 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
790 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
791 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
792 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
793 case K_X1MOUSE: /* TODO */ return 0;
794 case K_X1DRAG: /* TODO */ return 0;
795 case K_X1RELEASE: /* TODO */ return 0;
796 case K_X2MOUSE: /* TODO */ return 0;
797 case K_X2DRAG: /* TODO */ return 0;
798 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200799
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200800 case K_IGNORE: return 0;
801 case K_NOP: return 0;
802 case K_UNDO: return 0;
803 case K_HELP: return 0;
804 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
805 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
806 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
807 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
808 case K_SELECT: return 0;
809#ifdef FEAT_GUI
810 case K_VER_SCROLLBAR: return 0;
811 case K_HOR_SCROLLBAR: return 0;
812#endif
813#ifdef FEAT_GUI_TABLINE
814 case K_TABLINE: return 0;
815 case K_TABMENU: return 0;
816#endif
817#ifdef FEAT_NETBEANS_INTG
818 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
819#endif
820#ifdef FEAT_DND
821 case K_DROP: return 0;
822#endif
823#ifdef FEAT_AUTOCMD
824 case K_CURSORHOLD: return 0;
825#endif
826 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
827 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200828 }
829
830 /*
831 * Convert special keys to vterm keys:
832 * - Write keys to vterm: vterm_keyboard_key()
833 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200834 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200835 */
836 if (key != VTERM_KEY_NONE)
837 /* Special key, let vterm convert it. */
838 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200839 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200840 /* Normal character, let vterm convert it. */
841 vterm_keyboard_unichar(vterm, c, mod);
842
843 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200844 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200845}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200846
Bram Moolenaar938783d2017-07-16 20:13:26 +0200847/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200848 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200849 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200850 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200851term_job_running(term_T *term)
852{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200853 /* Also consider the job finished when the channel is closed, to avoid a
854 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200855 return term != NULL
856 && term->tl_job != NULL
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200857 && channel_is_open(term->tl_job->jv_channel)
858 && (term->tl_job->jv_status == JOB_STARTED
859 || term->tl_job->jv_channel->ch_keep_open);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200860}
861
862/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200863 * Add the last line of the scrollback buffer to the buffer in the window.
864 */
865 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200866add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200867{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200868 buf_T *buf = term->tl_buffer;
869 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
870 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200871
Bram Moolenaar58302322017-08-22 20:33:53 +0200872#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +0200873 if (!enc_utf8 && enc_codepage > 0)
874 {
875 WCHAR *ret = NULL;
876 int length = 0;
877
878 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
879 &ret, &length);
880 if (ret != NULL)
881 {
882 WideCharToMultiByte_alloc(enc_codepage, 0,
883 ret, length, (char **)&text, &len, 0, 0);
884 vim_free(ret);
885 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
886 vim_free(text);
887 }
888 }
889 else
890#endif
891 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200892 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200893 {
894 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200895 curbuf = buf;
896 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200897 curbuf = curwin->w_buffer;
898 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200899}
900
901/*
902 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200903 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200904 */
905 static void
906move_terminal_to_buffer(term_T *term)
907{
908 win_T *wp;
909 int len;
910 int lines_skipped = 0;
911 VTermPos pos;
912 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200913 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200914 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200915
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200916 if (term->tl_vterm == NULL)
917 return;
918 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200919 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
920 {
921 len = 0;
922 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
923 if (vterm_screen_get_cell(screen, pos, &cell) != 0
924 && cell.chars[0] != NUL)
925 len = pos.col + 1;
926
927 if (len == 0)
928 ++lines_skipped;
929 else
930 {
931 while (lines_skipped > 0)
932 {
933 /* Line was skipped, add an empty line. */
934 --lines_skipped;
935 if (ga_grow(&term->tl_scrollback, 1) == OK)
936 {
937 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
938 + term->tl_scrollback.ga_len;
939
940 line->sb_cols = 0;
941 line->sb_cells = NULL;
942 ++term->tl_scrollback.ga_len;
943
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200944 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200945 }
946 }
947
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200948 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200949 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
950 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200951 garray_T ga;
952 int width;
953 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200954 + term->tl_scrollback.ga_len;
955
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200956 ga_init2(&ga, 1, 100);
957 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200958 {
959 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200960 {
961 width = 1;
962 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
963 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +0200964 ga.ga_len += utf_char2bytes(' ',
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200965 (char_u *)ga.ga_data + ga.ga_len);
966 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200967 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200968 {
969 width = cell.width;
970
971 p[pos.col].width = cell.width;
972 p[pos.col].attrs = cell.attrs;
973 p[pos.col].fg = cell.fg;
974 p[pos.col].bg = cell.bg;
975
976 if (ga_grow(&ga, MB_MAXBYTES) == OK)
977 {
978 int i;
979 int c;
980
981 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +0200982 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200983 (char_u *)ga.ga_data + ga.ga_len);
984 }
985 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200986 }
987 line->sb_cols = len;
988 line->sb_cells = p;
989 ++term->tl_scrollback.ga_len;
990
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200991 if (ga_grow(&ga, 1) == FAIL)
992 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
993 else
994 {
995 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
996 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
997 }
998 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200999 }
1000 else
1001 vim_free(p);
1002 }
1003 }
1004
1005 FOR_ALL_WINDOWS(wp)
1006 {
1007 if (wp->w_buffer == term->tl_buffer)
1008 {
1009 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1010 wp->w_cursor.col = 0;
1011 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +02001012 if (wp->w_cursor.lnum >= wp->w_height)
1013 {
1014 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
1015
1016 if (wp->w_topline < min_topline)
1017 wp->w_topline = min_topline;
1018 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02001019 redraw_win_later(wp, NOT_VALID);
1020 }
1021 }
1022}
1023
1024 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001025set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001026{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001027 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001028 vim_free(term->tl_status_text);
1029 term->tl_status_text = NULL;
1030 if (term->tl_buffer == curbuf)
1031 maketitle();
1032}
1033
1034/*
1035 * Called after the job if finished and Terminal mode is not active:
1036 * Move the vterm contents into the scrollback buffer and free the vterm.
1037 */
1038 static void
1039cleanup_vterm(term_T *term)
1040{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001041 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001042 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001043 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001044 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001045}
1046
1047/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001048 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001049 * Suspends updating the terminal window.
1050 */
1051 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001052term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001053{
1054 term_T *term = curbuf->b_term;
1055
1056 /* Append the current terminal contents to the buffer. */
1057 move_terminal_to_buffer(term);
1058
Bram Moolenaar6d819742017-08-06 14:57:49 +02001059 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001060
Bram Moolenaar6d819742017-08-06 14:57:49 +02001061 /* Move the window cursor to the position of the cursor in the
1062 * terminal. */
1063 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1064 + term->tl_cursor_pos.row + 1;
1065 check_cursor();
1066 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001067
Bram Moolenaar6d819742017-08-06 14:57:49 +02001068 /* Display the same lines as in the terminal. */
1069 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001070}
1071
1072/*
1073 * Returns TRUE if the current window contains a terminal and we are in
1074 * Terminal-Normal mode.
1075 */
1076 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001077term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001078{
1079 term_T *term = curbuf->b_term;
1080
Bram Moolenaar6d819742017-08-06 14:57:49 +02001081 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001082}
1083
1084/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001085 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001086 * Restores updating the terminal window.
1087 */
1088 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001089term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001090{
1091 term_T *term = curbuf->b_term;
1092 sb_line_T *line;
1093 garray_T *gap;
1094
1095 /* Remove the terminal contents from the scrollback and the buffer. */
1096 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001097 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1098 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001099 {
1100 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1101 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1102 vim_free(line->sb_cells);
1103 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001104 }
1105 check_cursor();
1106
Bram Moolenaar6d819742017-08-06 14:57:49 +02001107 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001108
1109 if (term->tl_channel_closed)
1110 cleanup_vterm(term);
1111 redraw_buf_and_status_later(curbuf, NOT_VALID);
1112}
1113
1114/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001115 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001116 * Note: while waiting a terminal may be closed and freed if the channel is
1117 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001118 * TODO: use terminal mode mappings.
1119 */
1120 static int
1121term_vgetc()
1122{
1123 int c;
1124
1125 ++no_mapping;
1126 ++allow_keys;
1127 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001128#ifdef WIN3264
1129 ctrl_break_was_pressed = FALSE;
1130#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001131 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001132 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001133 --no_mapping;
1134 --allow_keys;
1135 return c;
1136}
1137
1138/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001139 * Get the part that is connected to the tty. Normally this is PART_IN, but
1140 * when writing buffer lines to the job it can be another. This makes it
1141 * possible to do "1,5term vim -".
1142 */
1143 static ch_part_T
1144get_tty_part(term_T *term)
1145{
1146#ifdef UNIX
1147 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1148 int i;
1149
1150 for (i = 0; i < 3; ++i)
1151 {
1152 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1153
1154 if (isatty(fd))
1155 return parts[i];
1156 }
1157#endif
1158 return PART_IN;
1159}
1160
1161/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001162 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001163 * Return FAIL when the key needs to be handled in Normal mode.
1164 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001165 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001166 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001167send_keys_to_term(term_T *term, int c, int typed)
1168{
1169 char msg[KEY_BUF_LEN];
1170 size_t len;
1171 static int mouse_was_outside = FALSE;
1172 int dragging_outside = FALSE;
1173
1174 /* Catch keys that need to be handled as in Normal mode. */
1175 switch (c)
1176 {
1177 case NUL:
1178 case K_ZERO:
1179 if (typed)
1180 stuffcharReadbuff(c);
1181 return FAIL;
1182
1183 case K_IGNORE:
1184 return FAIL;
1185
1186 case K_LEFTDRAG:
1187 case K_MIDDLEDRAG:
1188 case K_RIGHTDRAG:
1189 case K_X1DRAG:
1190 case K_X2DRAG:
1191 dragging_outside = mouse_was_outside;
1192 /* FALLTHROUGH */
1193 case K_LEFTMOUSE:
1194 case K_LEFTMOUSE_NM:
1195 case K_LEFTRELEASE:
1196 case K_LEFTRELEASE_NM:
1197 case K_MIDDLEMOUSE:
1198 case K_MIDDLERELEASE:
1199 case K_RIGHTMOUSE:
1200 case K_RIGHTRELEASE:
1201 case K_X1MOUSE:
1202 case K_X1RELEASE:
1203 case K_X2MOUSE:
1204 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001205
1206 case K_MOUSEUP:
1207 case K_MOUSEDOWN:
1208 case K_MOUSELEFT:
1209 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001210 if (mouse_row < W_WINROW(curwin)
1211 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1212 || mouse_col < W_WINCOL(curwin)
1213 || mouse_col >= W_ENDCOL(curwin)
1214 || dragging_outside)
1215 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001216 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001217 if (typed)
1218 {
1219 stuffcharReadbuff(c);
1220 mouse_was_outside = TRUE;
1221 }
1222 return FAIL;
1223 }
1224 }
1225 if (typed)
1226 mouse_was_outside = FALSE;
1227
1228 /* Convert the typed key to a sequence of bytes for the job. */
1229 len = term_convert_key(term, c, msg);
1230 if (len > 0)
1231 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001232 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1233 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001234
1235 return OK;
1236}
1237
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001238 static void
1239position_cursor(win_T *wp, VTermPos *pos)
1240{
1241 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1242 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1243 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1244}
1245
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001246/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001247 * Handle CTRL-W "": send register contents to the job.
1248 */
1249 static void
1250term_paste_register(int prev_c UNUSED)
1251{
1252 int c;
1253 list_T *l;
1254 listitem_T *item;
1255 long reglen = 0;
1256 int type;
1257
1258#ifdef FEAT_CMDL_INFO
1259 if (add_to_showcmd(prev_c))
1260 if (add_to_showcmd('"'))
1261 out_flush();
1262#endif
1263 c = term_vgetc();
1264#ifdef FEAT_CMDL_INFO
1265 clear_showcmd();
1266#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001267 if (!term_use_loop())
1268 /* job finished while waiting for a character */
1269 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001270
1271 /* CTRL-W "= prompt for expression to evaluate. */
1272 if (c == '=' && get_expr_register() != '=')
1273 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001274 if (!term_use_loop())
1275 /* job finished while waiting for a character */
1276 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001277
1278 l = (list_T *)get_reg_contents(c, GREG_LIST);
1279 if (l != NULL)
1280 {
1281 type = get_reg_type(c, &reglen);
1282 for (item = l->lv_first; item != NULL; item = item->li_next)
1283 {
1284 char_u *s = get_tv_string(&item->li_tv);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001285#ifdef WIN3264
1286 char_u *tmp = s;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001287
Bram Moolenaar285f2432017-08-23 23:10:21 +02001288 if (!enc_utf8 && enc_codepage > 0)
1289 {
1290 WCHAR *ret = NULL;
1291 int length = 0;
1292
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001293 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
1294 (int)STRLEN(s), &ret, &length);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001295 if (ret != NULL)
1296 {
1297 WideCharToMultiByte_alloc(CP_UTF8, 0,
1298 ret, length, (char **)&s, &length, 0, 0);
1299 vim_free(ret);
1300 }
1301 }
1302#endif
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001303 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001304 s, (int)STRLEN(s), NULL);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001305#ifdef WIN3264
1306 if (tmp != s)
1307 vim_free(s);
1308#endif
1309
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001310 if (item->li_next != NULL || type == MLINE)
1311 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1312 (char_u *)"\r", 1, NULL);
1313 }
1314 list_free(l);
1315 }
1316}
1317
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001318#if defined(FEAT_GUI) || defined(PROTO)
1319/*
1320 * Return TRUE when the cursor of the terminal should be displayed.
1321 */
1322 int
1323use_terminal_cursor()
1324{
1325 return in_terminal_loop != NULL;
1326}
1327
1328 cursorentry_T *
1329term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1330{
1331 term_T *term = in_terminal_loop;
1332 static cursorentry_T entry;
1333
1334 vim_memset(&entry, 0, sizeof(entry));
1335 entry.shape = entry.mshape =
1336 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1337 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1338 SHAPE_BLOCK;
1339 entry.percentage = 20;
1340 if (term->tl_cursor_blink)
1341 {
1342 entry.blinkwait = 700;
1343 entry.blinkon = 400;
Bram Moolenaar58302322017-08-22 20:33:53 +02001344 entry.blinkoff = 250;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001345 }
1346 *fg = gui.back_pixel;
1347 if (term->tl_cursor_color == NULL)
1348 *bg = gui.norm_pixel;
1349 else
1350 *bg = color_name2handle(term->tl_cursor_color);
1351 entry.name = "n";
1352 entry.used_for = SHAPE_CURSOR;
1353
1354 return &entry;
1355}
1356#endif
1357
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001358static int did_change_cursor = FALSE;
1359
1360 static void
1361may_set_cursor_props(term_T *term)
1362{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001363#ifdef FEAT_GUI
1364 /* For the GUI the cursor properties are obtained with
1365 * term_get_cursor_shape(). */
1366 if (gui.in_use)
1367 return;
1368#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001369 if (in_terminal_loop == term)
1370 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001371 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001372 if (term->tl_cursor_color != NULL)
1373 term_cursor_color(term->tl_cursor_color);
1374 else
1375 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001376 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1377 }
1378}
1379
1380 static void
1381may_restore_cursor_props(void)
1382{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001383#ifdef FEAT_GUI
1384 if (gui.in_use)
1385 return;
1386#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001387 if (did_change_cursor)
1388 {
1389 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001390 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001391 /* this will restore the initial cursor style, if possible */
1392 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001393 }
1394}
1395
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001396/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001397 * Returns TRUE if the current window contains a terminal and we are sending
1398 * keys to the job.
1399 */
1400 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001401term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001402{
1403 term_T *term = curbuf->b_term;
1404
1405 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001406 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001407 && term->tl_vterm != NULL
1408 && term_job_running(term);
1409}
1410
1411/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001412 * Wait for input and send it to the job.
1413 * Return when the start of a CTRL-W command is typed or anything else that
1414 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001415 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1416 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001417 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001418 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001419terminal_loop(void)
1420{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001421 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001422 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001423 int ret;
1424
Bram Moolenaar679653e2017-08-13 14:13:19 +02001425 /* Remember the terminal we are sending keys to. However, the terminal
1426 * might be closed while waiting for a character, e.g. typing "exit" in a
1427 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1428 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001429 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001430
1431 if (*curwin->w_p_tk != NUL)
1432 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001433 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001434 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001435
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001436#ifdef UNIX
1437 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001438 int part = get_tty_part(curbuf->b_term);
1439 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001440
1441 if (isatty(fd))
1442 {
1443 ttyinfo_T info;
1444
1445 /* Get the current backspace and enter characters of the pty. */
1446 if (get_tty_info(fd, &info) == OK)
1447 {
1448 term_backspace_char = info.backspace;
1449 term_enter_char = info.enter;
1450 term_nl_does_cr = info.nl_does_cr;
1451 }
1452 }
1453 }
1454#endif
1455
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001456 for (;;)
1457 {
1458 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001459 /* Repeat redrawing in case a message is received while redrawing. */
1460 while (curwin->w_redr_type != 0)
1461 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001462 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001463
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001464 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001465 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001466 /* job finished while waiting for a character */
1467 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001468 if (c == K_IGNORE)
1469 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001470
Bram Moolenaarfae42832017-08-01 22:24:26 +02001471#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001472 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001473 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001474 if (ctrl_break_was_pressed)
1475 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001476#endif
1477
Bram Moolenaar69198192017-08-05 14:10:48 +02001478 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001479 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001480 int prev_c = c;
1481
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001482#ifdef FEAT_CMDL_INFO
1483 if (add_to_showcmd(c))
1484 out_flush();
1485#endif
1486 c = term_vgetc();
1487#ifdef FEAT_CMDL_INFO
1488 clear_showcmd();
1489#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001490 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001491 /* job finished while waiting for a character */
1492 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001493
Bram Moolenaar69198192017-08-05 14:10:48 +02001494 if (prev_c == Ctrl_BSL)
1495 {
1496 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001497 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001498 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1499 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001500 ret = FAIL;
1501 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001502 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001503 /* Send both keys to the terminal. */
1504 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1505 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001506 else if (c == Ctrl_C)
1507 {
1508 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1509 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1510 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001511 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001512 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001513 /* "CTRL-W .": send CTRL-W to the job */
1514 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001515 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001516 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001517 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001518 /* CTRL-W N : go to Terminal-Normal mode. */
1519 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001520 ret = FAIL;
1521 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001522 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001523 else if (c == '"')
1524 {
1525 term_paste_register(prev_c);
1526 continue;
1527 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001528 else if (termkey == 0 || c != termkey)
1529 {
1530 stuffcharReadbuff(Ctrl_W);
1531 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001532 ret = OK;
1533 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001534 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001535 }
Bram Moolenaar58302322017-08-22 20:33:53 +02001536# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02001537 if (!enc_utf8 && has_mbyte && c >= 0x80)
1538 {
1539 WCHAR wc;
1540 char_u mb[3];
1541
1542 mb[0] = (unsigned)c >> 8;
1543 mb[1] = c;
1544 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1545 c = wc;
1546 }
1547# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001548 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001549 {
1550 ret = OK;
1551 goto theend;
1552 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001553 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001554 ret = FAIL;
1555
1556theend:
1557 in_terminal_loop = NULL;
1558 may_restore_cursor_props();
1559 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001560}
1561
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001562/*
1563 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001564 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001565 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001566 */
1567 void
1568term_job_ended(job_T *job)
1569{
1570 term_T *term;
1571 int did_one = FALSE;
1572
1573 for (term = first_term; term != NULL; term = term->tl_next)
1574 if (term->tl_job == job)
1575 {
1576 vim_free(term->tl_title);
1577 term->tl_title = NULL;
1578 vim_free(term->tl_status_text);
1579 term->tl_status_text = NULL;
1580 redraw_buf_and_status_later(term->tl_buffer, VALID);
1581 did_one = TRUE;
1582 }
1583 if (did_one)
1584 redraw_statuslines();
1585 if (curbuf->b_term != NULL)
1586 {
1587 if (curbuf->b_term->tl_job == job)
1588 maketitle();
1589 update_cursor(curbuf->b_term, TRUE);
1590 }
1591}
1592
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001593 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001594may_toggle_cursor(term_T *term)
1595{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001596 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001597 {
1598 if (term->tl_cursor_visible)
1599 cursor_on();
1600 else
1601 cursor_off();
1602 }
1603}
1604
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001605/*
1606 * Reverse engineer the RGB value into a cterm color index.
1607 * First color is 1. Return 0 if no match found.
1608 */
1609 static int
1610color2index(VTermColor *color, int fg, int *boldp)
1611{
1612 int red = color->red;
1613 int blue = color->blue;
1614 int green = color->green;
1615
1616 /* The argument for lookup_color() is for the color_names[] table. */
1617 if (red == 0)
1618 {
1619 if (green == 0)
1620 {
1621 if (blue == 0)
1622 return lookup_color(0, fg, boldp) + 1; /* black */
1623 if (blue == 224)
1624 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1625 }
1626 else if (green == 224)
1627 {
1628 if (blue == 0)
1629 return lookup_color(2, fg, boldp) + 1; /* dark green */
1630 if (blue == 224)
1631 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1632 }
1633 }
1634 else if (red == 224)
1635 {
1636 if (green == 0)
1637 {
1638 if (blue == 0)
1639 return lookup_color(4, fg, boldp) + 1; /* dark red */
1640 if (blue == 224)
1641 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1642 }
1643 else if (green == 224)
1644 {
1645 if (blue == 0)
1646 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1647 if (blue == 224)
1648 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1649 }
1650 }
1651 else if (red == 128)
1652 {
1653 if (green == 128 && blue == 128)
1654 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1655 }
1656 else if (red == 255)
1657 {
1658 if (green == 64)
1659 {
1660 if (blue == 64)
1661 return lookup_color(20, fg, boldp) + 1; /* light red */
1662 if (blue == 255)
1663 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1664 }
1665 else if (green == 255)
1666 {
1667 if (blue == 64)
1668 return lookup_color(24, fg, boldp) + 1; /* yellow */
1669 if (blue == 255)
1670 return lookup_color(26, fg, boldp) + 1; /* white */
1671 }
1672 }
1673 else if (red == 64)
1674 {
1675 if (green == 64)
1676 {
1677 if (blue == 255)
1678 return lookup_color(14, fg, boldp) + 1; /* light blue */
1679 }
1680 else if (green == 255)
1681 {
1682 if (blue == 64)
1683 return lookup_color(16, fg, boldp) + 1; /* light green */
1684 if (blue == 255)
1685 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1686 }
1687 }
1688 if (t_colors >= 256)
1689 {
1690 if (red == blue && red == green)
1691 {
1692 /* 24-color greyscale */
1693 static int cutoff[23] = {
1694 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1695 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1696 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1697 int i;
1698
1699 for (i = 0; i < 23; ++i)
1700 if (red < cutoff[i])
1701 return i + 233;
1702 return 256;
1703 }
1704
1705 /* 216-color cube */
1706 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001707 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001708 + (blue + 25) / 0x33;
1709 }
1710 return 0;
1711}
1712
1713/*
1714 * Convert the attributes of a vterm cell into an attribute index.
1715 */
1716 static int
1717cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1718{
1719 int attr = 0;
1720
1721 if (cellattrs.bold)
1722 attr |= HL_BOLD;
1723 if (cellattrs.underline)
1724 attr |= HL_UNDERLINE;
1725 if (cellattrs.italic)
1726 attr |= HL_ITALIC;
1727 if (cellattrs.strike)
1728 attr |= HL_STANDOUT;
1729 if (cellattrs.reverse)
1730 attr |= HL_INVERSE;
1731
1732#ifdef FEAT_GUI
1733 if (gui.in_use)
1734 {
1735 guicolor_T fg, bg;
1736
1737 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1738 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1739 return get_gui_attr_idx(attr, fg, bg);
1740 }
1741 else
1742#endif
1743#ifdef FEAT_TERMGUICOLORS
1744 if (p_tgc)
1745 {
1746 guicolor_T fg, bg;
1747
1748 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1749 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1750
1751 return get_tgc_attr_idx(attr, fg, bg);
1752 }
1753 else
1754#endif
1755 {
1756 int bold = MAYBE;
1757 int fg = color2index(&cellfg, TRUE, &bold);
1758 int bg = color2index(&cellbg, FALSE, &bold);
1759
1760 /* with 8 colors set the bold attribute to get a bright foreground */
1761 if (bold == TRUE)
1762 attr |= HL_BOLD;
1763 return get_cterm_attr_idx(attr, fg, bg);
1764 }
1765 return 0;
1766}
1767
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001768 static int
1769handle_damage(VTermRect rect, void *user)
1770{
1771 term_T *term = (term_T *)user;
1772
1773 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1774 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1775 redraw_buf_later(term->tl_buffer, NOT_VALID);
1776 return 1;
1777}
1778
1779 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001780handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001781{
1782 term_T *term = (term_T *)user;
1783
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001784 /* Scrolling up is done much more efficiently by deleting lines instead of
1785 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001786 if (dest.start_col == src.start_col
1787 && dest.end_col == src.end_col
1788 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001789 {
1790 win_T *wp;
1791 VTermColor fg, bg;
1792 VTermScreenCellAttrs attr;
1793 int clear_attr;
1794
1795 /* Set the color to clear lines with. */
1796 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1797 &fg, &bg);
1798 vim_memset(&attr, 0, sizeof(attr));
1799 clear_attr = cell2attr(attr, fg, bg);
1800
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001801 FOR_ALL_WINDOWS(wp)
1802 {
1803 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001804 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001805 src.start_row - dest.start_row, FALSE, FALSE,
1806 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001807 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001808 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001809 redraw_buf_later(term->tl_buffer, NOT_VALID);
1810 return 1;
1811}
1812
1813 static int
1814handle_movecursor(
1815 VTermPos pos,
1816 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001817 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001818 void *user)
1819{
1820 term_T *term = (term_T *)user;
1821 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001822
1823 term->tl_cursor_pos = pos;
1824 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001825
1826 FOR_ALL_WINDOWS(wp)
1827 {
1828 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001829 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001830 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001831 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001832 {
1833 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001834 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001835 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001836
1837 return 1;
1838}
1839
Bram Moolenaar21554412017-07-24 21:44:43 +02001840 static int
1841handle_settermprop(
1842 VTermProp prop,
1843 VTermValue *value,
1844 void *user)
1845{
1846 term_T *term = (term_T *)user;
1847
1848 switch (prop)
1849 {
1850 case VTERM_PROP_TITLE:
1851 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001852 /* a blank title isn't useful, make it empty, so that "running" is
1853 * displayed */
1854 if (*skipwhite((char_u *)value->string) == NUL)
1855 term->tl_title = NULL;
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001856#ifdef WIN3264
1857 else if (!enc_utf8 && enc_codepage > 0)
1858 {
1859 WCHAR *ret = NULL;
1860 int length = 0;
1861
1862 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaar4ad3b2b2017-08-30 15:57:33 +02001863 (char*)value->string, (int)STRLEN(value->string),
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001864 &ret, &length);
1865 if (ret != NULL)
1866 {
1867 WideCharToMultiByte_alloc(enc_codepage, 0,
1868 ret, length, (char**)&term->tl_title,
1869 &length, 0, 0);
1870 vim_free(ret);
1871 }
1872 }
1873#endif
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001874 else
1875 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001876 vim_free(term->tl_status_text);
1877 term->tl_status_text = NULL;
1878 if (term == curbuf->b_term)
1879 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001880 break;
1881
1882 case VTERM_PROP_CURSORVISIBLE:
1883 term->tl_cursor_visible = value->boolean;
1884 may_toggle_cursor(term);
1885 out_flush();
1886 break;
1887
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001888 case VTERM_PROP_CURSORBLINK:
1889 term->tl_cursor_blink = value->boolean;
1890 may_set_cursor_props(term);
1891 break;
1892
1893 case VTERM_PROP_CURSORSHAPE:
1894 term->tl_cursor_shape = value->number;
1895 may_set_cursor_props(term);
1896 break;
1897
1898 case VTERM_PROP_CURSORCOLOR:
1899 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001900 if (*value->string == NUL)
1901 term->tl_cursor_color = NULL;
1902 else
1903 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001904 may_set_cursor_props(term);
1905 break;
1906
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001907 case VTERM_PROP_ALTSCREEN:
1908 /* TODO: do anything else? */
1909 term->tl_using_altscreen = value->boolean;
1910 break;
1911
Bram Moolenaar21554412017-07-24 21:44:43 +02001912 default:
1913 break;
1914 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001915 /* Always return 1, otherwise vterm doesn't store the value internally. */
1916 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001917}
1918
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001919/*
1920 * The job running in the terminal resized the terminal.
1921 */
1922 static int
1923handle_resize(int rows, int cols, void *user)
1924{
1925 term_T *term = (term_T *)user;
1926 win_T *wp;
1927
1928 term->tl_rows = rows;
1929 term->tl_cols = cols;
1930 FOR_ALL_WINDOWS(wp)
1931 {
1932 if (wp->w_buffer == term->tl_buffer)
1933 {
1934 win_setheight_win(rows, wp);
1935 win_setwidth_win(cols, wp);
1936 }
1937 }
1938
1939 redraw_buf_later(term->tl_buffer, NOT_VALID);
1940 return 1;
1941}
1942
Bram Moolenaard85f2712017-07-28 21:51:57 +02001943/*
1944 * Handle a line that is pushed off the top of the screen.
1945 */
1946 static int
1947handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1948{
1949 term_T *term = (term_T *)user;
1950
1951 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001952 if (ga_grow(&term->tl_scrollback, 1) == OK)
1953 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001954 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001955 int len = 0;
1956 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001957 int c;
1958 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001959 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001960 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001961
1962 /* do not store empty cells at the end */
1963 for (i = 0; i < cols; ++i)
1964 if (cells[i].chars[0] != 0)
1965 len = i + 1;
1966
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001967 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001968 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001969 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001970 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001971 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001972 for (col = 0; col < len; col += cells[col].width)
1973 {
1974 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1975 {
1976 ga.ga_len = 0;
1977 break;
1978 }
1979 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +02001980 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001981 (char_u *)ga.ga_data + ga.ga_len);
1982 p[col].width = cells[col].width;
1983 p[col].attrs = cells[col].attrs;
1984 p[col].fg = cells[col].fg;
1985 p[col].bg = cells[col].bg;
1986 }
1987 }
1988 if (ga_grow(&ga, 1) == FAIL)
1989 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1990 else
1991 {
1992 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1993 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1994 }
1995 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001996
1997 line = (sb_line_T *)term->tl_scrollback.ga_data
1998 + term->tl_scrollback.ga_len;
1999 line->sb_cols = len;
2000 line->sb_cells = p;
2001 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002002 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002003 }
2004 return 0; /* ignored */
2005}
2006
Bram Moolenaar21554412017-07-24 21:44:43 +02002007static VTermScreenCallbacks screen_callbacks = {
2008 handle_damage, /* damage */
2009 handle_moverect, /* moverect */
2010 handle_movecursor, /* movecursor */
2011 handle_settermprop, /* settermprop */
2012 NULL, /* bell */
2013 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002014 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02002015 NULL /* sb_popline */
2016};
2017
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002018/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02002019 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002020 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02002021 */
2022 void
2023term_channel_closed(channel_T *ch)
2024{
2025 term_T *term;
2026 int did_one = FALSE;
2027
2028 for (term = first_term; term != NULL; term = term->tl_next)
2029 if (term->tl_job == ch->ch_job)
2030 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002031 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002032 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002033
Bram Moolenaard85f2712017-07-28 21:51:57 +02002034 vim_free(term->tl_title);
2035 term->tl_title = NULL;
2036 vim_free(term->tl_status_text);
2037 term->tl_status_text = NULL;
2038
Bram Moolenaar423802d2017-07-30 16:52:24 +02002039 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02002040 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002041 {
2042 int fnum = term->tl_buffer->b_fnum;
2043
Bram Moolenaar423802d2017-07-30 16:52:24 +02002044 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002045
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002046 if (term->tl_finish == 'c')
2047 {
2048 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002049 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002050 curbuf = term->tl_buffer;
2051 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
2052 break;
2053 }
2054 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
2055 {
2056 char buf[50];
2057
2058 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002059 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02002060 vim_snprintf(buf, sizeof(buf),
2061 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02002062 ? "botright sbuf %d"
2063 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002064 do_cmdline_cmd((char_u *)buf);
2065 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002066 else
2067 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002068 }
2069
Bram Moolenaard85f2712017-07-28 21:51:57 +02002070 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002071 }
2072 if (did_one)
2073 {
2074 redraw_statuslines();
2075
2076 /* Need to break out of vgetc(). */
2077 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002078 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002079
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002080 term = curbuf->b_term;
2081 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002082 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002083 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002084 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002085 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002086 }
2087 }
2088}
2089
2090/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002091 * Called to update a window that contains an active terminal.
2092 * Returns FAIL when there is no terminal running in this window or in
2093 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002094 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002095 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002096term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002097{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002098 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002099 VTerm *vterm;
2100 VTermScreen *screen;
2101 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002102 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002103
Bram Moolenaar6d819742017-08-06 14:57:49 +02002104 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002105 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002106
Bram Moolenaard85f2712017-07-28 21:51:57 +02002107 vterm = term->tl_vterm;
2108 screen = vterm_obtain_screen(vterm);
2109 state = vterm_obtain_state(vterm);
2110
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002111 /*
2112 * If the window was resized a redraw will be triggered and we get here.
2113 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2114 */
2115 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2116 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002117 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002118 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2119 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2120 win_T *twp;
2121
2122 FOR_ALL_WINDOWS(twp)
2123 {
2124 /* When more than one window shows the same terminal, use the
2125 * smallest size. */
2126 if (twp->w_buffer == term->tl_buffer)
2127 {
2128 if (!term->tl_rows_fixed && rows > twp->w_height)
2129 rows = twp->w_height;
2130 if (!term->tl_cols_fixed && cols > twp->w_width)
2131 cols = twp->w_width;
2132 }
2133 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002134
2135 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002136 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002137 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002138 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002139 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002140
2141 /* The cursor may have been moved when resizing. */
2142 vterm_state_get_cursorpos(state, &pos);
2143 position_cursor(wp, &pos);
2144
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002145 /* TODO: Only redraw what changed. */
2146 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002147 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002148 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002149 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002150
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002151 if (pos.row < term->tl_rows)
2152 {
2153 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002154 {
2155 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002156 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002157
Bram Moolenaareeac6772017-07-23 15:48:37 +02002158 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2159 vim_memset(&cell, 0, sizeof(cell));
2160
2161 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002162 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002163 if (c == NUL)
2164 {
2165 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002166 if (enc_utf8)
2167 ScreenLinesUC[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002168 }
2169 else
2170 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002171 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002172 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002173 if (c >= 0x80)
2174 {
2175 ScreenLines[off] = ' ';
2176 ScreenLinesUC[off] = c;
2177 }
2178 else
2179 {
2180 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002181 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002182 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002183 }
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002184#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02002185 else if (has_mbyte && c >= 0x80)
2186 {
2187 char_u mb[MB_MAXBYTES+1];
2188 WCHAR wc = c;
2189
2190 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2191 (char*)mb, 2, 0, 0) > 1)
2192 {
2193 ScreenLines[off] = mb[0];
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002194 ScreenLines[off + 1] = mb[1];
Bram Moolenaar740c4332017-08-21 22:01:27 +02002195 cell.width = mb_ptr2cells(mb);
2196 }
2197 else
2198 ScreenLines[off] = c;
2199 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002200#endif
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002201 else
Bram Moolenaar740c4332017-08-21 22:01:27 +02002202 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002203 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002204 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002205
2206 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002207 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002208 if (cell.width == 2)
2209 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002210 if (enc_utf8)
2211 ScreenLinesUC[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002212
2213 /* don't set the second byte to NUL for a DBCS encoding, it
2214 * has been set above */
2215 if (enc_utf8 || !has_mbyte)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002216 ScreenLines[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002217
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002218 ++pos.col;
2219 ++off;
2220 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002221 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002222 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002223 else
2224 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002225
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002226 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2227 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002228 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002229
2230 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002231}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002232
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002233/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002234 * Return TRUE if "wp" is a terminal window where the job has finished.
2235 */
2236 int
2237term_is_finished(buf_T *buf)
2238{
2239 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2240}
2241
2242/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002243 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002244 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002245 */
2246 int
2247term_show_buffer(buf_T *buf)
2248{
2249 term_T *term = buf->b_term;
2250
Bram Moolenaar6d819742017-08-06 14:57:49 +02002251 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002252}
2253
2254/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002255 * The current buffer is going to be changed. If there is terminal
2256 * highlighting remove it now.
2257 */
2258 void
2259term_change_in_curbuf(void)
2260{
2261 term_T *term = curbuf->b_term;
2262
2263 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2264 {
2265 free_scrollback(term);
2266 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002267
2268 /* The buffer is now like a normal buffer, it cannot be easily
2269 * abandoned when changed. */
2270 set_string_option_direct((char_u *)"buftype", -1,
2271 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002272 }
2273}
2274
2275/*
2276 * Get the screen attribute for a position in the buffer.
2277 */
2278 int
2279term_get_attr(buf_T *buf, linenr_T lnum, int col)
2280{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002281 term_T *term = buf->b_term;
2282 sb_line_T *line;
2283 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002284
Bram Moolenaar70229f92017-07-29 16:01:53 +02002285 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002286 return 0;
2287 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2288 if (col >= line->sb_cols)
2289 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002290 cellattr = line->sb_cells + col;
2291 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002292}
2293
2294/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002295 * Create a new vterm and initialize it.
2296 */
2297 static void
2298create_vterm(term_T *term, int rows, int cols)
2299{
2300 VTerm *vterm;
2301 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002302 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002303
2304 vterm = vterm_new(rows, cols);
2305 term->tl_vterm = vterm;
2306 screen = vterm_obtain_screen(vterm);
2307 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2308 /* TODO: depends on 'encoding'. */
2309 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002310
2311 /* Vterm uses a default black background. Set it to white when
2312 * 'background' is "light". */
2313 if (*p_bg == 'l')
2314 {
2315 VTermColor fg, bg;
2316
2317 fg.red = fg.green = fg.blue = 0;
2318 bg.red = bg.green = bg.blue = 255;
2319 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2320 }
2321
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002322 /* Required to initialize most things. */
2323 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002324
2325 /* Allow using alternate screen. */
2326 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002327
Bram Moolenaar58302322017-08-22 20:33:53 +02002328 /* For unix do not use a blinking cursor. In an xterm this causes the
2329 * cursor to blink if it's blinking in the xterm.
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002330 * For Windows we respect the system wide setting. */
Bram Moolenaar58302322017-08-22 20:33:53 +02002331#ifdef WIN3264
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002332 if (GetCaretBlinkTime() == INFINITE)
2333 value.boolean = 0;
2334 else
2335 value.boolean = 1;
Bram Moolenaar58302322017-08-22 20:33:53 +02002336#else
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002337 value.boolean = 0;
Bram Moolenaar58302322017-08-22 20:33:53 +02002338#endif
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002339 vterm_state_set_termprop(vterm_obtain_state(vterm),
2340 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002341}
2342
Bram Moolenaar21554412017-07-24 21:44:43 +02002343/*
2344 * Return the text to show for the buffer name and status.
2345 */
2346 char_u *
2347term_get_status_text(term_T *term)
2348{
2349 if (term->tl_status_text == NULL)
2350 {
2351 char_u *txt;
2352 size_t len;
2353
Bram Moolenaar6d819742017-08-06 14:57:49 +02002354 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002355 {
2356 if (term_job_running(term))
2357 txt = (char_u *)_("Terminal");
2358 else
2359 txt = (char_u *)_("Terminal-finished");
2360 }
2361 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002362 txt = term->tl_title;
2363 else if (term_job_running(term))
2364 txt = (char_u *)_("running");
2365 else
2366 txt = (char_u *)_("finished");
2367 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002368 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002369 if (term->tl_status_text != NULL)
2370 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2371 term->tl_buffer->b_fname, txt);
2372 }
2373 return term->tl_status_text;
2374}
2375
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002376/*
2377 * Mark references in jobs of terminals.
2378 */
2379 int
2380set_ref_in_term(int copyID)
2381{
2382 int abort = FALSE;
2383 term_T *term;
2384 typval_T tv;
2385
2386 for (term = first_term; term != NULL; term = term->tl_next)
2387 if (term->tl_job != NULL)
2388 {
2389 tv.v_type = VAR_JOB;
2390 tv.vval.v_job = term->tl_job;
2391 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2392 }
2393 return abort;
2394}
2395
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002396/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002397 * Get the buffer from the first argument in "argvars".
2398 * Returns NULL when the buffer is not for a terminal window.
2399 */
2400 static buf_T *
2401term_get_buf(typval_T *argvars)
2402{
2403 buf_T *buf;
2404
2405 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2406 ++emsg_off;
2407 buf = get_buf_tv(&argvars[0], FALSE);
2408 --emsg_off;
2409 if (buf == NULL || buf->b_term == NULL)
2410 return NULL;
2411 return buf;
2412}
2413
2414/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002415 * "term_getaltscreen(buf)" function
2416 */
2417 void
2418f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2419{
2420 buf_T *buf = term_get_buf(argvars);
2421
2422 if (buf == NULL)
2423 return;
2424 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2425}
2426
2427/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002428 * "term_getattr(attr, name)" function
2429 */
2430 void
2431f_term_getattr(typval_T *argvars, typval_T *rettv)
2432{
2433 int attr;
2434 size_t i;
2435 char_u *name;
2436
2437 static struct {
2438 char *name;
2439 int attr;
2440 } attrs[] = {
2441 {"bold", HL_BOLD},
2442 {"italic", HL_ITALIC},
2443 {"underline", HL_UNDERLINE},
2444 {"strike", HL_STANDOUT},
2445 {"reverse", HL_INVERSE},
2446 };
2447
2448 attr = get_tv_number(&argvars[0]);
2449 name = get_tv_string_chk(&argvars[1]);
2450 if (name == NULL)
2451 return;
2452
2453 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2454 if (STRCMP(name, attrs[i].name) == 0)
2455 {
2456 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2457 break;
2458 }
2459}
2460
2461/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002462 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002463 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002464 void
2465f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002466{
Bram Moolenaar97870002017-07-30 18:28:38 +02002467 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002468 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002469 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002470 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002471
Bram Moolenaar97870002017-07-30 18:28:38 +02002472 if (rettv_list_alloc(rettv) == FAIL)
2473 return;
2474 if (buf == NULL)
2475 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002476 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002477
2478 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002479 list_append_number(l, term->tl_cursor_pos.row + 1);
2480 list_append_number(l, term->tl_cursor_pos.col + 1);
2481
2482 d = dict_alloc();
2483 if (d != NULL)
2484 {
2485 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
Bram Moolenaar4db25542017-08-28 22:43:05 +02002486 dict_add_nr_str(d, "blink", blink_state_is_inverted()
2487 ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002488 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2489 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2490 ? (char_u *)"" : term->tl_cursor_color);
2491 list_append_dict(l, d);
2492 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002493}
2494
2495/*
2496 * "term_getjob(buf)" function
2497 */
2498 void
2499f_term_getjob(typval_T *argvars, typval_T *rettv)
2500{
2501 buf_T *buf = term_get_buf(argvars);
2502
2503 rettv->v_type = VAR_JOB;
2504 rettv->vval.v_job = NULL;
2505 if (buf == NULL)
2506 return;
2507
2508 rettv->vval.v_job = buf->b_term->tl_job;
2509 if (rettv->vval.v_job != NULL)
2510 ++rettv->vval.v_job->jv_refcount;
2511}
2512
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002513 static int
2514get_row_number(typval_T *tv, term_T *term)
2515{
2516 if (tv->v_type == VAR_STRING
2517 && tv->vval.v_string != NULL
2518 && STRCMP(tv->vval.v_string, ".") == 0)
2519 return term->tl_cursor_pos.row;
2520 return (int)get_tv_number(tv) - 1;
2521}
2522
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002523/*
2524 * "term_getline(buf, row)" function
2525 */
2526 void
2527f_term_getline(typval_T *argvars, typval_T *rettv)
2528{
2529 buf_T *buf = term_get_buf(argvars);
2530 term_T *term;
2531 int row;
2532
2533 rettv->v_type = VAR_STRING;
2534 if (buf == NULL)
2535 return;
2536 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002537 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002538
2539 if (term->tl_vterm == NULL)
2540 {
2541 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2542
2543 /* vterm is finished, get the text from the buffer */
2544 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2545 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2546 }
2547 else
2548 {
2549 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2550 VTermRect rect;
2551 int len;
2552 char_u *p;
2553
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002554 if (row < 0 || row >= term->tl_rows)
2555 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002556 len = term->tl_cols * MB_MAXBYTES + 1;
2557 p = alloc(len);
2558 if (p == NULL)
2559 return;
2560 rettv->vval.v_string = p;
2561
2562 rect.start_col = 0;
2563 rect.end_col = term->tl_cols;
2564 rect.start_row = row;
2565 rect.end_row = row + 1;
2566 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2567 }
2568}
2569
2570/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002571 * "term_getscrolled(buf)" function
2572 */
2573 void
2574f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2575{
2576 buf_T *buf = term_get_buf(argvars);
2577
2578 if (buf == NULL)
2579 return;
2580 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2581}
2582
2583/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002584 * "term_getsize(buf)" function
2585 */
2586 void
2587f_term_getsize(typval_T *argvars, typval_T *rettv)
2588{
2589 buf_T *buf = term_get_buf(argvars);
2590 list_T *l;
2591
2592 if (rettv_list_alloc(rettv) == FAIL)
2593 return;
2594 if (buf == NULL)
2595 return;
2596
2597 l = rettv->vval.v_list;
2598 list_append_number(l, buf->b_term->tl_rows);
2599 list_append_number(l, buf->b_term->tl_cols);
2600}
2601
2602/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002603 * "term_getstatus(buf)" function
2604 */
2605 void
2606f_term_getstatus(typval_T *argvars, typval_T *rettv)
2607{
2608 buf_T *buf = term_get_buf(argvars);
2609 term_T *term;
2610 char_u val[100];
2611
2612 rettv->v_type = VAR_STRING;
2613 if (buf == NULL)
2614 return;
2615 term = buf->b_term;
2616
2617 if (term_job_running(term))
2618 STRCPY(val, "running");
2619 else
2620 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002621 if (term->tl_normal_mode)
2622 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002623 rettv->vval.v_string = vim_strsave(val);
2624}
2625
2626/*
2627 * "term_gettitle(buf)" function
2628 */
2629 void
2630f_term_gettitle(typval_T *argvars, typval_T *rettv)
2631{
2632 buf_T *buf = term_get_buf(argvars);
2633
2634 rettv->v_type = VAR_STRING;
2635 if (buf == NULL)
2636 return;
2637
2638 if (buf->b_term->tl_title != NULL)
2639 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2640}
2641
2642/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002643 * "term_gettty(buf)" function
2644 */
2645 void
2646f_term_gettty(typval_T *argvars, typval_T *rettv)
2647{
2648 buf_T *buf = term_get_buf(argvars);
2649 char_u *p;
2650
2651 rettv->v_type = VAR_STRING;
2652 if (buf == NULL)
2653 return;
2654 if (buf->b_term->tl_job != NULL)
2655 p = buf->b_term->tl_job->jv_tty_name;
2656 else
2657 p = buf->b_term->tl_tty_name;
2658 if (p != NULL)
2659 rettv->vval.v_string = vim_strsave(p);
2660}
2661
2662/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002663 * "term_list()" function
2664 */
2665 void
2666f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2667{
2668 term_T *tp;
2669 list_T *l;
2670
2671 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2672 return;
2673
2674 l = rettv->vval.v_list;
2675 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2676 if (tp != NULL && tp->tl_buffer != NULL)
2677 if (list_append_number(l,
2678 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2679 return;
2680}
2681
2682/*
2683 * "term_scrape(buf, row)" function
2684 */
2685 void
2686f_term_scrape(typval_T *argvars, typval_T *rettv)
2687{
2688 buf_T *buf = term_get_buf(argvars);
2689 VTermScreen *screen = NULL;
2690 VTermPos pos;
2691 list_T *l;
2692 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002693 char_u *p;
2694 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002695
2696 if (rettv_list_alloc(rettv) == FAIL)
2697 return;
2698 if (buf == NULL)
2699 return;
2700 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002701
2702 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002703 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002704
2705 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002706 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002707 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002708 p = NULL;
2709 line = NULL;
2710 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002711 else
2712 {
2713 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2714
2715 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2716 return;
2717 p = ml_get_buf(buf, lnum + 1, FALSE);
2718 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2719 }
2720
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002721 for (pos.col = 0; pos.col < term->tl_cols; )
2722 {
2723 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002724 int width;
2725 VTermScreenCellAttrs attrs;
2726 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002727 char_u rgb[8];
2728 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2729 int off = 0;
2730 int i;
2731
2732 if (screen == NULL)
2733 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002734 cellattr_T *cellattr;
2735 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002736
2737 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002738 if (pos.col >= line->sb_cols)
2739 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002740 cellattr = line->sb_cells + pos.col;
2741 width = cellattr->width;
2742 attrs = cellattr->attrs;
2743 fg = cellattr->fg;
2744 bg = cellattr->bg;
2745 len = MB_PTR2LEN(p);
2746 mch_memmove(mbs, p, len);
2747 mbs[len] = NUL;
2748 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002749 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002750 else
2751 {
2752 VTermScreenCell cell;
2753 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2754 break;
2755 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2756 {
2757 if (cell.chars[i] == 0)
2758 break;
2759 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2760 }
2761 mbs[off] = NUL;
2762 width = cell.width;
2763 attrs = cell.attrs;
2764 fg = cell.fg;
2765 bg = cell.bg;
2766 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002767 dcell = dict_alloc();
2768 list_append_dict(l, dcell);
2769
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002770 dict_add_nr_str(dcell, "chars", 0, mbs);
2771
2772 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002773 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002774 dict_add_nr_str(dcell, "fg", 0, rgb);
2775 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002776 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002777 dict_add_nr_str(dcell, "bg", 0, rgb);
2778
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002779 dict_add_nr_str(dcell, "attr",
2780 cell2attr(attrs, fg, bg), NULL);
2781 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002782
2783 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002784 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002785 ++pos.col;
2786 }
2787}
2788
2789/*
2790 * "term_sendkeys(buf, keys)" function
2791 */
2792 void
2793f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2794{
2795 buf_T *buf = term_get_buf(argvars);
2796 char_u *msg;
2797 term_T *term;
2798
2799 rettv->v_type = VAR_UNKNOWN;
2800 if (buf == NULL)
2801 return;
2802
2803 msg = get_tv_string_chk(&argvars[1]);
2804 if (msg == NULL)
2805 return;
2806 term = buf->b_term;
2807 if (term->tl_vterm == NULL)
2808 return;
2809
2810 while (*msg != NUL)
2811 {
2812 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2813 msg += MB_PTR2LEN(msg);
2814 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002815}
2816
2817/*
2818 * "term_start(command, options)" function
2819 */
2820 void
2821f_term_start(typval_T *argvars, typval_T *rettv)
2822{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002823 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002824
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002825 init_job_options(&opt);
2826 /* TODO: allow more job options */
2827 if (argvars[1].v_type != VAR_UNKNOWN
2828 && get_job_options(&argvars[1], &opt,
2829 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002830 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002831 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002832 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar3346cc42017-09-02 14:54:21 +02002833 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002834 return;
2835
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002836 if (opt.jo_vertical)
2837 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002838 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002839
2840 if (curbuf->b_term != NULL)
2841 rettv->vval.v_number = curbuf->b_fnum;
2842}
2843
2844/*
2845 * "term_wait" function
2846 */
2847 void
2848f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2849{
2850 buf_T *buf = term_get_buf(argvars);
2851
2852 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002853 {
2854 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002855 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002856 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002857 if (buf->b_term->tl_job == NULL)
2858 {
2859 ch_log(NULL, "term_wait(): no job to wait for");
2860 return;
2861 }
Bram Moolenaar13ebb032017-08-26 22:02:51 +02002862 if (buf->b_term->tl_job->jv_channel == NULL)
2863 /* channel is closed, nothing to do */
2864 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002865
2866 /* Get the job status, this will detect a job that finished. */
Bram Moolenaar13ebb032017-08-26 22:02:51 +02002867 if ((buf->b_term->tl_job->jv_channel == NULL
2868 || !buf->b_term->tl_job->jv_channel->ch_keep_open)
2869 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002870 {
2871 /* The job is dead, keep reading channel I/O until the channel is
2872 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002873 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002874 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2875 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002876 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002877 parse_queued_messages();
2878 ui_delay(10L, FALSE);
2879 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002880 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002881 parse_queued_messages();
2882 }
2883 else
2884 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002885 long wait = 10L;
2886
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002887 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002888 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002889
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002890 /* Wait for some time for any channel I/O. */
2891 if (argvars[1].v_type != VAR_UNKNOWN)
2892 wait = get_tv_number(&argvars[1]);
2893 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002894 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002895
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002896 /* Flushing messages on channels is hopefully sufficient.
2897 * TODO: is there a better way? */
2898 parse_queued_messages();
2899 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002900}
2901
Bram Moolenaara83e3962017-08-17 14:39:07 +02002902# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002903
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002904/**************************************
2905 * 2. MS-Windows implementation.
2906 */
2907
Bram Moolenaara83e3962017-08-17 14:39:07 +02002908# ifndef PROTO
2909
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002910#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2911#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2912
Bram Moolenaar8a773062017-07-24 22:29:21 +02002913void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002914void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002915void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002916BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2917void (*winpty_config_set_initial_size)(void*, int, int);
2918LPCWSTR (*winpty_conin_name)(void*);
2919LPCWSTR (*winpty_conout_name)(void*);
2920LPCWSTR (*winpty_conerr_name)(void*);
2921void (*winpty_free)(void*);
2922void (*winpty_config_free)(void*);
2923void (*winpty_spawn_config_free)(void*);
2924void (*winpty_error_free)(void*);
2925LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002926BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002927HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002928
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002929#define WINPTY_DLL "winpty.dll"
2930
2931static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002932# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002933
Bram Moolenaara83e3962017-08-17 14:39:07 +02002934 static int
2935dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002936{
2937 int i;
2938 static struct
2939 {
2940 char *name;
2941 FARPROC *ptr;
2942 } winpty_entry[] =
2943 {
2944 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2945 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2946 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2947 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2948 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2949 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2950 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2951 {"winpty_free", (FARPROC*)&winpty_free},
2952 {"winpty_open", (FARPROC*)&winpty_open},
2953 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2954 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2955 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2956 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002957 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002958 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002959 {NULL, NULL}
2960 };
2961
2962 /* No need to initialize twice. */
2963 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002964 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002965 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2966 * winpty.dll. */
2967 if (*p_winptydll != NUL)
2968 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2969 if (!hWinPtyDLL)
2970 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002971 if (!hWinPtyDLL)
2972 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002973 if (verbose)
2974 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2975 : (char_u *)WINPTY_DLL);
2976 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002977 }
2978 for (i = 0; winpty_entry[i].name != NULL
2979 && winpty_entry[i].ptr != NULL; ++i)
2980 {
2981 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2982 winpty_entry[i].name)) == NULL)
2983 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002984 if (verbose)
2985 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2986 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002987 }
2988 }
2989
Bram Moolenaara83e3962017-08-17 14:39:07 +02002990 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002991}
2992
2993/*
2994 * Create a new terminal of "rows" by "cols" cells.
2995 * Store a reference in "term".
2996 * Return OK or FAIL.
2997 */
2998 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02002999term_and_job_init(
3000 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02003001 typval_T *argvar,
3002 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003003{
Bram Moolenaar5983d502017-08-20 19:22:56 +02003004 WCHAR *cmd_wchar = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003005 channel_T *channel = NULL;
3006 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003007 DWORD error;
Bram Moolenaar5983d502017-08-20 19:22:56 +02003008 HANDLE jo = NULL;
3009 HANDLE child_process_handle;
3010 HANDLE child_thread_handle;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003011 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003012 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003013 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003014 garray_T ga;
3015 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003016
Bram Moolenaara83e3962017-08-17 14:39:07 +02003017 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003018 return FAIL;
3019
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003020 if (argvar->v_type == VAR_STRING)
3021 cmd = argvar->vval.v_string;
3022 else
3023 {
3024 ga_init2(&ga, (int)sizeof(char*), 20);
3025 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
3026 goto failed;
3027 cmd = ga.ga_data;
3028 }
3029
Bram Moolenaar5983d502017-08-20 19:22:56 +02003030 cmd_wchar = enc_to_utf16(cmd, NULL);
3031 if (cmd_wchar == NULL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003032 return FAIL;
3033
3034 job = job_alloc();
3035 if (job == NULL)
3036 goto failed;
3037
3038 channel = add_channel();
3039 if (channel == NULL)
3040 goto failed;
3041
3042 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
3043 if (term->tl_winpty_config == NULL)
3044 goto failed;
3045
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003046 winpty_config_set_initial_size(term->tl_winpty_config,
3047 term->tl_cols, term->tl_rows);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003048 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
3049 if (term->tl_winpty == NULL)
3050 goto failed;
3051
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003052 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003053 spawn_config = winpty_spawn_config_new(
3054 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
3055 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
3056 NULL,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003057 cmd_wchar,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003058 NULL,
3059 NULL,
3060 &winpty_err);
3061 if (spawn_config == NULL)
3062 goto failed;
3063
3064 channel = add_channel();
3065 if (channel == NULL)
3066 goto failed;
3067
3068 job = job_alloc();
3069 if (job == NULL)
3070 goto failed;
3071
Bram Moolenaar5983d502017-08-20 19:22:56 +02003072 /* TODO: when all lines are written and the fd is closed, the command
3073 * doesn't get EOF and hangs. */
3074 if (opt->jo_set & JO_IN_BUF)
3075 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
3076
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003077 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
3078 &child_thread_handle, &error, &winpty_err))
3079 goto failed;
3080
3081 channel_set_pipes(channel,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003082 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003083 winpty_conin_name(term->tl_winpty),
3084 GENERIC_WRITE, 0, NULL,
3085 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003086 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003087 winpty_conout_name(term->tl_winpty),
3088 GENERIC_READ, 0, NULL,
3089 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003090 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003091 winpty_conerr_name(term->tl_winpty),
3092 GENERIC_READ, 0, NULL,
3093 OPEN_EXISTING, 0, NULL));
3094
3095 jo = CreateJobObject(NULL, NULL);
3096 if (jo == NULL)
3097 goto failed;
3098
3099 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003100 {
3101 /* Failed, switch the way to terminate process with TerminateProcess. */
3102 CloseHandle(jo);
3103 jo = NULL;
3104 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003105
3106 winpty_spawn_config_free(spawn_config);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003107 vim_free(cmd_wchar);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003108
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003109 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003110
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02003111 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02003112 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003113
3114 job->jv_channel = channel;
3115 job->jv_proc_info.hProcess = child_process_handle;
3116 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
3117 job->jv_job_object = jo;
3118 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003119 sprintf(buf, "winpty://%lu",
3120 GetProcessId(winpty_agent_process(term->tl_winpty)));
3121 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003122 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003123 term->tl_job = job;
3124
3125 return OK;
3126
3127failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003128 if (argvar->v_type == VAR_LIST)
3129 vim_free(ga.ga_data);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003130 if (cmd_wchar != NULL)
3131 vim_free(cmd_wchar);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003132 if (spawn_config != NULL)
3133 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003134 if (channel != NULL)
3135 channel_clear(channel);
3136 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003137 {
3138 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003139 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003140 }
3141 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003142 if (jo != NULL)
3143 CloseHandle(jo);
3144 if (term->tl_winpty != NULL)
3145 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003146 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003147 if (term->tl_winpty_config != NULL)
3148 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003149 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003150 if (winpty_err != NULL)
3151 {
3152 char_u *msg = utf16_to_enc(
3153 (short_u *)winpty_error_msg(winpty_err), NULL);
3154
3155 EMSG(msg);
3156 winpty_error_free(winpty_err);
3157 }
3158 return FAIL;
3159}
3160
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003161 static int
3162create_pty_only(term_T *term, jobopt_T *opt)
3163{
3164 /* TODO: implement this */
3165 return FAIL;
3166}
3167
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003168/*
3169 * Free the terminal emulator part of "term".
3170 */
3171 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003172term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003173{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003174 if (term->tl_winpty != NULL)
3175 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003176 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003177 if (term->tl_winpty_config != NULL)
3178 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003179 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003180 if (term->tl_vterm != NULL)
3181 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003182 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003183}
3184
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003185/*
3186 * Request size to terminal.
3187 */
3188 static void
3189term_report_winsize(term_T *term, int rows, int cols)
3190{
3191 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3192}
3193
Bram Moolenaara83e3962017-08-17 14:39:07 +02003194 int
3195terminal_enabled(void)
3196{
3197 return dyn_winpty_init(FALSE) == OK;
3198}
3199
Bram Moolenaar3346cc42017-09-02 14:54:21 +02003200/*
3201 * Called when a channel has sent all the lines to a terminal.
3202 * Send a CTRL-D to mark the end of the text.
3203 */
3204 void
3205term_send_eof(channel_T *ch)
3206{
3207 term_T *term;
3208
3209 for (term = first_term; term != NULL; term = term->tl_next)
3210 if (term->tl_job == ch->ch_job)
3211 channel_send(ch, PART_IN, term->tl_eof_chars != NULL
3212 ? term->tl_eof_chars : (char_u *)"\004\r\n", 3, NULL);
3213}
Bram Moolenaara83e3962017-08-17 14:39:07 +02003214
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003215# else
3216
3217/**************************************
3218 * 3. Unix-like implementation.
3219 */
3220
3221/*
3222 * Create a new terminal of "rows" by "cols" cells.
3223 * Start job for "cmd".
3224 * Store the pointers in "term".
3225 * Return OK or FAIL.
3226 */
3227 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003228term_and_job_init(
3229 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02003230 typval_T *argvar,
3231 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003232{
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003233 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003234
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003235 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003236 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003237 if (term->tl_job != NULL)
3238 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003239
Bram Moolenaar61a66052017-07-22 18:39:00 +02003240 return term->tl_job != NULL
3241 && term->tl_job->jv_channel != NULL
3242 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003243}
3244
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003245 static int
3246create_pty_only(term_T *term, jobopt_T *opt)
3247{
3248 int ret;
3249
3250 create_vterm(term, term->tl_rows, term->tl_cols);
3251
3252 term->tl_job = job_alloc();
3253 if (term->tl_job == NULL)
3254 return FAIL;
3255 ++term->tl_job->jv_refcount;
3256
3257 /* behave like the job is already finished */
3258 term->tl_job->jv_status = JOB_FINISHED;
3259
3260 ret = mch_create_pty_channel(term->tl_job, opt);
3261
3262 return ret;
3263}
3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003265/*
3266 * Free the terminal emulator part of "term".
3267 */
3268 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003269term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003270{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003271 if (term->tl_vterm != NULL)
3272 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003273 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003274}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003275
3276/*
3277 * Request size to terminal.
3278 */
3279 static void
3280term_report_winsize(term_T *term, int rows, int cols)
3281{
3282 /* Use an ioctl() to report the new window size to the job. */
3283 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3284 {
3285 int fd = -1;
3286 int part;
3287
3288 for (part = PART_OUT; part < PART_COUNT; ++part)
3289 {
3290 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3291 if (isatty(fd))
3292 break;
3293 }
3294 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003295 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003296 }
3297}
3298
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003299# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003300
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003301#endif /* FEAT_TERMINAL */