blob: b2a3e7f3d33128ae0f6193779e553b2e385139fb [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 Moolenaar8c0095c2017-07-18 22:53:21 +0200117#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200118
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200119 /* last known vterm size */
120 int tl_rows;
121 int tl_cols;
122 /* vterm size does not follow window size */
123 int tl_rows_fixed;
124 int tl_cols_fixed;
125
Bram Moolenaar21554412017-07-24 21:44:43 +0200126 char_u *tl_title; /* NULL or allocated */
127 char_u *tl_status_text; /* NULL or allocated */
128
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200129 /* Range of screen rows to update. Zero based. */
130 int tl_dirty_row_start; /* -1 if nothing dirty */
131 int tl_dirty_row_end; /* row below last one to update */
132
Bram Moolenaard85f2712017-07-28 21:51:57 +0200133 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200134 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200135
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200136 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200137 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200138 int tl_cursor_blink;
139 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
140 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200141
142 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200143};
144
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200145#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
146#define TMODE_LOOP 2 /* CTRL-W N used */
147
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200148/*
149 * List of all active terminals.
150 */
151static term_T *first_term = NULL;
152
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200153/* Terminal active in terminal_loop(). */
154static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200155
156#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
157#define KEY_BUF_LEN 200
158
159/*
160 * Functions with separate implementation for MS-Windows and Unix-like systems.
161 */
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200162static int term_and_job_init(term_T *term, typval_T *argvar, jobopt_T *opt);
163static int create_pty_only(term_T *term, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200164static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200165static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200166
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200167/* The characters that we know (or assume) that the terminal expects for the
168 * backspace and enter keys. */
169static int term_backspace_char = BS;
170static int term_enter_char = CAR;
171static int term_nl_does_cr = FALSE;
172
173
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200174/**************************************
175 * 1. Generic code for all systems.
176 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200177
178/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200179 * Determine the terminal size from 'termsize' and the current window.
180 * Assumes term->tl_rows and term->tl_cols are zero.
181 */
182 static void
183set_term_and_win_size(term_T *term)
184{
185 if (*curwin->w_p_tms != NUL)
186 {
187 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
188
189 term->tl_rows = atoi((char *)curwin->w_p_tms);
190 term->tl_cols = atoi((char *)p);
191 }
192 if (term->tl_rows == 0)
193 term->tl_rows = curwin->w_height;
194 else
195 {
196 win_setheight_win(term->tl_rows, curwin);
197 term->tl_rows_fixed = TRUE;
198 }
199 if (term->tl_cols == 0)
200 term->tl_cols = curwin->w_width;
201 else
202 {
203 win_setwidth_win(term->tl_cols, curwin);
204 term->tl_cols_fixed = TRUE;
205 }
206}
207
208/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200209 * Initialize job options for a terminal job.
210 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200211 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200212 static void
213init_job_options(jobopt_T *opt)
214{
215 clear_job_options(opt);
216
217 opt->jo_mode = MODE_RAW;
218 opt->jo_out_mode = MODE_RAW;
219 opt->jo_err_mode = MODE_RAW;
220 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
221
222 opt->jo_io[PART_OUT] = JIO_BUFFER;
223 opt->jo_io[PART_ERR] = JIO_BUFFER;
224 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
225
226 opt->jo_modifiable[PART_OUT] = 0;
227 opt->jo_modifiable[PART_ERR] = 0;
228 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
229
230 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
231}
232
233/*
234 * Set job options mandatory for a terminal job.
235 */
236 static void
237setup_job_options(jobopt_T *opt, int rows, int cols)
238{
239 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
240 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
241 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200242 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
243 opt->jo_term_rows = rows;
244 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
245 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200246}
247
248 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200249term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251 exarg_T split_ea;
252 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200253 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200254 buf_T *old_curbuf = NULL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200255 int res;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200256
257 if (check_restricted() || check_secure())
258 return;
259
260 term = (term_T *)alloc_clear(sizeof(term_T));
261 if (term == NULL)
262 return;
263 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200264 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200265 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200266 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200267 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200268
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200269 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200270 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200271 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200272 /* Create a new buffer in the current window. */
273 if (!can_abandon(curbuf, forceit))
274 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200275 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200276 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200277 return;
278 }
279 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
280 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200281 {
282 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200283 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200284 }
285 }
286 else if (opt->jo_hidden)
287 {
288 buf_T *buf;
289
290 /* Create a new buffer without a window. Make it the current buffer for
291 * a moment to be able to do the initialisations. */
292 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
293 BLN_NEW | BLN_LISTED);
294 if (buf == NULL || ml_open(buf) == FAIL)
295 {
296 vim_free(term);
297 return;
298 }
299 old_curbuf = curbuf;
300 --curbuf->b_nwindows;
301 curbuf = buf;
302 curwin->w_buffer = buf;
303 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200304 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200305 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200306 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200307 /* Open a new window or tab. */
308 split_ea.cmdidx = CMD_new;
309 split_ea.cmd = (char_u *)"new";
310 split_ea.arg = (char_u *)"";
311 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
312 {
313 split_ea.line2 = opt->jo_term_rows;
314 split_ea.addr_count = 1;
315 }
316 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
317 {
318 split_ea.line2 = opt->jo_term_cols;
319 split_ea.addr_count = 1;
320 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200321
Bram Moolenaarda43b612017-08-11 22:27:50 +0200322 ex_splitview(&split_ea);
323 if (curwin == old_curwin)
324 {
325 /* split failed */
326 vim_free(term);
327 return;
328 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200329 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200330 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200331 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200332
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200333 if (!opt->jo_hidden)
334 {
335 /* only one size was taken care of with :new, do the other one */
336 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
337 win_setheight(opt->jo_term_rows);
338 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
339 win_setwidth(opt->jo_term_cols);
340 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200341
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200342 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200343 term->tl_next = first_term;
344 first_term = term;
345
Bram Moolenaar78712a72017-08-05 14:50:12 +0200346 if (opt->jo_term_name != NULL)
347 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
348 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200349 {
350 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200351 size_t len;
352 char_u *cmd, *p;
353
354 if (argvar->v_type == VAR_STRING)
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200355 {
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200356 cmd = argvar->vval.v_string;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200357 if (cmd == NULL)
358 cmd = (char_u *)"";
359 else if (STRCMP(cmd, "NONE") == 0)
360 cmd = (char_u *)"pty";
361 }
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200362 else if (argvar->v_type != VAR_LIST
363 || argvar->vval.v_list == NULL
364 || argvar->vval.v_list->lv_len < 1)
365 cmd = (char_u*)"";
366 else
367 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
368
369 len = STRLEN(cmd) + 10;
370 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200371
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200372 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200373 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200374 /* Prepend a ! to the command name to avoid the buffer name equals
375 * the executable, otherwise ":w!" would overwrite it. */
376 if (i == 0)
377 vim_snprintf((char *)p, len, "!%s", cmd);
378 else
379 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200380 if (buflist_findname(p) == NULL)
381 {
382 curbuf->b_ffname = p;
383 break;
384 }
385 }
386 }
387 curbuf->b_fname = curbuf->b_ffname;
388
Bram Moolenaar37c45832017-08-12 16:01:04 +0200389 if (opt->jo_term_opencmd != NULL)
390 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
391
Bram Moolenaareb44a682017-08-03 22:44:55 +0200392 set_string_option_direct((char_u *)"buftype", -1,
393 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
394
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200395 /* Mark the buffer as not modifiable. It can only be made modifiable after
396 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200397 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200398
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200399 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200400 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200401
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200402 /* System dependent: setup the vterm and maybe start the job in it. */
403 if (argvar->v_type == VAR_STRING
404 && argvar->vval.v_string != NULL
405 && STRCMP(argvar->vval.v_string, "NONE") == 0)
406 res = create_pty_only(term, opt);
407 else
408 res = term_and_job_init(term, argvar, opt);
409
410 if (res == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200411 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200412 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200413 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200414 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200415
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200416 /* Make sure we don't get stuck on sending keys to the job, it leads to
417 * a deadlock if the job is waiting for Vim to read. */
418 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
419
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200420 if (old_curbuf != NULL)
421 {
422 --curbuf->b_nwindows;
423 curbuf = old_curbuf;
424 curwin->w_buffer = curbuf;
425 ++curbuf->b_nwindows;
426 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200427 }
428 else
429 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200430 buf_T *buf = curbuf;
431
Bram Moolenaard85f2712017-07-28 21:51:57 +0200432 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200433 if (old_curbuf != NULL)
434 {
435 --curbuf->b_nwindows;
436 curbuf = old_curbuf;
437 curwin->w_buffer = curbuf;
438 ++curbuf->b_nwindows;
439 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200440
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200441 /* Wiping out the buffer will also close the window and call
442 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200443 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200444 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200445}
446
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200447/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200448 * ":terminal": open a terminal window and execute a job in it.
449 */
450 void
451ex_terminal(exarg_T *eap)
452{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200453 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200454 jobopt_T opt;
455 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200456 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200457
458 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200459
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200460 cmd = eap->arg;
461 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
462 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200463 char_u *p, *ep;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200464
465 cmd += 2;
466 p = skiptowhite(cmd);
Bram Moolenaarb2412082017-08-20 18:09:14 +0200467 ep = vim_strchr(cmd, '=');
468 if (ep != NULL && ep < p)
469 p = ep;
470
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200471 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
472 opt.jo_term_finish = 'c';
473 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
474 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200475 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
476 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200477 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
478 opt.jo_hidden = 1;
Bram Moolenaarb2412082017-08-20 18:09:14 +0200479 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
480 && ep != NULL && isdigit(ep[1]))
481 {
482 opt.jo_set2 |= JO2_TERM_ROWS;
483 opt.jo_term_rows = atoi((char *)ep + 1);
484 p = skiptowhite(cmd);
485 }
486 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
487 && ep != NULL && isdigit(ep[1]))
488 {
489 opt.jo_set2 |= JO2_TERM_COLS;
490 opt.jo_term_cols = atoi((char *)ep + 1);
491 p = skiptowhite(cmd);
492 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200493 else
494 {
495 if (*p)
496 *p = NUL;
497 EMSG2(_("E181: Invalid attribute: %s"), cmd);
498 return;
499 }
500 cmd = skipwhite(p);
501 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200502 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200503 /* Make a copy, an autocommand may set 'shell'. */
504 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200505
Bram Moolenaarb2412082017-08-20 18:09:14 +0200506 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200507 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200508 /* Write lines from current buffer to the job. */
509 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
510 opt.jo_io[PART_IN] = JIO_BUFFER;
511 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
512 opt.jo_in_top = eap->line1;
513 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200514 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200515
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200516 argvar.v_type = VAR_STRING;
517 argvar.vval.v_string = cmd;
518 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200519 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200520}
521
522/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200523 * Free the scrollback buffer for "term".
524 */
525 static void
526free_scrollback(term_T *term)
527{
528 int i;
529
530 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
531 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
532 ga_clear(&term->tl_scrollback);
533}
534
535/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200536 * Free a terminal and everything it refers to.
537 * Kills the job if there is one.
538 * Called when wiping out a buffer.
539 */
540 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200541free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200542{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200543 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200544 term_T *tp;
545
546 if (term == NULL)
547 return;
548 if (first_term == term)
549 first_term = term->tl_next;
550 else
551 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
552 if (tp->tl_next == term)
553 {
554 tp->tl_next = term->tl_next;
555 break;
556 }
557
558 if (term->tl_job != NULL)
559 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200560 if (term->tl_job->jv_status != JOB_ENDED
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200561 && term->tl_job->jv_status != JOB_FINISHED
562 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200563 job_stop(term->tl_job, NULL, "kill");
564 job_unref(term->tl_job);
565 }
566
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200567 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200568
569 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200570 vim_free(term->tl_title);
571 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200572 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200573 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200574 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200575 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200576 if (in_terminal_loop == term)
577 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200578}
579
580/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200581 * Write job output "msg[len]" to the vterm.
582 */
583 static void
584term_write_job_output(term_T *term, char_u *msg, size_t len)
585{
586 VTerm *vterm = term->tl_vterm;
587 char_u *p;
588 size_t done;
589 size_t len_now;
590
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200591 if (term_nl_does_cr)
592 vterm_input_write(vterm, (char *)msg, len);
593 else
594 /* need to convert NL to CR-NL */
595 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200596 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200597 for (p = msg + done; p < msg + len; )
598 {
599 if (*p == NL)
600 break;
601 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
602 }
603 len_now = p - msg - done;
604 vterm_input_write(vterm, (char *)msg + done, len_now);
605 if (p < msg + len && *p == NL)
606 {
607 vterm_input_write(vterm, "\r\n", 2);
608 ++len_now;
609 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200610 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200611
612 /* this invokes the damage callbacks */
613 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
614}
615
Bram Moolenaar1c844932017-07-24 23:36:41 +0200616 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200617update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200618{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200619 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200620 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200621 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200622 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200623 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200624 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200625 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200626 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200627#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200628 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200629 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200630#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200631 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200632}
633
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200634/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200635 * Invoked when "msg" output from a job was received. Write it to the terminal
636 * of "buffer".
637 */
638 void
639write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
640{
641 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200642 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200643
Bram Moolenaard85f2712017-07-28 21:51:57 +0200644 if (term->tl_vterm == NULL)
645 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200646 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200647 return;
648 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200649 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200650 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200651
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200652 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
653 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200654 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200655 {
656 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200657 ch_log(term->tl_job->jv_channel, "updating screen");
658 if (buffer == curbuf)
659 {
660 update_screen(0);
661 update_cursor(term, TRUE);
662 }
663 else
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200664 redraw_after_callback(TRUE);
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200665 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200666}
667
668/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200669 * Send a mouse position and click to the vterm
670 */
671 static int
672term_send_mouse(VTerm *vterm, int button, int pressed)
673{
674 VTermModifier mod = VTERM_MOD_NONE;
675
676 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
677 mouse_col - W_WINCOL(curwin), mod);
678 vterm_mouse_button(vterm, button, pressed, mod);
679 return TRUE;
680}
681
682/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200683 * Convert typed key "c" into bytes to send to the job.
684 * Return the number of bytes in "buf".
685 */
686 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200687term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200688{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200689 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200690 VTermKey key = VTERM_KEY_NONE;
691 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200692 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200693
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200694 switch (c)
695 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200696 case CAR: c = term_enter_char; break;
697 /* don't use VTERM_KEY_BACKSPACE, it always
698 * becomes 0x7f DEL */
699 case K_BS: c = term_backspace_char; break;
700
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200701 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200702 case K_DEL: key = VTERM_KEY_DEL; break;
703 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200704 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
705 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200706 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200707 case K_S_END: mod = VTERM_MOD_SHIFT;
708 key = VTERM_KEY_END; break;
709 case K_C_END: mod = VTERM_MOD_CTRL;
710 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200711 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
712 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
713 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
714 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
715 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
716 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
717 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
718 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
719 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
720 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
721 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
722 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
723 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200724 case K_S_HOME: mod = VTERM_MOD_SHIFT;
725 key = VTERM_KEY_HOME; break;
726 case K_C_HOME: mod = VTERM_MOD_CTRL;
727 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200728 case K_INS: key = VTERM_KEY_INS; break;
729 case K_K0: key = VTERM_KEY_KP_0; break;
730 case K_K1: key = VTERM_KEY_KP_1; break;
731 case K_K2: key = VTERM_KEY_KP_2; break;
732 case K_K3: key = VTERM_KEY_KP_3; break;
733 case K_K4: key = VTERM_KEY_KP_4; break;
734 case K_K5: key = VTERM_KEY_KP_5; break;
735 case K_K6: key = VTERM_KEY_KP_6; break;
736 case K_K7: key = VTERM_KEY_KP_7; break;
737 case K_K8: key = VTERM_KEY_KP_8; break;
738 case K_K9: key = VTERM_KEY_KP_9; break;
739 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
740 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
741 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
742 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
743 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
744 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
745 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
746 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
747 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
748 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
749 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
750 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
751 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200752 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
753 key = VTERM_KEY_LEFT; break;
754 case K_C_LEFT: mod = VTERM_MOD_CTRL;
755 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200756 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
757 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
758 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200759 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
760 key = VTERM_KEY_RIGHT; break;
761 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
762 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200763 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200764 case K_S_UP: mod = VTERM_MOD_SHIFT;
765 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200766 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200767
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200768 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
769 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
770 case K_MOUSELEFT: /* TODO */ return 0;
771 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200772
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200773 case K_LEFTMOUSE:
774 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
775 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
776 case K_LEFTRELEASE:
777 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
778 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
779 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
780 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
781 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
782 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
783 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
784 case K_X1MOUSE: /* TODO */ return 0;
785 case K_X1DRAG: /* TODO */ return 0;
786 case K_X1RELEASE: /* TODO */ return 0;
787 case K_X2MOUSE: /* TODO */ return 0;
788 case K_X2DRAG: /* TODO */ return 0;
789 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200790
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200791 case K_IGNORE: return 0;
792 case K_NOP: return 0;
793 case K_UNDO: return 0;
794 case K_HELP: return 0;
795 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
796 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
797 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
798 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
799 case K_SELECT: return 0;
800#ifdef FEAT_GUI
801 case K_VER_SCROLLBAR: return 0;
802 case K_HOR_SCROLLBAR: return 0;
803#endif
804#ifdef FEAT_GUI_TABLINE
805 case K_TABLINE: return 0;
806 case K_TABMENU: return 0;
807#endif
808#ifdef FEAT_NETBEANS_INTG
809 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
810#endif
811#ifdef FEAT_DND
812 case K_DROP: return 0;
813#endif
814#ifdef FEAT_AUTOCMD
815 case K_CURSORHOLD: return 0;
816#endif
817 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
818 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200819 }
820
821 /*
822 * Convert special keys to vterm keys:
823 * - Write keys to vterm: vterm_keyboard_key()
824 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200825 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200826 */
827 if (key != VTERM_KEY_NONE)
828 /* Special key, let vterm convert it. */
829 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200830 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200831 /* Normal character, let vterm convert it. */
832 vterm_keyboard_unichar(vterm, c, mod);
833
834 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200835 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200836}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200837
Bram Moolenaar938783d2017-07-16 20:13:26 +0200838/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200839 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200840 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200841 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200842term_job_running(term_T *term)
843{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200844 /* Also consider the job finished when the channel is closed, to avoid a
845 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200846 return term != NULL
847 && term->tl_job != NULL
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200848 && channel_is_open(term->tl_job->jv_channel)
849 && (term->tl_job->jv_status == JOB_STARTED
850 || term->tl_job->jv_channel->ch_keep_open);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200851}
852
853/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200854 * Add the last line of the scrollback buffer to the buffer in the window.
855 */
856 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200857add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200858{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200859 buf_T *buf = term->tl_buffer;
860 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
861 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200862
Bram Moolenaar58302322017-08-22 20:33:53 +0200863#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +0200864 if (!enc_utf8 && enc_codepage > 0)
865 {
866 WCHAR *ret = NULL;
867 int length = 0;
868
869 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
870 &ret, &length);
871 if (ret != NULL)
872 {
873 WideCharToMultiByte_alloc(enc_codepage, 0,
874 ret, length, (char **)&text, &len, 0, 0);
875 vim_free(ret);
876 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
877 vim_free(text);
878 }
879 }
880 else
881#endif
882 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200883 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200884 {
885 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200886 curbuf = buf;
887 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200888 curbuf = curwin->w_buffer;
889 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200890}
891
892/*
893 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200894 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200895 */
896 static void
897move_terminal_to_buffer(term_T *term)
898{
899 win_T *wp;
900 int len;
901 int lines_skipped = 0;
902 VTermPos pos;
903 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200904 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200905 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200906
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200907 if (term->tl_vterm == NULL)
908 return;
909 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200910 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
911 {
912 len = 0;
913 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
914 if (vterm_screen_get_cell(screen, pos, &cell) != 0
915 && cell.chars[0] != NUL)
916 len = pos.col + 1;
917
918 if (len == 0)
919 ++lines_skipped;
920 else
921 {
922 while (lines_skipped > 0)
923 {
924 /* Line was skipped, add an empty line. */
925 --lines_skipped;
926 if (ga_grow(&term->tl_scrollback, 1) == OK)
927 {
928 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
929 + term->tl_scrollback.ga_len;
930
931 line->sb_cols = 0;
932 line->sb_cells = NULL;
933 ++term->tl_scrollback.ga_len;
934
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200935 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200936 }
937 }
938
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200939 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200940 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
941 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200942 garray_T ga;
943 int width;
944 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200945 + term->tl_scrollback.ga_len;
946
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200947 ga_init2(&ga, 1, 100);
948 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200949 {
950 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200951 {
952 width = 1;
953 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
954 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +0200955 ga.ga_len += utf_char2bytes(' ',
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200956 (char_u *)ga.ga_data + ga.ga_len);
957 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200958 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200959 {
960 width = cell.width;
961
962 p[pos.col].width = cell.width;
963 p[pos.col].attrs = cell.attrs;
964 p[pos.col].fg = cell.fg;
965 p[pos.col].bg = cell.bg;
966
967 if (ga_grow(&ga, MB_MAXBYTES) == OK)
968 {
969 int i;
970 int c;
971
972 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +0200973 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200974 (char_u *)ga.ga_data + ga.ga_len);
975 }
976 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200977 }
978 line->sb_cols = len;
979 line->sb_cells = p;
980 ++term->tl_scrollback.ga_len;
981
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200982 if (ga_grow(&ga, 1) == FAIL)
983 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
984 else
985 {
986 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
987 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
988 }
989 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200990 }
991 else
992 vim_free(p);
993 }
994 }
995
996 FOR_ALL_WINDOWS(wp)
997 {
998 if (wp->w_buffer == term->tl_buffer)
999 {
1000 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1001 wp->w_cursor.col = 0;
1002 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +02001003 if (wp->w_cursor.lnum >= wp->w_height)
1004 {
1005 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
1006
1007 if (wp->w_topline < min_topline)
1008 wp->w_topline = min_topline;
1009 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02001010 redraw_win_later(wp, NOT_VALID);
1011 }
1012 }
1013}
1014
1015 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001016set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001017{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001018 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001019 vim_free(term->tl_status_text);
1020 term->tl_status_text = NULL;
1021 if (term->tl_buffer == curbuf)
1022 maketitle();
1023}
1024
1025/*
1026 * Called after the job if finished and Terminal mode is not active:
1027 * Move the vterm contents into the scrollback buffer and free the vterm.
1028 */
1029 static void
1030cleanup_vterm(term_T *term)
1031{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001032 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001033 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001034 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001035 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001036}
1037
1038/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001039 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001040 * Suspends updating the terminal window.
1041 */
1042 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001043term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001044{
1045 term_T *term = curbuf->b_term;
1046
1047 /* Append the current terminal contents to the buffer. */
1048 move_terminal_to_buffer(term);
1049
Bram Moolenaar6d819742017-08-06 14:57:49 +02001050 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001051
Bram Moolenaar6d819742017-08-06 14:57:49 +02001052 /* Move the window cursor to the position of the cursor in the
1053 * terminal. */
1054 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1055 + term->tl_cursor_pos.row + 1;
1056 check_cursor();
1057 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001058
Bram Moolenaar6d819742017-08-06 14:57:49 +02001059 /* Display the same lines as in the terminal. */
1060 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001061}
1062
1063/*
1064 * Returns TRUE if the current window contains a terminal and we are in
1065 * Terminal-Normal mode.
1066 */
1067 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001068term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001069{
1070 term_T *term = curbuf->b_term;
1071
Bram Moolenaar6d819742017-08-06 14:57:49 +02001072 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001073}
1074
1075/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001076 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001077 * Restores updating the terminal window.
1078 */
1079 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001080term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001081{
1082 term_T *term = curbuf->b_term;
1083 sb_line_T *line;
1084 garray_T *gap;
1085
1086 /* Remove the terminal contents from the scrollback and the buffer. */
1087 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001088 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1089 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001090 {
1091 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1092 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1093 vim_free(line->sb_cells);
1094 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001095 }
1096 check_cursor();
1097
Bram Moolenaar6d819742017-08-06 14:57:49 +02001098 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001099
1100 if (term->tl_channel_closed)
1101 cleanup_vterm(term);
1102 redraw_buf_and_status_later(curbuf, NOT_VALID);
1103}
1104
1105/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001106 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001107 * Note: while waiting a terminal may be closed and freed if the channel is
1108 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001109 * TODO: use terminal mode mappings.
1110 */
1111 static int
1112term_vgetc()
1113{
1114 int c;
1115
1116 ++no_mapping;
1117 ++allow_keys;
1118 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001119#ifdef WIN3264
1120 ctrl_break_was_pressed = FALSE;
1121#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001122 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001123 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001124 --no_mapping;
1125 --allow_keys;
1126 return c;
1127}
1128
1129/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001130 * Get the part that is connected to the tty. Normally this is PART_IN, but
1131 * when writing buffer lines to the job it can be another. This makes it
1132 * possible to do "1,5term vim -".
1133 */
1134 static ch_part_T
1135get_tty_part(term_T *term)
1136{
1137#ifdef UNIX
1138 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1139 int i;
1140
1141 for (i = 0; i < 3; ++i)
1142 {
1143 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1144
1145 if (isatty(fd))
1146 return parts[i];
1147 }
1148#endif
1149 return PART_IN;
1150}
1151
1152/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001153 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001154 * Return FAIL when the key needs to be handled in Normal mode.
1155 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001156 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001157 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001158send_keys_to_term(term_T *term, int c, int typed)
1159{
1160 char msg[KEY_BUF_LEN];
1161 size_t len;
1162 static int mouse_was_outside = FALSE;
1163 int dragging_outside = FALSE;
1164
1165 /* Catch keys that need to be handled as in Normal mode. */
1166 switch (c)
1167 {
1168 case NUL:
1169 case K_ZERO:
1170 if (typed)
1171 stuffcharReadbuff(c);
1172 return FAIL;
1173
1174 case K_IGNORE:
1175 return FAIL;
1176
1177 case K_LEFTDRAG:
1178 case K_MIDDLEDRAG:
1179 case K_RIGHTDRAG:
1180 case K_X1DRAG:
1181 case K_X2DRAG:
1182 dragging_outside = mouse_was_outside;
1183 /* FALLTHROUGH */
1184 case K_LEFTMOUSE:
1185 case K_LEFTMOUSE_NM:
1186 case K_LEFTRELEASE:
1187 case K_LEFTRELEASE_NM:
1188 case K_MIDDLEMOUSE:
1189 case K_MIDDLERELEASE:
1190 case K_RIGHTMOUSE:
1191 case K_RIGHTRELEASE:
1192 case K_X1MOUSE:
1193 case K_X1RELEASE:
1194 case K_X2MOUSE:
1195 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001196
1197 case K_MOUSEUP:
1198 case K_MOUSEDOWN:
1199 case K_MOUSELEFT:
1200 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001201 if (mouse_row < W_WINROW(curwin)
1202 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1203 || mouse_col < W_WINCOL(curwin)
1204 || mouse_col >= W_ENDCOL(curwin)
1205 || dragging_outside)
1206 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001207 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001208 if (typed)
1209 {
1210 stuffcharReadbuff(c);
1211 mouse_was_outside = TRUE;
1212 }
1213 return FAIL;
1214 }
1215 }
1216 if (typed)
1217 mouse_was_outside = FALSE;
1218
1219 /* Convert the typed key to a sequence of bytes for the job. */
1220 len = term_convert_key(term, c, msg);
1221 if (len > 0)
1222 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001223 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1224 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001225
1226 return OK;
1227}
1228
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001229 static void
1230position_cursor(win_T *wp, VTermPos *pos)
1231{
1232 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1233 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1234 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1235}
1236
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001237/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001238 * Handle CTRL-W "": send register contents to the job.
1239 */
1240 static void
1241term_paste_register(int prev_c UNUSED)
1242{
1243 int c;
1244 list_T *l;
1245 listitem_T *item;
1246 long reglen = 0;
1247 int type;
1248
1249#ifdef FEAT_CMDL_INFO
1250 if (add_to_showcmd(prev_c))
1251 if (add_to_showcmd('"'))
1252 out_flush();
1253#endif
1254 c = term_vgetc();
1255#ifdef FEAT_CMDL_INFO
1256 clear_showcmd();
1257#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001258 if (!term_use_loop())
1259 /* job finished while waiting for a character */
1260 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001261
1262 /* CTRL-W "= prompt for expression to evaluate. */
1263 if (c == '=' && get_expr_register() != '=')
1264 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001265 if (!term_use_loop())
1266 /* job finished while waiting for a character */
1267 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001268
1269 l = (list_T *)get_reg_contents(c, GREG_LIST);
1270 if (l != NULL)
1271 {
1272 type = get_reg_type(c, &reglen);
1273 for (item = l->lv_first; item != NULL; item = item->li_next)
1274 {
1275 char_u *s = get_tv_string(&item->li_tv);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001276#ifdef WIN3264
1277 char_u *tmp = s;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001278
Bram Moolenaar285f2432017-08-23 23:10:21 +02001279 if (!enc_utf8 && enc_codepage > 0)
1280 {
1281 WCHAR *ret = NULL;
1282 int length = 0;
1283
1284 MultiByteToWideChar_alloc(enc_codepage, 0, (char*)s, STRLEN(s),
1285 &ret, &length);
1286 if (ret != NULL)
1287 {
1288 WideCharToMultiByte_alloc(CP_UTF8, 0,
1289 ret, length, (char **)&s, &length, 0, 0);
1290 vim_free(ret);
1291 }
1292 }
1293#endif
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001294 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1295 s, STRLEN(s), NULL);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001296#ifdef WIN3264
1297 if (tmp != s)
1298 vim_free(s);
1299#endif
1300
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001301 if (item->li_next != NULL || type == MLINE)
1302 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1303 (char_u *)"\r", 1, NULL);
1304 }
1305 list_free(l);
1306 }
1307}
1308
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001309#if defined(FEAT_GUI) || defined(PROTO)
1310/*
1311 * Return TRUE when the cursor of the terminal should be displayed.
1312 */
1313 int
1314use_terminal_cursor()
1315{
1316 return in_terminal_loop != NULL;
1317}
1318
1319 cursorentry_T *
1320term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1321{
1322 term_T *term = in_terminal_loop;
1323 static cursorentry_T entry;
1324
1325 vim_memset(&entry, 0, sizeof(entry));
1326 entry.shape = entry.mshape =
1327 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1328 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1329 SHAPE_BLOCK;
1330 entry.percentage = 20;
1331 if (term->tl_cursor_blink)
1332 {
1333 entry.blinkwait = 700;
1334 entry.blinkon = 400;
Bram Moolenaar58302322017-08-22 20:33:53 +02001335 entry.blinkoff = 250;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001336 }
1337 *fg = gui.back_pixel;
1338 if (term->tl_cursor_color == NULL)
1339 *bg = gui.norm_pixel;
1340 else
1341 *bg = color_name2handle(term->tl_cursor_color);
1342 entry.name = "n";
1343 entry.used_for = SHAPE_CURSOR;
1344
1345 return &entry;
1346}
1347#endif
1348
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001349static int did_change_cursor = FALSE;
1350
1351 static void
1352may_set_cursor_props(term_T *term)
1353{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001354#ifdef FEAT_GUI
1355 /* For the GUI the cursor properties are obtained with
1356 * term_get_cursor_shape(). */
1357 if (gui.in_use)
1358 return;
1359#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001360 if (in_terminal_loop == term)
1361 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001362 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001363 if (term->tl_cursor_color != NULL)
1364 term_cursor_color(term->tl_cursor_color);
1365 else
1366 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001367 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1368 }
1369}
1370
1371 static void
1372may_restore_cursor_props(void)
1373{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001374#ifdef FEAT_GUI
1375 if (gui.in_use)
1376 return;
1377#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001378 if (did_change_cursor)
1379 {
1380 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001381 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001382 /* this will restore the initial cursor style, if possible */
1383 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001384 }
1385}
1386
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001387/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001388 * Returns TRUE if the current window contains a terminal and we are sending
1389 * keys to the job.
1390 */
1391 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001392term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001393{
1394 term_T *term = curbuf->b_term;
1395
1396 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001397 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001398 && term->tl_vterm != NULL
1399 && term_job_running(term);
1400}
1401
1402/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001403 * Wait for input and send it to the job.
1404 * Return when the start of a CTRL-W command is typed or anything else that
1405 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001406 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1407 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001408 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001409 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001410terminal_loop(void)
1411{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001412 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001413 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001414 int ret;
1415
Bram Moolenaar679653e2017-08-13 14:13:19 +02001416 /* Remember the terminal we are sending keys to. However, the terminal
1417 * might be closed while waiting for a character, e.g. typing "exit" in a
1418 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1419 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001420 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001421
1422 if (*curwin->w_p_tk != NUL)
1423 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001424 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001425 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001426
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001427#ifdef UNIX
1428 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001429 int part = get_tty_part(curbuf->b_term);
1430 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001431
1432 if (isatty(fd))
1433 {
1434 ttyinfo_T info;
1435
1436 /* Get the current backspace and enter characters of the pty. */
1437 if (get_tty_info(fd, &info) == OK)
1438 {
1439 term_backspace_char = info.backspace;
1440 term_enter_char = info.enter;
1441 term_nl_does_cr = info.nl_does_cr;
1442 }
1443 }
1444 }
1445#endif
1446
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001447 for (;;)
1448 {
1449 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001450 /* Repeat redrawing in case a message is received while redrawing. */
1451 while (curwin->w_redr_type != 0)
1452 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001453 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001454
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001455 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001456 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001457 /* job finished while waiting for a character */
1458 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001459 if (c == K_IGNORE)
1460 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001461
Bram Moolenaarfae42832017-08-01 22:24:26 +02001462#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001463 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001464 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001465 if (ctrl_break_was_pressed)
1466 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001467#endif
1468
Bram Moolenaar69198192017-08-05 14:10:48 +02001469 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001470 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001471 int prev_c = c;
1472
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001473#ifdef FEAT_CMDL_INFO
1474 if (add_to_showcmd(c))
1475 out_flush();
1476#endif
1477 c = term_vgetc();
1478#ifdef FEAT_CMDL_INFO
1479 clear_showcmd();
1480#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001481 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001482 /* job finished while waiting for a character */
1483 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001484
Bram Moolenaar69198192017-08-05 14:10:48 +02001485 if (prev_c == Ctrl_BSL)
1486 {
1487 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001488 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001489 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1490 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001491 ret = FAIL;
1492 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001493 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001494 /* Send both keys to the terminal. */
1495 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1496 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001497 else if (c == Ctrl_C)
1498 {
1499 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1500 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1501 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001502 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001503 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001504 /* "CTRL-W .": send CTRL-W to the job */
1505 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001506 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001507 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001508 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001509 /* CTRL-W N : go to Terminal-Normal mode. */
1510 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001511 ret = FAIL;
1512 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001513 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001514 else if (c == '"')
1515 {
1516 term_paste_register(prev_c);
1517 continue;
1518 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001519 else if (termkey == 0 || c != termkey)
1520 {
1521 stuffcharReadbuff(Ctrl_W);
1522 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001523 ret = OK;
1524 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001525 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001526 }
Bram Moolenaar58302322017-08-22 20:33:53 +02001527# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02001528 if (!enc_utf8 && has_mbyte && c >= 0x80)
1529 {
1530 WCHAR wc;
1531 char_u mb[3];
1532
1533 mb[0] = (unsigned)c >> 8;
1534 mb[1] = c;
1535 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1536 c = wc;
1537 }
1538# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001539 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001540 {
1541 ret = OK;
1542 goto theend;
1543 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001544 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001545 ret = FAIL;
1546
1547theend:
1548 in_terminal_loop = NULL;
1549 may_restore_cursor_props();
1550 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001551}
1552
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001553/*
1554 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001555 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001556 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001557 */
1558 void
1559term_job_ended(job_T *job)
1560{
1561 term_T *term;
1562 int did_one = FALSE;
1563
1564 for (term = first_term; term != NULL; term = term->tl_next)
1565 if (term->tl_job == job)
1566 {
1567 vim_free(term->tl_title);
1568 term->tl_title = NULL;
1569 vim_free(term->tl_status_text);
1570 term->tl_status_text = NULL;
1571 redraw_buf_and_status_later(term->tl_buffer, VALID);
1572 did_one = TRUE;
1573 }
1574 if (did_one)
1575 redraw_statuslines();
1576 if (curbuf->b_term != NULL)
1577 {
1578 if (curbuf->b_term->tl_job == job)
1579 maketitle();
1580 update_cursor(curbuf->b_term, TRUE);
1581 }
1582}
1583
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001584 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001585may_toggle_cursor(term_T *term)
1586{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001587 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001588 {
1589 if (term->tl_cursor_visible)
1590 cursor_on();
1591 else
1592 cursor_off();
1593 }
1594}
1595
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001596/*
1597 * Reverse engineer the RGB value into a cterm color index.
1598 * First color is 1. Return 0 if no match found.
1599 */
1600 static int
1601color2index(VTermColor *color, int fg, int *boldp)
1602{
1603 int red = color->red;
1604 int blue = color->blue;
1605 int green = color->green;
1606
1607 /* The argument for lookup_color() is for the color_names[] table. */
1608 if (red == 0)
1609 {
1610 if (green == 0)
1611 {
1612 if (blue == 0)
1613 return lookup_color(0, fg, boldp) + 1; /* black */
1614 if (blue == 224)
1615 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1616 }
1617 else if (green == 224)
1618 {
1619 if (blue == 0)
1620 return lookup_color(2, fg, boldp) + 1; /* dark green */
1621 if (blue == 224)
1622 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1623 }
1624 }
1625 else if (red == 224)
1626 {
1627 if (green == 0)
1628 {
1629 if (blue == 0)
1630 return lookup_color(4, fg, boldp) + 1; /* dark red */
1631 if (blue == 224)
1632 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1633 }
1634 else if (green == 224)
1635 {
1636 if (blue == 0)
1637 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1638 if (blue == 224)
1639 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1640 }
1641 }
1642 else if (red == 128)
1643 {
1644 if (green == 128 && blue == 128)
1645 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1646 }
1647 else if (red == 255)
1648 {
1649 if (green == 64)
1650 {
1651 if (blue == 64)
1652 return lookup_color(20, fg, boldp) + 1; /* light red */
1653 if (blue == 255)
1654 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1655 }
1656 else if (green == 255)
1657 {
1658 if (blue == 64)
1659 return lookup_color(24, fg, boldp) + 1; /* yellow */
1660 if (blue == 255)
1661 return lookup_color(26, fg, boldp) + 1; /* white */
1662 }
1663 }
1664 else if (red == 64)
1665 {
1666 if (green == 64)
1667 {
1668 if (blue == 255)
1669 return lookup_color(14, fg, boldp) + 1; /* light blue */
1670 }
1671 else if (green == 255)
1672 {
1673 if (blue == 64)
1674 return lookup_color(16, fg, boldp) + 1; /* light green */
1675 if (blue == 255)
1676 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1677 }
1678 }
1679 if (t_colors >= 256)
1680 {
1681 if (red == blue && red == green)
1682 {
1683 /* 24-color greyscale */
1684 static int cutoff[23] = {
1685 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1686 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1687 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1688 int i;
1689
1690 for (i = 0; i < 23; ++i)
1691 if (red < cutoff[i])
1692 return i + 233;
1693 return 256;
1694 }
1695
1696 /* 216-color cube */
1697 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001698 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001699 + (blue + 25) / 0x33;
1700 }
1701 return 0;
1702}
1703
1704/*
1705 * Convert the attributes of a vterm cell into an attribute index.
1706 */
1707 static int
1708cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1709{
1710 int attr = 0;
1711
1712 if (cellattrs.bold)
1713 attr |= HL_BOLD;
1714 if (cellattrs.underline)
1715 attr |= HL_UNDERLINE;
1716 if (cellattrs.italic)
1717 attr |= HL_ITALIC;
1718 if (cellattrs.strike)
1719 attr |= HL_STANDOUT;
1720 if (cellattrs.reverse)
1721 attr |= HL_INVERSE;
1722
1723#ifdef FEAT_GUI
1724 if (gui.in_use)
1725 {
1726 guicolor_T fg, bg;
1727
1728 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1729 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1730 return get_gui_attr_idx(attr, fg, bg);
1731 }
1732 else
1733#endif
1734#ifdef FEAT_TERMGUICOLORS
1735 if (p_tgc)
1736 {
1737 guicolor_T fg, bg;
1738
1739 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1740 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1741
1742 return get_tgc_attr_idx(attr, fg, bg);
1743 }
1744 else
1745#endif
1746 {
1747 int bold = MAYBE;
1748 int fg = color2index(&cellfg, TRUE, &bold);
1749 int bg = color2index(&cellbg, FALSE, &bold);
1750
1751 /* with 8 colors set the bold attribute to get a bright foreground */
1752 if (bold == TRUE)
1753 attr |= HL_BOLD;
1754 return get_cterm_attr_idx(attr, fg, bg);
1755 }
1756 return 0;
1757}
1758
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001759 static int
1760handle_damage(VTermRect rect, void *user)
1761{
1762 term_T *term = (term_T *)user;
1763
1764 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1765 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1766 redraw_buf_later(term->tl_buffer, NOT_VALID);
1767 return 1;
1768}
1769
1770 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001771handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001772{
1773 term_T *term = (term_T *)user;
1774
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001775 /* Scrolling up is done much more efficiently by deleting lines instead of
1776 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001777 if (dest.start_col == src.start_col
1778 && dest.end_col == src.end_col
1779 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001780 {
1781 win_T *wp;
1782 VTermColor fg, bg;
1783 VTermScreenCellAttrs attr;
1784 int clear_attr;
1785
1786 /* Set the color to clear lines with. */
1787 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1788 &fg, &bg);
1789 vim_memset(&attr, 0, sizeof(attr));
1790 clear_attr = cell2attr(attr, fg, bg);
1791
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001792 FOR_ALL_WINDOWS(wp)
1793 {
1794 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001795 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001796 src.start_row - dest.start_row, FALSE, FALSE,
1797 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001798 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001799 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001800 redraw_buf_later(term->tl_buffer, NOT_VALID);
1801 return 1;
1802}
1803
1804 static int
1805handle_movecursor(
1806 VTermPos pos,
1807 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001808 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001809 void *user)
1810{
1811 term_T *term = (term_T *)user;
1812 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001813
1814 term->tl_cursor_pos = pos;
1815 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001816
1817 FOR_ALL_WINDOWS(wp)
1818 {
1819 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001820 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001821 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001822 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001823 {
1824 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001825 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001826 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001827
1828 return 1;
1829}
1830
Bram Moolenaar21554412017-07-24 21:44:43 +02001831 static int
1832handle_settermprop(
1833 VTermProp prop,
1834 VTermValue *value,
1835 void *user)
1836{
1837 term_T *term = (term_T *)user;
1838
1839 switch (prop)
1840 {
1841 case VTERM_PROP_TITLE:
1842 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001843 /* a blank title isn't useful, make it empty, so that "running" is
1844 * displayed */
1845 if (*skipwhite((char_u *)value->string) == NUL)
1846 term->tl_title = NULL;
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001847#ifdef WIN3264
1848 else if (!enc_utf8 && enc_codepage > 0)
1849 {
1850 WCHAR *ret = NULL;
1851 int length = 0;
1852
1853 MultiByteToWideChar_alloc(CP_UTF8, 0,
1854 (char*)value->string, STRLEN(value->string),
1855 &ret, &length);
1856 if (ret != NULL)
1857 {
1858 WideCharToMultiByte_alloc(enc_codepage, 0,
1859 ret, length, (char**)&term->tl_title,
1860 &length, 0, 0);
1861 vim_free(ret);
1862 }
1863 }
1864#endif
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001865 else
1866 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001867 vim_free(term->tl_status_text);
1868 term->tl_status_text = NULL;
1869 if (term == curbuf->b_term)
1870 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001871 break;
1872
1873 case VTERM_PROP_CURSORVISIBLE:
1874 term->tl_cursor_visible = value->boolean;
1875 may_toggle_cursor(term);
1876 out_flush();
1877 break;
1878
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001879 case VTERM_PROP_CURSORBLINK:
1880 term->tl_cursor_blink = value->boolean;
1881 may_set_cursor_props(term);
1882 break;
1883
1884 case VTERM_PROP_CURSORSHAPE:
1885 term->tl_cursor_shape = value->number;
1886 may_set_cursor_props(term);
1887 break;
1888
1889 case VTERM_PROP_CURSORCOLOR:
1890 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001891 if (*value->string == NUL)
1892 term->tl_cursor_color = NULL;
1893 else
1894 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001895 may_set_cursor_props(term);
1896 break;
1897
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001898 case VTERM_PROP_ALTSCREEN:
1899 /* TODO: do anything else? */
1900 term->tl_using_altscreen = value->boolean;
1901 break;
1902
Bram Moolenaar21554412017-07-24 21:44:43 +02001903 default:
1904 break;
1905 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001906 /* Always return 1, otherwise vterm doesn't store the value internally. */
1907 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001908}
1909
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001910/*
1911 * The job running in the terminal resized the terminal.
1912 */
1913 static int
1914handle_resize(int rows, int cols, void *user)
1915{
1916 term_T *term = (term_T *)user;
1917 win_T *wp;
1918
1919 term->tl_rows = rows;
1920 term->tl_cols = cols;
1921 FOR_ALL_WINDOWS(wp)
1922 {
1923 if (wp->w_buffer == term->tl_buffer)
1924 {
1925 win_setheight_win(rows, wp);
1926 win_setwidth_win(cols, wp);
1927 }
1928 }
1929
1930 redraw_buf_later(term->tl_buffer, NOT_VALID);
1931 return 1;
1932}
1933
Bram Moolenaard85f2712017-07-28 21:51:57 +02001934/*
1935 * Handle a line that is pushed off the top of the screen.
1936 */
1937 static int
1938handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1939{
1940 term_T *term = (term_T *)user;
1941
1942 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001943 if (ga_grow(&term->tl_scrollback, 1) == OK)
1944 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001945 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001946 int len = 0;
1947 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001948 int c;
1949 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001950 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001951 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001952
1953 /* do not store empty cells at the end */
1954 for (i = 0; i < cols; ++i)
1955 if (cells[i].chars[0] != 0)
1956 len = i + 1;
1957
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001958 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001959 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001960 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001961 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001962 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001963 for (col = 0; col < len; col += cells[col].width)
1964 {
1965 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1966 {
1967 ga.ga_len = 0;
1968 break;
1969 }
1970 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +02001971 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001972 (char_u *)ga.ga_data + ga.ga_len);
1973 p[col].width = cells[col].width;
1974 p[col].attrs = cells[col].attrs;
1975 p[col].fg = cells[col].fg;
1976 p[col].bg = cells[col].bg;
1977 }
1978 }
1979 if (ga_grow(&ga, 1) == FAIL)
1980 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1981 else
1982 {
1983 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1984 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1985 }
1986 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001987
1988 line = (sb_line_T *)term->tl_scrollback.ga_data
1989 + term->tl_scrollback.ga_len;
1990 line->sb_cols = len;
1991 line->sb_cells = p;
1992 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001993 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001994 }
1995 return 0; /* ignored */
1996}
1997
Bram Moolenaar21554412017-07-24 21:44:43 +02001998static VTermScreenCallbacks screen_callbacks = {
1999 handle_damage, /* damage */
2000 handle_moverect, /* moverect */
2001 handle_movecursor, /* movecursor */
2002 handle_settermprop, /* settermprop */
2003 NULL, /* bell */
2004 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002005 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02002006 NULL /* sb_popline */
2007};
2008
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002009/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02002010 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002011 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02002012 */
2013 void
2014term_channel_closed(channel_T *ch)
2015{
2016 term_T *term;
2017 int did_one = FALSE;
2018
2019 for (term = first_term; term != NULL; term = term->tl_next)
2020 if (term->tl_job == ch->ch_job)
2021 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002022 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002023 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002024
Bram Moolenaard85f2712017-07-28 21:51:57 +02002025 vim_free(term->tl_title);
2026 term->tl_title = NULL;
2027 vim_free(term->tl_status_text);
2028 term->tl_status_text = NULL;
2029
Bram Moolenaar423802d2017-07-30 16:52:24 +02002030 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02002031 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002032 {
2033 int fnum = term->tl_buffer->b_fnum;
2034
Bram Moolenaar423802d2017-07-30 16:52:24 +02002035 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002036
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002037 if (term->tl_finish == 'c')
2038 {
2039 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002040 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002041 curbuf = term->tl_buffer;
2042 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
2043 break;
2044 }
2045 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
2046 {
2047 char buf[50];
2048
2049 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002050 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02002051 vim_snprintf(buf, sizeof(buf),
2052 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02002053 ? "botright sbuf %d"
2054 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002055 do_cmdline_cmd((char_u *)buf);
2056 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002057 else
2058 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002059 }
2060
Bram Moolenaard85f2712017-07-28 21:51:57 +02002061 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002062 }
2063 if (did_one)
2064 {
2065 redraw_statuslines();
2066
2067 /* Need to break out of vgetc(). */
2068 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002069 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002070
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002071 term = curbuf->b_term;
2072 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002073 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002074 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002075 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002076 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002077 }
2078 }
2079}
2080
2081/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002082 * Called to update a window that contains an active terminal.
2083 * Returns FAIL when there is no terminal running in this window or in
2084 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002085 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002086 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002087term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002088{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002089 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002090 VTerm *vterm;
2091 VTermScreen *screen;
2092 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002093 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002094
Bram Moolenaar6d819742017-08-06 14:57:49 +02002095 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002096 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002097
Bram Moolenaard85f2712017-07-28 21:51:57 +02002098 vterm = term->tl_vterm;
2099 screen = vterm_obtain_screen(vterm);
2100 state = vterm_obtain_state(vterm);
2101
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002102 /*
2103 * If the window was resized a redraw will be triggered and we get here.
2104 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2105 */
2106 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2107 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002108 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002109 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2110 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2111 win_T *twp;
2112
2113 FOR_ALL_WINDOWS(twp)
2114 {
2115 /* When more than one window shows the same terminal, use the
2116 * smallest size. */
2117 if (twp->w_buffer == term->tl_buffer)
2118 {
2119 if (!term->tl_rows_fixed && rows > twp->w_height)
2120 rows = twp->w_height;
2121 if (!term->tl_cols_fixed && cols > twp->w_width)
2122 cols = twp->w_width;
2123 }
2124 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002125
2126 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002127 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002128 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002129 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002130 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002131
2132 /* The cursor may have been moved when resizing. */
2133 vterm_state_get_cursorpos(state, &pos);
2134 position_cursor(wp, &pos);
2135
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002136 /* TODO: Only redraw what changed. */
2137 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002138 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002139 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002140 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002141
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002142 if (pos.row < term->tl_rows)
2143 {
2144 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002145 {
2146 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002147 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002148
Bram Moolenaareeac6772017-07-23 15:48:37 +02002149 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2150 vim_memset(&cell, 0, sizeof(cell));
2151
2152 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002153 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002154 if (c == NUL)
2155 {
2156 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002157 if (enc_utf8)
2158 ScreenLinesUC[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002159 }
2160 else
2161 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002162 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002163 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002164 if (c >= 0x80)
2165 {
2166 ScreenLines[off] = ' ';
2167 ScreenLinesUC[off] = c;
2168 }
2169 else
2170 {
2171 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002172 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002173 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002174 }
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002175#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02002176 else if (has_mbyte && c >= 0x80)
2177 {
2178 char_u mb[MB_MAXBYTES+1];
2179 WCHAR wc = c;
2180
2181 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2182 (char*)mb, 2, 0, 0) > 1)
2183 {
2184 ScreenLines[off] = mb[0];
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002185 ScreenLines[off + 1] = mb[1];
Bram Moolenaar740c4332017-08-21 22:01:27 +02002186 cell.width = mb_ptr2cells(mb);
2187 }
2188 else
2189 ScreenLines[off] = c;
2190 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002191#endif
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002192 else
Bram Moolenaar740c4332017-08-21 22:01:27 +02002193 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002194 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002195 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002196
2197 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002198 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002199 if (cell.width == 2)
2200 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002201 if (enc_utf8)
2202 ScreenLinesUC[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002203
2204 /* don't set the second byte to NUL for a DBCS encoding, it
2205 * has been set above */
2206 if (enc_utf8 || !has_mbyte)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002207 ScreenLines[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002208
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002209 ++pos.col;
2210 ++off;
2211 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002212 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002213 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002214 else
2215 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002216
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002217 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2218 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002219 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002220
2221 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002222}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002223
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002224/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002225 * Return TRUE if "wp" is a terminal window where the job has finished.
2226 */
2227 int
2228term_is_finished(buf_T *buf)
2229{
2230 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2231}
2232
2233/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002234 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002235 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002236 */
2237 int
2238term_show_buffer(buf_T *buf)
2239{
2240 term_T *term = buf->b_term;
2241
Bram Moolenaar6d819742017-08-06 14:57:49 +02002242 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002243}
2244
2245/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002246 * The current buffer is going to be changed. If there is terminal
2247 * highlighting remove it now.
2248 */
2249 void
2250term_change_in_curbuf(void)
2251{
2252 term_T *term = curbuf->b_term;
2253
2254 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2255 {
2256 free_scrollback(term);
2257 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002258
2259 /* The buffer is now like a normal buffer, it cannot be easily
2260 * abandoned when changed. */
2261 set_string_option_direct((char_u *)"buftype", -1,
2262 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002263 }
2264}
2265
2266/*
2267 * Get the screen attribute for a position in the buffer.
2268 */
2269 int
2270term_get_attr(buf_T *buf, linenr_T lnum, int col)
2271{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002272 term_T *term = buf->b_term;
2273 sb_line_T *line;
2274 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002275
Bram Moolenaar70229f92017-07-29 16:01:53 +02002276 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002277 return 0;
2278 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2279 if (col >= line->sb_cols)
2280 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002281 cellattr = line->sb_cells + col;
2282 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002283}
2284
2285/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002286 * Create a new vterm and initialize it.
2287 */
2288 static void
2289create_vterm(term_T *term, int rows, int cols)
2290{
2291 VTerm *vterm;
2292 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002293 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002294
2295 vterm = vterm_new(rows, cols);
2296 term->tl_vterm = vterm;
2297 screen = vterm_obtain_screen(vterm);
2298 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2299 /* TODO: depends on 'encoding'. */
2300 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002301
2302 /* Vterm uses a default black background. Set it to white when
2303 * 'background' is "light". */
2304 if (*p_bg == 'l')
2305 {
2306 VTermColor fg, bg;
2307
2308 fg.red = fg.green = fg.blue = 0;
2309 bg.red = bg.green = bg.blue = 255;
2310 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2311 }
2312
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002313 /* Required to initialize most things. */
2314 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002315
2316 /* Allow using alternate screen. */
2317 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002318
Bram Moolenaar58302322017-08-22 20:33:53 +02002319 /* For unix do not use a blinking cursor. In an xterm this causes the
2320 * cursor to blink if it's blinking in the xterm.
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002321 * For Windows we respect the system wide setting. */
Bram Moolenaar58302322017-08-22 20:33:53 +02002322#ifdef WIN3264
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002323 if (GetCaretBlinkTime() == INFINITE)
2324 value.boolean = 0;
2325 else
2326 value.boolean = 1;
Bram Moolenaar58302322017-08-22 20:33:53 +02002327#else
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002328 value.boolean = 0;
Bram Moolenaar58302322017-08-22 20:33:53 +02002329#endif
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002330 vterm_state_set_termprop(vterm_obtain_state(vterm),
2331 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002332}
2333
Bram Moolenaar21554412017-07-24 21:44:43 +02002334/*
2335 * Return the text to show for the buffer name and status.
2336 */
2337 char_u *
2338term_get_status_text(term_T *term)
2339{
2340 if (term->tl_status_text == NULL)
2341 {
2342 char_u *txt;
2343 size_t len;
2344
Bram Moolenaar6d819742017-08-06 14:57:49 +02002345 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002346 {
2347 if (term_job_running(term))
2348 txt = (char_u *)_("Terminal");
2349 else
2350 txt = (char_u *)_("Terminal-finished");
2351 }
2352 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002353 txt = term->tl_title;
2354 else if (term_job_running(term))
2355 txt = (char_u *)_("running");
2356 else
2357 txt = (char_u *)_("finished");
2358 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002359 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002360 if (term->tl_status_text != NULL)
2361 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2362 term->tl_buffer->b_fname, txt);
2363 }
2364 return term->tl_status_text;
2365}
2366
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002367/*
2368 * Mark references in jobs of terminals.
2369 */
2370 int
2371set_ref_in_term(int copyID)
2372{
2373 int abort = FALSE;
2374 term_T *term;
2375 typval_T tv;
2376
2377 for (term = first_term; term != NULL; term = term->tl_next)
2378 if (term->tl_job != NULL)
2379 {
2380 tv.v_type = VAR_JOB;
2381 tv.vval.v_job = term->tl_job;
2382 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2383 }
2384 return abort;
2385}
2386
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002387/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002388 * Get the buffer from the first argument in "argvars".
2389 * Returns NULL when the buffer is not for a terminal window.
2390 */
2391 static buf_T *
2392term_get_buf(typval_T *argvars)
2393{
2394 buf_T *buf;
2395
2396 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2397 ++emsg_off;
2398 buf = get_buf_tv(&argvars[0], FALSE);
2399 --emsg_off;
2400 if (buf == NULL || buf->b_term == NULL)
2401 return NULL;
2402 return buf;
2403}
2404
2405/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002406 * "term_getaltscreen(buf)" function
2407 */
2408 void
2409f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2410{
2411 buf_T *buf = term_get_buf(argvars);
2412
2413 if (buf == NULL)
2414 return;
2415 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2416}
2417
2418/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002419 * "term_getattr(attr, name)" function
2420 */
2421 void
2422f_term_getattr(typval_T *argvars, typval_T *rettv)
2423{
2424 int attr;
2425 size_t i;
2426 char_u *name;
2427
2428 static struct {
2429 char *name;
2430 int attr;
2431 } attrs[] = {
2432 {"bold", HL_BOLD},
2433 {"italic", HL_ITALIC},
2434 {"underline", HL_UNDERLINE},
2435 {"strike", HL_STANDOUT},
2436 {"reverse", HL_INVERSE},
2437 };
2438
2439 attr = get_tv_number(&argvars[0]);
2440 name = get_tv_string_chk(&argvars[1]);
2441 if (name == NULL)
2442 return;
2443
2444 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2445 if (STRCMP(name, attrs[i].name) == 0)
2446 {
2447 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2448 break;
2449 }
2450}
2451
2452/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002453 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002454 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002455 void
2456f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002457{
Bram Moolenaar97870002017-07-30 18:28:38 +02002458 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002459 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002460 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002461 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002462
Bram Moolenaar97870002017-07-30 18:28:38 +02002463 if (rettv_list_alloc(rettv) == FAIL)
2464 return;
2465 if (buf == NULL)
2466 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002467 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002468
2469 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002470 list_append_number(l, term->tl_cursor_pos.row + 1);
2471 list_append_number(l, term->tl_cursor_pos.col + 1);
2472
2473 d = dict_alloc();
2474 if (d != NULL)
2475 {
2476 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
Bram Moolenaar4db25542017-08-28 22:43:05 +02002477 dict_add_nr_str(d, "blink", blink_state_is_inverted()
2478 ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002479 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2480 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2481 ? (char_u *)"" : term->tl_cursor_color);
2482 list_append_dict(l, d);
2483 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002484}
2485
2486/*
2487 * "term_getjob(buf)" function
2488 */
2489 void
2490f_term_getjob(typval_T *argvars, typval_T *rettv)
2491{
2492 buf_T *buf = term_get_buf(argvars);
2493
2494 rettv->v_type = VAR_JOB;
2495 rettv->vval.v_job = NULL;
2496 if (buf == NULL)
2497 return;
2498
2499 rettv->vval.v_job = buf->b_term->tl_job;
2500 if (rettv->vval.v_job != NULL)
2501 ++rettv->vval.v_job->jv_refcount;
2502}
2503
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002504 static int
2505get_row_number(typval_T *tv, term_T *term)
2506{
2507 if (tv->v_type == VAR_STRING
2508 && tv->vval.v_string != NULL
2509 && STRCMP(tv->vval.v_string, ".") == 0)
2510 return term->tl_cursor_pos.row;
2511 return (int)get_tv_number(tv) - 1;
2512}
2513
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002514/*
2515 * "term_getline(buf, row)" function
2516 */
2517 void
2518f_term_getline(typval_T *argvars, typval_T *rettv)
2519{
2520 buf_T *buf = term_get_buf(argvars);
2521 term_T *term;
2522 int row;
2523
2524 rettv->v_type = VAR_STRING;
2525 if (buf == NULL)
2526 return;
2527 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002528 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002529
2530 if (term->tl_vterm == NULL)
2531 {
2532 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2533
2534 /* vterm is finished, get the text from the buffer */
2535 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2536 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2537 }
2538 else
2539 {
2540 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2541 VTermRect rect;
2542 int len;
2543 char_u *p;
2544
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002545 if (row < 0 || row >= term->tl_rows)
2546 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002547 len = term->tl_cols * MB_MAXBYTES + 1;
2548 p = alloc(len);
2549 if (p == NULL)
2550 return;
2551 rettv->vval.v_string = p;
2552
2553 rect.start_col = 0;
2554 rect.end_col = term->tl_cols;
2555 rect.start_row = row;
2556 rect.end_row = row + 1;
2557 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2558 }
2559}
2560
2561/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002562 * "term_getscrolled(buf)" function
2563 */
2564 void
2565f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2566{
2567 buf_T *buf = term_get_buf(argvars);
2568
2569 if (buf == NULL)
2570 return;
2571 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2572}
2573
2574/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002575 * "term_getsize(buf)" function
2576 */
2577 void
2578f_term_getsize(typval_T *argvars, typval_T *rettv)
2579{
2580 buf_T *buf = term_get_buf(argvars);
2581 list_T *l;
2582
2583 if (rettv_list_alloc(rettv) == FAIL)
2584 return;
2585 if (buf == NULL)
2586 return;
2587
2588 l = rettv->vval.v_list;
2589 list_append_number(l, buf->b_term->tl_rows);
2590 list_append_number(l, buf->b_term->tl_cols);
2591}
2592
2593/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002594 * "term_getstatus(buf)" function
2595 */
2596 void
2597f_term_getstatus(typval_T *argvars, typval_T *rettv)
2598{
2599 buf_T *buf = term_get_buf(argvars);
2600 term_T *term;
2601 char_u val[100];
2602
2603 rettv->v_type = VAR_STRING;
2604 if (buf == NULL)
2605 return;
2606 term = buf->b_term;
2607
2608 if (term_job_running(term))
2609 STRCPY(val, "running");
2610 else
2611 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002612 if (term->tl_normal_mode)
2613 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002614 rettv->vval.v_string = vim_strsave(val);
2615}
2616
2617/*
2618 * "term_gettitle(buf)" function
2619 */
2620 void
2621f_term_gettitle(typval_T *argvars, typval_T *rettv)
2622{
2623 buf_T *buf = term_get_buf(argvars);
2624
2625 rettv->v_type = VAR_STRING;
2626 if (buf == NULL)
2627 return;
2628
2629 if (buf->b_term->tl_title != NULL)
2630 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2631}
2632
2633/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002634 * "term_gettty(buf)" function
2635 */
2636 void
2637f_term_gettty(typval_T *argvars, typval_T *rettv)
2638{
2639 buf_T *buf = term_get_buf(argvars);
2640 char_u *p;
2641
2642 rettv->v_type = VAR_STRING;
2643 if (buf == NULL)
2644 return;
2645 if (buf->b_term->tl_job != NULL)
2646 p = buf->b_term->tl_job->jv_tty_name;
2647 else
2648 p = buf->b_term->tl_tty_name;
2649 if (p != NULL)
2650 rettv->vval.v_string = vim_strsave(p);
2651}
2652
2653/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002654 * "term_list()" function
2655 */
2656 void
2657f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2658{
2659 term_T *tp;
2660 list_T *l;
2661
2662 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2663 return;
2664
2665 l = rettv->vval.v_list;
2666 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2667 if (tp != NULL && tp->tl_buffer != NULL)
2668 if (list_append_number(l,
2669 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2670 return;
2671}
2672
2673/*
2674 * "term_scrape(buf, row)" function
2675 */
2676 void
2677f_term_scrape(typval_T *argvars, typval_T *rettv)
2678{
2679 buf_T *buf = term_get_buf(argvars);
2680 VTermScreen *screen = NULL;
2681 VTermPos pos;
2682 list_T *l;
2683 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002684 char_u *p;
2685 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002686
2687 if (rettv_list_alloc(rettv) == FAIL)
2688 return;
2689 if (buf == NULL)
2690 return;
2691 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002692
2693 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002694 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002695
2696 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002697 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002698 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002699 p = NULL;
2700 line = NULL;
2701 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002702 else
2703 {
2704 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2705
2706 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2707 return;
2708 p = ml_get_buf(buf, lnum + 1, FALSE);
2709 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2710 }
2711
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002712 for (pos.col = 0; pos.col < term->tl_cols; )
2713 {
2714 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002715 int width;
2716 VTermScreenCellAttrs attrs;
2717 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002718 char_u rgb[8];
2719 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2720 int off = 0;
2721 int i;
2722
2723 if (screen == NULL)
2724 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002725 cellattr_T *cellattr;
2726 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002727
2728 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002729 if (pos.col >= line->sb_cols)
2730 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002731 cellattr = line->sb_cells + pos.col;
2732 width = cellattr->width;
2733 attrs = cellattr->attrs;
2734 fg = cellattr->fg;
2735 bg = cellattr->bg;
2736 len = MB_PTR2LEN(p);
2737 mch_memmove(mbs, p, len);
2738 mbs[len] = NUL;
2739 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002740 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002741 else
2742 {
2743 VTermScreenCell cell;
2744 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2745 break;
2746 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2747 {
2748 if (cell.chars[i] == 0)
2749 break;
2750 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2751 }
2752 mbs[off] = NUL;
2753 width = cell.width;
2754 attrs = cell.attrs;
2755 fg = cell.fg;
2756 bg = cell.bg;
2757 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002758 dcell = dict_alloc();
2759 list_append_dict(l, dcell);
2760
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002761 dict_add_nr_str(dcell, "chars", 0, mbs);
2762
2763 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002764 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002765 dict_add_nr_str(dcell, "fg", 0, rgb);
2766 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002767 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002768 dict_add_nr_str(dcell, "bg", 0, rgb);
2769
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002770 dict_add_nr_str(dcell, "attr",
2771 cell2attr(attrs, fg, bg), NULL);
2772 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002773
2774 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002775 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002776 ++pos.col;
2777 }
2778}
2779
2780/*
2781 * "term_sendkeys(buf, keys)" function
2782 */
2783 void
2784f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2785{
2786 buf_T *buf = term_get_buf(argvars);
2787 char_u *msg;
2788 term_T *term;
2789
2790 rettv->v_type = VAR_UNKNOWN;
2791 if (buf == NULL)
2792 return;
2793
2794 msg = get_tv_string_chk(&argvars[1]);
2795 if (msg == NULL)
2796 return;
2797 term = buf->b_term;
2798 if (term->tl_vterm == NULL)
2799 return;
2800
2801 while (*msg != NUL)
2802 {
2803 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2804 msg += MB_PTR2LEN(msg);
2805 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002806}
2807
2808/*
2809 * "term_start(command, options)" function
2810 */
2811 void
2812f_term_start(typval_T *argvars, typval_T *rettv)
2813{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002814 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002815
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002816 init_job_options(&opt);
2817 /* TODO: allow more job options */
2818 if (argvars[1].v_type != VAR_UNKNOWN
2819 && get_job_options(&argvars[1], &opt,
2820 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002821 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002822 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002823 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002824 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002825 return;
2826
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002827 if (opt.jo_vertical)
2828 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002829 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002830
2831 if (curbuf->b_term != NULL)
2832 rettv->vval.v_number = curbuf->b_fnum;
2833}
2834
2835/*
2836 * "term_wait" function
2837 */
2838 void
2839f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2840{
2841 buf_T *buf = term_get_buf(argvars);
2842
2843 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002844 {
2845 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002846 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002847 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002848 if (buf->b_term->tl_job == NULL)
2849 {
2850 ch_log(NULL, "term_wait(): no job to wait for");
2851 return;
2852 }
Bram Moolenaar13ebb032017-08-26 22:02:51 +02002853 if (buf->b_term->tl_job->jv_channel == NULL)
2854 /* channel is closed, nothing to do */
2855 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002856
2857 /* Get the job status, this will detect a job that finished. */
Bram Moolenaar13ebb032017-08-26 22:02:51 +02002858 if ((buf->b_term->tl_job->jv_channel == NULL
2859 || !buf->b_term->tl_job->jv_channel->ch_keep_open)
2860 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002861 {
2862 /* The job is dead, keep reading channel I/O until the channel is
2863 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002864 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002865 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2866 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002867 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002868 parse_queued_messages();
2869 ui_delay(10L, FALSE);
2870 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002871 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002872 parse_queued_messages();
2873 }
2874 else
2875 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002876 long wait = 10L;
2877
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002878 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002879 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002880
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002881 /* Wait for some time for any channel I/O. */
2882 if (argvars[1].v_type != VAR_UNKNOWN)
2883 wait = get_tv_number(&argvars[1]);
2884 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002885 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002886
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002887 /* Flushing messages on channels is hopefully sufficient.
2888 * TODO: is there a better way? */
2889 parse_queued_messages();
2890 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002891}
2892
Bram Moolenaara83e3962017-08-17 14:39:07 +02002893# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002894
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002895/**************************************
2896 * 2. MS-Windows implementation.
2897 */
2898
Bram Moolenaara83e3962017-08-17 14:39:07 +02002899# ifndef PROTO
2900
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002901#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2902#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2903
Bram Moolenaar8a773062017-07-24 22:29:21 +02002904void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002905void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002906void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002907BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2908void (*winpty_config_set_initial_size)(void*, int, int);
2909LPCWSTR (*winpty_conin_name)(void*);
2910LPCWSTR (*winpty_conout_name)(void*);
2911LPCWSTR (*winpty_conerr_name)(void*);
2912void (*winpty_free)(void*);
2913void (*winpty_config_free)(void*);
2914void (*winpty_spawn_config_free)(void*);
2915void (*winpty_error_free)(void*);
2916LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002917BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002918HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002919
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002920#define WINPTY_DLL "winpty.dll"
2921
2922static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002923# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002924
Bram Moolenaara83e3962017-08-17 14:39:07 +02002925 static int
2926dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002927{
2928 int i;
2929 static struct
2930 {
2931 char *name;
2932 FARPROC *ptr;
2933 } winpty_entry[] =
2934 {
2935 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2936 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2937 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2938 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2939 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2940 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2941 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2942 {"winpty_free", (FARPROC*)&winpty_free},
2943 {"winpty_open", (FARPROC*)&winpty_open},
2944 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2945 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2946 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2947 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002948 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002949 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002950 {NULL, NULL}
2951 };
2952
2953 /* No need to initialize twice. */
2954 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002955 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002956 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2957 * winpty.dll. */
2958 if (*p_winptydll != NUL)
2959 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2960 if (!hWinPtyDLL)
2961 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002962 if (!hWinPtyDLL)
2963 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002964 if (verbose)
2965 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2966 : (char_u *)WINPTY_DLL);
2967 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002968 }
2969 for (i = 0; winpty_entry[i].name != NULL
2970 && winpty_entry[i].ptr != NULL; ++i)
2971 {
2972 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2973 winpty_entry[i].name)) == NULL)
2974 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002975 if (verbose)
2976 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2977 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002978 }
2979 }
2980
Bram Moolenaara83e3962017-08-17 14:39:07 +02002981 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002982}
2983
2984/*
2985 * Create a new terminal of "rows" by "cols" cells.
2986 * Store a reference in "term".
2987 * Return OK or FAIL.
2988 */
2989 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02002990term_and_job_init(
2991 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02002992 typval_T *argvar,
2993 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002994{
Bram Moolenaar5983d502017-08-20 19:22:56 +02002995 WCHAR *cmd_wchar = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002996 channel_T *channel = NULL;
2997 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002998 DWORD error;
Bram Moolenaar5983d502017-08-20 19:22:56 +02002999 HANDLE jo = NULL;
3000 HANDLE child_process_handle;
3001 HANDLE child_thread_handle;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003002 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003003 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003004 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003005 garray_T ga;
3006 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003007
Bram Moolenaara83e3962017-08-17 14:39:07 +02003008 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003009 return FAIL;
3010
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003011 if (argvar->v_type == VAR_STRING)
3012 cmd = argvar->vval.v_string;
3013 else
3014 {
3015 ga_init2(&ga, (int)sizeof(char*), 20);
3016 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
3017 goto failed;
3018 cmd = ga.ga_data;
3019 }
3020
Bram Moolenaar5983d502017-08-20 19:22:56 +02003021 cmd_wchar = enc_to_utf16(cmd, NULL);
3022 if (cmd_wchar == NULL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003023 return FAIL;
3024
3025 job = job_alloc();
3026 if (job == NULL)
3027 goto failed;
3028
3029 channel = add_channel();
3030 if (channel == NULL)
3031 goto failed;
3032
3033 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
3034 if (term->tl_winpty_config == NULL)
3035 goto failed;
3036
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003037 winpty_config_set_initial_size(term->tl_winpty_config,
3038 term->tl_cols, term->tl_rows);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003039 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
3040 if (term->tl_winpty == NULL)
3041 goto failed;
3042
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003043 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003044 spawn_config = winpty_spawn_config_new(
3045 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
3046 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
3047 NULL,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003048 cmd_wchar,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003049 NULL,
3050 NULL,
3051 &winpty_err);
3052 if (spawn_config == NULL)
3053 goto failed;
3054
3055 channel = add_channel();
3056 if (channel == NULL)
3057 goto failed;
3058
3059 job = job_alloc();
3060 if (job == NULL)
3061 goto failed;
3062
Bram Moolenaar5983d502017-08-20 19:22:56 +02003063 /* TODO: when all lines are written and the fd is closed, the command
3064 * doesn't get EOF and hangs. */
3065 if (opt->jo_set & JO_IN_BUF)
3066 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
3067
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003068 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
3069 &child_thread_handle, &error, &winpty_err))
3070 goto failed;
3071
3072 channel_set_pipes(channel,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003073 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003074 winpty_conin_name(term->tl_winpty),
3075 GENERIC_WRITE, 0, NULL,
3076 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003077 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003078 winpty_conout_name(term->tl_winpty),
3079 GENERIC_READ, 0, NULL,
3080 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003081 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003082 winpty_conerr_name(term->tl_winpty),
3083 GENERIC_READ, 0, NULL,
3084 OPEN_EXISTING, 0, NULL));
3085
3086 jo = CreateJobObject(NULL, NULL);
3087 if (jo == NULL)
3088 goto failed;
3089
3090 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003091 {
3092 /* Failed, switch the way to terminate process with TerminateProcess. */
3093 CloseHandle(jo);
3094 jo = NULL;
3095 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003096
3097 winpty_spawn_config_free(spawn_config);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003098 vim_free(cmd_wchar);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003099
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003100 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003101
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02003102 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02003103 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003104
3105 job->jv_channel = channel;
3106 job->jv_proc_info.hProcess = child_process_handle;
3107 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
3108 job->jv_job_object = jo;
3109 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003110 sprintf(buf, "winpty://%lu",
3111 GetProcessId(winpty_agent_process(term->tl_winpty)));
3112 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003113 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003114 term->tl_job = job;
3115
3116 return OK;
3117
3118failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003119 if (argvar->v_type == VAR_LIST)
3120 vim_free(ga.ga_data);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003121 if (cmd_wchar != NULL)
3122 vim_free(cmd_wchar);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003123 if (spawn_config != NULL)
3124 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003125 if (channel != NULL)
3126 channel_clear(channel);
3127 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003128 {
3129 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003130 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003131 }
3132 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003133 if (jo != NULL)
3134 CloseHandle(jo);
3135 if (term->tl_winpty != NULL)
3136 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003137 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003138 if (term->tl_winpty_config != NULL)
3139 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003140 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003141 if (winpty_err != NULL)
3142 {
3143 char_u *msg = utf16_to_enc(
3144 (short_u *)winpty_error_msg(winpty_err), NULL);
3145
3146 EMSG(msg);
3147 winpty_error_free(winpty_err);
3148 }
3149 return FAIL;
3150}
3151
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003152 static int
3153create_pty_only(term_T *term, jobopt_T *opt)
3154{
3155 /* TODO: implement this */
3156 return FAIL;
3157}
3158
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003159/*
3160 * Free the terminal emulator part of "term".
3161 */
3162 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003163term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003164{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003165 if (term->tl_winpty != NULL)
3166 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003167 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003168 if (term->tl_winpty_config != NULL)
3169 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003170 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003171 if (term->tl_vterm != NULL)
3172 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003173 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003174}
3175
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003176/*
3177 * Request size to terminal.
3178 */
3179 static void
3180term_report_winsize(term_T *term, int rows, int cols)
3181{
3182 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3183}
3184
Bram Moolenaara83e3962017-08-17 14:39:07 +02003185 int
3186terminal_enabled(void)
3187{
3188 return dyn_winpty_init(FALSE) == OK;
3189}
3190
3191
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003192# else
3193
3194/**************************************
3195 * 3. Unix-like implementation.
3196 */
3197
3198/*
3199 * Create a new terminal of "rows" by "cols" cells.
3200 * Start job for "cmd".
3201 * Store the pointers in "term".
3202 * Return OK or FAIL.
3203 */
3204 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003205term_and_job_init(
3206 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02003207 typval_T *argvar,
3208 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003209{
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003210 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003211
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003212 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003213 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003214 if (term->tl_job != NULL)
3215 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003216
Bram Moolenaar61a66052017-07-22 18:39:00 +02003217 return term->tl_job != NULL
3218 && term->tl_job->jv_channel != NULL
3219 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003220}
3221
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003222 static int
3223create_pty_only(term_T *term, jobopt_T *opt)
3224{
3225 int ret;
3226
3227 create_vterm(term, term->tl_rows, term->tl_cols);
3228
3229 term->tl_job = job_alloc();
3230 if (term->tl_job == NULL)
3231 return FAIL;
3232 ++term->tl_job->jv_refcount;
3233
3234 /* behave like the job is already finished */
3235 term->tl_job->jv_status = JOB_FINISHED;
3236
3237 ret = mch_create_pty_channel(term->tl_job, opt);
3238
3239 return ret;
3240}
3241
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003242/*
3243 * Free the terminal emulator part of "term".
3244 */
3245 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003246term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003247{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003248 if (term->tl_vterm != NULL)
3249 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003250 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003251}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003252
3253/*
3254 * Request size to terminal.
3255 */
3256 static void
3257term_report_winsize(term_T *term, int rows, int cols)
3258{
3259 /* Use an ioctl() to report the new window size to the job. */
3260 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3261 {
3262 int fd = -1;
3263 int part;
3264
3265 for (part = PART_OUT; part < PART_COUNT; ++part)
3266 {
3267 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3268 if (isatty(fd))
3269 break;
3270 }
3271 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003272 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003273 }
3274}
3275
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003276# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003277
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003278#endif /* FEAT_TERMINAL */