blob: bab2a5ba47cc68fdac51588a77597ebcdcdb5986 [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.
37 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020038 * TODO:
Bram Moolenaar423802d2017-07-30 16:52:24 +020039 * - add option values to the command:
Bram Moolenaar82b9ca02017-08-08 23:06:46 +020040 * :term ++24x80 ++close vim notes.txt
Bram Moolenaardd693ce2017-08-10 23:15:19 +020041 * - When using term_finish "open" have a way to specify how the window is to
42 * be opened. E.g. term_opencmd "10split buffer %d".
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020043 * - support different cursor shapes, colors and attributes
44 * - make term_getcursor() return type (none/block/bar/underline) and
45 * attributes (color, blink, etc.)
Bram Moolenaardd693ce2017-08-10 23:15:19 +020046 * - Make argument list work on MS-Windows. #1954
Bram Moolenaar292d5692017-08-08 21:52:22 +020047 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaard85f2712017-07-28 21:51:57 +020048 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020049 * For the GUI fill termios with default values, perhaps like pangoterm:
50 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
51 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020052 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020053 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020054 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020055 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020056 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020057 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
58 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020059 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020060 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020061 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020062 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020063 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020064 * terminal. Allow:
65 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
66 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
67 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
68 * Check that something is connected to the terminal.
69 * Test: "cat" reading from a file or buffer
70 * "ls" writing stdout to a file or buffer
71 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020072 * - support ":term NONE" to open a terminal with a pty but not running a job
73 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020074 * - if the job in the terminal does not support the mouse, we can use the
75 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020076 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
77 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020078 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020079 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020080 * - Copy text in the vterm to the Vim buffer once in a while, so that
81 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082 */
83
84#include "vim.h"
85
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020086#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020087
Bram Moolenaard5310982017-08-05 15:16:32 +020088#ifndef MIN
89# define MIN(x,y) ((x) < (y) ? (x) : (y))
90#endif
91#ifndef MAX
92# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020093#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020094
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020095#include "libvterm/include/vterm.h"
96
Bram Moolenaar33a43be2017-08-06 21:36:22 +020097/* This is VTermScreenCell without the characters, thus much smaller. */
98typedef struct {
99 VTermScreenCellAttrs attrs;
100 char width;
101 VTermColor fg, bg;
102} cellattr_T;
103
Bram Moolenaard85f2712017-07-28 21:51:57 +0200104typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200105 int sb_cols; /* can differ per line */
106 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200107} sb_line_T;
108
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200109/* typedef term_T in structs.h */
110struct terminal_S {
111 term_T *tl_next;
112
Bram Moolenaar423802d2017-07-30 16:52:24 +0200113 VTerm *tl_vterm;
114 job_T *tl_job;
115 buf_T *tl_buffer;
116
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200117 /* used when tl_job is NULL and only a pty was created */
118 int tl_tty_fd;
119 char_u *tl_tty_name;
120
Bram Moolenaar6d819742017-08-06 14:57:49 +0200121 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200122 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200123 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200124
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200125#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200126 void *tl_winpty_config;
127 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200128#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200129
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200130 /* last known vterm size */
131 int tl_rows;
132 int tl_cols;
133 /* vterm size does not follow window size */
134 int tl_rows_fixed;
135 int tl_cols_fixed;
136
Bram Moolenaar21554412017-07-24 21:44:43 +0200137 char_u *tl_title; /* NULL or allocated */
138 char_u *tl_status_text; /* NULL or allocated */
139
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200140 /* Range of screen rows to update. Zero based. */
141 int tl_dirty_row_start; /* -1 if nothing dirty */
142 int tl_dirty_row_end; /* row below last one to update */
143
Bram Moolenaard85f2712017-07-28 21:51:57 +0200144 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200145 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200146
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200147 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200148 int tl_cursor_visible;
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200149
150 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200151};
152
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200153#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
154#define TMODE_LOOP 2 /* CTRL-W N used */
155
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200156/*
157 * List of all active terminals.
158 */
159static term_T *first_term = NULL;
160
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200161
162#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
163#define KEY_BUF_LEN 200
164
165/*
166 * Functions with separate implementation for MS-Windows and Unix-like systems.
167 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200168static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200169static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200170static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200171
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200172/**************************************
173 * 1. Generic code for all systems.
174 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200175
176/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200177 * Determine the terminal size from 'termsize' and the current window.
178 * Assumes term->tl_rows and term->tl_cols are zero.
179 */
180 static void
181set_term_and_win_size(term_T *term)
182{
183 if (*curwin->w_p_tms != NUL)
184 {
185 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
186
187 term->tl_rows = atoi((char *)curwin->w_p_tms);
188 term->tl_cols = atoi((char *)p);
189 }
190 if (term->tl_rows == 0)
191 term->tl_rows = curwin->w_height;
192 else
193 {
194 win_setheight_win(term->tl_rows, curwin);
195 term->tl_rows_fixed = TRUE;
196 }
197 if (term->tl_cols == 0)
198 term->tl_cols = curwin->w_width;
199 else
200 {
201 win_setwidth_win(term->tl_cols, curwin);
202 term->tl_cols_fixed = TRUE;
203 }
204}
205
206/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200207 * Initialize job options for a terminal job.
208 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200209 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200210 static void
211init_job_options(jobopt_T *opt)
212{
213 clear_job_options(opt);
214
215 opt->jo_mode = MODE_RAW;
216 opt->jo_out_mode = MODE_RAW;
217 opt->jo_err_mode = MODE_RAW;
218 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
219
220 opt->jo_io[PART_OUT] = JIO_BUFFER;
221 opt->jo_io[PART_ERR] = JIO_BUFFER;
222 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
223
224 opt->jo_modifiable[PART_OUT] = 0;
225 opt->jo_modifiable[PART_ERR] = 0;
226 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
227
228 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
229}
230
231/*
232 * Set job options mandatory for a terminal job.
233 */
234 static void
235setup_job_options(jobopt_T *opt, int rows, int cols)
236{
237 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
238 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
239 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200240 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
241 opt->jo_term_rows = rows;
242 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
243 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200244}
245
246 static void
247term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 exarg_T split_ea;
250 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252
253 if (check_restricted() || check_secure())
254 return;
255
256 term = (term_T *)alloc_clear(sizeof(term_T));
257 if (term == NULL)
258 return;
259 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200260 term->tl_cursor_visible = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200261 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200262 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263
264 /* Open a new window or tab. */
265 vim_memset(&split_ea, 0, sizeof(split_ea));
266 split_ea.cmdidx = CMD_new;
267 split_ea.cmd = (char_u *)"new";
268 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200269 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
270 {
271 split_ea.line2 = opt->jo_term_rows;
272 split_ea.addr_count = 1;
273 }
274 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
275 {
276 split_ea.line2 = opt->jo_term_cols;
277 split_ea.addr_count = 1;
278 }
279
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200280 ex_splitview(&split_ea);
281 if (curwin == old_curwin)
282 {
283 /* split failed */
284 vim_free(term);
285 return;
286 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200287 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200288 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200289
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200290 /* only one size was taken care of with :new, do the other one */
291 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
292 win_setheight(opt->jo_term_rows);
293 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
294 win_setwidth(opt->jo_term_cols);
295
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200296 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200297 term->tl_next = first_term;
298 first_term = term;
299
Bram Moolenaar293424c2017-07-26 23:11:01 +0200300 if (cmd == NULL || *cmd == NUL)
301 cmd = p_sh;
302
Bram Moolenaar78712a72017-08-05 14:50:12 +0200303 if (opt->jo_term_name != NULL)
304 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
305 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200306 {
307 int i;
308 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200309 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200310
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200311 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200312 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200313 /* Prepend a ! to the command name to avoid the buffer name equals
314 * the executable, otherwise ":w!" would overwrite it. */
315 if (i == 0)
316 vim_snprintf((char *)p, len, "!%s", cmd);
317 else
318 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200319 if (buflist_findname(p) == NULL)
320 {
321 curbuf->b_ffname = p;
322 break;
323 }
324 }
325 }
326 curbuf->b_fname = curbuf->b_ffname;
327
Bram Moolenaareb44a682017-08-03 22:44:55 +0200328 set_string_option_direct((char_u *)"buftype", -1,
329 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
330
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200331 /* Mark the buffer as not modifiable. It can only be made modifiable after
332 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200333 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200334
335 /* Set 'bufhidden' to "hide": allow closing the window. */
336 set_string_option_direct((char_u *)"bufhidden", -1,
337 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200338
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200339 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200340 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200341
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200342 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200343 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200344 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200345 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200346 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200347 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200348 }
349 else
350 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200351 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200352
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200353 /* Wiping out the buffer will also close the window and call
354 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200355 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200356 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200357}
358
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200359/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200360 * ":terminal": open a terminal window and execute a job in it.
361 */
362 void
363ex_terminal(exarg_T *eap)
364{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200365 jobopt_T opt;
366 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200367
368 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200369
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200370 cmd = eap->arg;
371 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
372 {
373 char_u *p;
374
375 cmd += 2;
376 p = skiptowhite(cmd);
377 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
378 opt.jo_term_finish = 'c';
379 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
380 opt.jo_term_finish = 'o';
381 else
382 {
383 if (*p)
384 *p = NUL;
385 EMSG2(_("E181: Invalid attribute: %s"), cmd);
386 return;
387 }
388 cmd = skipwhite(p);
389 }
390
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200391 if (eap->addr_count == 2)
392 {
393 opt.jo_term_rows = eap->line1;
394 opt.jo_term_cols = eap->line2;
395 }
396 else if (eap->addr_count == 1)
397 {
398 if (cmdmod.split & WSP_VERT)
399 opt.jo_term_cols = eap->line2;
400 else
401 opt.jo_term_rows = eap->line2;
402 }
403 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200404
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200405 term_start(cmd, &opt);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200406}
407
408/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200409 * Free the scrollback buffer for "term".
410 */
411 static void
412free_scrollback(term_T *term)
413{
414 int i;
415
416 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
417 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
418 ga_clear(&term->tl_scrollback);
419}
420
421/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200422 * Free a terminal and everything it refers to.
423 * Kills the job if there is one.
424 * Called when wiping out a buffer.
425 */
426 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200427free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200428{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200429 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200430 term_T *tp;
431
432 if (term == NULL)
433 return;
434 if (first_term == term)
435 first_term = term->tl_next;
436 else
437 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
438 if (tp->tl_next == term)
439 {
440 tp->tl_next = term->tl_next;
441 break;
442 }
443
444 if (term->tl_job != NULL)
445 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200446 if (term->tl_job->jv_status != JOB_ENDED
447 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200448 job_stop(term->tl_job, NULL, "kill");
449 job_unref(term->tl_job);
450 }
451
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200452 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200453
454 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200455 vim_free(term->tl_title);
456 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200457 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200458 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200459}
460
461/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200462 * Write job output "msg[len]" to the vterm.
463 */
464 static void
465term_write_job_output(term_T *term, char_u *msg, size_t len)
466{
467 VTerm *vterm = term->tl_vterm;
468 char_u *p;
469 size_t done;
470 size_t len_now;
471
472 for (done = 0; done < len; done += len_now)
473 {
474 for (p = msg + done; p < msg + len; )
475 {
476 if (*p == NL)
477 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200478 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200479 }
480 len_now = p - msg - done;
481 vterm_input_write(vterm, (char *)msg + done, len_now);
482 if (p < msg + len && *p == NL)
483 {
484 /* Convert NL to CR-NL, that appears to work best. */
485 vterm_input_write(vterm, "\r\n", 2);
486 ++len_now;
487 }
488 }
489
490 /* this invokes the damage callbacks */
491 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
492}
493
Bram Moolenaar1c844932017-07-24 23:36:41 +0200494 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200495update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200496{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200497 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200498 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200499 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200500 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200501 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200502 if (term->tl_cursor_visible)
503 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200504 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200505#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200506 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200507 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200508#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200509 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200510}
511
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200512/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200513 * Invoked when "msg" output from a job was received. Write it to the terminal
514 * of "buffer".
515 */
516 void
517write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
518{
519 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200520 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200521
Bram Moolenaard85f2712017-07-28 21:51:57 +0200522 if (term->tl_vterm == NULL)
523 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200524 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200525 return;
526 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200527 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200528 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200529
Bram Moolenaar6d819742017-08-06 14:57:49 +0200530 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200531 {
532 /* TODO: only update once in a while. */
533 update_screen(0);
534 update_cursor(term, TRUE);
535 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200536}
537
538/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200539 * Send a mouse position and click to the vterm
540 */
541 static int
542term_send_mouse(VTerm *vterm, int button, int pressed)
543{
544 VTermModifier mod = VTERM_MOD_NONE;
545
546 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
547 mouse_col - W_WINCOL(curwin), mod);
548 vterm_mouse_button(vterm, button, pressed, mod);
549 return TRUE;
550}
551
552/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200553 * Convert typed key "c" into bytes to send to the job.
554 * Return the number of bytes in "buf".
555 */
556 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200557term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200558{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200559 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200560 VTermKey key = VTERM_KEY_NONE;
561 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200562 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200563
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200564 switch (c)
565 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200566 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200567 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200568 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
569 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200570 case K_DEL: key = VTERM_KEY_DEL; break;
571 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200572 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
573 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200574 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200575 case K_S_END: mod = VTERM_MOD_SHIFT;
576 key = VTERM_KEY_END; break;
577 case K_C_END: mod = VTERM_MOD_CTRL;
578 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200579 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
580 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
581 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
582 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
583 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
584 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
585 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
586 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
587 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
588 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
589 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
590 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
591 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200592 case K_S_HOME: mod = VTERM_MOD_SHIFT;
593 key = VTERM_KEY_HOME; break;
594 case K_C_HOME: mod = VTERM_MOD_CTRL;
595 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200596 case K_INS: key = VTERM_KEY_INS; break;
597 case K_K0: key = VTERM_KEY_KP_0; break;
598 case K_K1: key = VTERM_KEY_KP_1; break;
599 case K_K2: key = VTERM_KEY_KP_2; break;
600 case K_K3: key = VTERM_KEY_KP_3; break;
601 case K_K4: key = VTERM_KEY_KP_4; break;
602 case K_K5: key = VTERM_KEY_KP_5; break;
603 case K_K6: key = VTERM_KEY_KP_6; break;
604 case K_K7: key = VTERM_KEY_KP_7; break;
605 case K_K8: key = VTERM_KEY_KP_8; break;
606 case K_K9: key = VTERM_KEY_KP_9; break;
607 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
608 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
609 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
610 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
611 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
612 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
613 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
614 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
615 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
616 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
617 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
618 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
619 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200620 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
621 key = VTERM_KEY_LEFT; break;
622 case K_C_LEFT: mod = VTERM_MOD_CTRL;
623 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200624 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
625 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
626 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200627 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
628 key = VTERM_KEY_RIGHT; break;
629 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
630 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200631 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200632 case K_S_UP: mod = VTERM_MOD_SHIFT;
633 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200634 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200635
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200636 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
637 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
638 case K_MOUSELEFT: /* TODO */ return 0;
639 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200640
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200641 case K_LEFTMOUSE:
642 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
643 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
644 case K_LEFTRELEASE:
645 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
646 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
647 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
648 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
649 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
650 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
651 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
652 case K_X1MOUSE: /* TODO */ return 0;
653 case K_X1DRAG: /* TODO */ return 0;
654 case K_X1RELEASE: /* TODO */ return 0;
655 case K_X2MOUSE: /* TODO */ return 0;
656 case K_X2DRAG: /* TODO */ return 0;
657 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200658
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200659 case K_IGNORE: return 0;
660 case K_NOP: return 0;
661 case K_UNDO: return 0;
662 case K_HELP: return 0;
663 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
664 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
665 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
666 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
667 case K_SELECT: return 0;
668#ifdef FEAT_GUI
669 case K_VER_SCROLLBAR: return 0;
670 case K_HOR_SCROLLBAR: return 0;
671#endif
672#ifdef FEAT_GUI_TABLINE
673 case K_TABLINE: return 0;
674 case K_TABMENU: return 0;
675#endif
676#ifdef FEAT_NETBEANS_INTG
677 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
678#endif
679#ifdef FEAT_DND
680 case K_DROP: return 0;
681#endif
682#ifdef FEAT_AUTOCMD
683 case K_CURSORHOLD: return 0;
684#endif
685 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
686 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200687 }
688
689 /*
690 * Convert special keys to vterm keys:
691 * - Write keys to vterm: vterm_keyboard_key()
692 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200693 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200694 */
695 if (key != VTERM_KEY_NONE)
696 /* Special key, let vterm convert it. */
697 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200698 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200699 /* Normal character, let vterm convert it. */
700 vterm_keyboard_unichar(vterm, c, mod);
701
702 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200703 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200704}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200705
Bram Moolenaar938783d2017-07-16 20:13:26 +0200706/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200707 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200708 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200709 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200710term_job_running(term_T *term)
711{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200712 /* Also consider the job finished when the channel is closed, to avoid a
713 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200714 return term != NULL
715 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200716 && term->tl_job->jv_status == JOB_STARTED
717 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200718}
719
720/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200721 * Add the last line of the scrollback buffer to the buffer in the window.
722 */
723 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200724add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200725{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200726 buf_T *buf = term->tl_buffer;
727 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
728 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200729
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200730 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200731 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200732 {
733 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200734 curbuf = buf;
735 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200736 curbuf = curwin->w_buffer;
737 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200738}
739
740/*
741 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200742 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200743 */
744 static void
745move_terminal_to_buffer(term_T *term)
746{
747 win_T *wp;
748 int len;
749 int lines_skipped = 0;
750 VTermPos pos;
751 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200752 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200753 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200754
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200755 if (term->tl_vterm == NULL)
756 return;
757 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200758 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
759 {
760 len = 0;
761 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
762 if (vterm_screen_get_cell(screen, pos, &cell) != 0
763 && cell.chars[0] != NUL)
764 len = pos.col + 1;
765
766 if (len == 0)
767 ++lines_skipped;
768 else
769 {
770 while (lines_skipped > 0)
771 {
772 /* Line was skipped, add an empty line. */
773 --lines_skipped;
774 if (ga_grow(&term->tl_scrollback, 1) == OK)
775 {
776 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
777 + term->tl_scrollback.ga_len;
778
779 line->sb_cols = 0;
780 line->sb_cells = NULL;
781 ++term->tl_scrollback.ga_len;
782
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200783 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200784 }
785 }
786
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200787 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200788 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
789 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200790 garray_T ga;
791 int width;
792 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200793 + term->tl_scrollback.ga_len;
794
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200795 ga_init2(&ga, 1, 100);
796 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200797 {
798 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200799 {
800 width = 1;
801 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
802 if (ga_grow(&ga, 1) == OK)
803 ga.ga_len += mb_char2bytes(' ',
804 (char_u *)ga.ga_data + ga.ga_len);
805 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200806 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200807 {
808 width = cell.width;
809
810 p[pos.col].width = cell.width;
811 p[pos.col].attrs = cell.attrs;
812 p[pos.col].fg = cell.fg;
813 p[pos.col].bg = cell.bg;
814
815 if (ga_grow(&ga, MB_MAXBYTES) == OK)
816 {
817 int i;
818 int c;
819
820 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
821 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
822 (char_u *)ga.ga_data + ga.ga_len);
823 }
824 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200825 }
826 line->sb_cols = len;
827 line->sb_cells = p;
828 ++term->tl_scrollback.ga_len;
829
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200830 if (ga_grow(&ga, 1) == FAIL)
831 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
832 else
833 {
834 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
835 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
836 }
837 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200838 }
839 else
840 vim_free(p);
841 }
842 }
843
844 FOR_ALL_WINDOWS(wp)
845 {
846 if (wp->w_buffer == term->tl_buffer)
847 {
848 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
849 wp->w_cursor.col = 0;
850 wp->w_valid = 0;
851 redraw_win_later(wp, NOT_VALID);
852 }
853 }
854}
855
856 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200857set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200858{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200859 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200860 vim_free(term->tl_status_text);
861 term->tl_status_text = NULL;
862 if (term->tl_buffer == curbuf)
863 maketitle();
864}
865
866/*
867 * Called after the job if finished and Terminal mode is not active:
868 * Move the vterm contents into the scrollback buffer and free the vterm.
869 */
870 static void
871cleanup_vterm(term_T *term)
872{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200873 if (term->tl_finish == 0)
874 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200875 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200876 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200877}
878
879/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200880 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200881 * Suspends updating the terminal window.
882 */
883 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200884term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200885{
886 term_T *term = curbuf->b_term;
887
888 /* Append the current terminal contents to the buffer. */
889 move_terminal_to_buffer(term);
890
Bram Moolenaar6d819742017-08-06 14:57:49 +0200891 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200892
Bram Moolenaar6d819742017-08-06 14:57:49 +0200893 /* Move the window cursor to the position of the cursor in the
894 * terminal. */
895 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
896 + term->tl_cursor_pos.row + 1;
897 check_cursor();
898 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200899
Bram Moolenaar6d819742017-08-06 14:57:49 +0200900 /* Display the same lines as in the terminal. */
901 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200902}
903
904/*
905 * Returns TRUE if the current window contains a terminal and we are in
906 * Terminal-Normal mode.
907 */
908 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200909term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200910{
911 term_T *term = curbuf->b_term;
912
Bram Moolenaar6d819742017-08-06 14:57:49 +0200913 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200914}
915
916/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200917 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200918 * Restores updating the terminal window.
919 */
920 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200921term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200922{
923 term_T *term = curbuf->b_term;
924 sb_line_T *line;
925 garray_T *gap;
926
927 /* Remove the terminal contents from the scrollback and the buffer. */
928 gap = &term->tl_scrollback;
929 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
930 {
931 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
932 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
933 vim_free(line->sb_cells);
934 --gap->ga_len;
935 if (gap->ga_len == 0)
936 break;
937 }
938 check_cursor();
939
Bram Moolenaar6d819742017-08-06 14:57:49 +0200940 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200941
942 if (term->tl_channel_closed)
943 cleanup_vterm(term);
944 redraw_buf_and_status_later(curbuf, NOT_VALID);
945}
946
947/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200948 * Get a key from the user without mapping.
949 * TODO: use terminal mode mappings.
950 */
951 static int
952term_vgetc()
953{
954 int c;
955
956 ++no_mapping;
957 ++allow_keys;
958 got_int = FALSE;
959 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200960 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200961 --no_mapping;
962 --allow_keys;
963 return c;
964}
965
966/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200967 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200968 * Return FAIL when the key needs to be handled in Normal mode.
969 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200970 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200971 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200972send_keys_to_term(term_T *term, int c, int typed)
973{
974 char msg[KEY_BUF_LEN];
975 size_t len;
976 static int mouse_was_outside = FALSE;
977 int dragging_outside = FALSE;
978
979 /* Catch keys that need to be handled as in Normal mode. */
980 switch (c)
981 {
982 case NUL:
983 case K_ZERO:
984 if (typed)
985 stuffcharReadbuff(c);
986 return FAIL;
987
988 case K_IGNORE:
989 return FAIL;
990
991 case K_LEFTDRAG:
992 case K_MIDDLEDRAG:
993 case K_RIGHTDRAG:
994 case K_X1DRAG:
995 case K_X2DRAG:
996 dragging_outside = mouse_was_outside;
997 /* FALLTHROUGH */
998 case K_LEFTMOUSE:
999 case K_LEFTMOUSE_NM:
1000 case K_LEFTRELEASE:
1001 case K_LEFTRELEASE_NM:
1002 case K_MIDDLEMOUSE:
1003 case K_MIDDLERELEASE:
1004 case K_RIGHTMOUSE:
1005 case K_RIGHTRELEASE:
1006 case K_X1MOUSE:
1007 case K_X1RELEASE:
1008 case K_X2MOUSE:
1009 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001010
1011 case K_MOUSEUP:
1012 case K_MOUSEDOWN:
1013 case K_MOUSELEFT:
1014 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001015 if (mouse_row < W_WINROW(curwin)
1016 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1017 || mouse_col < W_WINCOL(curwin)
1018 || mouse_col >= W_ENDCOL(curwin)
1019 || dragging_outside)
1020 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001021 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001022 if (typed)
1023 {
1024 stuffcharReadbuff(c);
1025 mouse_was_outside = TRUE;
1026 }
1027 return FAIL;
1028 }
1029 }
1030 if (typed)
1031 mouse_was_outside = FALSE;
1032
1033 /* Convert the typed key to a sequence of bytes for the job. */
1034 len = term_convert_key(term, c, msg);
1035 if (len > 0)
1036 /* TODO: if FAIL is returned, stop? */
1037 channel_send(term->tl_job->jv_channel, PART_IN,
1038 (char_u *)msg, (int)len, NULL);
1039
1040 return OK;
1041}
1042
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001043 static void
1044position_cursor(win_T *wp, VTermPos *pos)
1045{
1046 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1047 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1048 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1049}
1050
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001051/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001052 * Handle CTRL-W "": send register contents to the job.
1053 */
1054 static void
1055term_paste_register(int prev_c UNUSED)
1056{
1057 int c;
1058 list_T *l;
1059 listitem_T *item;
1060 long reglen = 0;
1061 int type;
1062
1063#ifdef FEAT_CMDL_INFO
1064 if (add_to_showcmd(prev_c))
1065 if (add_to_showcmd('"'))
1066 out_flush();
1067#endif
1068 c = term_vgetc();
1069#ifdef FEAT_CMDL_INFO
1070 clear_showcmd();
1071#endif
1072
1073 /* CTRL-W "= prompt for expression to evaluate. */
1074 if (c == '=' && get_expr_register() != '=')
1075 return;
1076
1077 l = (list_T *)get_reg_contents(c, GREG_LIST);
1078 if (l != NULL)
1079 {
1080 type = get_reg_type(c, &reglen);
1081 for (item = l->lv_first; item != NULL; item = item->li_next)
1082 {
1083 char_u *s = get_tv_string(&item->li_tv);
1084
1085 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1086 s, STRLEN(s), NULL);
1087 if (item->li_next != NULL || type == MLINE)
1088 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1089 (char_u *)"\r", 1, NULL);
1090 }
1091 list_free(l);
1092 }
1093}
1094
1095/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001096 * Returns TRUE if the current window contains a terminal and we are sending
1097 * keys to the job.
1098 */
1099 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001100term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001101{
1102 term_T *term = curbuf->b_term;
1103
1104 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001105 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001106 && term->tl_vterm != NULL
1107 && term_job_running(term);
1108}
1109
1110/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001111 * Wait for input and send it to the job.
1112 * Return when the start of a CTRL-W command is typed or anything else that
1113 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001114 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1115 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001116 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001117 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001118terminal_loop(void)
1119{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001120 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001121 int termkey = 0;
1122
1123 if (*curwin->w_p_tk != NUL)
1124 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001125 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001126
1127 for (;;)
1128 {
1129 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001130 /* Repeat redrawing in case a message is received while redrawing. */
1131 while (curwin->w_redr_type != 0)
1132 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001133 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001134
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001135 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001136 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001137 /* job finished while waiting for a character */
1138 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001139
Bram Moolenaarfae42832017-08-01 22:24:26 +02001140#ifdef UNIX
1141 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1142#endif
1143#ifdef WIN3264
1144 if (c == Ctrl_C)
1145 /* We don't know if the job can handle CTRL-C itself or not, this
1146 * may kill the shell instead of killing the command running in the
1147 * shell. */
Bram Moolenaar2d33e902017-08-11 16:31:54 +02001148 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001149#endif
1150
Bram Moolenaar69198192017-08-05 14:10:48 +02001151 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001152 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001153 int prev_c = c;
1154
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001155#ifdef FEAT_CMDL_INFO
1156 if (add_to_showcmd(c))
1157 out_flush();
1158#endif
1159 c = term_vgetc();
1160#ifdef FEAT_CMDL_INFO
1161 clear_showcmd();
1162#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001163 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001164 /* job finished while waiting for a character */
1165 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001166
Bram Moolenaar69198192017-08-05 14:10:48 +02001167 if (prev_c == Ctrl_BSL)
1168 {
1169 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001170 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001171 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1172 term_enter_normal_mode();
1173 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001174 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001175 /* Send both keys to the terminal. */
1176 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1177 }
1178 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001179 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001180 /* "CTRL-W .": send CTRL-W to the job */
1181 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001182 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001183 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001184 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001185 /* CTRL-W N : go to Terminal-Normal mode. */
1186 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001187 return FAIL;
1188 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001189 else if (c == '"')
1190 {
1191 term_paste_register(prev_c);
1192 continue;
1193 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001194 else if (termkey == 0 || c != termkey)
1195 {
1196 stuffcharReadbuff(Ctrl_W);
1197 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001198 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001199 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001200 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001201 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1202 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001203 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001204 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001205}
1206
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001207/*
1208 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001209 * This updates the title and status, but does not close the vter, because
1210 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001211 */
1212 void
1213term_job_ended(job_T *job)
1214{
1215 term_T *term;
1216 int did_one = FALSE;
1217
1218 for (term = first_term; term != NULL; term = term->tl_next)
1219 if (term->tl_job == job)
1220 {
1221 vim_free(term->tl_title);
1222 term->tl_title = NULL;
1223 vim_free(term->tl_status_text);
1224 term->tl_status_text = NULL;
1225 redraw_buf_and_status_later(term->tl_buffer, VALID);
1226 did_one = TRUE;
1227 }
1228 if (did_one)
1229 redraw_statuslines();
1230 if (curbuf->b_term != NULL)
1231 {
1232 if (curbuf->b_term->tl_job == job)
1233 maketitle();
1234 update_cursor(curbuf->b_term, TRUE);
1235 }
1236}
1237
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001238 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001239may_toggle_cursor(term_T *term)
1240{
1241 if (curbuf == term->tl_buffer)
1242 {
1243 if (term->tl_cursor_visible)
1244 cursor_on();
1245 else
1246 cursor_off();
1247 }
1248}
1249
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001250 static int
1251handle_damage(VTermRect rect, void *user)
1252{
1253 term_T *term = (term_T *)user;
1254
1255 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1256 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1257 redraw_buf_later(term->tl_buffer, NOT_VALID);
1258 return 1;
1259}
1260
1261 static int
1262handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1263{
1264 term_T *term = (term_T *)user;
1265
1266 /* TODO */
1267 redraw_buf_later(term->tl_buffer, NOT_VALID);
1268 return 1;
1269}
1270
1271 static int
1272handle_movecursor(
1273 VTermPos pos,
1274 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001275 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001276 void *user)
1277{
1278 term_T *term = (term_T *)user;
1279 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001280
1281 term->tl_cursor_pos = pos;
1282 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001283
1284 FOR_ALL_WINDOWS(wp)
1285 {
1286 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001287 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001288 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001289 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001290 {
1291 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001292 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001293 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001294
1295 return 1;
1296}
1297
Bram Moolenaar21554412017-07-24 21:44:43 +02001298 static int
1299handle_settermprop(
1300 VTermProp prop,
1301 VTermValue *value,
1302 void *user)
1303{
1304 term_T *term = (term_T *)user;
1305
1306 switch (prop)
1307 {
1308 case VTERM_PROP_TITLE:
1309 vim_free(term->tl_title);
1310 term->tl_title = vim_strsave((char_u *)value->string);
1311 vim_free(term->tl_status_text);
1312 term->tl_status_text = NULL;
1313 if (term == curbuf->b_term)
1314 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001315 break;
1316
1317 case VTERM_PROP_CURSORVISIBLE:
1318 term->tl_cursor_visible = value->boolean;
1319 may_toggle_cursor(term);
1320 out_flush();
1321 break;
1322
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001323 case VTERM_PROP_ALTSCREEN:
1324 /* TODO: do anything else? */
1325 term->tl_using_altscreen = value->boolean;
1326 break;
1327
Bram Moolenaar21554412017-07-24 21:44:43 +02001328 default:
1329 break;
1330 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001331 /* Always return 1, otherwise vterm doesn't store the value internally. */
1332 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001333}
1334
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001335/*
1336 * The job running in the terminal resized the terminal.
1337 */
1338 static int
1339handle_resize(int rows, int cols, void *user)
1340{
1341 term_T *term = (term_T *)user;
1342 win_T *wp;
1343
1344 term->tl_rows = rows;
1345 term->tl_cols = cols;
1346 FOR_ALL_WINDOWS(wp)
1347 {
1348 if (wp->w_buffer == term->tl_buffer)
1349 {
1350 win_setheight_win(rows, wp);
1351 win_setwidth_win(cols, wp);
1352 }
1353 }
1354
1355 redraw_buf_later(term->tl_buffer, NOT_VALID);
1356 return 1;
1357}
1358
Bram Moolenaard85f2712017-07-28 21:51:57 +02001359/*
1360 * Handle a line that is pushed off the top of the screen.
1361 */
1362 static int
1363handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1364{
1365 term_T *term = (term_T *)user;
1366
1367 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001368 if (ga_grow(&term->tl_scrollback, 1) == OK)
1369 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001370 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001371 int len = 0;
1372 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001373 int c;
1374 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001375 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001376 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001377
1378 /* do not store empty cells at the end */
1379 for (i = 0; i < cols; ++i)
1380 if (cells[i].chars[0] != 0)
1381 len = i + 1;
1382
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001383 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001384 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001385 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001386 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001387 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001388 for (col = 0; col < len; col += cells[col].width)
1389 {
1390 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1391 {
1392 ga.ga_len = 0;
1393 break;
1394 }
1395 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1396 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1397 (char_u *)ga.ga_data + ga.ga_len);
1398 p[col].width = cells[col].width;
1399 p[col].attrs = cells[col].attrs;
1400 p[col].fg = cells[col].fg;
1401 p[col].bg = cells[col].bg;
1402 }
1403 }
1404 if (ga_grow(&ga, 1) == FAIL)
1405 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1406 else
1407 {
1408 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1409 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1410 }
1411 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001412
1413 line = (sb_line_T *)term->tl_scrollback.ga_data
1414 + term->tl_scrollback.ga_len;
1415 line->sb_cols = len;
1416 line->sb_cells = p;
1417 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001418 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001419 }
1420 return 0; /* ignored */
1421}
1422
Bram Moolenaar21554412017-07-24 21:44:43 +02001423static VTermScreenCallbacks screen_callbacks = {
1424 handle_damage, /* damage */
1425 handle_moverect, /* moverect */
1426 handle_movecursor, /* movecursor */
1427 handle_settermprop, /* settermprop */
1428 NULL, /* bell */
1429 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001430 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001431 NULL /* sb_popline */
1432};
1433
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001434/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001435 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001436 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001437 */
1438 void
1439term_channel_closed(channel_T *ch)
1440{
1441 term_T *term;
1442 int did_one = FALSE;
1443
1444 for (term = first_term; term != NULL; term = term->tl_next)
1445 if (term->tl_job == ch->ch_job)
1446 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001447 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001448 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001449
Bram Moolenaard85f2712017-07-28 21:51:57 +02001450 vim_free(term->tl_title);
1451 term->tl_title = NULL;
1452 vim_free(term->tl_status_text);
1453 term->tl_status_text = NULL;
1454
Bram Moolenaar423802d2017-07-30 16:52:24 +02001455 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001456 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001457 {
1458 int fnum = term->tl_buffer->b_fnum;
1459
Bram Moolenaar423802d2017-07-30 16:52:24 +02001460 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001461
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001462 if (term->tl_finish == 'c')
1463 {
1464 /* ++close or term_finish == "close" */
1465 curbuf = term->tl_buffer;
1466 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1467 break;
1468 }
1469 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1470 {
1471 char buf[50];
1472
1473 /* TODO: use term_opencmd */
1474 vim_snprintf(buf, sizeof(buf), "botright sbuf %d", fnum);
1475 do_cmdline_cmd((char_u *)buf);
1476 }
1477 }
1478
Bram Moolenaard85f2712017-07-28 21:51:57 +02001479 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001480 }
1481 if (did_one)
1482 {
1483 redraw_statuslines();
1484
1485 /* Need to break out of vgetc(). */
1486 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001487 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001488
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001489 term = curbuf->b_term;
1490 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001491 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001492 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001493 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001494 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001495 }
1496 }
1497}
1498
1499/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001500 * Reverse engineer the RGB value into a cterm color index.
1501 * First color is 1. Return 0 if no match found.
1502 */
1503 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001504color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001505{
1506 int red = color->red;
1507 int blue = color->blue;
1508 int green = color->green;
1509
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001510 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001511 if (red == 0)
1512 {
1513 if (green == 0)
1514 {
1515 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001516 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001517 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001518 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001519 }
1520 else if (green == 224)
1521 {
1522 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001523 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001524 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001525 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001526 }
1527 }
1528 else if (red == 224)
1529 {
1530 if (green == 0)
1531 {
1532 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001533 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001534 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001535 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001536 }
1537 else if (green == 224)
1538 {
1539 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001540 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001541 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001542 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001543 }
1544 }
1545 else if (red == 128)
1546 {
1547 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001548 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001549 }
1550 else if (red == 255)
1551 {
1552 if (green == 64)
1553 {
1554 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001555 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001556 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001557 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001558 }
1559 else if (green == 255)
1560 {
1561 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001562 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001563 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001564 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001565 }
1566 }
1567 else if (red == 64)
1568 {
1569 if (green == 64)
1570 {
1571 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001572 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001573 }
1574 else if (green == 255)
1575 {
1576 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001577 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001578 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001579 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001580 }
1581 }
1582 if (t_colors >= 256)
1583 {
1584 if (red == blue && red == green)
1585 {
1586 /* 24-color greyscale */
1587 static int cutoff[23] = {
1588 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1589 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1590 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1591 int i;
1592
1593 for (i = 0; i < 23; ++i)
1594 if (red < cutoff[i])
1595 return i + 233;
1596 return 256;
1597 }
1598
1599 /* 216-color cube */
1600 return 17 + ((red + 25) / 0x33) * 36
1601 + ((green + 25) / 0x33) * 6
1602 + (blue + 25) / 0x33;
1603 }
1604 return 0;
1605}
1606
1607/*
1608 * Convert the attributes of a vterm cell into an attribute index.
1609 */
1610 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001611cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001612{
1613 int attr = 0;
1614
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001615 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001616 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001617 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001618 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001619 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001620 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001621 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001622 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001623 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001624 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001625
1626#ifdef FEAT_GUI
1627 if (gui.in_use)
1628 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001629 guicolor_T fg, bg;
1630
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001631 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1632 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001633 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001634 }
1635 else
1636#endif
1637#ifdef FEAT_TERMGUICOLORS
1638 if (p_tgc)
1639 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001640 guicolor_T fg, bg;
1641
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001642 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1643 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001644
1645 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001646 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001647 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001648#endif
1649 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001650 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001651 int fg = color2index(&cellfg, TRUE, &bold);
1652 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001653
1654 /* with 8 colors set the bold attribute to get a bright foreground */
1655 if (bold == TRUE)
1656 attr |= HL_BOLD;
1657 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001658 }
1659 return 0;
1660}
1661
1662/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001663 * Called to update a window that contains an active terminal.
1664 * Returns FAIL when there is no terminal running in this window or in
1665 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001666 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001667 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001668term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001669{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001670 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001671 VTerm *vterm;
1672 VTermScreen *screen;
1673 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001674 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001675
Bram Moolenaar6d819742017-08-06 14:57:49 +02001676 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001677 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001678
Bram Moolenaard85f2712017-07-28 21:51:57 +02001679 vterm = term->tl_vterm;
1680 screen = vterm_obtain_screen(vterm);
1681 state = vterm_obtain_state(vterm);
1682
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001683 /*
1684 * If the window was resized a redraw will be triggered and we get here.
1685 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1686 */
1687 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1688 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001689 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001690 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1691 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1692 win_T *twp;
1693
1694 FOR_ALL_WINDOWS(twp)
1695 {
1696 /* When more than one window shows the same terminal, use the
1697 * smallest size. */
1698 if (twp->w_buffer == term->tl_buffer)
1699 {
1700 if (!term->tl_rows_fixed && rows > twp->w_height)
1701 rows = twp->w_height;
1702 if (!term->tl_cols_fixed && cols > twp->w_width)
1703 cols = twp->w_width;
1704 }
1705 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001706
1707 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001708 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001709 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001710 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001711 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001712
1713 /* The cursor may have been moved when resizing. */
1714 vterm_state_get_cursorpos(state, &pos);
1715 position_cursor(wp, &pos);
1716
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001717 /* TODO: Only redraw what changed. */
1718 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001719 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001720 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001721 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001722
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001723 if (pos.row < term->tl_rows)
1724 {
1725 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001726 {
1727 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001728 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001729
Bram Moolenaareeac6772017-07-23 15:48:37 +02001730 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1731 vim_memset(&cell, 0, sizeof(cell));
1732
1733 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001734 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001735 if (c == NUL)
1736 {
1737 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001738#if defined(FEAT_MBYTE)
1739 if (enc_utf8)
1740 ScreenLinesUC[off] = NUL;
1741#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001742 }
1743 else
1744 {
1745#if defined(FEAT_MBYTE)
1746 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001747 {
1748 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001749 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001750 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001751 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001752 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001753 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001754 if (enc_utf8)
1755 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001756 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001757#else
1758 ScreenLines[off] = c;
1759#endif
1760 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001761 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001762
1763 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001764 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001765 if (cell.width == 2)
1766 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001767 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001768#if defined(FEAT_MBYTE)
1769 if (enc_utf8)
1770 ScreenLinesUC[off] = NUL;
1771#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001772 ++pos.col;
1773 ++off;
1774 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001775 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001776 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001777 else
1778 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001779
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001780 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1781 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001782 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001783
1784 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001785}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001786
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001787/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001788 * Return TRUE if "wp" is a terminal window where the job has finished.
1789 */
1790 int
1791term_is_finished(buf_T *buf)
1792{
1793 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1794}
1795
1796/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001797 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001798 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001799 */
1800 int
1801term_show_buffer(buf_T *buf)
1802{
1803 term_T *term = buf->b_term;
1804
Bram Moolenaar6d819742017-08-06 14:57:49 +02001805 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001806}
1807
1808/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001809 * The current buffer is going to be changed. If there is terminal
1810 * highlighting remove it now.
1811 */
1812 void
1813term_change_in_curbuf(void)
1814{
1815 term_T *term = curbuf->b_term;
1816
1817 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1818 {
1819 free_scrollback(term);
1820 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001821
1822 /* The buffer is now like a normal buffer, it cannot be easily
1823 * abandoned when changed. */
1824 set_string_option_direct((char_u *)"buftype", -1,
1825 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001826 }
1827}
1828
1829/*
1830 * Get the screen attribute for a position in the buffer.
1831 */
1832 int
1833term_get_attr(buf_T *buf, linenr_T lnum, int col)
1834{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001835 term_T *term = buf->b_term;
1836 sb_line_T *line;
1837 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001838
Bram Moolenaar70229f92017-07-29 16:01:53 +02001839 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001840 return 0;
1841 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1842 if (col >= line->sb_cols)
1843 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001844 cellattr = line->sb_cells + col;
1845 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001846}
1847
1848/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001849 * Create a new vterm and initialize it.
1850 */
1851 static void
1852create_vterm(term_T *term, int rows, int cols)
1853{
1854 VTerm *vterm;
1855 VTermScreen *screen;
1856
1857 vterm = vterm_new(rows, cols);
1858 term->tl_vterm = vterm;
1859 screen = vterm_obtain_screen(vterm);
1860 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1861 /* TODO: depends on 'encoding'. */
1862 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001863
1864 /* Vterm uses a default black background. Set it to white when
1865 * 'background' is "light". */
1866 if (*p_bg == 'l')
1867 {
1868 VTermColor fg, bg;
1869
1870 fg.red = fg.green = fg.blue = 0;
1871 bg.red = bg.green = bg.blue = 255;
1872 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1873 }
1874
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001875 /* Required to initialize most things. */
1876 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001877
1878 /* Allow using alternate screen. */
1879 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001880}
1881
Bram Moolenaar21554412017-07-24 21:44:43 +02001882/*
1883 * Return the text to show for the buffer name and status.
1884 */
1885 char_u *
1886term_get_status_text(term_T *term)
1887{
1888 if (term->tl_status_text == NULL)
1889 {
1890 char_u *txt;
1891 size_t len;
1892
Bram Moolenaar6d819742017-08-06 14:57:49 +02001893 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001894 {
1895 if (term_job_running(term))
1896 txt = (char_u *)_("Terminal");
1897 else
1898 txt = (char_u *)_("Terminal-finished");
1899 }
1900 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001901 txt = term->tl_title;
1902 else if (term_job_running(term))
1903 txt = (char_u *)_("running");
1904 else
1905 txt = (char_u *)_("finished");
1906 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001907 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001908 if (term->tl_status_text != NULL)
1909 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1910 term->tl_buffer->b_fname, txt);
1911 }
1912 return term->tl_status_text;
1913}
1914
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001915/*
1916 * Mark references in jobs of terminals.
1917 */
1918 int
1919set_ref_in_term(int copyID)
1920{
1921 int abort = FALSE;
1922 term_T *term;
1923 typval_T tv;
1924
1925 for (term = first_term; term != NULL; term = term->tl_next)
1926 if (term->tl_job != NULL)
1927 {
1928 tv.v_type = VAR_JOB;
1929 tv.vval.v_job = term->tl_job;
1930 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1931 }
1932 return abort;
1933}
1934
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001935/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001936 * Get the buffer from the first argument in "argvars".
1937 * Returns NULL when the buffer is not for a terminal window.
1938 */
1939 static buf_T *
1940term_get_buf(typval_T *argvars)
1941{
1942 buf_T *buf;
1943
1944 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1945 ++emsg_off;
1946 buf = get_buf_tv(&argvars[0], FALSE);
1947 --emsg_off;
1948 if (buf == NULL || buf->b_term == NULL)
1949 return NULL;
1950 return buf;
1951}
1952
1953/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001954 * "term_getaltscreen(buf)" function
1955 */
1956 void
1957f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
1958{
1959 buf_T *buf = term_get_buf(argvars);
1960
1961 if (buf == NULL)
1962 return;
1963 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
1964}
1965
1966/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001967 * "term_getattr(attr, name)" function
1968 */
1969 void
1970f_term_getattr(typval_T *argvars, typval_T *rettv)
1971{
1972 int attr;
1973 size_t i;
1974 char_u *name;
1975
1976 static struct {
1977 char *name;
1978 int attr;
1979 } attrs[] = {
1980 {"bold", HL_BOLD},
1981 {"italic", HL_ITALIC},
1982 {"underline", HL_UNDERLINE},
1983 {"strike", HL_STANDOUT},
1984 {"reverse", HL_INVERSE},
1985 };
1986
1987 attr = get_tv_number(&argvars[0]);
1988 name = get_tv_string_chk(&argvars[1]);
1989 if (name == NULL)
1990 return;
1991
1992 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1993 if (STRCMP(name, attrs[i].name) == 0)
1994 {
1995 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1996 break;
1997 }
1998}
1999
2000/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002001 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002002 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002003 void
2004f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002005{
Bram Moolenaar97870002017-07-30 18:28:38 +02002006 buf_T *buf = term_get_buf(argvars);
2007 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002008
Bram Moolenaar97870002017-07-30 18:28:38 +02002009 if (rettv_list_alloc(rettv) == FAIL)
2010 return;
2011 if (buf == NULL)
2012 return;
2013
2014 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002015 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
2016 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02002017 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002018}
2019
2020/*
2021 * "term_getjob(buf)" function
2022 */
2023 void
2024f_term_getjob(typval_T *argvars, typval_T *rettv)
2025{
2026 buf_T *buf = term_get_buf(argvars);
2027
2028 rettv->v_type = VAR_JOB;
2029 rettv->vval.v_job = NULL;
2030 if (buf == NULL)
2031 return;
2032
2033 rettv->vval.v_job = buf->b_term->tl_job;
2034 if (rettv->vval.v_job != NULL)
2035 ++rettv->vval.v_job->jv_refcount;
2036}
2037
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002038 static int
2039get_row_number(typval_T *tv, term_T *term)
2040{
2041 if (tv->v_type == VAR_STRING
2042 && tv->vval.v_string != NULL
2043 && STRCMP(tv->vval.v_string, ".") == 0)
2044 return term->tl_cursor_pos.row;
2045 return (int)get_tv_number(tv) - 1;
2046}
2047
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002048/*
2049 * "term_getline(buf, row)" function
2050 */
2051 void
2052f_term_getline(typval_T *argvars, typval_T *rettv)
2053{
2054 buf_T *buf = term_get_buf(argvars);
2055 term_T *term;
2056 int row;
2057
2058 rettv->v_type = VAR_STRING;
2059 if (buf == NULL)
2060 return;
2061 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002062 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002063
2064 if (term->tl_vterm == NULL)
2065 {
2066 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2067
2068 /* vterm is finished, get the text from the buffer */
2069 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2070 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2071 }
2072 else
2073 {
2074 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2075 VTermRect rect;
2076 int len;
2077 char_u *p;
2078
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002079 if (row < 0 || row >= term->tl_rows)
2080 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002081 len = term->tl_cols * MB_MAXBYTES + 1;
2082 p = alloc(len);
2083 if (p == NULL)
2084 return;
2085 rettv->vval.v_string = p;
2086
2087 rect.start_col = 0;
2088 rect.end_col = term->tl_cols;
2089 rect.start_row = row;
2090 rect.end_row = row + 1;
2091 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2092 }
2093}
2094
2095/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002096 * "term_getscrolled(buf)" function
2097 */
2098 void
2099f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2100{
2101 buf_T *buf = term_get_buf(argvars);
2102
2103 if (buf == NULL)
2104 return;
2105 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2106}
2107
2108/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002109 * "term_getsize(buf)" function
2110 */
2111 void
2112f_term_getsize(typval_T *argvars, typval_T *rettv)
2113{
2114 buf_T *buf = term_get_buf(argvars);
2115 list_T *l;
2116
2117 if (rettv_list_alloc(rettv) == FAIL)
2118 return;
2119 if (buf == NULL)
2120 return;
2121
2122 l = rettv->vval.v_list;
2123 list_append_number(l, buf->b_term->tl_rows);
2124 list_append_number(l, buf->b_term->tl_cols);
2125}
2126
2127/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002128 * "term_getstatus(buf)" function
2129 */
2130 void
2131f_term_getstatus(typval_T *argvars, typval_T *rettv)
2132{
2133 buf_T *buf = term_get_buf(argvars);
2134 term_T *term;
2135 char_u val[100];
2136
2137 rettv->v_type = VAR_STRING;
2138 if (buf == NULL)
2139 return;
2140 term = buf->b_term;
2141
2142 if (term_job_running(term))
2143 STRCPY(val, "running");
2144 else
2145 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002146 if (term->tl_normal_mode)
2147 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002148 rettv->vval.v_string = vim_strsave(val);
2149}
2150
2151/*
2152 * "term_gettitle(buf)" function
2153 */
2154 void
2155f_term_gettitle(typval_T *argvars, typval_T *rettv)
2156{
2157 buf_T *buf = term_get_buf(argvars);
2158
2159 rettv->v_type = VAR_STRING;
2160 if (buf == NULL)
2161 return;
2162
2163 if (buf->b_term->tl_title != NULL)
2164 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2165}
2166
2167/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002168 * "term_gettty(buf)" function
2169 */
2170 void
2171f_term_gettty(typval_T *argvars, typval_T *rettv)
2172{
2173 buf_T *buf = term_get_buf(argvars);
2174 char_u *p;
2175
2176 rettv->v_type = VAR_STRING;
2177 if (buf == NULL)
2178 return;
2179 if (buf->b_term->tl_job != NULL)
2180 p = buf->b_term->tl_job->jv_tty_name;
2181 else
2182 p = buf->b_term->tl_tty_name;
2183 if (p != NULL)
2184 rettv->vval.v_string = vim_strsave(p);
2185}
2186
2187/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002188 * "term_list()" function
2189 */
2190 void
2191f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2192{
2193 term_T *tp;
2194 list_T *l;
2195
2196 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2197 return;
2198
2199 l = rettv->vval.v_list;
2200 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2201 if (tp != NULL && tp->tl_buffer != NULL)
2202 if (list_append_number(l,
2203 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2204 return;
2205}
2206
2207/*
2208 * "term_scrape(buf, row)" function
2209 */
2210 void
2211f_term_scrape(typval_T *argvars, typval_T *rettv)
2212{
2213 buf_T *buf = term_get_buf(argvars);
2214 VTermScreen *screen = NULL;
2215 VTermPos pos;
2216 list_T *l;
2217 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002218 char_u *p;
2219 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002220
2221 if (rettv_list_alloc(rettv) == FAIL)
2222 return;
2223 if (buf == NULL)
2224 return;
2225 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002226
2227 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002228 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002229
2230 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002231 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002232 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002233 p = NULL;
2234 line = NULL;
2235 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002236 else
2237 {
2238 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2239
2240 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2241 return;
2242 p = ml_get_buf(buf, lnum + 1, FALSE);
2243 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2244 }
2245
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002246 for (pos.col = 0; pos.col < term->tl_cols; )
2247 {
2248 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002249 int width;
2250 VTermScreenCellAttrs attrs;
2251 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002252 char_u rgb[8];
2253 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2254 int off = 0;
2255 int i;
2256
2257 if (screen == NULL)
2258 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002259 cellattr_T *cellattr;
2260 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002261
2262 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002263 if (pos.col >= line->sb_cols)
2264 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002265 cellattr = line->sb_cells + pos.col;
2266 width = cellattr->width;
2267 attrs = cellattr->attrs;
2268 fg = cellattr->fg;
2269 bg = cellattr->bg;
2270 len = MB_PTR2LEN(p);
2271 mch_memmove(mbs, p, len);
2272 mbs[len] = NUL;
2273 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002274 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002275 else
2276 {
2277 VTermScreenCell cell;
2278 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2279 break;
2280 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2281 {
2282 if (cell.chars[i] == 0)
2283 break;
2284 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2285 }
2286 mbs[off] = NUL;
2287 width = cell.width;
2288 attrs = cell.attrs;
2289 fg = cell.fg;
2290 bg = cell.bg;
2291 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002292 dcell = dict_alloc();
2293 list_append_dict(l, dcell);
2294
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002295 dict_add_nr_str(dcell, "chars", 0, mbs);
2296
2297 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002298 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002299 dict_add_nr_str(dcell, "fg", 0, rgb);
2300 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002301 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002302 dict_add_nr_str(dcell, "bg", 0, rgb);
2303
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002304 dict_add_nr_str(dcell, "attr",
2305 cell2attr(attrs, fg, bg), NULL);
2306 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002307
2308 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002309 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002310 ++pos.col;
2311 }
2312}
2313
2314/*
2315 * "term_sendkeys(buf, keys)" function
2316 */
2317 void
2318f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2319{
2320 buf_T *buf = term_get_buf(argvars);
2321 char_u *msg;
2322 term_T *term;
2323
2324 rettv->v_type = VAR_UNKNOWN;
2325 if (buf == NULL)
2326 return;
2327
2328 msg = get_tv_string_chk(&argvars[1]);
2329 if (msg == NULL)
2330 return;
2331 term = buf->b_term;
2332 if (term->tl_vterm == NULL)
2333 return;
2334
2335 while (*msg != NUL)
2336 {
2337 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2338 msg += MB_PTR2LEN(msg);
2339 }
2340
Bram Moolenaar6d819742017-08-06 14:57:49 +02002341 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002342 {
2343 /* TODO: only update once in a while. */
2344 update_screen(0);
2345 if (buf == curbuf)
2346 update_cursor(term, TRUE);
2347 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002348}
2349
2350/*
2351 * "term_start(command, options)" function
2352 */
2353 void
2354f_term_start(typval_T *argvars, typval_T *rettv)
2355{
2356 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002357 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002358
2359 if (cmd == NULL)
2360 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002361 init_job_options(&opt);
2362 /* TODO: allow more job options */
2363 if (argvars[1].v_type != VAR_UNKNOWN
2364 && get_job_options(&argvars[1], &opt,
2365 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002366 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
2367 JO2_TERM_NAME + JO2_TERM_FINISH
2368 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL
2369 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002370 return;
2371
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002372 if (opt.jo_vertical)
2373 cmdmod.split = WSP_VERT;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002374 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002375
2376 if (curbuf->b_term != NULL)
2377 rettv->vval.v_number = curbuf->b_fnum;
2378}
2379
2380/*
2381 * "term_wait" function
2382 */
2383 void
2384f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2385{
2386 buf_T *buf = term_get_buf(argvars);
2387
2388 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002389 {
2390 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002391 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002392 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002393 if (buf->b_term->tl_job == NULL)
2394 {
2395 ch_log(NULL, "term_wait(): no job to wait for");
2396 return;
2397 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002398
2399 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002400 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002401 {
2402 /* The job is dead, keep reading channel I/O until the channel is
2403 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002404 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002405 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2406 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002407 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002408 parse_queued_messages();
2409 ui_delay(10L, FALSE);
2410 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002411 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002412 parse_queued_messages();
2413 }
2414 else
2415 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002416 long wait = 10L;
2417
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002418 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002419 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002420
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002421 /* Wait for some time for any channel I/O. */
2422 if (argvars[1].v_type != VAR_UNKNOWN)
2423 wait = get_tv_number(&argvars[1]);
2424 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002425 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002426
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002427 /* Flushing messages on channels is hopefully sufficient.
2428 * TODO: is there a better way? */
2429 parse_queued_messages();
2430 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002431}
2432
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002433# ifdef WIN3264
2434
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002435/**************************************
2436 * 2. MS-Windows implementation.
2437 */
2438
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002439#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2440#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2441
Bram Moolenaar8a773062017-07-24 22:29:21 +02002442void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002443void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002444void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002445BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2446void (*winpty_config_set_initial_size)(void*, int, int);
2447LPCWSTR (*winpty_conin_name)(void*);
2448LPCWSTR (*winpty_conout_name)(void*);
2449LPCWSTR (*winpty_conerr_name)(void*);
2450void (*winpty_free)(void*);
2451void (*winpty_config_free)(void*);
2452void (*winpty_spawn_config_free)(void*);
2453void (*winpty_error_free)(void*);
2454LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002455BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002456HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002457
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002458#define WINPTY_DLL "winpty.dll"
2459
2460static HINSTANCE hWinPtyDLL = NULL;
2461
2462 int
2463dyn_winpty_init(void)
2464{
2465 int i;
2466 static struct
2467 {
2468 char *name;
2469 FARPROC *ptr;
2470 } winpty_entry[] =
2471 {
2472 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2473 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2474 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2475 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2476 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2477 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2478 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2479 {"winpty_free", (FARPROC*)&winpty_free},
2480 {"winpty_open", (FARPROC*)&winpty_open},
2481 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2482 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2483 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2484 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002485 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002486 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002487 {NULL, NULL}
2488 };
2489
2490 /* No need to initialize twice. */
2491 if (hWinPtyDLL)
2492 return 1;
2493 /* Load winpty.dll */
2494 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2495 if (!hWinPtyDLL)
2496 {
2497 EMSG2(_(e_loadlib), WINPTY_DLL);
2498 return 0;
2499 }
2500 for (i = 0; winpty_entry[i].name != NULL
2501 && winpty_entry[i].ptr != NULL; ++i)
2502 {
2503 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2504 winpty_entry[i].name)) == NULL)
2505 {
2506 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2507 return 0;
2508 }
2509 }
2510
2511 return 1;
2512}
2513
2514/*
2515 * Create a new terminal of "rows" by "cols" cells.
2516 * Store a reference in "term".
2517 * Return OK or FAIL.
2518 */
2519 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002520term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002521{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002522 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002523 channel_T *channel = NULL;
2524 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002525 DWORD error;
2526 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2527 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002528 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002529 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002530
2531 if (!dyn_winpty_init())
2532 return FAIL;
2533
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002534 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002535 if (p == NULL)
2536 return FAIL;
2537
2538 job = job_alloc();
2539 if (job == NULL)
2540 goto failed;
2541
2542 channel = add_channel();
2543 if (channel == NULL)
2544 goto failed;
2545
2546 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2547 if (term->tl_winpty_config == NULL)
2548 goto failed;
2549
2550 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2551 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2552 if (term->tl_winpty == NULL)
2553 goto failed;
2554
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002555 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002556 spawn_config = winpty_spawn_config_new(
2557 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2558 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2559 NULL,
2560 p,
2561 NULL,
2562 NULL,
2563 &winpty_err);
2564 if (spawn_config == NULL)
2565 goto failed;
2566
2567 channel = add_channel();
2568 if (channel == NULL)
2569 goto failed;
2570
2571 job = job_alloc();
2572 if (job == NULL)
2573 goto failed;
2574
2575 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2576 &child_thread_handle, &error, &winpty_err))
2577 goto failed;
2578
2579 channel_set_pipes(channel,
2580 (sock_T) CreateFileW(
2581 winpty_conin_name(term->tl_winpty),
2582 GENERIC_WRITE, 0, NULL,
2583 OPEN_EXISTING, 0, NULL),
2584 (sock_T) CreateFileW(
2585 winpty_conout_name(term->tl_winpty),
2586 GENERIC_READ, 0, NULL,
2587 OPEN_EXISTING, 0, NULL),
2588 (sock_T) CreateFileW(
2589 winpty_conerr_name(term->tl_winpty),
2590 GENERIC_READ, 0, NULL,
2591 OPEN_EXISTING, 0, NULL));
2592
2593 jo = CreateJobObject(NULL, NULL);
2594 if (jo == NULL)
2595 goto failed;
2596
2597 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002598 {
2599 /* Failed, switch the way to terminate process with TerminateProcess. */
2600 CloseHandle(jo);
2601 jo = NULL;
2602 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002603
2604 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002605 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002606
2607 create_vterm(term, rows, cols);
2608
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002609 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002610 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002611
2612 job->jv_channel = channel;
2613 job->jv_proc_info.hProcess = child_process_handle;
2614 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2615 job->jv_job_object = jo;
2616 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002617 sprintf(buf, "winpty://%lu",
2618 GetProcessId(winpty_agent_process(term->tl_winpty)));
2619 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002620 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002621 term->tl_job = job;
2622
2623 return OK;
2624
2625failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002626 if (spawn_config != NULL)
2627 winpty_spawn_config_free(spawn_config);
2628 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002629 if (channel != NULL)
2630 channel_clear(channel);
2631 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002632 {
2633 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002634 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002635 }
2636 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002637 if (jo != NULL)
2638 CloseHandle(jo);
2639 if (term->tl_winpty != NULL)
2640 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002641 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002642 if (term->tl_winpty_config != NULL)
2643 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002644 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002645 if (winpty_err != NULL)
2646 {
2647 char_u *msg = utf16_to_enc(
2648 (short_u *)winpty_error_msg(winpty_err), NULL);
2649
2650 EMSG(msg);
2651 winpty_error_free(winpty_err);
2652 }
2653 return FAIL;
2654}
2655
2656/*
2657 * Free the terminal emulator part of "term".
2658 */
2659 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002660term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002661{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002662 if (term->tl_winpty != NULL)
2663 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002664 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002665 if (term->tl_winpty_config != NULL)
2666 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002667 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002668 if (term->tl_vterm != NULL)
2669 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002670 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002671}
2672
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002673/*
2674 * Request size to terminal.
2675 */
2676 static void
2677term_report_winsize(term_T *term, int rows, int cols)
2678{
2679 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2680}
2681
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002682# else
2683
2684/**************************************
2685 * 3. Unix-like implementation.
2686 */
2687
2688/*
2689 * Create a new terminal of "rows" by "cols" cells.
2690 * Start job for "cmd".
2691 * Store the pointers in "term".
2692 * Return OK or FAIL.
2693 */
2694 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002695term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002696{
2697 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002698
2699 create_vterm(term, rows, cols);
2700
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002701 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002702 argvars[0].v_type = VAR_STRING;
2703 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002704
2705 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002706 if (term->tl_job != NULL)
2707 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002708
Bram Moolenaar61a66052017-07-22 18:39:00 +02002709 return term->tl_job != NULL
2710 && term->tl_job->jv_channel != NULL
2711 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002712}
2713
2714/*
2715 * Free the terminal emulator part of "term".
2716 */
2717 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002718term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002719{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002720 if (term->tl_vterm != NULL)
2721 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002722 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002723}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002724
2725/*
2726 * Request size to terminal.
2727 */
2728 static void
2729term_report_winsize(term_T *term, int rows, int cols)
2730{
2731 /* Use an ioctl() to report the new window size to the job. */
2732 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2733 {
2734 int fd = -1;
2735 int part;
2736
2737 for (part = PART_OUT; part < PART_COUNT; ++part)
2738 {
2739 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2740 if (isatty(fd))
2741 break;
2742 }
2743 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002744 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002745 }
2746}
2747
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002748# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002749
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002750#endif /* FEAT_TERMINAL */