blob: 288ad28eb6070efd3e809f74af3d3584c74de3ce [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020022 * this library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020035 * When the job ends the text is put in a buffer. Redrawing then happens from
36 * that buffer, attributes come from the scrollback buffer tl_scrollback.
Bram Moolenaar679653e2017-08-13 14:13:19 +020037 * When the buffer is changed it is turned into a normal buffer, the attributes
38 * in tl_scrollback are no longer used.
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020039 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020040 * TODO:
Bram Moolenaar13ebb032017-08-26 22:02:51 +020041 * - ":term NONE" does not work in MS-Windows.
Bram Moolenaar0cbba822017-08-21 21:39:28 +020042 * - test for writing lines to terminal job does not work on MS-Windows
Bram Moolenaar94053a52017-08-01 21:44:33 +020043 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020044 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020045 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020046 * - support minimal size when 'termsize' is empty?
Bram Moolenaar58302322017-08-22 20:33:53 +020047 * - do not set bufhidden to "hide"? works like a buffer with changes.
48 * document that CTRL-W :hide can be used.
Bram Moolenaar285f2432017-08-23 23:10:21 +020049 * - GUI: when using tabs, focus in terminal, click on tab does not work.
Bram Moolenaar13ebb032017-08-26 22:02:51 +020050 * - When $HOME was set by Vim (MS-Windows), do not pass it to the job.
Bram Moolenaar285f2432017-08-23 23:10:21 +020051 * - GUI: when 'confirm' is set and trying to exit Vim, dialog offers to save
52 * changes to "!shell".
53 * (justrajdeep, 2017 Aug 22)
Bram Moolenaar58302322017-08-22 20:33:53 +020054 * - command argument with spaces doesn't work #1999
55 * :terminal ls dir\ with\ spaces
Bram Moolenaar4f44b882017-08-13 20:06:18 +020056 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020057 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
58 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
59 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
60 * Check that something is connected to the terminal.
61 * Test: "cat" reading from a file or buffer
Bram Moolenaar740c4332017-08-21 22:01:27 +020062 * "ls" writing stdout to a file or buffer
63 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020064 * - For the GUI fill termios with default values, perhaps like pangoterm:
65 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar423802d2017-07-30 16:52:24 +020066 * - if the job in the terminal does not support the mouse, we can use the
67 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020068 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
69 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020070 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020071 * - Copy text in the vterm to the Vim buffer once in a while, so that
72 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020073 */
74
75#include "vim.h"
76
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020077#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078
Bram Moolenaard5310982017-08-05 15:16:32 +020079#ifndef MIN
80# define MIN(x,y) ((x) < (y) ? (x) : (y))
81#endif
82#ifndef MAX
83# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020084#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020085
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020086#include "libvterm/include/vterm.h"
87
Bram Moolenaar33a43be2017-08-06 21:36:22 +020088/* This is VTermScreenCell without the characters, thus much smaller. */
89typedef struct {
90 VTermScreenCellAttrs attrs;
91 char width;
92 VTermColor fg, bg;
93} cellattr_T;
94
Bram Moolenaard85f2712017-07-28 21:51:57 +020095typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020096 int sb_cols; /* can differ per line */
97 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020098} sb_line_T;
99
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200100/* typedef term_T in structs.h */
101struct terminal_S {
102 term_T *tl_next;
103
Bram Moolenaar423802d2017-07-30 16:52:24 +0200104 VTerm *tl_vterm;
105 job_T *tl_job;
106 buf_T *tl_buffer;
107
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200108 /* used when tl_job is NULL and only a pty was created */
109 int tl_tty_fd;
110 char_u *tl_tty_name;
111
Bram Moolenaar6d819742017-08-06 14:57:49 +0200112 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200113 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200114 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200115 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200116
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200117#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200118 void *tl_winpty_config;
119 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200120#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200121
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200122 /* last known vterm size */
123 int tl_rows;
124 int tl_cols;
125 /* vterm size does not follow window size */
126 int tl_rows_fixed;
127 int tl_cols_fixed;
128
Bram Moolenaar21554412017-07-24 21:44:43 +0200129 char_u *tl_title; /* NULL or allocated */
130 char_u *tl_status_text; /* NULL or allocated */
131
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200132 /* Range of screen rows to update. Zero based. */
133 int tl_dirty_row_start; /* -1 if nothing dirty */
134 int tl_dirty_row_end; /* row below last one to update */
135
Bram Moolenaard85f2712017-07-28 21:51:57 +0200136 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200137 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200138
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200139 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200140 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200141 int tl_cursor_blink;
142 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
143 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200144
145 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200146};
147
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200148#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
149#define TMODE_LOOP 2 /* CTRL-W N used */
150
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200151/*
152 * List of all active terminals.
153 */
154static term_T *first_term = NULL;
155
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200156/* Terminal active in terminal_loop(). */
157static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200158
159#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
160#define KEY_BUF_LEN 200
161
162/*
163 * Functions with separate implementation for MS-Windows and Unix-like systems.
164 */
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200165static int term_and_job_init(term_T *term, typval_T *argvar, jobopt_T *opt);
166static int create_pty_only(term_T *term, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200167static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200168static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200169
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200170/* The characters that we know (or assume) that the terminal expects for the
171 * backspace and enter keys. */
172static int term_backspace_char = BS;
173static int term_enter_char = CAR;
174static int term_nl_does_cr = FALSE;
175
176
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200177/**************************************
178 * 1. Generic code for all systems.
179 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200180
181/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200182 * Determine the terminal size from 'termsize' and the current window.
183 * Assumes term->tl_rows and term->tl_cols are zero.
184 */
185 static void
186set_term_and_win_size(term_T *term)
187{
188 if (*curwin->w_p_tms != NUL)
189 {
190 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
191
192 term->tl_rows = atoi((char *)curwin->w_p_tms);
193 term->tl_cols = atoi((char *)p);
194 }
195 if (term->tl_rows == 0)
196 term->tl_rows = curwin->w_height;
197 else
198 {
199 win_setheight_win(term->tl_rows, curwin);
200 term->tl_rows_fixed = TRUE;
201 }
202 if (term->tl_cols == 0)
203 term->tl_cols = curwin->w_width;
204 else
205 {
206 win_setwidth_win(term->tl_cols, curwin);
207 term->tl_cols_fixed = TRUE;
208 }
209}
210
211/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200212 * Initialize job options for a terminal job.
213 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200214 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200215 static void
216init_job_options(jobopt_T *opt)
217{
218 clear_job_options(opt);
219
220 opt->jo_mode = MODE_RAW;
221 opt->jo_out_mode = MODE_RAW;
222 opt->jo_err_mode = MODE_RAW;
223 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
224
225 opt->jo_io[PART_OUT] = JIO_BUFFER;
226 opt->jo_io[PART_ERR] = JIO_BUFFER;
227 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
228
229 opt->jo_modifiable[PART_OUT] = 0;
230 opt->jo_modifiable[PART_ERR] = 0;
231 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
232
233 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
234}
235
236/*
237 * Set job options mandatory for a terminal job.
238 */
239 static void
240setup_job_options(jobopt_T *opt, int rows, int cols)
241{
242 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
243 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
244 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200245 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
246 opt->jo_term_rows = rows;
247 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
248 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200249}
250
251 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200252term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200253{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254 exarg_T split_ea;
255 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200256 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200257 buf_T *old_curbuf = NULL;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200258 int res;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200259
260 if (check_restricted() || check_secure())
261 return;
262
263 term = (term_T *)alloc_clear(sizeof(term_T));
264 if (term == NULL)
265 return;
266 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200267 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200268 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200269 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200270 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200271
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200272 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200273 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200274 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200275 /* Create a new buffer in the current window. */
276 if (!can_abandon(curbuf, forceit))
277 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200278 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200279 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200280 return;
281 }
282 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
283 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200284 {
285 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200286 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200287 }
288 }
289 else if (opt->jo_hidden)
290 {
291 buf_T *buf;
292
293 /* Create a new buffer without a window. Make it the current buffer for
294 * a moment to be able to do the initialisations. */
295 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
296 BLN_NEW | BLN_LISTED);
297 if (buf == NULL || ml_open(buf) == FAIL)
298 {
299 vim_free(term);
300 return;
301 }
302 old_curbuf = curbuf;
303 --curbuf->b_nwindows;
304 curbuf = buf;
305 curwin->w_buffer = buf;
306 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200307 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200308 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200309 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200310 /* Open a new window or tab. */
311 split_ea.cmdidx = CMD_new;
312 split_ea.cmd = (char_u *)"new";
313 split_ea.arg = (char_u *)"";
314 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
315 {
316 split_ea.line2 = opt->jo_term_rows;
317 split_ea.addr_count = 1;
318 }
319 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
320 {
321 split_ea.line2 = opt->jo_term_cols;
322 split_ea.addr_count = 1;
323 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200324
Bram Moolenaarda43b612017-08-11 22:27:50 +0200325 ex_splitview(&split_ea);
326 if (curwin == old_curwin)
327 {
328 /* split failed */
329 vim_free(term);
330 return;
331 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200332 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200333 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200334 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200335
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200336 if (!opt->jo_hidden)
337 {
338 /* only one size was taken care of with :new, do the other one */
339 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
340 win_setheight(opt->jo_term_rows);
341 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
342 win_setwidth(opt->jo_term_cols);
343 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200344
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200345 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200346 term->tl_next = first_term;
347 first_term = term;
348
Bram Moolenaar78712a72017-08-05 14:50:12 +0200349 if (opt->jo_term_name != NULL)
350 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
351 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200352 {
353 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200354 size_t len;
355 char_u *cmd, *p;
356
357 if (argvar->v_type == VAR_STRING)
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200358 {
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200359 cmd = argvar->vval.v_string;
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200360 if (cmd == NULL)
361 cmd = (char_u *)"";
362 else if (STRCMP(cmd, "NONE") == 0)
363 cmd = (char_u *)"pty";
364 }
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200365 else if (argvar->v_type != VAR_LIST
366 || argvar->vval.v_list == NULL
367 || argvar->vval.v_list->lv_len < 1)
368 cmd = (char_u*)"";
369 else
370 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
371
372 len = STRLEN(cmd) + 10;
373 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200374
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200375 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200376 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200377 /* Prepend a ! to the command name to avoid the buffer name equals
378 * the executable, otherwise ":w!" would overwrite it. */
379 if (i == 0)
380 vim_snprintf((char *)p, len, "!%s", cmd);
381 else
382 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200383 if (buflist_findname(p) == NULL)
384 {
385 curbuf->b_ffname = p;
386 break;
387 }
388 }
389 }
390 curbuf->b_fname = curbuf->b_ffname;
391
Bram Moolenaar37c45832017-08-12 16:01:04 +0200392 if (opt->jo_term_opencmd != NULL)
393 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
394
Bram Moolenaareb44a682017-08-03 22:44:55 +0200395 set_string_option_direct((char_u *)"buftype", -1,
396 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
397
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200398 /* Mark the buffer as not modifiable. It can only be made modifiable after
399 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200400 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200401
402 /* Set 'bufhidden' to "hide": allow closing the window. */
403 set_string_option_direct((char_u *)"bufhidden", -1,
404 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200405
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200406 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200407 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200408
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200409 /* System dependent: setup the vterm and maybe start the job in it. */
410 if (argvar->v_type == VAR_STRING
411 && argvar->vval.v_string != NULL
412 && STRCMP(argvar->vval.v_string, "NONE") == 0)
413 res = create_pty_only(term, opt);
414 else
415 res = term_and_job_init(term, argvar, opt);
416
417 if (res == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200418 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200419 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200420 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200421 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200422
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200423 /* Make sure we don't get stuck on sending keys to the job, it leads to
424 * a deadlock if the job is waiting for Vim to read. */
425 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
426
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200427 if (old_curbuf != NULL)
428 {
429 --curbuf->b_nwindows;
430 curbuf = old_curbuf;
431 curwin->w_buffer = curbuf;
432 ++curbuf->b_nwindows;
433 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200434 }
435 else
436 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200437 buf_T *buf = curbuf;
438
Bram Moolenaard85f2712017-07-28 21:51:57 +0200439 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200440 if (old_curbuf != NULL)
441 {
442 --curbuf->b_nwindows;
443 curbuf = old_curbuf;
444 curwin->w_buffer = curbuf;
445 ++curbuf->b_nwindows;
446 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200447
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200448 /* Wiping out the buffer will also close the window and call
449 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200450 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200451 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200452}
453
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200454/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200455 * ":terminal": open a terminal window and execute a job in it.
456 */
457 void
458ex_terminal(exarg_T *eap)
459{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200460 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200461 jobopt_T opt;
462 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200463 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200464
465 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200466
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200467 cmd = eap->arg;
468 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
469 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200470 char_u *p, *ep;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200471
472 cmd += 2;
473 p = skiptowhite(cmd);
Bram Moolenaarb2412082017-08-20 18:09:14 +0200474 ep = vim_strchr(cmd, '=');
475 if (ep != NULL && ep < p)
476 p = ep;
477
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200478 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
479 opt.jo_term_finish = 'c';
480 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
481 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200482 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
483 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200484 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
485 opt.jo_hidden = 1;
Bram Moolenaarb2412082017-08-20 18:09:14 +0200486 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
487 && ep != NULL && isdigit(ep[1]))
488 {
489 opt.jo_set2 |= JO2_TERM_ROWS;
490 opt.jo_term_rows = atoi((char *)ep + 1);
491 p = skiptowhite(cmd);
492 }
493 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
494 && ep != NULL && isdigit(ep[1]))
495 {
496 opt.jo_set2 |= JO2_TERM_COLS;
497 opt.jo_term_cols = atoi((char *)ep + 1);
498 p = skiptowhite(cmd);
499 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200500 else
501 {
502 if (*p)
503 *p = NUL;
504 EMSG2(_("E181: Invalid attribute: %s"), cmd);
505 return;
506 }
507 cmd = skipwhite(p);
508 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200509 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200510 /* Make a copy, an autocommand may set 'shell'. */
511 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200512
Bram Moolenaarb2412082017-08-20 18:09:14 +0200513 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200514 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200515 /* Write lines from current buffer to the job. */
516 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
517 opt.jo_io[PART_IN] = JIO_BUFFER;
518 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
519 opt.jo_in_top = eap->line1;
520 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200521 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200522
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200523 argvar.v_type = VAR_STRING;
524 argvar.vval.v_string = cmd;
525 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200526 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200527}
528
529/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200530 * Free the scrollback buffer for "term".
531 */
532 static void
533free_scrollback(term_T *term)
534{
535 int i;
536
537 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
538 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
539 ga_clear(&term->tl_scrollback);
540}
541
542/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200543 * Free a terminal and everything it refers to.
544 * Kills the job if there is one.
545 * Called when wiping out a buffer.
546 */
547 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200548free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200549{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200550 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200551 term_T *tp;
552
553 if (term == NULL)
554 return;
555 if (first_term == term)
556 first_term = term->tl_next;
557 else
558 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
559 if (tp->tl_next == term)
560 {
561 tp->tl_next = term->tl_next;
562 break;
563 }
564
565 if (term->tl_job != NULL)
566 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200567 if (term->tl_job->jv_status != JOB_ENDED
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200568 && term->tl_job->jv_status != JOB_FINISHED
569 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200570 job_stop(term->tl_job, NULL, "kill");
571 job_unref(term->tl_job);
572 }
573
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200574 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200575
576 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200577 vim_free(term->tl_title);
578 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200579 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200580 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200581 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200582 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200583 if (in_terminal_loop == term)
584 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200585}
586
587/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200588 * Write job output "msg[len]" to the vterm.
589 */
590 static void
591term_write_job_output(term_T *term, char_u *msg, size_t len)
592{
593 VTerm *vterm = term->tl_vterm;
594 char_u *p;
595 size_t done;
596 size_t len_now;
597
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200598 if (term_nl_does_cr)
599 vterm_input_write(vterm, (char *)msg, len);
600 else
601 /* need to convert NL to CR-NL */
602 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200603 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200604 for (p = msg + done; p < msg + len; )
605 {
606 if (*p == NL)
607 break;
608 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
609 }
610 len_now = p - msg - done;
611 vterm_input_write(vterm, (char *)msg + done, len_now);
612 if (p < msg + len && *p == NL)
613 {
614 vterm_input_write(vterm, "\r\n", 2);
615 ++len_now;
616 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200617 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200618
619 /* this invokes the damage callbacks */
620 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
621}
622
Bram Moolenaar1c844932017-07-24 23:36:41 +0200623 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200624update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200625{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200626 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200627 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200628 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200629 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200630 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200631 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200632 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200633 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200634#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200635 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200636 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200637#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200638 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200639}
640
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200641/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200642 * Invoked when "msg" output from a job was received. Write it to the terminal
643 * of "buffer".
644 */
645 void
646write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
647{
648 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200649 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200650
Bram Moolenaard85f2712017-07-28 21:51:57 +0200651 if (term->tl_vterm == NULL)
652 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200653 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200654 return;
655 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200656 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200657 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200658
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200659 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
660 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200661 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200662 {
663 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200664 ch_log(term->tl_job->jv_channel, "updating screen");
665 if (buffer == curbuf)
666 {
667 update_screen(0);
668 update_cursor(term, TRUE);
669 }
670 else
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200671 redraw_after_callback(TRUE);
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200672 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200673}
674
675/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200676 * Send a mouse position and click to the vterm
677 */
678 static int
679term_send_mouse(VTerm *vterm, int button, int pressed)
680{
681 VTermModifier mod = VTERM_MOD_NONE;
682
683 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
684 mouse_col - W_WINCOL(curwin), mod);
685 vterm_mouse_button(vterm, button, pressed, mod);
686 return TRUE;
687}
688
689/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200690 * Convert typed key "c" into bytes to send to the job.
691 * Return the number of bytes in "buf".
692 */
693 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200694term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200695{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200696 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200697 VTermKey key = VTERM_KEY_NONE;
698 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200699 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200700
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200701 switch (c)
702 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200703 case CAR: c = term_enter_char; break;
704 /* don't use VTERM_KEY_BACKSPACE, it always
705 * becomes 0x7f DEL */
706 case K_BS: c = term_backspace_char; break;
707
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200708 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200709 case K_DEL: key = VTERM_KEY_DEL; break;
710 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200711 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
712 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200713 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200714 case K_S_END: mod = VTERM_MOD_SHIFT;
715 key = VTERM_KEY_END; break;
716 case K_C_END: mod = VTERM_MOD_CTRL;
717 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200718 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
719 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
720 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
721 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
722 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
723 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
724 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
725 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
726 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
727 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
728 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
729 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
730 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200731 case K_S_HOME: mod = VTERM_MOD_SHIFT;
732 key = VTERM_KEY_HOME; break;
733 case K_C_HOME: mod = VTERM_MOD_CTRL;
734 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200735 case K_INS: key = VTERM_KEY_INS; break;
736 case K_K0: key = VTERM_KEY_KP_0; break;
737 case K_K1: key = VTERM_KEY_KP_1; break;
738 case K_K2: key = VTERM_KEY_KP_2; break;
739 case K_K3: key = VTERM_KEY_KP_3; break;
740 case K_K4: key = VTERM_KEY_KP_4; break;
741 case K_K5: key = VTERM_KEY_KP_5; break;
742 case K_K6: key = VTERM_KEY_KP_6; break;
743 case K_K7: key = VTERM_KEY_KP_7; break;
744 case K_K8: key = VTERM_KEY_KP_8; break;
745 case K_K9: key = VTERM_KEY_KP_9; break;
746 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
747 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
748 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
749 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
750 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
751 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
752 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
753 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
754 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
755 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
756 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
757 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
758 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200759 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
760 key = VTERM_KEY_LEFT; break;
761 case K_C_LEFT: mod = VTERM_MOD_CTRL;
762 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200763 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
764 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
765 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200766 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
767 key = VTERM_KEY_RIGHT; break;
768 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
769 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200770 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200771 case K_S_UP: mod = VTERM_MOD_SHIFT;
772 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200773 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200774
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200775 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
776 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
777 case K_MOUSELEFT: /* TODO */ return 0;
778 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200779
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200780 case K_LEFTMOUSE:
781 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
782 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
783 case K_LEFTRELEASE:
784 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
785 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
786 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
787 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
788 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
789 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
790 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
791 case K_X1MOUSE: /* TODO */ return 0;
792 case K_X1DRAG: /* TODO */ return 0;
793 case K_X1RELEASE: /* TODO */ return 0;
794 case K_X2MOUSE: /* TODO */ return 0;
795 case K_X2DRAG: /* TODO */ return 0;
796 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200797
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200798 case K_IGNORE: return 0;
799 case K_NOP: return 0;
800 case K_UNDO: return 0;
801 case K_HELP: return 0;
802 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
803 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
804 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
805 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
806 case K_SELECT: return 0;
807#ifdef FEAT_GUI
808 case K_VER_SCROLLBAR: return 0;
809 case K_HOR_SCROLLBAR: return 0;
810#endif
811#ifdef FEAT_GUI_TABLINE
812 case K_TABLINE: return 0;
813 case K_TABMENU: return 0;
814#endif
815#ifdef FEAT_NETBEANS_INTG
816 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
817#endif
818#ifdef FEAT_DND
819 case K_DROP: return 0;
820#endif
821#ifdef FEAT_AUTOCMD
822 case K_CURSORHOLD: return 0;
823#endif
824 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
825 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200826 }
827
828 /*
829 * Convert special keys to vterm keys:
830 * - Write keys to vterm: vterm_keyboard_key()
831 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200832 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200833 */
834 if (key != VTERM_KEY_NONE)
835 /* Special key, let vterm convert it. */
836 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200837 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200838 /* Normal character, let vterm convert it. */
839 vterm_keyboard_unichar(vterm, c, mod);
840
841 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200842 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200843}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200844
Bram Moolenaar938783d2017-07-16 20:13:26 +0200845/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200846 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200847 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200848 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200849term_job_running(term_T *term)
850{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200851 /* Also consider the job finished when the channel is closed, to avoid a
852 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200853 return term != NULL
854 && term->tl_job != NULL
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200855 && channel_is_open(term->tl_job->jv_channel)
856 && (term->tl_job->jv_status == JOB_STARTED
857 || term->tl_job->jv_channel->ch_keep_open);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200858}
859
860/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200861 * Add the last line of the scrollback buffer to the buffer in the window.
862 */
863 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200864add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200865{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200866 buf_T *buf = term->tl_buffer;
867 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
868 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200869
Bram Moolenaar58302322017-08-22 20:33:53 +0200870#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +0200871 if (!enc_utf8 && enc_codepage > 0)
872 {
873 WCHAR *ret = NULL;
874 int length = 0;
875
876 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
877 &ret, &length);
878 if (ret != NULL)
879 {
880 WideCharToMultiByte_alloc(enc_codepage, 0,
881 ret, length, (char **)&text, &len, 0, 0);
882 vim_free(ret);
883 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
884 vim_free(text);
885 }
886 }
887 else
888#endif
889 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200890 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200891 {
892 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200893 curbuf = buf;
894 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200895 curbuf = curwin->w_buffer;
896 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200897}
898
899/*
900 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200901 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200902 */
903 static void
904move_terminal_to_buffer(term_T *term)
905{
906 win_T *wp;
907 int len;
908 int lines_skipped = 0;
909 VTermPos pos;
910 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200911 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200912 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200913
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200914 if (term->tl_vterm == NULL)
915 return;
916 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200917 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
918 {
919 len = 0;
920 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
921 if (vterm_screen_get_cell(screen, pos, &cell) != 0
922 && cell.chars[0] != NUL)
923 len = pos.col + 1;
924
925 if (len == 0)
926 ++lines_skipped;
927 else
928 {
929 while (lines_skipped > 0)
930 {
931 /* Line was skipped, add an empty line. */
932 --lines_skipped;
933 if (ga_grow(&term->tl_scrollback, 1) == OK)
934 {
935 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
936 + term->tl_scrollback.ga_len;
937
938 line->sb_cols = 0;
939 line->sb_cells = NULL;
940 ++term->tl_scrollback.ga_len;
941
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200942 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200943 }
944 }
945
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200946 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200947 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
948 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200949 garray_T ga;
950 int width;
951 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200952 + term->tl_scrollback.ga_len;
953
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200954 ga_init2(&ga, 1, 100);
955 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200956 {
957 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200958 {
959 width = 1;
960 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
961 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +0200962 ga.ga_len += utf_char2bytes(' ',
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200963 (char_u *)ga.ga_data + ga.ga_len);
964 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200965 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200966 {
967 width = cell.width;
968
969 p[pos.col].width = cell.width;
970 p[pos.col].attrs = cell.attrs;
971 p[pos.col].fg = cell.fg;
972 p[pos.col].bg = cell.bg;
973
974 if (ga_grow(&ga, MB_MAXBYTES) == OK)
975 {
976 int i;
977 int c;
978
979 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +0200980 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200981 (char_u *)ga.ga_data + ga.ga_len);
982 }
983 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200984 }
985 line->sb_cols = len;
986 line->sb_cells = p;
987 ++term->tl_scrollback.ga_len;
988
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200989 if (ga_grow(&ga, 1) == FAIL)
990 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
991 else
992 {
993 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
994 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
995 }
996 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200997 }
998 else
999 vim_free(p);
1000 }
1001 }
1002
1003 FOR_ALL_WINDOWS(wp)
1004 {
1005 if (wp->w_buffer == term->tl_buffer)
1006 {
1007 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1008 wp->w_cursor.col = 0;
1009 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +02001010 if (wp->w_cursor.lnum >= wp->w_height)
1011 {
1012 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
1013
1014 if (wp->w_topline < min_topline)
1015 wp->w_topline = min_topline;
1016 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02001017 redraw_win_later(wp, NOT_VALID);
1018 }
1019 }
1020}
1021
1022 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001023set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001024{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001025 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001026 vim_free(term->tl_status_text);
1027 term->tl_status_text = NULL;
1028 if (term->tl_buffer == curbuf)
1029 maketitle();
1030}
1031
1032/*
1033 * Called after the job if finished and Terminal mode is not active:
1034 * Move the vterm contents into the scrollback buffer and free the vterm.
1035 */
1036 static void
1037cleanup_vterm(term_T *term)
1038{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001039 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001040 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001041 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001042 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001043}
1044
1045/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001046 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001047 * Suspends updating the terminal window.
1048 */
1049 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001050term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001051{
1052 term_T *term = curbuf->b_term;
1053
1054 /* Append the current terminal contents to the buffer. */
1055 move_terminal_to_buffer(term);
1056
Bram Moolenaar6d819742017-08-06 14:57:49 +02001057 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001058
Bram Moolenaar6d819742017-08-06 14:57:49 +02001059 /* Move the window cursor to the position of the cursor in the
1060 * terminal. */
1061 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1062 + term->tl_cursor_pos.row + 1;
1063 check_cursor();
1064 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001065
Bram Moolenaar6d819742017-08-06 14:57:49 +02001066 /* Display the same lines as in the terminal. */
1067 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001068}
1069
1070/*
1071 * Returns TRUE if the current window contains a terminal and we are in
1072 * Terminal-Normal mode.
1073 */
1074 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001075term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001076{
1077 term_T *term = curbuf->b_term;
1078
Bram Moolenaar6d819742017-08-06 14:57:49 +02001079 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001080}
1081
1082/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001083 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001084 * Restores updating the terminal window.
1085 */
1086 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001087term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001088{
1089 term_T *term = curbuf->b_term;
1090 sb_line_T *line;
1091 garray_T *gap;
1092
1093 /* Remove the terminal contents from the scrollback and the buffer. */
1094 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001095 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1096 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001097 {
1098 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1099 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1100 vim_free(line->sb_cells);
1101 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001102 }
1103 check_cursor();
1104
Bram Moolenaar6d819742017-08-06 14:57:49 +02001105 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001106
1107 if (term->tl_channel_closed)
1108 cleanup_vterm(term);
1109 redraw_buf_and_status_later(curbuf, NOT_VALID);
1110}
1111
1112/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001113 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001114 * Note: while waiting a terminal may be closed and freed if the channel is
1115 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001116 * TODO: use terminal mode mappings.
1117 */
1118 static int
1119term_vgetc()
1120{
1121 int c;
1122
1123 ++no_mapping;
1124 ++allow_keys;
1125 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001126#ifdef WIN3264
1127 ctrl_break_was_pressed = FALSE;
1128#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001129 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001130 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001131 --no_mapping;
1132 --allow_keys;
1133 return c;
1134}
1135
1136/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001137 * Get the part that is connected to the tty. Normally this is PART_IN, but
1138 * when writing buffer lines to the job it can be another. This makes it
1139 * possible to do "1,5term vim -".
1140 */
1141 static ch_part_T
1142get_tty_part(term_T *term)
1143{
1144#ifdef UNIX
1145 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1146 int i;
1147
1148 for (i = 0; i < 3; ++i)
1149 {
1150 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1151
1152 if (isatty(fd))
1153 return parts[i];
1154 }
1155#endif
1156 return PART_IN;
1157}
1158
1159/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001160 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001161 * Return FAIL when the key needs to be handled in Normal mode.
1162 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001163 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001164 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001165send_keys_to_term(term_T *term, int c, int typed)
1166{
1167 char msg[KEY_BUF_LEN];
1168 size_t len;
1169 static int mouse_was_outside = FALSE;
1170 int dragging_outside = FALSE;
1171
1172 /* Catch keys that need to be handled as in Normal mode. */
1173 switch (c)
1174 {
1175 case NUL:
1176 case K_ZERO:
1177 if (typed)
1178 stuffcharReadbuff(c);
1179 return FAIL;
1180
1181 case K_IGNORE:
1182 return FAIL;
1183
1184 case K_LEFTDRAG:
1185 case K_MIDDLEDRAG:
1186 case K_RIGHTDRAG:
1187 case K_X1DRAG:
1188 case K_X2DRAG:
1189 dragging_outside = mouse_was_outside;
1190 /* FALLTHROUGH */
1191 case K_LEFTMOUSE:
1192 case K_LEFTMOUSE_NM:
1193 case K_LEFTRELEASE:
1194 case K_LEFTRELEASE_NM:
1195 case K_MIDDLEMOUSE:
1196 case K_MIDDLERELEASE:
1197 case K_RIGHTMOUSE:
1198 case K_RIGHTRELEASE:
1199 case K_X1MOUSE:
1200 case K_X1RELEASE:
1201 case K_X2MOUSE:
1202 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001203
1204 case K_MOUSEUP:
1205 case K_MOUSEDOWN:
1206 case K_MOUSELEFT:
1207 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001208 if (mouse_row < W_WINROW(curwin)
1209 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1210 || mouse_col < W_WINCOL(curwin)
1211 || mouse_col >= W_ENDCOL(curwin)
1212 || dragging_outside)
1213 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001214 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001215 if (typed)
1216 {
1217 stuffcharReadbuff(c);
1218 mouse_was_outside = TRUE;
1219 }
1220 return FAIL;
1221 }
1222 }
1223 if (typed)
1224 mouse_was_outside = FALSE;
1225
1226 /* Convert the typed key to a sequence of bytes for the job. */
1227 len = term_convert_key(term, c, msg);
1228 if (len > 0)
1229 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001230 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1231 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001232
1233 return OK;
1234}
1235
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001236 static void
1237position_cursor(win_T *wp, VTermPos *pos)
1238{
1239 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1240 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1241 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1242}
1243
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001244/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001245 * Handle CTRL-W "": send register contents to the job.
1246 */
1247 static void
1248term_paste_register(int prev_c UNUSED)
1249{
1250 int c;
1251 list_T *l;
1252 listitem_T *item;
1253 long reglen = 0;
1254 int type;
1255
1256#ifdef FEAT_CMDL_INFO
1257 if (add_to_showcmd(prev_c))
1258 if (add_to_showcmd('"'))
1259 out_flush();
1260#endif
1261 c = term_vgetc();
1262#ifdef FEAT_CMDL_INFO
1263 clear_showcmd();
1264#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001265 if (!term_use_loop())
1266 /* job finished while waiting for a character */
1267 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001268
1269 /* CTRL-W "= prompt for expression to evaluate. */
1270 if (c == '=' && get_expr_register() != '=')
1271 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001272 if (!term_use_loop())
1273 /* job finished while waiting for a character */
1274 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001275
1276 l = (list_T *)get_reg_contents(c, GREG_LIST);
1277 if (l != NULL)
1278 {
1279 type = get_reg_type(c, &reglen);
1280 for (item = l->lv_first; item != NULL; item = item->li_next)
1281 {
1282 char_u *s = get_tv_string(&item->li_tv);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001283#ifdef WIN3264
1284 char_u *tmp = s;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001285
Bram Moolenaar285f2432017-08-23 23:10:21 +02001286 if (!enc_utf8 && enc_codepage > 0)
1287 {
1288 WCHAR *ret = NULL;
1289 int length = 0;
1290
1291 MultiByteToWideChar_alloc(enc_codepage, 0, (char*)s, STRLEN(s),
1292 &ret, &length);
1293 if (ret != NULL)
1294 {
1295 WideCharToMultiByte_alloc(CP_UTF8, 0,
1296 ret, length, (char **)&s, &length, 0, 0);
1297 vim_free(ret);
1298 }
1299 }
1300#endif
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001301 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1302 s, STRLEN(s), NULL);
Bram Moolenaar285f2432017-08-23 23:10:21 +02001303#ifdef WIN3264
1304 if (tmp != s)
1305 vim_free(s);
1306#endif
1307
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001308 if (item->li_next != NULL || type == MLINE)
1309 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1310 (char_u *)"\r", 1, NULL);
1311 }
1312 list_free(l);
1313 }
1314}
1315
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001316#if defined(FEAT_GUI) || defined(PROTO)
1317/*
1318 * Return TRUE when the cursor of the terminal should be displayed.
1319 */
1320 int
1321use_terminal_cursor()
1322{
1323 return in_terminal_loop != NULL;
1324}
1325
1326 cursorentry_T *
1327term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1328{
1329 term_T *term = in_terminal_loop;
1330 static cursorentry_T entry;
1331
1332 vim_memset(&entry, 0, sizeof(entry));
1333 entry.shape = entry.mshape =
1334 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1335 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1336 SHAPE_BLOCK;
1337 entry.percentage = 20;
1338 if (term->tl_cursor_blink)
1339 {
1340 entry.blinkwait = 700;
1341 entry.blinkon = 400;
Bram Moolenaar58302322017-08-22 20:33:53 +02001342 entry.blinkoff = 250;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001343 }
1344 *fg = gui.back_pixel;
1345 if (term->tl_cursor_color == NULL)
1346 *bg = gui.norm_pixel;
1347 else
1348 *bg = color_name2handle(term->tl_cursor_color);
1349 entry.name = "n";
1350 entry.used_for = SHAPE_CURSOR;
1351
1352 return &entry;
1353}
1354#endif
1355
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001356static int did_change_cursor = FALSE;
1357
1358 static void
1359may_set_cursor_props(term_T *term)
1360{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001361#ifdef FEAT_GUI
1362 /* For the GUI the cursor properties are obtained with
1363 * term_get_cursor_shape(). */
1364 if (gui.in_use)
1365 return;
1366#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001367 if (in_terminal_loop == term)
1368 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001369 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001370 if (term->tl_cursor_color != NULL)
1371 term_cursor_color(term->tl_cursor_color);
1372 else
1373 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001374 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1375 }
1376}
1377
1378 static void
1379may_restore_cursor_props(void)
1380{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001381#ifdef FEAT_GUI
1382 if (gui.in_use)
1383 return;
1384#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001385 if (did_change_cursor)
1386 {
1387 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001388 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001389 /* this will restore the initial cursor style, if possible */
1390 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001391 }
1392}
1393
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001394/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001395 * Returns TRUE if the current window contains a terminal and we are sending
1396 * keys to the job.
1397 */
1398 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001399term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001400{
1401 term_T *term = curbuf->b_term;
1402
1403 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001404 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001405 && term->tl_vterm != NULL
1406 && term_job_running(term);
1407}
1408
1409/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001410 * Wait for input and send it to the job.
1411 * Return when the start of a CTRL-W command is typed or anything else that
1412 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001413 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1414 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001415 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001416 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001417terminal_loop(void)
1418{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001419 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001420 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001421 int ret;
1422
Bram Moolenaar679653e2017-08-13 14:13:19 +02001423 /* Remember the terminal we are sending keys to. However, the terminal
1424 * might be closed while waiting for a character, e.g. typing "exit" in a
1425 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1426 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001427 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001428
1429 if (*curwin->w_p_tk != NUL)
1430 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001431 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001432 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001433
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001434#ifdef UNIX
1435 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001436 int part = get_tty_part(curbuf->b_term);
1437 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001438
1439 if (isatty(fd))
1440 {
1441 ttyinfo_T info;
1442
1443 /* Get the current backspace and enter characters of the pty. */
1444 if (get_tty_info(fd, &info) == OK)
1445 {
1446 term_backspace_char = info.backspace;
1447 term_enter_char = info.enter;
1448 term_nl_does_cr = info.nl_does_cr;
1449 }
1450 }
1451 }
1452#endif
1453
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001454 for (;;)
1455 {
1456 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001457 /* Repeat redrawing in case a message is received while redrawing. */
1458 while (curwin->w_redr_type != 0)
1459 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001460 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001461
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001462 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001463 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001464 /* job finished while waiting for a character */
1465 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001466 if (c == K_IGNORE)
1467 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001468
Bram Moolenaarfae42832017-08-01 22:24:26 +02001469#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001470 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001471 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001472 if (ctrl_break_was_pressed)
1473 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001474#endif
1475
Bram Moolenaar69198192017-08-05 14:10:48 +02001476 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001477 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001478 int prev_c = c;
1479
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001480#ifdef FEAT_CMDL_INFO
1481 if (add_to_showcmd(c))
1482 out_flush();
1483#endif
1484 c = term_vgetc();
1485#ifdef FEAT_CMDL_INFO
1486 clear_showcmd();
1487#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001488 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001489 /* job finished while waiting for a character */
1490 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001491
Bram Moolenaar69198192017-08-05 14:10:48 +02001492 if (prev_c == Ctrl_BSL)
1493 {
1494 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001495 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001496 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1497 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001498 ret = FAIL;
1499 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001500 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001501 /* Send both keys to the terminal. */
1502 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1503 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001504 else if (c == Ctrl_C)
1505 {
1506 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1507 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1508 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001509 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001510 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001511 /* "CTRL-W .": send CTRL-W to the job */
1512 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001513 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001514 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001515 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001516 /* CTRL-W N : go to Terminal-Normal mode. */
1517 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001518 ret = FAIL;
1519 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001520 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001521 else if (c == '"')
1522 {
1523 term_paste_register(prev_c);
1524 continue;
1525 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001526 else if (termkey == 0 || c != termkey)
1527 {
1528 stuffcharReadbuff(Ctrl_W);
1529 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001530 ret = OK;
1531 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001532 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001533 }
Bram Moolenaar58302322017-08-22 20:33:53 +02001534# ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02001535 if (!enc_utf8 && has_mbyte && c >= 0x80)
1536 {
1537 WCHAR wc;
1538 char_u mb[3];
1539
1540 mb[0] = (unsigned)c >> 8;
1541 mb[1] = c;
1542 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1543 c = wc;
1544 }
1545# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001546 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001547 {
1548 ret = OK;
1549 goto theend;
1550 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001551 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001552 ret = FAIL;
1553
1554theend:
1555 in_terminal_loop = NULL;
1556 may_restore_cursor_props();
1557 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001558}
1559
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001560/*
1561 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001562 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001563 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001564 */
1565 void
1566term_job_ended(job_T *job)
1567{
1568 term_T *term;
1569 int did_one = FALSE;
1570
1571 for (term = first_term; term != NULL; term = term->tl_next)
1572 if (term->tl_job == job)
1573 {
1574 vim_free(term->tl_title);
1575 term->tl_title = NULL;
1576 vim_free(term->tl_status_text);
1577 term->tl_status_text = NULL;
1578 redraw_buf_and_status_later(term->tl_buffer, VALID);
1579 did_one = TRUE;
1580 }
1581 if (did_one)
1582 redraw_statuslines();
1583 if (curbuf->b_term != NULL)
1584 {
1585 if (curbuf->b_term->tl_job == job)
1586 maketitle();
1587 update_cursor(curbuf->b_term, TRUE);
1588 }
1589}
1590
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001591 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001592may_toggle_cursor(term_T *term)
1593{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001594 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001595 {
1596 if (term->tl_cursor_visible)
1597 cursor_on();
1598 else
1599 cursor_off();
1600 }
1601}
1602
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001603/*
1604 * Reverse engineer the RGB value into a cterm color index.
1605 * First color is 1. Return 0 if no match found.
1606 */
1607 static int
1608color2index(VTermColor *color, int fg, int *boldp)
1609{
1610 int red = color->red;
1611 int blue = color->blue;
1612 int green = color->green;
1613
1614 /* The argument for lookup_color() is for the color_names[] table. */
1615 if (red == 0)
1616 {
1617 if (green == 0)
1618 {
1619 if (blue == 0)
1620 return lookup_color(0, fg, boldp) + 1; /* black */
1621 if (blue == 224)
1622 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1623 }
1624 else if (green == 224)
1625 {
1626 if (blue == 0)
1627 return lookup_color(2, fg, boldp) + 1; /* dark green */
1628 if (blue == 224)
1629 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1630 }
1631 }
1632 else if (red == 224)
1633 {
1634 if (green == 0)
1635 {
1636 if (blue == 0)
1637 return lookup_color(4, fg, boldp) + 1; /* dark red */
1638 if (blue == 224)
1639 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1640 }
1641 else if (green == 224)
1642 {
1643 if (blue == 0)
1644 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1645 if (blue == 224)
1646 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1647 }
1648 }
1649 else if (red == 128)
1650 {
1651 if (green == 128 && blue == 128)
1652 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1653 }
1654 else if (red == 255)
1655 {
1656 if (green == 64)
1657 {
1658 if (blue == 64)
1659 return lookup_color(20, fg, boldp) + 1; /* light red */
1660 if (blue == 255)
1661 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1662 }
1663 else if (green == 255)
1664 {
1665 if (blue == 64)
1666 return lookup_color(24, fg, boldp) + 1; /* yellow */
1667 if (blue == 255)
1668 return lookup_color(26, fg, boldp) + 1; /* white */
1669 }
1670 }
1671 else if (red == 64)
1672 {
1673 if (green == 64)
1674 {
1675 if (blue == 255)
1676 return lookup_color(14, fg, boldp) + 1; /* light blue */
1677 }
1678 else if (green == 255)
1679 {
1680 if (blue == 64)
1681 return lookup_color(16, fg, boldp) + 1; /* light green */
1682 if (blue == 255)
1683 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1684 }
1685 }
1686 if (t_colors >= 256)
1687 {
1688 if (red == blue && red == green)
1689 {
1690 /* 24-color greyscale */
1691 static int cutoff[23] = {
1692 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1693 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1694 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1695 int i;
1696
1697 for (i = 0; i < 23; ++i)
1698 if (red < cutoff[i])
1699 return i + 233;
1700 return 256;
1701 }
1702
1703 /* 216-color cube */
1704 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001705 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001706 + (blue + 25) / 0x33;
1707 }
1708 return 0;
1709}
1710
1711/*
1712 * Convert the attributes of a vterm cell into an attribute index.
1713 */
1714 static int
1715cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1716{
1717 int attr = 0;
1718
1719 if (cellattrs.bold)
1720 attr |= HL_BOLD;
1721 if (cellattrs.underline)
1722 attr |= HL_UNDERLINE;
1723 if (cellattrs.italic)
1724 attr |= HL_ITALIC;
1725 if (cellattrs.strike)
1726 attr |= HL_STANDOUT;
1727 if (cellattrs.reverse)
1728 attr |= HL_INVERSE;
1729
1730#ifdef FEAT_GUI
1731 if (gui.in_use)
1732 {
1733 guicolor_T fg, bg;
1734
1735 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1736 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1737 return get_gui_attr_idx(attr, fg, bg);
1738 }
1739 else
1740#endif
1741#ifdef FEAT_TERMGUICOLORS
1742 if (p_tgc)
1743 {
1744 guicolor_T fg, bg;
1745
1746 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1747 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1748
1749 return get_tgc_attr_idx(attr, fg, bg);
1750 }
1751 else
1752#endif
1753 {
1754 int bold = MAYBE;
1755 int fg = color2index(&cellfg, TRUE, &bold);
1756 int bg = color2index(&cellbg, FALSE, &bold);
1757
1758 /* with 8 colors set the bold attribute to get a bright foreground */
1759 if (bold == TRUE)
1760 attr |= HL_BOLD;
1761 return get_cterm_attr_idx(attr, fg, bg);
1762 }
1763 return 0;
1764}
1765
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001766 static int
1767handle_damage(VTermRect rect, void *user)
1768{
1769 term_T *term = (term_T *)user;
1770
1771 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1772 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1773 redraw_buf_later(term->tl_buffer, NOT_VALID);
1774 return 1;
1775}
1776
1777 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001778handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001779{
1780 term_T *term = (term_T *)user;
1781
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001782 /* Scrolling up is done much more efficiently by deleting lines instead of
1783 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001784 if (dest.start_col == src.start_col
1785 && dest.end_col == src.end_col
1786 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001787 {
1788 win_T *wp;
1789 VTermColor fg, bg;
1790 VTermScreenCellAttrs attr;
1791 int clear_attr;
1792
1793 /* Set the color to clear lines with. */
1794 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1795 &fg, &bg);
1796 vim_memset(&attr, 0, sizeof(attr));
1797 clear_attr = cell2attr(attr, fg, bg);
1798
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001799 FOR_ALL_WINDOWS(wp)
1800 {
1801 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001802 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001803 src.start_row - dest.start_row, FALSE, FALSE,
1804 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001805 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001806 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001807 redraw_buf_later(term->tl_buffer, NOT_VALID);
1808 return 1;
1809}
1810
1811 static int
1812handle_movecursor(
1813 VTermPos pos,
1814 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001815 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001816 void *user)
1817{
1818 term_T *term = (term_T *)user;
1819 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001820
1821 term->tl_cursor_pos = pos;
1822 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001823
1824 FOR_ALL_WINDOWS(wp)
1825 {
1826 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001827 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001828 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001829 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001830 {
1831 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001832 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001833 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001834
1835 return 1;
1836}
1837
Bram Moolenaar21554412017-07-24 21:44:43 +02001838 static int
1839handle_settermprop(
1840 VTermProp prop,
1841 VTermValue *value,
1842 void *user)
1843{
1844 term_T *term = (term_T *)user;
1845
1846 switch (prop)
1847 {
1848 case VTERM_PROP_TITLE:
1849 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001850 /* a blank title isn't useful, make it empty, so that "running" is
1851 * displayed */
1852 if (*skipwhite((char_u *)value->string) == NUL)
1853 term->tl_title = NULL;
Bram Moolenaar33d66bd2017-08-23 23:51:58 +02001854#ifdef WIN3264
1855 else if (!enc_utf8 && enc_codepage > 0)
1856 {
1857 WCHAR *ret = NULL;
1858 int length = 0;
1859
1860 MultiByteToWideChar_alloc(CP_UTF8, 0,
1861 (char*)value->string, STRLEN(value->string),
1862 &ret, &length);
1863 if (ret != NULL)
1864 {
1865 WideCharToMultiByte_alloc(enc_codepage, 0,
1866 ret, length, (char**)&term->tl_title,
1867 &length, 0, 0);
1868 vim_free(ret);
1869 }
1870 }
1871#endif
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001872 else
1873 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001874 vim_free(term->tl_status_text);
1875 term->tl_status_text = NULL;
1876 if (term == curbuf->b_term)
1877 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001878 break;
1879
1880 case VTERM_PROP_CURSORVISIBLE:
1881 term->tl_cursor_visible = value->boolean;
1882 may_toggle_cursor(term);
1883 out_flush();
1884 break;
1885
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001886 case VTERM_PROP_CURSORBLINK:
1887 term->tl_cursor_blink = value->boolean;
1888 may_set_cursor_props(term);
1889 break;
1890
1891 case VTERM_PROP_CURSORSHAPE:
1892 term->tl_cursor_shape = value->number;
1893 may_set_cursor_props(term);
1894 break;
1895
1896 case VTERM_PROP_CURSORCOLOR:
1897 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001898 if (*value->string == NUL)
1899 term->tl_cursor_color = NULL;
1900 else
1901 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001902 may_set_cursor_props(term);
1903 break;
1904
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001905 case VTERM_PROP_ALTSCREEN:
1906 /* TODO: do anything else? */
1907 term->tl_using_altscreen = value->boolean;
1908 break;
1909
Bram Moolenaar21554412017-07-24 21:44:43 +02001910 default:
1911 break;
1912 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001913 /* Always return 1, otherwise vterm doesn't store the value internally. */
1914 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001915}
1916
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001917/*
1918 * The job running in the terminal resized the terminal.
1919 */
1920 static int
1921handle_resize(int rows, int cols, void *user)
1922{
1923 term_T *term = (term_T *)user;
1924 win_T *wp;
1925
1926 term->tl_rows = rows;
1927 term->tl_cols = cols;
1928 FOR_ALL_WINDOWS(wp)
1929 {
1930 if (wp->w_buffer == term->tl_buffer)
1931 {
1932 win_setheight_win(rows, wp);
1933 win_setwidth_win(cols, wp);
1934 }
1935 }
1936
1937 redraw_buf_later(term->tl_buffer, NOT_VALID);
1938 return 1;
1939}
1940
Bram Moolenaard85f2712017-07-28 21:51:57 +02001941/*
1942 * Handle a line that is pushed off the top of the screen.
1943 */
1944 static int
1945handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1946{
1947 term_T *term = (term_T *)user;
1948
1949 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001950 if (ga_grow(&term->tl_scrollback, 1) == OK)
1951 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001952 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001953 int len = 0;
1954 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001955 int c;
1956 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001957 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001958 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001959
1960 /* do not store empty cells at the end */
1961 for (i = 0; i < cols; ++i)
1962 if (cells[i].chars[0] != 0)
1963 len = i + 1;
1964
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001965 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001966 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001967 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001968 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001969 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001970 for (col = 0; col < len; col += cells[col].width)
1971 {
1972 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1973 {
1974 ga.ga_len = 0;
1975 break;
1976 }
1977 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar6c4d12c2017-08-23 23:36:25 +02001978 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001979 (char_u *)ga.ga_data + ga.ga_len);
1980 p[col].width = cells[col].width;
1981 p[col].attrs = cells[col].attrs;
1982 p[col].fg = cells[col].fg;
1983 p[col].bg = cells[col].bg;
1984 }
1985 }
1986 if (ga_grow(&ga, 1) == FAIL)
1987 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1988 else
1989 {
1990 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1991 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1992 }
1993 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001994
1995 line = (sb_line_T *)term->tl_scrollback.ga_data
1996 + term->tl_scrollback.ga_len;
1997 line->sb_cols = len;
1998 line->sb_cells = p;
1999 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002000 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002001 }
2002 return 0; /* ignored */
2003}
2004
Bram Moolenaar21554412017-07-24 21:44:43 +02002005static VTermScreenCallbacks screen_callbacks = {
2006 handle_damage, /* damage */
2007 handle_moverect, /* moverect */
2008 handle_movecursor, /* movecursor */
2009 handle_settermprop, /* settermprop */
2010 NULL, /* bell */
2011 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002012 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02002013 NULL /* sb_popline */
2014};
2015
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002016/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02002017 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002018 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02002019 */
2020 void
2021term_channel_closed(channel_T *ch)
2022{
2023 term_T *term;
2024 int did_one = FALSE;
2025
2026 for (term = first_term; term != NULL; term = term->tl_next)
2027 if (term->tl_job == ch->ch_job)
2028 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002029 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002030 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002031
Bram Moolenaard85f2712017-07-28 21:51:57 +02002032 vim_free(term->tl_title);
2033 term->tl_title = NULL;
2034 vim_free(term->tl_status_text);
2035 term->tl_status_text = NULL;
2036
Bram Moolenaar423802d2017-07-30 16:52:24 +02002037 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02002038 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002039 {
2040 int fnum = term->tl_buffer->b_fnum;
2041
Bram Moolenaar423802d2017-07-30 16:52:24 +02002042 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002043
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002044 if (term->tl_finish == 'c')
2045 {
2046 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002047 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002048 curbuf = term->tl_buffer;
2049 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
2050 break;
2051 }
2052 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
2053 {
2054 char buf[50];
2055
2056 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002057 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02002058 vim_snprintf(buf, sizeof(buf),
2059 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02002060 ? "botright sbuf %d"
2061 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002062 do_cmdline_cmd((char_u *)buf);
2063 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002064 else
2065 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002066 }
2067
Bram Moolenaard85f2712017-07-28 21:51:57 +02002068 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002069 }
2070 if (did_one)
2071 {
2072 redraw_statuslines();
2073
2074 /* Need to break out of vgetc(). */
2075 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002076 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002077
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002078 term = curbuf->b_term;
2079 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002080 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002081 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002082 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002083 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002084 }
2085 }
2086}
2087
2088/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002089 * Called to update a window that contains an active terminal.
2090 * Returns FAIL when there is no terminal running in this window or in
2091 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002092 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002093 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002094term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002095{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002096 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002097 VTerm *vterm;
2098 VTermScreen *screen;
2099 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002100 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002101
Bram Moolenaar6d819742017-08-06 14:57:49 +02002102 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002103 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002104
Bram Moolenaard85f2712017-07-28 21:51:57 +02002105 vterm = term->tl_vterm;
2106 screen = vterm_obtain_screen(vterm);
2107 state = vterm_obtain_state(vterm);
2108
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002109 /*
2110 * If the window was resized a redraw will be triggered and we get here.
2111 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2112 */
2113 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2114 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002115 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002116 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2117 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2118 win_T *twp;
2119
2120 FOR_ALL_WINDOWS(twp)
2121 {
2122 /* When more than one window shows the same terminal, use the
2123 * smallest size. */
2124 if (twp->w_buffer == term->tl_buffer)
2125 {
2126 if (!term->tl_rows_fixed && rows > twp->w_height)
2127 rows = twp->w_height;
2128 if (!term->tl_cols_fixed && cols > twp->w_width)
2129 cols = twp->w_width;
2130 }
2131 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002132
2133 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002134 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002135 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002136 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002137 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002138
2139 /* The cursor may have been moved when resizing. */
2140 vterm_state_get_cursorpos(state, &pos);
2141 position_cursor(wp, &pos);
2142
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002143 /* TODO: Only redraw what changed. */
2144 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002145 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002146 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002147 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002148
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002149 if (pos.row < term->tl_rows)
2150 {
2151 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002152 {
2153 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002154 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002155
Bram Moolenaareeac6772017-07-23 15:48:37 +02002156 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2157 vim_memset(&cell, 0, sizeof(cell));
2158
2159 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002160 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002161 if (c == NUL)
2162 {
2163 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002164 if (enc_utf8)
2165 ScreenLinesUC[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002166 }
2167 else
2168 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002169 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002170 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002171 if (c >= 0x80)
2172 {
2173 ScreenLines[off] = ' ';
2174 ScreenLinesUC[off] = c;
2175 }
2176 else
2177 {
2178 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002179 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002180 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002181 }
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002182#ifdef WIN3264
Bram Moolenaar740c4332017-08-21 22:01:27 +02002183 else if (has_mbyte && c >= 0x80)
2184 {
2185 char_u mb[MB_MAXBYTES+1];
2186 WCHAR wc = c;
2187
2188 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2189 (char*)mb, 2, 0, 0) > 1)
2190 {
2191 ScreenLines[off] = mb[0];
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002192 ScreenLines[off + 1] = mb[1];
Bram Moolenaar740c4332017-08-21 22:01:27 +02002193 cell.width = mb_ptr2cells(mb);
2194 }
2195 else
2196 ScreenLines[off] = c;
2197 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002198#endif
Bram Moolenaarec0e07a2017-08-22 22:21:37 +02002199 else
Bram Moolenaar740c4332017-08-21 22:01:27 +02002200 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002201 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002202 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002203
2204 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002205 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002206 if (cell.width == 2)
2207 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002208 if (enc_utf8)
2209 ScreenLinesUC[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002210
2211 /* don't set the second byte to NUL for a DBCS encoding, it
2212 * has been set above */
2213 if (enc_utf8 || !has_mbyte)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002214 ScreenLines[off] = NUL;
Bram Moolenaard2c45a12017-08-22 22:29:00 +02002215
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002216 ++pos.col;
2217 ++off;
2218 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002219 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002220 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002221 else
2222 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002223
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002224 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2225 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002226 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002227
2228 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002229}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002230
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002231/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002232 * Return TRUE if "wp" is a terminal window where the job has finished.
2233 */
2234 int
2235term_is_finished(buf_T *buf)
2236{
2237 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2238}
2239
2240/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002241 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002242 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002243 */
2244 int
2245term_show_buffer(buf_T *buf)
2246{
2247 term_T *term = buf->b_term;
2248
Bram Moolenaar6d819742017-08-06 14:57:49 +02002249 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002250}
2251
2252/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002253 * The current buffer is going to be changed. If there is terminal
2254 * highlighting remove it now.
2255 */
2256 void
2257term_change_in_curbuf(void)
2258{
2259 term_T *term = curbuf->b_term;
2260
2261 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2262 {
2263 free_scrollback(term);
2264 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002265
2266 /* The buffer is now like a normal buffer, it cannot be easily
2267 * abandoned when changed. */
2268 set_string_option_direct((char_u *)"buftype", -1,
2269 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002270 }
2271}
2272
2273/*
2274 * Get the screen attribute for a position in the buffer.
2275 */
2276 int
2277term_get_attr(buf_T *buf, linenr_T lnum, int col)
2278{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002279 term_T *term = buf->b_term;
2280 sb_line_T *line;
2281 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002282
Bram Moolenaar70229f92017-07-29 16:01:53 +02002283 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002284 return 0;
2285 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2286 if (col >= line->sb_cols)
2287 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002288 cellattr = line->sb_cells + col;
2289 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002290}
2291
2292/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002293 * Create a new vterm and initialize it.
2294 */
2295 static void
2296create_vterm(term_T *term, int rows, int cols)
2297{
2298 VTerm *vterm;
2299 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002300 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002301
2302 vterm = vterm_new(rows, cols);
2303 term->tl_vterm = vterm;
2304 screen = vterm_obtain_screen(vterm);
2305 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2306 /* TODO: depends on 'encoding'. */
2307 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002308
2309 /* Vterm uses a default black background. Set it to white when
2310 * 'background' is "light". */
2311 if (*p_bg == 'l')
2312 {
2313 VTermColor fg, bg;
2314
2315 fg.red = fg.green = fg.blue = 0;
2316 bg.red = bg.green = bg.blue = 255;
2317 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2318 }
2319
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002320 /* Required to initialize most things. */
2321 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002322
2323 /* Allow using alternate screen. */
2324 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002325
Bram Moolenaar58302322017-08-22 20:33:53 +02002326 /* For unix do not use a blinking cursor. In an xterm this causes the
2327 * cursor to blink if it's blinking in the xterm.
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002328 * For Windows we respect the system wide setting. */
Bram Moolenaar58302322017-08-22 20:33:53 +02002329#ifdef WIN3264
Bram Moolenaarbe0b7292017-08-24 21:48:26 +02002330 if (GetCaretBlinkTime() == INFINITE)
2331 value.boolean = 0;
2332 else
2333 value.boolean = 1;
Bram Moolenaar58302322017-08-22 20:33:53 +02002334#else
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002335 value.boolean = 0;
Bram Moolenaar58302322017-08-22 20:33:53 +02002336#endif
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002337 vterm_state_set_termprop(vterm_obtain_state(vterm),
2338 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002339}
2340
Bram Moolenaar21554412017-07-24 21:44:43 +02002341/*
2342 * Return the text to show for the buffer name and status.
2343 */
2344 char_u *
2345term_get_status_text(term_T *term)
2346{
2347 if (term->tl_status_text == NULL)
2348 {
2349 char_u *txt;
2350 size_t len;
2351
Bram Moolenaar6d819742017-08-06 14:57:49 +02002352 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002353 {
2354 if (term_job_running(term))
2355 txt = (char_u *)_("Terminal");
2356 else
2357 txt = (char_u *)_("Terminal-finished");
2358 }
2359 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002360 txt = term->tl_title;
2361 else if (term_job_running(term))
2362 txt = (char_u *)_("running");
2363 else
2364 txt = (char_u *)_("finished");
2365 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002366 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002367 if (term->tl_status_text != NULL)
2368 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2369 term->tl_buffer->b_fname, txt);
2370 }
2371 return term->tl_status_text;
2372}
2373
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002374/*
2375 * Mark references in jobs of terminals.
2376 */
2377 int
2378set_ref_in_term(int copyID)
2379{
2380 int abort = FALSE;
2381 term_T *term;
2382 typval_T tv;
2383
2384 for (term = first_term; term != NULL; term = term->tl_next)
2385 if (term->tl_job != NULL)
2386 {
2387 tv.v_type = VAR_JOB;
2388 tv.vval.v_job = term->tl_job;
2389 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2390 }
2391 return abort;
2392}
2393
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002394/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002395 * Get the buffer from the first argument in "argvars".
2396 * Returns NULL when the buffer is not for a terminal window.
2397 */
2398 static buf_T *
2399term_get_buf(typval_T *argvars)
2400{
2401 buf_T *buf;
2402
2403 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2404 ++emsg_off;
2405 buf = get_buf_tv(&argvars[0], FALSE);
2406 --emsg_off;
2407 if (buf == NULL || buf->b_term == NULL)
2408 return NULL;
2409 return buf;
2410}
2411
2412/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002413 * "term_getaltscreen(buf)" function
2414 */
2415 void
2416f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2417{
2418 buf_T *buf = term_get_buf(argvars);
2419
2420 if (buf == NULL)
2421 return;
2422 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2423}
2424
2425/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002426 * "term_getattr(attr, name)" function
2427 */
2428 void
2429f_term_getattr(typval_T *argvars, typval_T *rettv)
2430{
2431 int attr;
2432 size_t i;
2433 char_u *name;
2434
2435 static struct {
2436 char *name;
2437 int attr;
2438 } attrs[] = {
2439 {"bold", HL_BOLD},
2440 {"italic", HL_ITALIC},
2441 {"underline", HL_UNDERLINE},
2442 {"strike", HL_STANDOUT},
2443 {"reverse", HL_INVERSE},
2444 };
2445
2446 attr = get_tv_number(&argvars[0]);
2447 name = get_tv_string_chk(&argvars[1]);
2448 if (name == NULL)
2449 return;
2450
2451 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2452 if (STRCMP(name, attrs[i].name) == 0)
2453 {
2454 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2455 break;
2456 }
2457}
2458
2459/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002460 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002461 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002462 void
2463f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002464{
Bram Moolenaar97870002017-07-30 18:28:38 +02002465 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002466 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002467 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002468 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002469
Bram Moolenaar97870002017-07-30 18:28:38 +02002470 if (rettv_list_alloc(rettv) == FAIL)
2471 return;
2472 if (buf == NULL)
2473 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002474 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002475
2476 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002477 list_append_number(l, term->tl_cursor_pos.row + 1);
2478 list_append_number(l, term->tl_cursor_pos.col + 1);
2479
2480 d = dict_alloc();
2481 if (d != NULL)
2482 {
2483 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
Bram Moolenaar4db25542017-08-28 22:43:05 +02002484 dict_add_nr_str(d, "blink", blink_state_is_inverted()
2485 ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002486 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 */