blob: a8c7ae59de13334270ce48bacc8fa0e9eca8793f [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 Moolenaar285f2432017-08-23 23:10:21 +020042 * - better check for blinking - reply from Thomas Dickey Aug 22
Bram Moolenaar0cbba822017-08-21 21:39:28 +020043 * - test for writing lines to terminal job does not work on MS-Windows
Bram Moolenaar94053a52017-08-01 21:44:33 +020044 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020045 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020046 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020047 * - support minimal size when 'termsize' is empty?
Bram Moolenaar58302322017-08-22 20:33:53 +020048 * - do not set bufhidden to "hide"? works like a buffer with changes.
49 * document that CTRL-W :hide can be used.
Bram Moolenaar285f2432017-08-23 23:10:21 +020050 * - GUI: when using tabs, focus in terminal, click on tab does not work.
Bram Moolenaar13ebb032017-08-26 22:02:51 +020051 * - When $HOME was set by Vim (MS-Windows), do not pass it to the job.
Bram Moolenaar285f2432017-08-23 23:10:21 +020052 * - GUI: when 'confirm' is set and trying to exit Vim, dialog offers to save
53 * changes to "!shell".
54 * (justrajdeep, 2017 Aug 22)
Bram Moolenaar58302322017-08-22 20:33:53 +020055 * - command argument with spaces doesn't work #1999
56 * :terminal ls dir\ with\ spaces
Bram Moolenaar4f44b882017-08-13 20:06:18 +020057 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020058 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
59 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
60 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
61 * Check that something is connected to the terminal.
62 * Test: "cat" reading from a file or buffer
Bram Moolenaar740c4332017-08-21 22:01:27 +020063 * "ls" writing stdout to a file or buffer
64 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020065 * - For the GUI fill termios with default values, perhaps like pangoterm:
66 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar423802d2017-07-30 16:52:24 +020067 * - if the job in the terminal does not support the mouse, we can use the
68 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020069 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
70 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020071 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020072 * - Copy text in the vterm to the Vim buffer once in a while, so that
73 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020074 */
75
76#include "vim.h"
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079
Bram Moolenaard5310982017-08-05 15:16:32 +020080#ifndef MIN
81# define MIN(x,y) ((x) < (y) ? (x) : (y))
82#endif
83#ifndef MAX
84# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020085#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020086
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020087#include "libvterm/include/vterm.h"
88
Bram Moolenaar33a43be2017-08-06 21:36:22 +020089/* This is VTermScreenCell without the characters, thus much smaller. */
90typedef struct {
91 VTermScreenCellAttrs attrs;
92 char width;
93 VTermColor fg, bg;
94} cellattr_T;
95
Bram Moolenaard85f2712017-07-28 21:51:57 +020096typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020097 int sb_cols; /* can differ per line */
98 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020099} sb_line_T;
100
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200101/* typedef term_T in structs.h */
102struct terminal_S {
103 term_T *tl_next;
104
Bram Moolenaar423802d2017-07-30 16:52:24 +0200105 VTerm *tl_vterm;
106 job_T *tl_job;
107 buf_T *tl_buffer;
108
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200109 /* used when tl_job is NULL and only a pty was created */
110 int tl_tty_fd;
111 char_u *tl_tty_name;
112
Bram Moolenaar6d819742017-08-06 14:57:49 +0200113 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200114 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200115 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200116 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200117
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200118#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200119 void *tl_winpty_config;
120 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200121#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200122
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200123 /* last known vterm size */
124 int tl_rows;
125 int tl_cols;
126 /* vterm size does not follow window size */
127 int tl_rows_fixed;
128 int tl_cols_fixed;
129
Bram Moolenaar21554412017-07-24 21:44:43 +0200130 char_u *tl_title; /* NULL or allocated */
131 char_u *tl_status_text; /* NULL or allocated */
132
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200133 /* Range of screen rows to update. Zero based. */
134 int tl_dirty_row_start; /* -1 if nothing dirty */
135 int tl_dirty_row_end; /* row below last one to update */
136
Bram Moolenaard85f2712017-07-28 21:51:57 +0200137 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200138 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200139
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200140 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200141 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200142 int tl_cursor_blink;
143 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
144 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200145
146 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200147};
148
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200149#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
150#define TMODE_LOOP 2 /* CTRL-W N used */
151
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200152/*
153 * List of all active terminals.
154 */
155static term_T *first_term = NULL;
156
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200157/* Terminal active in terminal_loop(). */
158static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200159
160#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
161#define KEY_BUF_LEN 200
162
163/*
164 * Functions with separate implementation for MS-Windows and Unix-like systems.
165 */
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200166static int term_and_job_init(term_T *term, typval_T *argvar, jobopt_T *opt);
167static int create_pty_only(term_T *term, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200168static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200169static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200170
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200171/* The characters that we know (or assume) that the terminal expects for the
172 * backspace and enter keys. */
173static int term_backspace_char = BS;
174static int term_enter_char = CAR;
175static int term_nl_does_cr = FALSE;
176
177
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200178/**************************************
179 * 1. Generic code for all systems.
180 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200181
182/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200183 * Determine the terminal size from 'termsize' and the current window.
184 * Assumes term->tl_rows and term->tl_cols are zero.
185 */
186 static void
187set_term_and_win_size(term_T *term)
188{
189 if (*curwin->w_p_tms != NUL)
190 {
191 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
192
193 term->tl_rows = atoi((char *)curwin->w_p_tms);
194 term->tl_cols = atoi((char *)p);
195 }
196 if (term->tl_rows == 0)
197 term->tl_rows = curwin->w_height;
198 else
199 {
200 win_setheight_win(term->tl_rows, curwin);
201 term->tl_rows_fixed = TRUE;
202 }
203 if (term->tl_cols == 0)
204 term->tl_cols = curwin->w_width;
205 else
206 {
207 win_setwidth_win(term->tl_cols, curwin);
208 term->tl_cols_fixed = TRUE;
209 }
210}
211
212/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200213 * Initialize job options for a terminal job.
214 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200215 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200216 static void
217init_job_options(jobopt_T *opt)
218{
219 clear_job_options(opt);
220
221 opt->jo_mode = MODE_RAW;
222 opt->jo_out_mode = MODE_RAW;
223 opt->jo_err_mode = MODE_RAW;
224 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
225
226 opt->jo_io[PART_OUT] = JIO_BUFFER;
227 opt->jo_io[PART_ERR] = JIO_BUFFER;
228 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
229
230 opt->jo_modifiable[PART_OUT] = 0;
231 opt->jo_modifiable[PART_ERR] = 0;
232 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
233
234 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
235}
236
237/*
238 * Set job options mandatory for a terminal job.
239 */
240 static void
241setup_job_options(jobopt_T *opt, int rows, int cols)
242{
243 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
244 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
245 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200246 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
247 opt->jo_term_rows = rows;
248 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
249 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200250}
251
252 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200253term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200255 exarg_T split_ea;
256 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200257 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200258 buf_T *old_curbuf = NULL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200259 int res;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200260
261 if (check_restricted() || check_secure())
262 return;
263
264 term = (term_T *)alloc_clear(sizeof(term_T));
265 if (term == NULL)
266 return;
267 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200268 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200269 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200270 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200271 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200272
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200273 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200274 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200275 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200276 /* Create a new buffer in the current window. */
277 if (!can_abandon(curbuf, forceit))
278 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200279 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200280 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200281 return;
282 }
283 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
284 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200285 {
286 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200287 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200288 }
289 }
290 else if (opt->jo_hidden)
291 {
292 buf_T *buf;
293
294 /* Create a new buffer without a window. Make it the current buffer for
295 * a moment to be able to do the initialisations. */
296 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
297 BLN_NEW | BLN_LISTED);
298 if (buf == NULL || ml_open(buf) == FAIL)
299 {
300 vim_free(term);
301 return;
302 }
303 old_curbuf = curbuf;
304 --curbuf->b_nwindows;
305 curbuf = buf;
306 curwin->w_buffer = buf;
307 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200308 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200309 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200310 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200311 /* Open a new window or tab. */
312 split_ea.cmdidx = CMD_new;
313 split_ea.cmd = (char_u *)"new";
314 split_ea.arg = (char_u *)"";
315 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
316 {
317 split_ea.line2 = opt->jo_term_rows;
318 split_ea.addr_count = 1;
319 }
320 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
321 {
322 split_ea.line2 = opt->jo_term_cols;
323 split_ea.addr_count = 1;
324 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200325
Bram Moolenaarda43b612017-08-11 22:27:50 +0200326 ex_splitview(&split_ea);
327 if (curwin == old_curwin)
328 {
329 /* split failed */
330 vim_free(term);
331 return;
332 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200333 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200334 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200335 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200336
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200337 if (!opt->jo_hidden)
338 {
339 /* only one size was taken care of with :new, do the other one */
340 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
341 win_setheight(opt->jo_term_rows);
342 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
343 win_setwidth(opt->jo_term_cols);
344 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200345
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200346 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200347 term->tl_next = first_term;
348 first_term = term;
349
Bram Moolenaar78712a72017-08-05 14:50:12 +0200350 if (opt->jo_term_name != NULL)
351 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
352 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200353 {
354 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200355 size_t len;
356 char_u *cmd, *p;
357
358 if (argvar->v_type == VAR_STRING)
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200359 {
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200360 cmd = argvar->vval.v_string;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200361 if (cmd == NULL)
362 cmd = (char_u *)"";
363 else if (STRCMP(cmd, "NONE") == 0)
364 cmd = (char_u *)"pty";
365 }
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200366 else if (argvar->v_type != VAR_LIST
367 || argvar->vval.v_list == NULL
368 || argvar->vval.v_list->lv_len < 1)
369 cmd = (char_u*)"";
370 else
371 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
372
373 len = STRLEN(cmd) + 10;
374 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200375
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200376 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200377 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200378 /* Prepend a ! to the command name to avoid the buffer name equals
379 * the executable, otherwise ":w!" would overwrite it. */
380 if (i == 0)
381 vim_snprintf((char *)p, len, "!%s", cmd);
382 else
383 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200384 if (buflist_findname(p) == NULL)
385 {
386 curbuf->b_ffname = p;
387 break;
388 }
389 }
390 }
391 curbuf->b_fname = curbuf->b_ffname;
392
Bram Moolenaar37c45832017-08-12 16:01:04 +0200393 if (opt->jo_term_opencmd != NULL)
394 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
395
Bram Moolenaareb44a682017-08-03 22:44:55 +0200396 set_string_option_direct((char_u *)"buftype", -1,
397 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
398
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200399 /* Mark the buffer as not modifiable. It can only be made modifiable after
400 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200401 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200402
403 /* Set 'bufhidden' to "hide": allow closing the window. */
404 set_string_option_direct((char_u *)"bufhidden", -1,
405 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200406
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200407 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200408 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200409
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200410 /* System dependent: setup the vterm and maybe start the job in it. */
411 if (argvar->v_type == VAR_STRING
412 && argvar->vval.v_string != NULL
413 && STRCMP(argvar->vval.v_string, "NONE") == 0)
414 res = create_pty_only(term, opt);
415 else
416 res = term_and_job_init(term, argvar, opt);
417
418 if (res == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200419 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200420 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200421 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200422 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200423
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200424 /* Make sure we don't get stuck on sending keys to the job, it leads to
425 * a deadlock if the job is waiting for Vim to read. */
426 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
427
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200428 if (old_curbuf != NULL)
429 {
430 --curbuf->b_nwindows;
431 curbuf = old_curbuf;
432 curwin->w_buffer = curbuf;
433 ++curbuf->b_nwindows;
434 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200435 }
436 else
437 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200438 buf_T *buf = curbuf;
439
Bram Moolenaard85f2712017-07-28 21:51:57 +0200440 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200441 if (old_curbuf != NULL)
442 {
443 --curbuf->b_nwindows;
444 curbuf = old_curbuf;
445 curwin->w_buffer = curbuf;
446 ++curbuf->b_nwindows;
447 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200448
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200449 /* Wiping out the buffer will also close the window and call
450 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200451 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200452 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200453}
454
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200455/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200456 * ":terminal": open a terminal window and execute a job in it.
457 */
458 void
459ex_terminal(exarg_T *eap)
460{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200461 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200462 jobopt_T opt;
463 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200464 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200465
466 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200467
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200468 cmd = eap->arg;
469 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
470 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200471 char_u *p, *ep;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200472
473 cmd += 2;
474 p = skiptowhite(cmd);
Bram Moolenaarb2412082017-08-20 18:09:14 +0200475 ep = vim_strchr(cmd, '=');
476 if (ep != NULL && ep < p)
477 p = ep;
478
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200479 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
480 opt.jo_term_finish = 'c';
481 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
482 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200483 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
484 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200485 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
486 opt.jo_hidden = 1;
Bram Moolenaarb2412082017-08-20 18:09:14 +0200487 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
488 && ep != NULL && isdigit(ep[1]))
489 {
490 opt.jo_set2 |= JO2_TERM_ROWS;
491 opt.jo_term_rows = atoi((char *)ep + 1);
492 p = skiptowhite(cmd);
493 }
494 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
495 && ep != NULL && isdigit(ep[1]))
496 {
497 opt.jo_set2 |= JO2_TERM_COLS;
498 opt.jo_term_cols = atoi((char *)ep + 1);
499 p = skiptowhite(cmd);
500 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200501 else
502 {
503 if (*p)
504 *p = NUL;
505 EMSG2(_("E181: Invalid attribute: %s"), cmd);
506 return;
507 }
508 cmd = skipwhite(p);
509 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200510 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200511 /* Make a copy, an autocommand may set 'shell'. */
512 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200513
Bram Moolenaarb2412082017-08-20 18:09:14 +0200514 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200515 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200516 /* Write lines from current buffer to the job. */
517 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
518 opt.jo_io[PART_IN] = JIO_BUFFER;
519 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
520 opt.jo_in_top = eap->line1;
521 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200522 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200523
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200524 argvar.v_type = VAR_STRING;
525 argvar.vval.v_string = cmd;
526 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200527 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200528}
529
530/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200531 * Free the scrollback buffer for "term".
532 */
533 static void
534free_scrollback(term_T *term)
535{
536 int i;
537
538 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
539 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
540 ga_clear(&term->tl_scrollback);
541}
542
543/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200544 * Free a terminal and everything it refers to.
545 * Kills the job if there is one.
546 * Called when wiping out a buffer.
547 */
548 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200549free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200550{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200551 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200552 term_T *tp;
553
554 if (term == NULL)
555 return;
556 if (first_term == term)
557 first_term = term->tl_next;
558 else
559 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
560 if (tp->tl_next == term)
561 {
562 tp->tl_next = term->tl_next;
563 break;
564 }
565
566 if (term->tl_job != NULL)
567 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200568 if (term->tl_job->jv_status != JOB_ENDED
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200569 && term->tl_job->jv_status != JOB_FINISHED
570 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200571 job_stop(term->tl_job, NULL, "kill");
572 job_unref(term->tl_job);
573 }
574
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200575 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200576
577 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200578 vim_free(term->tl_title);
579 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200580 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200581 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200582 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200583 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200584 if (in_terminal_loop == term)
585 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200586}
587
588/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200589 * Write job output "msg[len]" to the vterm.
590 */
591 static void
592term_write_job_output(term_T *term, char_u *msg, size_t len)
593{
594 VTerm *vterm = term->tl_vterm;
595 char_u *p;
596 size_t done;
597 size_t len_now;
598
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200599 if (term_nl_does_cr)
600 vterm_input_write(vterm, (char *)msg, len);
601 else
602 /* need to convert NL to CR-NL */
603 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200604 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200605 for (p = msg + done; p < msg + len; )
606 {
607 if (*p == NL)
608 break;
609 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
610 }
611 len_now = p - msg - done;
612 vterm_input_write(vterm, (char *)msg + done, len_now);
613 if (p < msg + len && *p == NL)
614 {
615 vterm_input_write(vterm, "\r\n", 2);
616 ++len_now;
617 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200618 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200619
620 /* this invokes the damage callbacks */
621 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
622}
623
Bram Moolenaar1c844932017-07-24 23:36:41 +0200624 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200625update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200626{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200627 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200628 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200629 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200630 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200631 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200632 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200633 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200634 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200635#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200636 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200637 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200638#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200639 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200640}
641
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200642/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200643 * Invoked when "msg" output from a job was received. Write it to the terminal
644 * of "buffer".
645 */
646 void
647write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
648{
649 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200650 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200651
Bram Moolenaard85f2712017-07-28 21:51:57 +0200652 if (term->tl_vterm == NULL)
653 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200654 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200655 return;
656 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200657 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200658 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200659
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200660 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
661 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200662 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200663 {
664 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200665 ch_log(term->tl_job->jv_channel, "updating screen");
666 if (buffer == curbuf)
667 {
668 update_screen(0);
669 update_cursor(term, TRUE);
670 }
671 else
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200672 redraw_after_callback(TRUE);
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200673 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200674}
675
676/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200677 * Send a mouse position and click to the vterm
678 */
679 static int
680term_send_mouse(VTerm *vterm, int button, int pressed)
681{
682 VTermModifier mod = VTERM_MOD_NONE;
683
684 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
685 mouse_col - W_WINCOL(curwin), mod);
686 vterm_mouse_button(vterm, button, pressed, mod);
687 return TRUE;
688}
689
690/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200691 * Convert typed key "c" into bytes to send to the job.
692 * Return the number of bytes in "buf".
693 */
694 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200695term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200696{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200697 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200698 VTermKey key = VTERM_KEY_NONE;
699 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200700 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200701
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200702 switch (c)
703 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200704 case CAR: c = term_enter_char; break;
705 /* don't use VTERM_KEY_BACKSPACE, it always
706 * becomes 0x7f DEL */
707 case K_BS: c = term_backspace_char; break;
708
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200709 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200710 case K_DEL: key = VTERM_KEY_DEL; break;
711 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200712 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
713 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200714 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200715 case K_S_END: mod = VTERM_MOD_SHIFT;
716 key = VTERM_KEY_END; break;
717 case K_C_END: mod = VTERM_MOD_CTRL;
718 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200719 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
720 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
721 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
722 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
723 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
724 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
725 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
726 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
727 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
728 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
729 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
730 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
731 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200732 case K_S_HOME: mod = VTERM_MOD_SHIFT;
733 key = VTERM_KEY_HOME; break;
734 case K_C_HOME: mod = VTERM_MOD_CTRL;
735 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200736 case K_INS: key = VTERM_KEY_INS; break;
737 case K_K0: key = VTERM_KEY_KP_0; break;
738 case K_K1: key = VTERM_KEY_KP_1; break;
739 case K_K2: key = VTERM_KEY_KP_2; break;
740 case K_K3: key = VTERM_KEY_KP_3; break;
741 case K_K4: key = VTERM_KEY_KP_4; break;
742 case K_K5: key = VTERM_KEY_KP_5; break;
743 case K_K6: key = VTERM_KEY_KP_6; break;
744 case K_K7: key = VTERM_KEY_KP_7; break;
745 case K_K8: key = VTERM_KEY_KP_8; break;
746 case K_K9: key = VTERM_KEY_KP_9; break;
747 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
748 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
749 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
750 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
751 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
752 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
753 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
754 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
755 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
756 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
757 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
758 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
759 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200760 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
761 key = VTERM_KEY_LEFT; break;
762 case K_C_LEFT: mod = VTERM_MOD_CTRL;
763 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200764 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
765 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
766 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200767 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
768 key = VTERM_KEY_RIGHT; break;
769 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
770 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200771 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200772 case K_S_UP: mod = VTERM_MOD_SHIFT;
773 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200774 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200775
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200776 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
777 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
778 case K_MOUSELEFT: /* TODO */ return 0;
779 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200780
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200781 case K_LEFTMOUSE:
782 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
783 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
784 case K_LEFTRELEASE:
785 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
786 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
787 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
788 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
789 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
790 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
791 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
792 case K_X1MOUSE: /* TODO */ return 0;
793 case K_X1DRAG: /* TODO */ return 0;
794 case K_X1RELEASE: /* TODO */ return 0;
795 case K_X2MOUSE: /* TODO */ return 0;
796 case K_X2DRAG: /* TODO */ return 0;
797 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200798
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200799 case K_IGNORE: return 0;
800 case K_NOP: return 0;
801 case K_UNDO: return 0;
802 case K_HELP: return 0;
803 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
804 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
805 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
806 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
807 case K_SELECT: return 0;
808#ifdef FEAT_GUI
809 case K_VER_SCROLLBAR: return 0;
810 case K_HOR_SCROLLBAR: return 0;
811#endif
812#ifdef FEAT_GUI_TABLINE
813 case K_TABLINE: return 0;
814 case K_TABMENU: return 0;
815#endif
816#ifdef FEAT_NETBEANS_INTG
817 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
818#endif
819#ifdef FEAT_DND
820 case K_DROP: return 0;
821#endif
822#ifdef FEAT_AUTOCMD
823 case K_CURSORHOLD: return 0;
824#endif
825 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
826 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200827 }
828
829 /*
830 * Convert special keys to vterm keys:
831 * - Write keys to vterm: vterm_keyboard_key()
832 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200833 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200834 */
835 if (key != VTERM_KEY_NONE)
836 /* Special key, let vterm convert it. */
837 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200838 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200839 /* Normal character, let vterm convert it. */
840 vterm_keyboard_unichar(vterm, c, mod);
841
842 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200843 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200844}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200845
Bram Moolenaar938783d2017-07-16 20:13:26 +0200846/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200847 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200848 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200849 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200850term_job_running(term_T *term)
851{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200852 /* Also consider the job finished when the channel is closed, to avoid a
853 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200854 return term != NULL
855 && term->tl_job != NULL
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200856 && channel_is_open(term->tl_job->jv_channel)
857 && (term->tl_job->jv_status == JOB_STARTED
858 || term->tl_job->jv_channel->ch_keep_open);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200859}
860
861/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200862 * Add the last line of the scrollback buffer to the buffer in the window.
863 */
864 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200865add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200866{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200867 buf_T *buf = term->tl_buffer;
868 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
869 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200870
Bram Moolenaar58302322017-08-22 20:33:53 +0200871#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +0200872 if (!enc_utf8 && enc_codepage > 0)
873 {
874 WCHAR *ret = NULL;
875 int length = 0;
876
877 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
878 &ret, &length);
879 if (ret != NULL)
880 {
881 WideCharToMultiByte_alloc(enc_codepage, 0,
882 ret, length, (char **)&text, &len, 0, 0);
883 vim_free(ret);
884 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
885 vim_free(text);
886 }
887 }
888 else
889#endif
890 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200891 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200892 {
893 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200894 curbuf = buf;
895 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200896 curbuf = curwin->w_buffer;
897 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200898}
899
900/*
901 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200902 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200903 */
904 static void
905move_terminal_to_buffer(term_T *term)
906{
907 win_T *wp;
908 int len;
909 int lines_skipped = 0;
910 VTermPos pos;
911 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200912 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200913 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200914
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200915 if (term->tl_vterm == NULL)
916 return;
917 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200918 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
919 {
920 len = 0;
921 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
922 if (vterm_screen_get_cell(screen, pos, &cell) != 0
923 && cell.chars[0] != NUL)
924 len = pos.col + 1;
925
926 if (len == 0)
927 ++lines_skipped;
928 else
929 {
930 while (lines_skipped > 0)
931 {
932 /* Line was skipped, add an empty line. */
933 --lines_skipped;
934 if (ga_grow(&term->tl_scrollback, 1) == OK)
935 {
936 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
937 + term->tl_scrollback.ga_len;
938
939 line->sb_cols = 0;
940 line->sb_cells = NULL;
941 ++term->tl_scrollback.ga_len;
942
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200943 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200944 }
945 }
946
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200947 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200948 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
949 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200950 garray_T ga;
951 int width;
952 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200953 + term->tl_scrollback.ga_len;
954
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200955 ga_init2(&ga, 1, 100);
956 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200957 {
958 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200959 {
960 width = 1;
961 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
962 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +0200963 ga.ga_len += utf_char2bytes(' ',
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200964 (char_u *)ga.ga_data + ga.ga_len);
965 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200966 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200967 {
968 width = cell.width;
969
970 p[pos.col].width = cell.width;
971 p[pos.col].attrs = cell.attrs;
972 p[pos.col].fg = cell.fg;
973 p[pos.col].bg = cell.bg;
974
975 if (ga_grow(&ga, MB_MAXBYTES) == OK)
976 {
977 int i;
978 int c;
979
980 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +0200981 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200982 (char_u *)ga.ga_data + ga.ga_len);
983 }
984 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200985 }
986 line->sb_cols = len;
987 line->sb_cells = p;
988 ++term->tl_scrollback.ga_len;
989
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200990 if (ga_grow(&ga, 1) == FAIL)
991 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
992 else
993 {
994 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
995 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
996 }
997 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200998 }
999 else
1000 vim_free(p);
1001 }
1002 }
1003
1004 FOR_ALL_WINDOWS(wp)
1005 {
1006 if (wp->w_buffer == term->tl_buffer)
1007 {
1008 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1009 wp->w_cursor.col = 0;
1010 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +02001011 if (wp->w_cursor.lnum >= wp->w_height)
1012 {
1013 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
1014
1015 if (wp->w_topline < min_topline)
1016 wp->w_topline = min_topline;
1017 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02001018 redraw_win_later(wp, NOT_VALID);
1019 }
1020 }
1021}
1022
1023 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001024set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001025{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001026 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001027 vim_free(term->tl_status_text);
1028 term->tl_status_text = NULL;
1029 if (term->tl_buffer == curbuf)
1030 maketitle();
1031}
1032
1033/*
1034 * Called after the job if finished and Terminal mode is not active:
1035 * Move the vterm contents into the scrollback buffer and free the vterm.
1036 */
1037 static void
1038cleanup_vterm(term_T *term)
1039{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001040 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001041 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001042 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001043 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001044}
1045
1046/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001047 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001048 * Suspends updating the terminal window.
1049 */
1050 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001051term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001052{
1053 term_T *term = curbuf->b_term;
1054
1055 /* Append the current terminal contents to the buffer. */
1056 move_terminal_to_buffer(term);
1057
Bram Moolenaar6d819742017-08-06 14:57:49 +02001058 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001059
Bram Moolenaar6d819742017-08-06 14:57:49 +02001060 /* Move the window cursor to the position of the cursor in the
1061 * terminal. */
1062 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1063 + term->tl_cursor_pos.row + 1;
1064 check_cursor();
1065 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001066
Bram Moolenaar6d819742017-08-06 14:57:49 +02001067 /* Display the same lines as in the terminal. */
1068 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001069}
1070
1071/*
1072 * Returns TRUE if the current window contains a terminal and we are in
1073 * Terminal-Normal mode.
1074 */
1075 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001076term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001077{
1078 term_T *term = curbuf->b_term;
1079
Bram Moolenaar6d819742017-08-06 14:57:49 +02001080 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001081}
1082
1083/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001084 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001085 * Restores updating the terminal window.
1086 */
1087 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001088term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001089{
1090 term_T *term = curbuf->b_term;
1091 sb_line_T *line;
1092 garray_T *gap;
1093
1094 /* Remove the terminal contents from the scrollback and the buffer. */
1095 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001096 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1097 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001098 {
1099 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1100 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1101 vim_free(line->sb_cells);
1102 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001103 }
1104 check_cursor();
1105
Bram Moolenaar6d819742017-08-06 14:57:49 +02001106 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001107
1108 if (term->tl_channel_closed)
1109 cleanup_vterm(term);
1110 redraw_buf_and_status_later(curbuf, NOT_VALID);
1111}
1112
1113/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001114 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001115 * Note: while waiting a terminal may be closed and freed if the channel is
1116 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001117 * TODO: use terminal mode mappings.
1118 */
1119 static int
1120term_vgetc()
1121{
1122 int c;
1123
1124 ++no_mapping;
1125 ++allow_keys;
1126 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001127#ifdef WIN3264
1128 ctrl_break_was_pressed = FALSE;
1129#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001130 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001131 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001132 --no_mapping;
1133 --allow_keys;
1134 return c;
1135}
1136
1137/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001138 * Get the part that is connected to the tty. Normally this is PART_IN, but
1139 * when writing buffer lines to the job it can be another. This makes it
1140 * possible to do "1,5term vim -".
1141 */
1142 static ch_part_T
1143get_tty_part(term_T *term)
1144{
1145#ifdef UNIX
1146 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1147 int i;
1148
1149 for (i = 0; i < 3; ++i)
1150 {
1151 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1152
1153 if (isatty(fd))
1154 return parts[i];
1155 }
1156#endif
1157 return PART_IN;
1158}
1159
1160/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001161 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001162 * Return FAIL when the key needs to be handled in Normal mode.
1163 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001164 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001165 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001166send_keys_to_term(term_T *term, int c, int typed)
1167{
1168 char msg[KEY_BUF_LEN];
1169 size_t len;
1170 static int mouse_was_outside = FALSE;
1171 int dragging_outside = FALSE;
1172
1173 /* Catch keys that need to be handled as in Normal mode. */
1174 switch (c)
1175 {
1176 case NUL:
1177 case K_ZERO:
1178 if (typed)
1179 stuffcharReadbuff(c);
1180 return FAIL;
1181
1182 case K_IGNORE:
1183 return FAIL;
1184
1185 case K_LEFTDRAG:
1186 case K_MIDDLEDRAG:
1187 case K_RIGHTDRAG:
1188 case K_X1DRAG:
1189 case K_X2DRAG:
1190 dragging_outside = mouse_was_outside;
1191 /* FALLTHROUGH */
1192 case K_LEFTMOUSE:
1193 case K_LEFTMOUSE_NM:
1194 case K_LEFTRELEASE:
1195 case K_LEFTRELEASE_NM:
1196 case K_MIDDLEMOUSE:
1197 case K_MIDDLERELEASE:
1198 case K_RIGHTMOUSE:
1199 case K_RIGHTRELEASE:
1200 case K_X1MOUSE:
1201 case K_X1RELEASE:
1202 case K_X2MOUSE:
1203 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001204
1205 case K_MOUSEUP:
1206 case K_MOUSEDOWN:
1207 case K_MOUSELEFT:
1208 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001209 if (mouse_row < W_WINROW(curwin)
1210 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1211 || mouse_col < W_WINCOL(curwin)
1212 || mouse_col >= W_ENDCOL(curwin)
1213 || dragging_outside)
1214 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001215 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001216 if (typed)
1217 {
1218 stuffcharReadbuff(c);
1219 mouse_was_outside = TRUE;
1220 }
1221 return FAIL;
1222 }
1223 }
1224 if (typed)
1225 mouse_was_outside = FALSE;
1226
1227 /* Convert the typed key to a sequence of bytes for the job. */
1228 len = term_convert_key(term, c, msg);
1229 if (len > 0)
1230 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001231 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1232 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001233
1234 return OK;
1235}
1236
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001237 static void
1238position_cursor(win_T *wp, VTermPos *pos)
1239{
1240 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1241 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1242 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1243}
1244
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001245/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001246 * Handle CTRL-W "": send register contents to the job.
1247 */
1248 static void
1249term_paste_register(int prev_c UNUSED)
1250{
1251 int c;
1252 list_T *l;
1253 listitem_T *item;
1254 long reglen = 0;
1255 int type;
1256
1257#ifdef FEAT_CMDL_INFO
1258 if (add_to_showcmd(prev_c))
1259 if (add_to_showcmd('"'))
1260 out_flush();
1261#endif
1262 c = term_vgetc();
1263#ifdef FEAT_CMDL_INFO
1264 clear_showcmd();
1265#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001266 if (!term_use_loop())
1267 /* job finished while waiting for a character */
1268 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001269
1270 /* CTRL-W "= prompt for expression to evaluate. */
1271 if (c == '=' && get_expr_register() != '=')
1272 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001273 if (!term_use_loop())
1274 /* job finished while waiting for a character */
1275 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001276
1277 l = (list_T *)get_reg_contents(c, GREG_LIST);
1278 if (l != NULL)
1279 {
1280 type = get_reg_type(c, &reglen);
1281 for (item = l->lv_first; item != NULL; item = item->li_next)
1282 {
1283 char_u *s = get_tv_string(&item->li_tv);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001284#ifdef WIN3264
1285 char_u *tmp = s;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001286
Bram Moolenaar285f2432017-08-23 23:10:21 +02001287 if (!enc_utf8 && enc_codepage > 0)
1288 {
1289 WCHAR *ret = NULL;
1290 int length = 0;
1291
1292 MultiByteToWideChar_alloc(enc_codepage, 0, (char*)s, STRLEN(s),
1293 &ret, &length);
1294 if (ret != NULL)
1295 {
1296 WideCharToMultiByte_alloc(CP_UTF8, 0,
1297 ret, length, (char **)&s, &length, 0, 0);
1298 vim_free(ret);
1299 }
1300 }
1301#endif
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001302 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1303 s, STRLEN(s), NULL);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001304#ifdef WIN3264
1305 if (tmp != s)
1306 vim_free(s);
1307#endif
1308
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001309 if (item->li_next != NULL || type == MLINE)
1310 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1311 (char_u *)"\r", 1, NULL);
1312 }
1313 list_free(l);
1314 }
1315}
1316
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001317#if defined(FEAT_GUI) || defined(PROTO)
1318/*
1319 * Return TRUE when the cursor of the terminal should be displayed.
1320 */
1321 int
1322use_terminal_cursor()
1323{
1324 return in_terminal_loop != NULL;
1325}
1326
1327 cursorentry_T *
1328term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1329{
1330 term_T *term = in_terminal_loop;
1331 static cursorentry_T entry;
1332
1333 vim_memset(&entry, 0, sizeof(entry));
1334 entry.shape = entry.mshape =
1335 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1336 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1337 SHAPE_BLOCK;
1338 entry.percentage = 20;
1339 if (term->tl_cursor_blink)
1340 {
1341 entry.blinkwait = 700;
1342 entry.blinkon = 400;
Bram Moolenaar58302322017-08-22 20:33:53 +02001343 entry.blinkoff = 250;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001344 }
1345 *fg = gui.back_pixel;
1346 if (term->tl_cursor_color == NULL)
1347 *bg = gui.norm_pixel;
1348 else
1349 *bg = color_name2handle(term->tl_cursor_color);
1350 entry.name = "n";
1351 entry.used_for = SHAPE_CURSOR;
1352
1353 return &entry;
1354}
1355#endif
1356
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001357static int did_change_cursor = FALSE;
1358
1359 static void
1360may_set_cursor_props(term_T *term)
1361{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001362#ifdef FEAT_GUI
1363 /* For the GUI the cursor properties are obtained with
1364 * term_get_cursor_shape(). */
1365 if (gui.in_use)
1366 return;
1367#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001368 if (in_terminal_loop == term)
1369 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001370 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001371 if (term->tl_cursor_color != NULL)
1372 term_cursor_color(term->tl_cursor_color);
1373 else
1374 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001375 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1376 }
1377}
1378
1379 static void
1380may_restore_cursor_props(void)
1381{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001382#ifdef FEAT_GUI
1383 if (gui.in_use)
1384 return;
1385#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001386 if (did_change_cursor)
1387 {
1388 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001389 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001390 /* this will restore the initial cursor style, if possible */
1391 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001392 }
1393}
1394
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001395/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001396 * Returns TRUE if the current window contains a terminal and we are sending
1397 * keys to the job.
1398 */
1399 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001400term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001401{
1402 term_T *term = curbuf->b_term;
1403
1404 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001405 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001406 && term->tl_vterm != NULL
1407 && term_job_running(term);
1408}
1409
1410/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001411 * Wait for input and send it to the job.
1412 * Return when the start of a CTRL-W command is typed or anything else that
1413 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001414 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1415 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001416 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001417 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001418terminal_loop(void)
1419{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001420 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001421 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001422 int ret;
1423
Bram Moolenaar679653e2017-08-13 14:13:19 +02001424 /* Remember the terminal we are sending keys to. However, the terminal
1425 * might be closed while waiting for a character, e.g. typing "exit" in a
1426 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1427 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001428 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001429
1430 if (*curwin->w_p_tk != NUL)
1431 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001432 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001433 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001434
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001435#ifdef UNIX
1436 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001437 int part = get_tty_part(curbuf->b_term);
1438 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001439
1440 if (isatty(fd))
1441 {
1442 ttyinfo_T info;
1443
1444 /* Get the current backspace and enter characters of the pty. */
1445 if (get_tty_info(fd, &info) == OK)
1446 {
1447 term_backspace_char = info.backspace;
1448 term_enter_char = info.enter;
1449 term_nl_does_cr = info.nl_does_cr;
1450 }
1451 }
1452 }
1453#endif
1454
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001455 for (;;)
1456 {
1457 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001458 /* Repeat redrawing in case a message is received while redrawing. */
1459 while (curwin->w_redr_type != 0)
1460 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001461 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001462
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001463 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001464 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001465 /* job finished while waiting for a character */
1466 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001467 if (c == K_IGNORE)
1468 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001469
Bram Moolenaarfae42832017-08-01 22:24:26 +02001470#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001471 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001472 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001473 if (ctrl_break_was_pressed)
1474 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001475#endif
1476
Bram Moolenaar69198192017-08-05 14:10:48 +02001477 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001478 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001479 int prev_c = c;
1480
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001481#ifdef FEAT_CMDL_INFO
1482 if (add_to_showcmd(c))
1483 out_flush();
1484#endif
1485 c = term_vgetc();
1486#ifdef FEAT_CMDL_INFO
1487 clear_showcmd();
1488#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001489 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001490 /* job finished while waiting for a character */
1491 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001492
Bram Moolenaar69198192017-08-05 14:10:48 +02001493 if (prev_c == Ctrl_BSL)
1494 {
1495 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001496 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001497 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1498 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001499 ret = FAIL;
1500 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001501 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001502 /* Send both keys to the terminal. */
1503 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1504 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001505 else if (c == Ctrl_C)
1506 {
1507 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1508 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1509 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001510 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001511 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001512 /* "CTRL-W .": send CTRL-W to the job */
1513 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001514 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001515 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001516 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001517 /* CTRL-W N : go to Terminal-Normal mode. */
1518 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001519 ret = FAIL;
1520 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001521 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001522 else if (c == '"')
1523 {
1524 term_paste_register(prev_c);
1525 continue;
1526 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001527 else if (termkey == 0 || c != termkey)
1528 {
1529 stuffcharReadbuff(Ctrl_W);
1530 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001531 ret = OK;
1532 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001533 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001534 }
Bram Moolenaar58302322017-08-22 20:33:53 +02001535# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02001536 if (!enc_utf8 && has_mbyte && c >= 0x80)
1537 {
1538 WCHAR wc;
1539 char_u mb[3];
1540
1541 mb[0] = (unsigned)c >> 8;
1542 mb[1] = c;
1543 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1544 c = wc;
1545 }
1546# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001547 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001548 {
1549 ret = OK;
1550 goto theend;
1551 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001552 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001553 ret = FAIL;
1554
1555theend:
1556 in_terminal_loop = NULL;
1557 may_restore_cursor_props();
1558 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001559}
1560
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001561/*
1562 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001563 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001564 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001565 */
1566 void
1567term_job_ended(job_T *job)
1568{
1569 term_T *term;
1570 int did_one = FALSE;
1571
1572 for (term = first_term; term != NULL; term = term->tl_next)
1573 if (term->tl_job == job)
1574 {
1575 vim_free(term->tl_title);
1576 term->tl_title = NULL;
1577 vim_free(term->tl_status_text);
1578 term->tl_status_text = NULL;
1579 redraw_buf_and_status_later(term->tl_buffer, VALID);
1580 did_one = TRUE;
1581 }
1582 if (did_one)
1583 redraw_statuslines();
1584 if (curbuf->b_term != NULL)
1585 {
1586 if (curbuf->b_term->tl_job == job)
1587 maketitle();
1588 update_cursor(curbuf->b_term, TRUE);
1589 }
1590}
1591
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001592 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001593may_toggle_cursor(term_T *term)
1594{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001595 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001596 {
1597 if (term->tl_cursor_visible)
1598 cursor_on();
1599 else
1600 cursor_off();
1601 }
1602}
1603
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001604/*
1605 * Reverse engineer the RGB value into a cterm color index.
1606 * First color is 1. Return 0 if no match found.
1607 */
1608 static int
1609color2index(VTermColor *color, int fg, int *boldp)
1610{
1611 int red = color->red;
1612 int blue = color->blue;
1613 int green = color->green;
1614
1615 /* The argument for lookup_color() is for the color_names[] table. */
1616 if (red == 0)
1617 {
1618 if (green == 0)
1619 {
1620 if (blue == 0)
1621 return lookup_color(0, fg, boldp) + 1; /* black */
1622 if (blue == 224)
1623 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1624 }
1625 else if (green == 224)
1626 {
1627 if (blue == 0)
1628 return lookup_color(2, fg, boldp) + 1; /* dark green */
1629 if (blue == 224)
1630 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1631 }
1632 }
1633 else if (red == 224)
1634 {
1635 if (green == 0)
1636 {
1637 if (blue == 0)
1638 return lookup_color(4, fg, boldp) + 1; /* dark red */
1639 if (blue == 224)
1640 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1641 }
1642 else if (green == 224)
1643 {
1644 if (blue == 0)
1645 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1646 if (blue == 224)
1647 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1648 }
1649 }
1650 else if (red == 128)
1651 {
1652 if (green == 128 && blue == 128)
1653 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1654 }
1655 else if (red == 255)
1656 {
1657 if (green == 64)
1658 {
1659 if (blue == 64)
1660 return lookup_color(20, fg, boldp) + 1; /* light red */
1661 if (blue == 255)
1662 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1663 }
1664 else if (green == 255)
1665 {
1666 if (blue == 64)
1667 return lookup_color(24, fg, boldp) + 1; /* yellow */
1668 if (blue == 255)
1669 return lookup_color(26, fg, boldp) + 1; /* white */
1670 }
1671 }
1672 else if (red == 64)
1673 {
1674 if (green == 64)
1675 {
1676 if (blue == 255)
1677 return lookup_color(14, fg, boldp) + 1; /* light blue */
1678 }
1679 else if (green == 255)
1680 {
1681 if (blue == 64)
1682 return lookup_color(16, fg, boldp) + 1; /* light green */
1683 if (blue == 255)
1684 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1685 }
1686 }
1687 if (t_colors >= 256)
1688 {
1689 if (red == blue && red == green)
1690 {
1691 /* 24-color greyscale */
1692 static int cutoff[23] = {
1693 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1694 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1695 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1696 int i;
1697
1698 for (i = 0; i < 23; ++i)
1699 if (red < cutoff[i])
1700 return i + 233;
1701 return 256;
1702 }
1703
1704 /* 216-color cube */
1705 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001706 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001707 + (blue + 25) / 0x33;
1708 }
1709 return 0;
1710}
1711
1712/*
1713 * Convert the attributes of a vterm cell into an attribute index.
1714 */
1715 static int
1716cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1717{
1718 int attr = 0;
1719
1720 if (cellattrs.bold)
1721 attr |= HL_BOLD;
1722 if (cellattrs.underline)
1723 attr |= HL_UNDERLINE;
1724 if (cellattrs.italic)
1725 attr |= HL_ITALIC;
1726 if (cellattrs.strike)
1727 attr |= HL_STANDOUT;
1728 if (cellattrs.reverse)
1729 attr |= HL_INVERSE;
1730
1731#ifdef FEAT_GUI
1732 if (gui.in_use)
1733 {
1734 guicolor_T fg, bg;
1735
1736 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1737 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1738 return get_gui_attr_idx(attr, fg, bg);
1739 }
1740 else
1741#endif
1742#ifdef FEAT_TERMGUICOLORS
1743 if (p_tgc)
1744 {
1745 guicolor_T fg, bg;
1746
1747 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1748 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1749
1750 return get_tgc_attr_idx(attr, fg, bg);
1751 }
1752 else
1753#endif
1754 {
1755 int bold = MAYBE;
1756 int fg = color2index(&cellfg, TRUE, &bold);
1757 int bg = color2index(&cellbg, FALSE, &bold);
1758
1759 /* with 8 colors set the bold attribute to get a bright foreground */
1760 if (bold == TRUE)
1761 attr |= HL_BOLD;
1762 return get_cterm_attr_idx(attr, fg, bg);
1763 }
1764 return 0;
1765}
1766
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001767 static int
1768handle_damage(VTermRect rect, void *user)
1769{
1770 term_T *term = (term_T *)user;
1771
1772 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1773 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1774 redraw_buf_later(term->tl_buffer, NOT_VALID);
1775 return 1;
1776}
1777
1778 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001779handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001780{
1781 term_T *term = (term_T *)user;
1782
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001783 /* Scrolling up is done much more efficiently by deleting lines instead of
1784 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001785 if (dest.start_col == src.start_col
1786 && dest.end_col == src.end_col
1787 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001788 {
1789 win_T *wp;
1790 VTermColor fg, bg;
1791 VTermScreenCellAttrs attr;
1792 int clear_attr;
1793
1794 /* Set the color to clear lines with. */
1795 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1796 &fg, &bg);
1797 vim_memset(&attr, 0, sizeof(attr));
1798 clear_attr = cell2attr(attr, fg, bg);
1799
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001800 FOR_ALL_WINDOWS(wp)
1801 {
1802 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001803 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001804 src.start_row - dest.start_row, FALSE, FALSE,
1805 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001806 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001807 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001808 redraw_buf_later(term->tl_buffer, NOT_VALID);
1809 return 1;
1810}
1811
1812 static int
1813handle_movecursor(
1814 VTermPos pos,
1815 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001816 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001817 void *user)
1818{
1819 term_T *term = (term_T *)user;
1820 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001821
1822 term->tl_cursor_pos = pos;
1823 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001824
1825 FOR_ALL_WINDOWS(wp)
1826 {
1827 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001828 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001829 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001830 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001831 {
1832 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001833 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001834 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001835
1836 return 1;
1837}
1838
Bram Moolenaar21554412017-07-24 21:44:43 +02001839 static int
1840handle_settermprop(
1841 VTermProp prop,
1842 VTermValue *value,
1843 void *user)
1844{
1845 term_T *term = (term_T *)user;
1846
1847 switch (prop)
1848 {
1849 case VTERM_PROP_TITLE:
1850 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001851 /* a blank title isn't useful, make it empty, so that "running" is
1852 * displayed */
1853 if (*skipwhite((char_u *)value->string) == NUL)
1854 term->tl_title = NULL;
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001855#ifdef WIN3264
1856 else if (!enc_utf8 && enc_codepage > 0)
1857 {
1858 WCHAR *ret = NULL;
1859 int length = 0;
1860
1861 MultiByteToWideChar_alloc(CP_UTF8, 0,
1862 (char*)value->string, STRLEN(value->string),
1863 &ret, &length);
1864 if (ret != NULL)
1865 {
1866 WideCharToMultiByte_alloc(enc_codepage, 0,
1867 ret, length, (char**)&term->tl_title,
1868 &length, 0, 0);
1869 vim_free(ret);
1870 }
1871 }
1872#endif
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001873 else
1874 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001875 vim_free(term->tl_status_text);
1876 term->tl_status_text = NULL;
1877 if (term == curbuf->b_term)
1878 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001879 break;
1880
1881 case VTERM_PROP_CURSORVISIBLE:
1882 term->tl_cursor_visible = value->boolean;
1883 may_toggle_cursor(term);
1884 out_flush();
1885 break;
1886
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001887 case VTERM_PROP_CURSORBLINK:
1888 term->tl_cursor_blink = value->boolean;
1889 may_set_cursor_props(term);
1890 break;
1891
1892 case VTERM_PROP_CURSORSHAPE:
1893 term->tl_cursor_shape = value->number;
1894 may_set_cursor_props(term);
1895 break;
1896
1897 case VTERM_PROP_CURSORCOLOR:
1898 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001899 if (*value->string == NUL)
1900 term->tl_cursor_color = NULL;
1901 else
1902 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001903 may_set_cursor_props(term);
1904 break;
1905
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001906 case VTERM_PROP_ALTSCREEN:
1907 /* TODO: do anything else? */
1908 term->tl_using_altscreen = value->boolean;
1909 break;
1910
Bram Moolenaar21554412017-07-24 21:44:43 +02001911 default:
1912 break;
1913 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001914 /* Always return 1, otherwise vterm doesn't store the value internally. */
1915 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001916}
1917
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001918/*
1919 * The job running in the terminal resized the terminal.
1920 */
1921 static int
1922handle_resize(int rows, int cols, void *user)
1923{
1924 term_T *term = (term_T *)user;
1925 win_T *wp;
1926
1927 term->tl_rows = rows;
1928 term->tl_cols = cols;
1929 FOR_ALL_WINDOWS(wp)
1930 {
1931 if (wp->w_buffer == term->tl_buffer)
1932 {
1933 win_setheight_win(rows, wp);
1934 win_setwidth_win(cols, wp);
1935 }
1936 }
1937
1938 redraw_buf_later(term->tl_buffer, NOT_VALID);
1939 return 1;
1940}
1941
Bram Moolenaard85f2712017-07-28 21:51:57 +02001942/*
1943 * Handle a line that is pushed off the top of the screen.
1944 */
1945 static int
1946handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1947{
1948 term_T *term = (term_T *)user;
1949
1950 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001951 if (ga_grow(&term->tl_scrollback, 1) == OK)
1952 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001953 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001954 int len = 0;
1955 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001956 int c;
1957 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001958 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001959 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001960
1961 /* do not store empty cells at the end */
1962 for (i = 0; i < cols; ++i)
1963 if (cells[i].chars[0] != 0)
1964 len = i + 1;
1965
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001966 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001967 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001968 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001969 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001970 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001971 for (col = 0; col < len; col += cells[col].width)
1972 {
1973 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1974 {
1975 ga.ga_len = 0;
1976 break;
1977 }
1978 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +02001979 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001980 (char_u *)ga.ga_data + ga.ga_len);
1981 p[col].width = cells[col].width;
1982 p[col].attrs = cells[col].attrs;
1983 p[col].fg = cells[col].fg;
1984 p[col].bg = cells[col].bg;
1985 }
1986 }
1987 if (ga_grow(&ga, 1) == FAIL)
1988 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1989 else
1990 {
1991 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1992 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1993 }
1994 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001995
1996 line = (sb_line_T *)term->tl_scrollback.ga_data
1997 + term->tl_scrollback.ga_len;
1998 line->sb_cols = len;
1999 line->sb_cells = p;
2000 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002001 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002002 }
2003 return 0; /* ignored */
2004}
2005
Bram Moolenaar21554412017-07-24 21:44:43 +02002006static VTermScreenCallbacks screen_callbacks = {
2007 handle_damage, /* damage */
2008 handle_moverect, /* moverect */
2009 handle_movecursor, /* movecursor */
2010 handle_settermprop, /* settermprop */
2011 NULL, /* bell */
2012 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002013 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02002014 NULL /* sb_popline */
2015};
2016
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002017/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02002018 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002019 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02002020 */
2021 void
2022term_channel_closed(channel_T *ch)
2023{
2024 term_T *term;
2025 int did_one = FALSE;
2026
2027 for (term = first_term; term != NULL; term = term->tl_next)
2028 if (term->tl_job == ch->ch_job)
2029 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002030 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002031 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002032
Bram Moolenaard85f2712017-07-28 21:51:57 +02002033 vim_free(term->tl_title);
2034 term->tl_title = NULL;
2035 vim_free(term->tl_status_text);
2036 term->tl_status_text = NULL;
2037
Bram Moolenaar423802d2017-07-30 16:52:24 +02002038 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02002039 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002040 {
2041 int fnum = term->tl_buffer->b_fnum;
2042
Bram Moolenaar423802d2017-07-30 16:52:24 +02002043 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002044
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002045 if (term->tl_finish == 'c')
2046 {
2047 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002048 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002049 curbuf = term->tl_buffer;
2050 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
2051 break;
2052 }
2053 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
2054 {
2055 char buf[50];
2056
2057 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002058 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02002059 vim_snprintf(buf, sizeof(buf),
2060 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02002061 ? "botright sbuf %d"
2062 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002063 do_cmdline_cmd((char_u *)buf);
2064 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002065 else
2066 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002067 }
2068
Bram Moolenaard85f2712017-07-28 21:51:57 +02002069 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002070 }
2071 if (did_one)
2072 {
2073 redraw_statuslines();
2074
2075 /* Need to break out of vgetc(). */
2076 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002077 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002078
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002079 term = curbuf->b_term;
2080 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002081 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002082 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002083 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002084 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002085 }
2086 }
2087}
2088
2089/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002090 * Called to update a window that contains an active terminal.
2091 * Returns FAIL when there is no terminal running in this window or in
2092 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002093 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002094 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002095term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002096{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002097 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002098 VTerm *vterm;
2099 VTermScreen *screen;
2100 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002101 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002102
Bram Moolenaar6d819742017-08-06 14:57:49 +02002103 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002104 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002105
Bram Moolenaard85f2712017-07-28 21:51:57 +02002106 vterm = term->tl_vterm;
2107 screen = vterm_obtain_screen(vterm);
2108 state = vterm_obtain_state(vterm);
2109
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002110 /*
2111 * If the window was resized a redraw will be triggered and we get here.
2112 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2113 */
2114 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2115 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002116 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002117 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2118 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2119 win_T *twp;
2120
2121 FOR_ALL_WINDOWS(twp)
2122 {
2123 /* When more than one window shows the same terminal, use the
2124 * smallest size. */
2125 if (twp->w_buffer == term->tl_buffer)
2126 {
2127 if (!term->tl_rows_fixed && rows > twp->w_height)
2128 rows = twp->w_height;
2129 if (!term->tl_cols_fixed && cols > twp->w_width)
2130 cols = twp->w_width;
2131 }
2132 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002133
2134 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002135 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002136 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002137 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002138 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002139
2140 /* The cursor may have been moved when resizing. */
2141 vterm_state_get_cursorpos(state, &pos);
2142 position_cursor(wp, &pos);
2143
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002144 /* TODO: Only redraw what changed. */
2145 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002146 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002147 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002148 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002149
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002150 if (pos.row < term->tl_rows)
2151 {
2152 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002153 {
2154 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002155 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002156
Bram Moolenaareeac6772017-07-23 15:48:37 +02002157 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2158 vim_memset(&cell, 0, sizeof(cell));
2159
2160 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002161 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002162 if (c == NUL)
2163 {
2164 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002165 if (enc_utf8)
2166 ScreenLinesUC[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002167 }
2168 else
2169 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002170 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002171 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002172 if (c >= 0x80)
2173 {
2174 ScreenLines[off] = ' ';
2175 ScreenLinesUC[off] = c;
2176 }
2177 else
2178 {
2179 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002180 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002181 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002182 }
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002183#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02002184 else if (has_mbyte && c >= 0x80)
2185 {
2186 char_u mb[MB_MAXBYTES+1];
2187 WCHAR wc = c;
2188
2189 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2190 (char*)mb, 2, 0, 0) > 1)
2191 {
2192 ScreenLines[off] = mb[0];
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002193 ScreenLines[off + 1] = mb[1];
Bram Moolenaar740c4332017-08-21 22:01:27 +02002194 cell.width = mb_ptr2cells(mb);
2195 }
2196 else
2197 ScreenLines[off] = c;
2198 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002199#endif
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002200 else
Bram Moolenaar740c4332017-08-21 22:01:27 +02002201 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002202 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002203 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002204
2205 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002206 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002207 if (cell.width == 2)
2208 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002209 if (enc_utf8)
2210 ScreenLinesUC[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002211
2212 /* don't set the second byte to NUL for a DBCS encoding, it
2213 * has been set above */
2214 if (enc_utf8 || !has_mbyte)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002215 ScreenLines[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002216
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002217 ++pos.col;
2218 ++off;
2219 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002220 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002221 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002222 else
2223 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002224
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002225 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2226 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002227 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002228
2229 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002230}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002231
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002232/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002233 * Return TRUE if "wp" is a terminal window where the job has finished.
2234 */
2235 int
2236term_is_finished(buf_T *buf)
2237{
2238 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2239}
2240
2241/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002242 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002243 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002244 */
2245 int
2246term_show_buffer(buf_T *buf)
2247{
2248 term_T *term = buf->b_term;
2249
Bram Moolenaar6d819742017-08-06 14:57:49 +02002250 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002251}
2252
2253/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002254 * The current buffer is going to be changed. If there is terminal
2255 * highlighting remove it now.
2256 */
2257 void
2258term_change_in_curbuf(void)
2259{
2260 term_T *term = curbuf->b_term;
2261
2262 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2263 {
2264 free_scrollback(term);
2265 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002266
2267 /* The buffer is now like a normal buffer, it cannot be easily
2268 * abandoned when changed. */
2269 set_string_option_direct((char_u *)"buftype", -1,
2270 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002271 }
2272}
2273
2274/*
2275 * Get the screen attribute for a position in the buffer.
2276 */
2277 int
2278term_get_attr(buf_T *buf, linenr_T lnum, int col)
2279{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002280 term_T *term = buf->b_term;
2281 sb_line_T *line;
2282 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002283
Bram Moolenaar70229f92017-07-29 16:01:53 +02002284 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002285 return 0;
2286 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2287 if (col >= line->sb_cols)
2288 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002289 cellattr = line->sb_cells + col;
2290 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002291}
2292
2293/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002294 * Create a new vterm and initialize it.
2295 */
2296 static void
2297create_vterm(term_T *term, int rows, int cols)
2298{
2299 VTerm *vterm;
2300 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002301 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002302
2303 vterm = vterm_new(rows, cols);
2304 term->tl_vterm = vterm;
2305 screen = vterm_obtain_screen(vterm);
2306 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2307 /* TODO: depends on 'encoding'. */
2308 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002309
2310 /* Vterm uses a default black background. Set it to white when
2311 * 'background' is "light". */
2312 if (*p_bg == 'l')
2313 {
2314 VTermColor fg, bg;
2315
2316 fg.red = fg.green = fg.blue = 0;
2317 bg.red = bg.green = bg.blue = 255;
2318 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2319 }
2320
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002321 /* Required to initialize most things. */
2322 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002323
2324 /* Allow using alternate screen. */
2325 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002326
Bram Moolenaar58302322017-08-22 20:33:53 +02002327 /* For unix do not use a blinking cursor. In an xterm this causes the
2328 * cursor to blink if it's blinking in the xterm.
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002329 * For Windows we respect the system wide setting. */
Bram Moolenaar58302322017-08-22 20:33:53 +02002330#ifdef WIN3264
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002331 if (GetCaretBlinkTime() == INFINITE)
2332 value.boolean = 0;
2333 else
2334 value.boolean = 1;
Bram Moolenaar58302322017-08-22 20:33:53 +02002335#else
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002336 value.boolean = 0;
Bram Moolenaar58302322017-08-22 20:33:53 +02002337#endif
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002338 vterm_state_set_termprop(vterm_obtain_state(vterm),
2339 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002340}
2341
Bram Moolenaar21554412017-07-24 21:44:43 +02002342/*
2343 * Return the text to show for the buffer name and status.
2344 */
2345 char_u *
2346term_get_status_text(term_T *term)
2347{
2348 if (term->tl_status_text == NULL)
2349 {
2350 char_u *txt;
2351 size_t len;
2352
Bram Moolenaar6d819742017-08-06 14:57:49 +02002353 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002354 {
2355 if (term_job_running(term))
2356 txt = (char_u *)_("Terminal");
2357 else
2358 txt = (char_u *)_("Terminal-finished");
2359 }
2360 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002361 txt = term->tl_title;
2362 else if (term_job_running(term))
2363 txt = (char_u *)_("running");
2364 else
2365 txt = (char_u *)_("finished");
2366 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002367 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002368 if (term->tl_status_text != NULL)
2369 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2370 term->tl_buffer->b_fname, txt);
2371 }
2372 return term->tl_status_text;
2373}
2374
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002375/*
2376 * Mark references in jobs of terminals.
2377 */
2378 int
2379set_ref_in_term(int copyID)
2380{
2381 int abort = FALSE;
2382 term_T *term;
2383 typval_T tv;
2384
2385 for (term = first_term; term != NULL; term = term->tl_next)
2386 if (term->tl_job != NULL)
2387 {
2388 tv.v_type = VAR_JOB;
2389 tv.vval.v_job = term->tl_job;
2390 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2391 }
2392 return abort;
2393}
2394
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002395/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002396 * Get the buffer from the first argument in "argvars".
2397 * Returns NULL when the buffer is not for a terminal window.
2398 */
2399 static buf_T *
2400term_get_buf(typval_T *argvars)
2401{
2402 buf_T *buf;
2403
2404 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2405 ++emsg_off;
2406 buf = get_buf_tv(&argvars[0], FALSE);
2407 --emsg_off;
2408 if (buf == NULL || buf->b_term == NULL)
2409 return NULL;
2410 return buf;
2411}
2412
2413/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002414 * "term_getaltscreen(buf)" function
2415 */
2416 void
2417f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2418{
2419 buf_T *buf = term_get_buf(argvars);
2420
2421 if (buf == NULL)
2422 return;
2423 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2424}
2425
2426/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002427 * "term_getattr(attr, name)" function
2428 */
2429 void
2430f_term_getattr(typval_T *argvars, typval_T *rettv)
2431{
2432 int attr;
2433 size_t i;
2434 char_u *name;
2435
2436 static struct {
2437 char *name;
2438 int attr;
2439 } attrs[] = {
2440 {"bold", HL_BOLD},
2441 {"italic", HL_ITALIC},
2442 {"underline", HL_UNDERLINE},
2443 {"strike", HL_STANDOUT},
2444 {"reverse", HL_INVERSE},
2445 };
2446
2447 attr = get_tv_number(&argvars[0]);
2448 name = get_tv_string_chk(&argvars[1]);
2449 if (name == NULL)
2450 return;
2451
2452 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2453 if (STRCMP(name, attrs[i].name) == 0)
2454 {
2455 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2456 break;
2457 }
2458}
2459
2460/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002461 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002462 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002463 void
2464f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002465{
Bram Moolenaar97870002017-07-30 18:28:38 +02002466 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002467 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002468 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002469 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002470
Bram Moolenaar97870002017-07-30 18:28:38 +02002471 if (rettv_list_alloc(rettv) == FAIL)
2472 return;
2473 if (buf == NULL)
2474 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002475 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002476
2477 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002478 list_append_number(l, term->tl_cursor_pos.row + 1);
2479 list_append_number(l, term->tl_cursor_pos.col + 1);
2480
2481 d = dict_alloc();
2482 if (d != NULL)
2483 {
2484 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2485 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2486 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2487 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2488 ? (char_u *)"" : term->tl_cursor_color);
2489 list_append_dict(l, d);
2490 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002491}
2492
2493/*
2494 * "term_getjob(buf)" function
2495 */
2496 void
2497f_term_getjob(typval_T *argvars, typval_T *rettv)
2498{
2499 buf_T *buf = term_get_buf(argvars);
2500
2501 rettv->v_type = VAR_JOB;
2502 rettv->vval.v_job = NULL;
2503 if (buf == NULL)
2504 return;
2505
2506 rettv->vval.v_job = buf->b_term->tl_job;
2507 if (rettv->vval.v_job != NULL)
2508 ++rettv->vval.v_job->jv_refcount;
2509}
2510
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002511 static int
2512get_row_number(typval_T *tv, term_T *term)
2513{
2514 if (tv->v_type == VAR_STRING
2515 && tv->vval.v_string != NULL
2516 && STRCMP(tv->vval.v_string, ".") == 0)
2517 return term->tl_cursor_pos.row;
2518 return (int)get_tv_number(tv) - 1;
2519}
2520
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002521/*
2522 * "term_getline(buf, row)" function
2523 */
2524 void
2525f_term_getline(typval_T *argvars, typval_T *rettv)
2526{
2527 buf_T *buf = term_get_buf(argvars);
2528 term_T *term;
2529 int row;
2530
2531 rettv->v_type = VAR_STRING;
2532 if (buf == NULL)
2533 return;
2534 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002535 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002536
2537 if (term->tl_vterm == NULL)
2538 {
2539 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2540
2541 /* vterm is finished, get the text from the buffer */
2542 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2543 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2544 }
2545 else
2546 {
2547 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2548 VTermRect rect;
2549 int len;
2550 char_u *p;
2551
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002552 if (row < 0 || row >= term->tl_rows)
2553 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002554 len = term->tl_cols * MB_MAXBYTES + 1;
2555 p = alloc(len);
2556 if (p == NULL)
2557 return;
2558 rettv->vval.v_string = p;
2559
2560 rect.start_col = 0;
2561 rect.end_col = term->tl_cols;
2562 rect.start_row = row;
2563 rect.end_row = row + 1;
2564 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2565 }
2566}
2567
2568/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002569 * "term_getscrolled(buf)" function
2570 */
2571 void
2572f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2573{
2574 buf_T *buf = term_get_buf(argvars);
2575
2576 if (buf == NULL)
2577 return;
2578 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2579}
2580
2581/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002582 * "term_getsize(buf)" function
2583 */
2584 void
2585f_term_getsize(typval_T *argvars, typval_T *rettv)
2586{
2587 buf_T *buf = term_get_buf(argvars);
2588 list_T *l;
2589
2590 if (rettv_list_alloc(rettv) == FAIL)
2591 return;
2592 if (buf == NULL)
2593 return;
2594
2595 l = rettv->vval.v_list;
2596 list_append_number(l, buf->b_term->tl_rows);
2597 list_append_number(l, buf->b_term->tl_cols);
2598}
2599
2600/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002601 * "term_getstatus(buf)" function
2602 */
2603 void
2604f_term_getstatus(typval_T *argvars, typval_T *rettv)
2605{
2606 buf_T *buf = term_get_buf(argvars);
2607 term_T *term;
2608 char_u val[100];
2609
2610 rettv->v_type = VAR_STRING;
2611 if (buf == NULL)
2612 return;
2613 term = buf->b_term;
2614
2615 if (term_job_running(term))
2616 STRCPY(val, "running");
2617 else
2618 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002619 if (term->tl_normal_mode)
2620 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002621 rettv->vval.v_string = vim_strsave(val);
2622}
2623
2624/*
2625 * "term_gettitle(buf)" function
2626 */
2627 void
2628f_term_gettitle(typval_T *argvars, typval_T *rettv)
2629{
2630 buf_T *buf = term_get_buf(argvars);
2631
2632 rettv->v_type = VAR_STRING;
2633 if (buf == NULL)
2634 return;
2635
2636 if (buf->b_term->tl_title != NULL)
2637 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2638}
2639
2640/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002641 * "term_gettty(buf)" function
2642 */
2643 void
2644f_term_gettty(typval_T *argvars, typval_T *rettv)
2645{
2646 buf_T *buf = term_get_buf(argvars);
2647 char_u *p;
2648
2649 rettv->v_type = VAR_STRING;
2650 if (buf == NULL)
2651 return;
2652 if (buf->b_term->tl_job != NULL)
2653 p = buf->b_term->tl_job->jv_tty_name;
2654 else
2655 p = buf->b_term->tl_tty_name;
2656 if (p != NULL)
2657 rettv->vval.v_string = vim_strsave(p);
2658}
2659
2660/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002661 * "term_list()" function
2662 */
2663 void
2664f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2665{
2666 term_T *tp;
2667 list_T *l;
2668
2669 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2670 return;
2671
2672 l = rettv->vval.v_list;
2673 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2674 if (tp != NULL && tp->tl_buffer != NULL)
2675 if (list_append_number(l,
2676 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2677 return;
2678}
2679
2680/*
2681 * "term_scrape(buf, row)" function
2682 */
2683 void
2684f_term_scrape(typval_T *argvars, typval_T *rettv)
2685{
2686 buf_T *buf = term_get_buf(argvars);
2687 VTermScreen *screen = NULL;
2688 VTermPos pos;
2689 list_T *l;
2690 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002691 char_u *p;
2692 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002693
2694 if (rettv_list_alloc(rettv) == FAIL)
2695 return;
2696 if (buf == NULL)
2697 return;
2698 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002699
2700 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002701 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002702
2703 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002704 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002705 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002706 p = NULL;
2707 line = NULL;
2708 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002709 else
2710 {
2711 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2712
2713 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2714 return;
2715 p = ml_get_buf(buf, lnum + 1, FALSE);
2716 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2717 }
2718
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002719 for (pos.col = 0; pos.col < term->tl_cols; )
2720 {
2721 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002722 int width;
2723 VTermScreenCellAttrs attrs;
2724 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002725 char_u rgb[8];
2726 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2727 int off = 0;
2728 int i;
2729
2730 if (screen == NULL)
2731 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002732 cellattr_T *cellattr;
2733 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002734
2735 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002736 if (pos.col >= line->sb_cols)
2737 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002738 cellattr = line->sb_cells + pos.col;
2739 width = cellattr->width;
2740 attrs = cellattr->attrs;
2741 fg = cellattr->fg;
2742 bg = cellattr->bg;
2743 len = MB_PTR2LEN(p);
2744 mch_memmove(mbs, p, len);
2745 mbs[len] = NUL;
2746 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002747 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002748 else
2749 {
2750 VTermScreenCell cell;
2751 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2752 break;
2753 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2754 {
2755 if (cell.chars[i] == 0)
2756 break;
2757 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2758 }
2759 mbs[off] = NUL;
2760 width = cell.width;
2761 attrs = cell.attrs;
2762 fg = cell.fg;
2763 bg = cell.bg;
2764 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002765 dcell = dict_alloc();
2766 list_append_dict(l, dcell);
2767
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002768 dict_add_nr_str(dcell, "chars", 0, mbs);
2769
2770 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002771 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002772 dict_add_nr_str(dcell, "fg", 0, rgb);
2773 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002774 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002775 dict_add_nr_str(dcell, "bg", 0, rgb);
2776
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002777 dict_add_nr_str(dcell, "attr",
2778 cell2attr(attrs, fg, bg), NULL);
2779 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002780
2781 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002782 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002783 ++pos.col;
2784 }
2785}
2786
2787/*
2788 * "term_sendkeys(buf, keys)" function
2789 */
2790 void
2791f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2792{
2793 buf_T *buf = term_get_buf(argvars);
2794 char_u *msg;
2795 term_T *term;
2796
2797 rettv->v_type = VAR_UNKNOWN;
2798 if (buf == NULL)
2799 return;
2800
2801 msg = get_tv_string_chk(&argvars[1]);
2802 if (msg == NULL)
2803 return;
2804 term = buf->b_term;
2805 if (term->tl_vterm == NULL)
2806 return;
2807
2808 while (*msg != NUL)
2809 {
2810 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2811 msg += MB_PTR2LEN(msg);
2812 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002813}
2814
2815/*
2816 * "term_start(command, options)" function
2817 */
2818 void
2819f_term_start(typval_T *argvars, typval_T *rettv)
2820{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002821 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002822
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002823 init_job_options(&opt);
2824 /* TODO: allow more job options */
2825 if (argvars[1].v_type != VAR_UNKNOWN
2826 && get_job_options(&argvars[1], &opt,
2827 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002828 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002829 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002830 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002831 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002832 return;
2833
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002834 if (opt.jo_vertical)
2835 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002836 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002837
2838 if (curbuf->b_term != NULL)
2839 rettv->vval.v_number = curbuf->b_fnum;
2840}
2841
2842/*
2843 * "term_wait" function
2844 */
2845 void
2846f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2847{
2848 buf_T *buf = term_get_buf(argvars);
2849
2850 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002851 {
2852 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002853 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002854 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002855 if (buf->b_term->tl_job == NULL)
2856 {
2857 ch_log(NULL, "term_wait(): no job to wait for");
2858 return;
2859 }
Bram Moolenaar13ebb032017-08-26 22:02:51 +02002860 if (buf->b_term->tl_job->jv_channel == NULL)
2861 /* channel is closed, nothing to do */
2862 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002863
2864 /* Get the job status, this will detect a job that finished. */
Bram Moolenaar13ebb032017-08-26 22:02:51 +02002865 if ((buf->b_term->tl_job->jv_channel == NULL
2866 || !buf->b_term->tl_job->jv_channel->ch_keep_open)
2867 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002868 {
2869 /* The job is dead, keep reading channel I/O until the channel is
2870 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002871 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002872 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2873 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002874 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002875 parse_queued_messages();
2876 ui_delay(10L, FALSE);
2877 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002878 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002879 parse_queued_messages();
2880 }
2881 else
2882 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002883 long wait = 10L;
2884
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002885 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002886 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002887
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002888 /* Wait for some time for any channel I/O. */
2889 if (argvars[1].v_type != VAR_UNKNOWN)
2890 wait = get_tv_number(&argvars[1]);
2891 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002892 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002893
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002894 /* Flushing messages on channels is hopefully sufficient.
2895 * TODO: is there a better way? */
2896 parse_queued_messages();
2897 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002898}
2899
Bram Moolenaara83e3962017-08-17 14:39:07 +02002900# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002901
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002902/**************************************
2903 * 2. MS-Windows implementation.
2904 */
2905
Bram Moolenaara83e3962017-08-17 14:39:07 +02002906# ifndef PROTO
2907
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002908#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2909#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2910
Bram Moolenaar8a773062017-07-24 22:29:21 +02002911void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002912void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002913void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002914BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2915void (*winpty_config_set_initial_size)(void*, int, int);
2916LPCWSTR (*winpty_conin_name)(void*);
2917LPCWSTR (*winpty_conout_name)(void*);
2918LPCWSTR (*winpty_conerr_name)(void*);
2919void (*winpty_free)(void*);
2920void (*winpty_config_free)(void*);
2921void (*winpty_spawn_config_free)(void*);
2922void (*winpty_error_free)(void*);
2923LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002924BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002925HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002926
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002927#define WINPTY_DLL "winpty.dll"
2928
2929static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002930# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002931
Bram Moolenaara83e3962017-08-17 14:39:07 +02002932 static int
2933dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002934{
2935 int i;
2936 static struct
2937 {
2938 char *name;
2939 FARPROC *ptr;
2940 } winpty_entry[] =
2941 {
2942 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2943 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2944 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2945 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2946 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2947 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2948 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2949 {"winpty_free", (FARPROC*)&winpty_free},
2950 {"winpty_open", (FARPROC*)&winpty_open},
2951 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2952 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2953 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2954 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002955 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002956 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002957 {NULL, NULL}
2958 };
2959
2960 /* No need to initialize twice. */
2961 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002962 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002963 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2964 * winpty.dll. */
2965 if (*p_winptydll != NUL)
2966 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2967 if (!hWinPtyDLL)
2968 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002969 if (!hWinPtyDLL)
2970 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002971 if (verbose)
2972 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2973 : (char_u *)WINPTY_DLL);
2974 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002975 }
2976 for (i = 0; winpty_entry[i].name != NULL
2977 && winpty_entry[i].ptr != NULL; ++i)
2978 {
2979 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2980 winpty_entry[i].name)) == NULL)
2981 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002982 if (verbose)
2983 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2984 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002985 }
2986 }
2987
Bram Moolenaara83e3962017-08-17 14:39:07 +02002988 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002989}
2990
2991/*
2992 * Create a new terminal of "rows" by "cols" cells.
2993 * Store a reference in "term".
2994 * Return OK or FAIL.
2995 */
2996 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02002997term_and_job_init(
2998 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02002999 typval_T *argvar,
3000 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003001{
Bram Moolenaar5983d502017-08-20 19:22:56 +02003002 WCHAR *cmd_wchar = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003003 channel_T *channel = NULL;
3004 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003005 DWORD error;
Bram Moolenaar5983d502017-08-20 19:22:56 +02003006 HANDLE jo = NULL;
3007 HANDLE child_process_handle;
3008 HANDLE child_thread_handle;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003009 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003010 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003011 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003012 garray_T ga;
3013 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003014
Bram Moolenaara83e3962017-08-17 14:39:07 +02003015 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003016 return FAIL;
3017
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003018 if (argvar->v_type == VAR_STRING)
3019 cmd = argvar->vval.v_string;
3020 else
3021 {
3022 ga_init2(&ga, (int)sizeof(char*), 20);
3023 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
3024 goto failed;
3025 cmd = ga.ga_data;
3026 }
3027
Bram Moolenaar5983d502017-08-20 19:22:56 +02003028 cmd_wchar = enc_to_utf16(cmd, NULL);
3029 if (cmd_wchar == NULL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003030 return FAIL;
3031
3032 job = job_alloc();
3033 if (job == NULL)
3034 goto failed;
3035
3036 channel = add_channel();
3037 if (channel == NULL)
3038 goto failed;
3039
3040 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
3041 if (term->tl_winpty_config == NULL)
3042 goto failed;
3043
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003044 winpty_config_set_initial_size(term->tl_winpty_config,
3045 term->tl_cols, term->tl_rows);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003046 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
3047 if (term->tl_winpty == NULL)
3048 goto failed;
3049
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003050 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003051 spawn_config = winpty_spawn_config_new(
3052 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
3053 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
3054 NULL,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003055 cmd_wchar,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003056 NULL,
3057 NULL,
3058 &winpty_err);
3059 if (spawn_config == NULL)
3060 goto failed;
3061
3062 channel = add_channel();
3063 if (channel == NULL)
3064 goto failed;
3065
3066 job = job_alloc();
3067 if (job == NULL)
3068 goto failed;
3069
Bram Moolenaar5983d502017-08-20 19:22:56 +02003070 /* TODO: when all lines are written and the fd is closed, the command
3071 * doesn't get EOF and hangs. */
3072 if (opt->jo_set & JO_IN_BUF)
3073 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
3074
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003075 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
3076 &child_thread_handle, &error, &winpty_err))
3077 goto failed;
3078
3079 channel_set_pipes(channel,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003080 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003081 winpty_conin_name(term->tl_winpty),
3082 GENERIC_WRITE, 0, NULL,
3083 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003084 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003085 winpty_conout_name(term->tl_winpty),
3086 GENERIC_READ, 0, NULL,
3087 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003088 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003089 winpty_conerr_name(term->tl_winpty),
3090 GENERIC_READ, 0, NULL,
3091 OPEN_EXISTING, 0, NULL));
3092
3093 jo = CreateJobObject(NULL, NULL);
3094 if (jo == NULL)
3095 goto failed;
3096
3097 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003098 {
3099 /* Failed, switch the way to terminate process with TerminateProcess. */
3100 CloseHandle(jo);
3101 jo = NULL;
3102 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003103
3104 winpty_spawn_config_free(spawn_config);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003105 vim_free(cmd_wchar);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003106
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003107 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003108
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02003109 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02003110 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003111
3112 job->jv_channel = channel;
3113 job->jv_proc_info.hProcess = child_process_handle;
3114 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
3115 job->jv_job_object = jo;
3116 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003117 sprintf(buf, "winpty://%lu",
3118 GetProcessId(winpty_agent_process(term->tl_winpty)));
3119 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003120 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003121 term->tl_job = job;
3122
3123 return OK;
3124
3125failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003126 if (argvar->v_type == VAR_LIST)
3127 vim_free(ga.ga_data);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003128 if (cmd_wchar != NULL)
3129 vim_free(cmd_wchar);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003130 if (spawn_config != NULL)
3131 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003132 if (channel != NULL)
3133 channel_clear(channel);
3134 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003135 {
3136 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003137 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003138 }
3139 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003140 if (jo != NULL)
3141 CloseHandle(jo);
3142 if (term->tl_winpty != NULL)
3143 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003144 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003145 if (term->tl_winpty_config != NULL)
3146 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003147 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003148 if (winpty_err != NULL)
3149 {
3150 char_u *msg = utf16_to_enc(
3151 (short_u *)winpty_error_msg(winpty_err), NULL);
3152
3153 EMSG(msg);
3154 winpty_error_free(winpty_err);
3155 }
3156 return FAIL;
3157}
3158
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003159 static int
3160create_pty_only(term_T *term, jobopt_T *opt)
3161{
3162 /* TODO: implement this */
3163 return FAIL;
3164}
3165
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003166/*
3167 * Free the terminal emulator part of "term".
3168 */
3169 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003170term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003171{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003172 if (term->tl_winpty != NULL)
3173 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003174 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003175 if (term->tl_winpty_config != NULL)
3176 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003177 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003178 if (term->tl_vterm != NULL)
3179 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003180 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003181}
3182
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003183/*
3184 * Request size to terminal.
3185 */
3186 static void
3187term_report_winsize(term_T *term, int rows, int cols)
3188{
3189 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3190}
3191
Bram Moolenaara83e3962017-08-17 14:39:07 +02003192 int
3193terminal_enabled(void)
3194{
3195 return dyn_winpty_init(FALSE) == OK;
3196}
3197
3198
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003199# else
3200
3201/**************************************
3202 * 3. Unix-like implementation.
3203 */
3204
3205/*
3206 * Create a new terminal of "rows" by "cols" cells.
3207 * Start job for "cmd".
3208 * Store the pointers in "term".
3209 * Return OK or FAIL.
3210 */
3211 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003212term_and_job_init(
3213 term_T *term,
Bram Moolenaarb2412082017-08-20 18:09:14 +02003214 typval_T *argvar,
3215 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003216{
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003217 create_vterm(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003218
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003219 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003220 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003221 if (term->tl_job != NULL)
3222 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003223
Bram Moolenaar61a66052017-07-22 18:39:00 +02003224 return term->tl_job != NULL
3225 && term->tl_job->jv_channel != NULL
3226 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003227}
3228
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003229 static int
3230create_pty_only(term_T *term, jobopt_T *opt)
3231{
3232 int ret;
3233
3234 create_vterm(term, term->tl_rows, term->tl_cols);
3235
3236 term->tl_job = job_alloc();
3237 if (term->tl_job == NULL)
3238 return FAIL;
3239 ++term->tl_job->jv_refcount;
3240
3241 /* behave like the job is already finished */
3242 term->tl_job->jv_status = JOB_FINISHED;
3243
3244 ret = mch_create_pty_channel(term->tl_job, opt);
3245
3246 return ret;
3247}
3248
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003249/*
3250 * Free the terminal emulator part of "term".
3251 */
3252 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003253term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003254{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003255 if (term->tl_vterm != NULL)
3256 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003257 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003258}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003259
3260/*
3261 * Request size to terminal.
3262 */
3263 static void
3264term_report_winsize(term_T *term, int rows, int cols)
3265{
3266 /* Use an ioctl() to report the new window size to the job. */
3267 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3268 {
3269 int fd = -1;
3270 int part;
3271
3272 for (part = PART_OUT; part < PART_COUNT; ++part)
3273 {
3274 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3275 if (isatty(fd))
3276 break;
3277 }
3278 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003279 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003280 }
3281}
3282
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003283# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003284
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003285#endif /* FEAT_TERMINAL */