blob: a4120b39c63f150e69eda3be8abada7eb08ed3f9 [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 Moolenaard8dc1792017-08-03 11:55:21 +020039 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar423802d2017-07-30 16:52:24 +020040 * - in bash mouse clicks are inserting characters.
41 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020042 * - add argument to term_wait() for waiting time.
Bram Moolenaard85f2712017-07-28 21:51:57 +020043 * - For the scrollback buffer store lines in the buffer, only attributes in
44 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020045 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020046 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020047 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
48 * job finishes).
49 * - add option values to the command:
50 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020051 * - support different cursor shapes, colors and attributes
52 * - make term_getcursor() return type (none/block/bar/underline) and
53 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020054 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020055 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020056 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020057 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020058 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020059 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020060 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
61 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020062 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020063 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020064 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020066 * - implement "term" for job_start(): more job options when starting a
67 * terminal.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020068 * - support ":term NONE" to open a terminal with a pty but not running a job
69 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020070 * - if the job in the terminal does not support the mouse, we can use the
71 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020072 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
73 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020074 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020075 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020076 * - Copy text in the vterm to the Vim buffer once in a while, so that
77 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078 */
79
80#include "vim.h"
81
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020082#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020083
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020084#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020085# define MIN(x,y) (x < y ? x : y)
86# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020087#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020088
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020089#include "libvterm/include/vterm.h"
90
Bram Moolenaard85f2712017-07-28 21:51:57 +020091typedef struct sb_line_S {
92 int sb_cols; /* can differ per line */
93 VTermScreenCell *sb_cells; /* allocated */
94} sb_line_T;
95
Bram Moolenaare4f25e42017-07-07 11:54:15 +020096/* typedef term_T in structs.h */
97struct terminal_S {
98 term_T *tl_next;
99
Bram Moolenaar423802d2017-07-30 16:52:24 +0200100 VTerm *tl_vterm;
101 job_T *tl_job;
102 buf_T *tl_buffer;
103
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200104 /* used when tl_job is NULL and only a pty was created */
105 int tl_tty_fd;
106 char_u *tl_tty_name;
107
Bram Moolenaar423802d2017-07-30 16:52:24 +0200108 int tl_terminal_mode;
109 int tl_channel_closed;
110
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200111#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200112 void *tl_winpty_config;
113 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200114#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200115
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200116 /* last known vterm size */
117 int tl_rows;
118 int tl_cols;
119 /* vterm size does not follow window size */
120 int tl_rows_fixed;
121 int tl_cols_fixed;
122
Bram Moolenaar21554412017-07-24 21:44:43 +0200123 char_u *tl_title; /* NULL or allocated */
124 char_u *tl_status_text; /* NULL or allocated */
125
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126 /* Range of screen rows to update. Zero based. */
127 int tl_dirty_row_start; /* -1 if nothing dirty */
128 int tl_dirty_row_end; /* row below last one to update */
129
Bram Moolenaard85f2712017-07-28 21:51:57 +0200130 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200131 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200132
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200133 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200134 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200135};
136
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200137/*
138 * List of all active terminals.
139 */
140static term_T *first_term = NULL;
141
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200142
143#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
144#define KEY_BUF_LEN 200
145
146/*
147 * Functions with separate implementation for MS-Windows and Unix-like systems.
148 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200149static 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 +0200150static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200151static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200152
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200153/**************************************
154 * 1. Generic code for all systems.
155 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200156
157/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200158 * Determine the terminal size from 'termsize' and the current window.
159 * Assumes term->tl_rows and term->tl_cols are zero.
160 */
161 static void
162set_term_and_win_size(term_T *term)
163{
164 if (*curwin->w_p_tms != NUL)
165 {
166 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
167
168 term->tl_rows = atoi((char *)curwin->w_p_tms);
169 term->tl_cols = atoi((char *)p);
170 }
171 if (term->tl_rows == 0)
172 term->tl_rows = curwin->w_height;
173 else
174 {
175 win_setheight_win(term->tl_rows, curwin);
176 term->tl_rows_fixed = TRUE;
177 }
178 if (term->tl_cols == 0)
179 term->tl_cols = curwin->w_width;
180 else
181 {
182 win_setwidth_win(term->tl_cols, curwin);
183 term->tl_cols_fixed = TRUE;
184 }
185}
186
187/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200188 * Initialize job options for a terminal job.
189 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200190 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200191 static void
192init_job_options(jobopt_T *opt)
193{
194 clear_job_options(opt);
195
196 opt->jo_mode = MODE_RAW;
197 opt->jo_out_mode = MODE_RAW;
198 opt->jo_err_mode = MODE_RAW;
199 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
200
201 opt->jo_io[PART_OUT] = JIO_BUFFER;
202 opt->jo_io[PART_ERR] = JIO_BUFFER;
203 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
204
205 opt->jo_modifiable[PART_OUT] = 0;
206 opt->jo_modifiable[PART_ERR] = 0;
207 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
208
209 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
210}
211
212/*
213 * Set job options mandatory for a terminal job.
214 */
215 static void
216setup_job_options(jobopt_T *opt, int rows, int cols)
217{
218 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
219 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
220 opt->jo_pty = TRUE;
221 opt->jo_term_rows = rows;
222 opt->jo_term_cols = cols;
223}
224
225 static void
226term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200227{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200228 exarg_T split_ea;
229 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200230 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200231
232 if (check_restricted() || check_secure())
233 return;
234
235 term = (term_T *)alloc_clear(sizeof(term_T));
236 if (term == NULL)
237 return;
238 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200239 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200240 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200241
242 /* Open a new window or tab. */
243 vim_memset(&split_ea, 0, sizeof(split_ea));
244 split_ea.cmdidx = CMD_new;
245 split_ea.cmd = (char_u *)"new";
246 split_ea.arg = (char_u *)"";
247 ex_splitview(&split_ea);
248 if (curwin == old_curwin)
249 {
250 /* split failed */
251 vim_free(term);
252 return;
253 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200254 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200255 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200256
257 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200258 term->tl_next = first_term;
259 first_term = term;
260
Bram Moolenaar293424c2017-07-26 23:11:01 +0200261 if (cmd == NULL || *cmd == NUL)
262 cmd = p_sh;
263
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200264 {
265 int i;
266 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200267 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200268
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200269 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200270 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200271 /* Prepend a ! to the command name to avoid the buffer name equals
272 * the executable, otherwise ":w!" would overwrite it. */
273 if (i == 0)
274 vim_snprintf((char *)p, len, "!%s", cmd);
275 else
276 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200277 if (buflist_findname(p) == NULL)
278 {
279 curbuf->b_ffname = p;
280 break;
281 }
282 }
283 }
284 curbuf->b_fname = curbuf->b_ffname;
285
Bram Moolenaareb44a682017-08-03 22:44:55 +0200286 set_string_option_direct((char_u *)"buftype", -1,
287 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
288
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200289 /* Mark the buffer as not modifiable. It can only be made modifiable after
290 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200291 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200292
293 /* Set 'bufhidden' to "hide": allow closing the window. */
294 set_string_option_direct((char_u *)"bufhidden", -1,
295 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200296
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200297 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200298 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200299
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200300 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200301 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200302 {
303 /* store the size we ended up with */
304 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200305 }
306 else
307 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200308 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200309
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200310 /* Wiping out the buffer will also close the window and call
311 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200312 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200313 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200314}
315
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200316/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200317 * ":terminal": open a terminal window and execute a job in it.
318 */
319 void
320ex_terminal(exarg_T *eap)
321{
322 jobopt_T opt;
323
324 init_job_options(&opt);
325 /* TODO: get options from before the command */
326
327 term_start(eap->arg, &opt);
328}
329
330/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200331 * Free the scrollback buffer for "term".
332 */
333 static void
334free_scrollback(term_T *term)
335{
336 int i;
337
338 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
339 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
340 ga_clear(&term->tl_scrollback);
341}
342
343/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200344 * Free a terminal and everything it refers to.
345 * Kills the job if there is one.
346 * Called when wiping out a buffer.
347 */
348 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200349free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200350{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200351 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200352 term_T *tp;
353
354 if (term == NULL)
355 return;
356 if (first_term == term)
357 first_term = term->tl_next;
358 else
359 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
360 if (tp->tl_next == term)
361 {
362 tp->tl_next = term->tl_next;
363 break;
364 }
365
366 if (term->tl_job != NULL)
367 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200368 if (term->tl_job->jv_status != JOB_ENDED
369 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200370 job_stop(term->tl_job, NULL, "kill");
371 job_unref(term->tl_job);
372 }
373
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200374 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200375
376 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200377 vim_free(term->tl_title);
378 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200379 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200380 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200381}
382
383/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200384 * Write job output "msg[len]" to the vterm.
385 */
386 static void
387term_write_job_output(term_T *term, char_u *msg, size_t len)
388{
389 VTerm *vterm = term->tl_vterm;
390 char_u *p;
391 size_t done;
392 size_t len_now;
393
394 for (done = 0; done < len; done += len_now)
395 {
396 for (p = msg + done; p < msg + len; )
397 {
398 if (*p == NL)
399 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200400 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200401 }
402 len_now = p - msg - done;
403 vterm_input_write(vterm, (char *)msg + done, len_now);
404 if (p < msg + len && *p == NL)
405 {
406 /* Convert NL to CR-NL, that appears to work best. */
407 vterm_input_write(vterm, "\r\n", 2);
408 ++len_now;
409 }
410 }
411
412 /* this invokes the damage callbacks */
413 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
414}
415
Bram Moolenaar1c844932017-07-24 23:36:41 +0200416 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200417update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200418{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200419 if (term->tl_terminal_mode)
420 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200421 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200422 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200423 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200424 if (term->tl_cursor_visible)
425 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200426 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200427#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200428 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200429 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200430#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200431 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200432}
433
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200434/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200435 * Invoked when "msg" output from a job was received. Write it to the terminal
436 * of "buffer".
437 */
438 void
439write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
440{
441 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200442 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200443
Bram Moolenaard85f2712017-07-28 21:51:57 +0200444 if (term->tl_vterm == NULL)
445 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200446 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200447 return;
448 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200449 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200450 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200451
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200452 if (!term->tl_terminal_mode)
453 {
454 /* TODO: only update once in a while. */
455 update_screen(0);
456 update_cursor(term, TRUE);
457 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200458}
459
460/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200461 * Send a mouse position and click to the vterm
462 */
463 static int
464term_send_mouse(VTerm *vterm, int button, int pressed)
465{
466 VTermModifier mod = VTERM_MOD_NONE;
467
468 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
469 mouse_col - W_WINCOL(curwin), mod);
470 vterm_mouse_button(vterm, button, pressed, mod);
471 return TRUE;
472}
473
474/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200475 * Convert typed key "c" into bytes to send to the job.
476 * Return the number of bytes in "buf".
477 */
478 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200479term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200480{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200481 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200482 VTermKey key = VTERM_KEY_NONE;
483 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200484 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200485
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200486 switch (c)
487 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200488 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200489 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200490 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
491 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200492 case K_DEL: key = VTERM_KEY_DEL; break;
493 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200494 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
495 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200496 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200497 case K_S_END: mod = VTERM_MOD_SHIFT;
498 key = VTERM_KEY_END; break;
499 case K_C_END: mod = VTERM_MOD_CTRL;
500 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200501 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
502 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
503 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
504 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
505 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
506 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
507 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
508 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
509 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
510 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
511 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
512 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
513 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200514 case K_S_HOME: mod = VTERM_MOD_SHIFT;
515 key = VTERM_KEY_HOME; break;
516 case K_C_HOME: mod = VTERM_MOD_CTRL;
517 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200518 case K_INS: key = VTERM_KEY_INS; break;
519 case K_K0: key = VTERM_KEY_KP_0; break;
520 case K_K1: key = VTERM_KEY_KP_1; break;
521 case K_K2: key = VTERM_KEY_KP_2; break;
522 case K_K3: key = VTERM_KEY_KP_3; break;
523 case K_K4: key = VTERM_KEY_KP_4; break;
524 case K_K5: key = VTERM_KEY_KP_5; break;
525 case K_K6: key = VTERM_KEY_KP_6; break;
526 case K_K7: key = VTERM_KEY_KP_7; break;
527 case K_K8: key = VTERM_KEY_KP_8; break;
528 case K_K9: key = VTERM_KEY_KP_9; break;
529 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
530 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
531 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
532 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
533 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
534 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
535 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
536 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
537 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
538 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
539 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
540 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
541 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200542 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
543 key = VTERM_KEY_LEFT; break;
544 case K_C_LEFT: mod = VTERM_MOD_CTRL;
545 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200546 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
547 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
548 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200549 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
550 key = VTERM_KEY_RIGHT; break;
551 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
552 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200553 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200554 case K_S_UP: mod = VTERM_MOD_SHIFT;
555 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200556 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200557
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200558 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
559 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
560 case K_MOUSELEFT: /* TODO */ return 0;
561 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200562
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200563 case K_LEFTMOUSE:
564 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
565 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
566 case K_LEFTRELEASE:
567 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
568 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
569 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
570 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
571 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
572 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
573 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
574 case K_X1MOUSE: /* TODO */ return 0;
575 case K_X1DRAG: /* TODO */ return 0;
576 case K_X1RELEASE: /* TODO */ return 0;
577 case K_X2MOUSE: /* TODO */ return 0;
578 case K_X2DRAG: /* TODO */ return 0;
579 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200580
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200581 case K_IGNORE: return 0;
582 case K_NOP: return 0;
583 case K_UNDO: return 0;
584 case K_HELP: return 0;
585 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
586 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
587 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
588 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
589 case K_SELECT: return 0;
590#ifdef FEAT_GUI
591 case K_VER_SCROLLBAR: return 0;
592 case K_HOR_SCROLLBAR: return 0;
593#endif
594#ifdef FEAT_GUI_TABLINE
595 case K_TABLINE: return 0;
596 case K_TABMENU: return 0;
597#endif
598#ifdef FEAT_NETBEANS_INTG
599 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
600#endif
601#ifdef FEAT_DND
602 case K_DROP: return 0;
603#endif
604#ifdef FEAT_AUTOCMD
605 case K_CURSORHOLD: return 0;
606#endif
607 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
608 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200609 }
610
611 /*
612 * Convert special keys to vterm keys:
613 * - Write keys to vterm: vterm_keyboard_key()
614 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200615 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200616 */
617 if (key != VTERM_KEY_NONE)
618 /* Special key, let vterm convert it. */
619 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200620 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200621 /* Normal character, let vterm convert it. */
622 vterm_keyboard_unichar(vterm, c, mod);
623
624 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200625 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200626}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200627
Bram Moolenaar938783d2017-07-16 20:13:26 +0200628/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200629 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200630 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200631 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200632term_job_running(term_T *term)
633{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200634 /* Also consider the job finished when the channel is closed, to avoid a
635 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200636 return term != NULL
637 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200638 && term->tl_job->jv_status == JOB_STARTED
639 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200640}
641
642/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200643 * Add the last line of the scrollback buffer to the buffer in the window.
644 */
645 static void
646add_scrollback_line_to_buffer(term_T *term)
647{
648 linenr_T lnum = term->tl_scrollback.ga_len - 1;
649 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
650 garray_T ga;
651 int c;
652 int col;
653 int i;
654
655 ga_init2(&ga, 1, 100);
656 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
657 {
658 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
659 goto failed;
660 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
661 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
662 (char_u *)ga.ga_data + ga.ga_len);
663 }
664 if (ga_grow(&ga, 1) == FAIL)
665 goto failed;
666 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
667 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
668
669 if (lnum == 0)
670 {
671 /* Delete the empty line that was in the empty buffer. */
672 curbuf = term->tl_buffer;
673 ml_delete(2, FALSE);
674 curbuf = curwin->w_buffer;
675 }
676
677failed:
678 ga_clear(&ga);
679}
680
681/*
682 * Add the current lines of the terminal to scrollback and to the buffer.
683 * Called after the job has ended and when switching to Terminal mode.
684 */
685 static void
686move_terminal_to_buffer(term_T *term)
687{
688 win_T *wp;
689 int len;
690 int lines_skipped = 0;
691 VTermPos pos;
692 VTermScreenCell cell;
693 VTermScreenCell *p;
694 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
695
696 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
697 {
698 len = 0;
699 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
700 if (vterm_screen_get_cell(screen, pos, &cell) != 0
701 && cell.chars[0] != NUL)
702 len = pos.col + 1;
703
704 if (len == 0)
705 ++lines_skipped;
706 else
707 {
708 while (lines_skipped > 0)
709 {
710 /* Line was skipped, add an empty line. */
711 --lines_skipped;
712 if (ga_grow(&term->tl_scrollback, 1) == OK)
713 {
714 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
715 + term->tl_scrollback.ga_len;
716
717 line->sb_cols = 0;
718 line->sb_cells = NULL;
719 ++term->tl_scrollback.ga_len;
720
721 add_scrollback_line_to_buffer(term);
722 }
723 }
724
725 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
726 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
727 {
728 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
729 + term->tl_scrollback.ga_len;
730
731 for (pos.col = 0; pos.col < len; ++pos.col)
732 {
733 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
734 vim_memset(p + pos.col, 0, sizeof(cell));
735 else
736 p[pos.col] = cell;
737 }
738 line->sb_cols = len;
739 line->sb_cells = p;
740 ++term->tl_scrollback.ga_len;
741
742 add_scrollback_line_to_buffer(term);
743 }
744 else
745 vim_free(p);
746 }
747 }
748
749 FOR_ALL_WINDOWS(wp)
750 {
751 if (wp->w_buffer == term->tl_buffer)
752 {
753 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
754 wp->w_cursor.col = 0;
755 wp->w_valid = 0;
756 redraw_win_later(wp, NOT_VALID);
757 }
758 }
759}
760
761 static void
762set_terminal_mode(term_T *term, int on)
763{
764 term->tl_terminal_mode = on;
765 vim_free(term->tl_status_text);
766 term->tl_status_text = NULL;
767 if (term->tl_buffer == curbuf)
768 maketitle();
769}
770
771/*
772 * Called after the job if finished and Terminal mode is not active:
773 * Move the vterm contents into the scrollback buffer and free the vterm.
774 */
775 static void
776cleanup_vterm(term_T *term)
777{
778 move_terminal_to_buffer(term);
779 term_free_vterm(term);
780 set_terminal_mode(term, FALSE);
781}
782
783/*
784 * Switch from sending keys to the job to Terminal-Normal mode.
785 * Suspends updating the terminal window.
786 */
787 static void
788term_enter_terminal_mode()
789{
790 term_T *term = curbuf->b_term;
791
792 /* Append the current terminal contents to the buffer. */
793 move_terminal_to_buffer(term);
794
795 set_terminal_mode(term, TRUE);
796}
797
798/*
799 * Returns TRUE if the current window contains a terminal and we are in
800 * Terminal-Normal mode.
801 */
802 int
803term_in_terminal_mode()
804{
805 term_T *term = curbuf->b_term;
806
807 return term != NULL && term->tl_terminal_mode;
808}
809
810/*
811 * Switch from Terminal-Normal mode to sending keys to the job.
812 * Restores updating the terminal window.
813 */
814 void
815term_leave_terminal_mode()
816{
817 term_T *term = curbuf->b_term;
818 sb_line_T *line;
819 garray_T *gap;
820
821 /* Remove the terminal contents from the scrollback and the buffer. */
822 gap = &term->tl_scrollback;
823 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
824 {
825 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
826 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
827 vim_free(line->sb_cells);
828 --gap->ga_len;
829 if (gap->ga_len == 0)
830 break;
831 }
832 check_cursor();
833
834 set_terminal_mode(term, FALSE);
835
836 if (term->tl_channel_closed)
837 cleanup_vterm(term);
838 redraw_buf_and_status_later(curbuf, NOT_VALID);
839}
840
841/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200842 * Get a key from the user without mapping.
843 * TODO: use terminal mode mappings.
844 */
845 static int
846term_vgetc()
847{
848 int c;
849
850 ++no_mapping;
851 ++allow_keys;
852 got_int = FALSE;
853 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200854 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200855 --no_mapping;
856 --allow_keys;
857 return c;
858}
859
860/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200861 * Send keys to terminal.
862 */
863 static int
864send_keys_to_term(term_T *term, int c, int typed)
865{
866 char msg[KEY_BUF_LEN];
867 size_t len;
868 static int mouse_was_outside = FALSE;
869 int dragging_outside = FALSE;
870
871 /* Catch keys that need to be handled as in Normal mode. */
872 switch (c)
873 {
874 case NUL:
875 case K_ZERO:
876 if (typed)
877 stuffcharReadbuff(c);
878 return FAIL;
879
880 case K_IGNORE:
881 return FAIL;
882
883 case K_LEFTDRAG:
884 case K_MIDDLEDRAG:
885 case K_RIGHTDRAG:
886 case K_X1DRAG:
887 case K_X2DRAG:
888 dragging_outside = mouse_was_outside;
889 /* FALLTHROUGH */
890 case K_LEFTMOUSE:
891 case K_LEFTMOUSE_NM:
892 case K_LEFTRELEASE:
893 case K_LEFTRELEASE_NM:
894 case K_MIDDLEMOUSE:
895 case K_MIDDLERELEASE:
896 case K_RIGHTMOUSE:
897 case K_RIGHTRELEASE:
898 case K_X1MOUSE:
899 case K_X1RELEASE:
900 case K_X2MOUSE:
901 case K_X2RELEASE:
902 if (mouse_row < W_WINROW(curwin)
903 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
904 || mouse_col < W_WINCOL(curwin)
905 || mouse_col >= W_ENDCOL(curwin)
906 || dragging_outside)
907 {
908 /* click outside the current window */
909 if (typed)
910 {
911 stuffcharReadbuff(c);
912 mouse_was_outside = TRUE;
913 }
914 return FAIL;
915 }
916 }
917 if (typed)
918 mouse_was_outside = FALSE;
919
920 /* Convert the typed key to a sequence of bytes for the job. */
921 len = term_convert_key(term, c, msg);
922 if (len > 0)
923 /* TODO: if FAIL is returned, stop? */
924 channel_send(term->tl_job->jv_channel, PART_IN,
925 (char_u *)msg, (int)len, NULL);
926
927 return OK;
928}
929
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200930 static void
931position_cursor(win_T *wp, VTermPos *pos)
932{
933 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
934 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
935 wp->w_valid |= (VALID_WCOL|VALID_WROW);
936}
937
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200938/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200939 * Handle CTRL-W "": send register contents to the job.
940 */
941 static void
942term_paste_register(int prev_c UNUSED)
943{
944 int c;
945 list_T *l;
946 listitem_T *item;
947 long reglen = 0;
948 int type;
949
950#ifdef FEAT_CMDL_INFO
951 if (add_to_showcmd(prev_c))
952 if (add_to_showcmd('"'))
953 out_flush();
954#endif
955 c = term_vgetc();
956#ifdef FEAT_CMDL_INFO
957 clear_showcmd();
958#endif
959
960 /* CTRL-W "= prompt for expression to evaluate. */
961 if (c == '=' && get_expr_register() != '=')
962 return;
963
964 l = (list_T *)get_reg_contents(c, GREG_LIST);
965 if (l != NULL)
966 {
967 type = get_reg_type(c, &reglen);
968 for (item = l->lv_first; item != NULL; item = item->li_next)
969 {
970 char_u *s = get_tv_string(&item->li_tv);
971
972 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
973 s, STRLEN(s), NULL);
974 if (item->li_next != NULL || type == MLINE)
975 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
976 (char_u *)"\r", 1, NULL);
977 }
978 list_free(l);
979 }
980}
981
982/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200983 * Returns TRUE if the current window contains a terminal and we are sending
984 * keys to the job.
985 */
986 int
987term_use_loop()
988{
989 term_T *term = curbuf->b_term;
990
991 return term != NULL
992 && !term->tl_terminal_mode
993 && term->tl_vterm != NULL
994 && term_job_running(term);
995}
996
997/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200998 * Wait for input and send it to the job.
999 * Return when the start of a CTRL-W command is typed or anything else that
1000 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001001 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1002 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001003 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001004 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001005terminal_loop(void)
1006{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001007 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001008 int termkey = 0;
1009
1010 if (*curwin->w_p_tk != NUL)
1011 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001012 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001013
1014 for (;;)
1015 {
1016 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001017 /* Repeat redrawing in case a message is received while redrawing. */
1018 while (curwin->w_redr_type != 0)
1019 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001020 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001021
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001022 c = term_vgetc();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001023 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001024 /* job finished while waiting for a character */
1025 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001026
Bram Moolenaarfae42832017-08-01 22:24:26 +02001027#ifdef UNIX
1028 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1029#endif
1030#ifdef WIN3264
1031 if (c == Ctrl_C)
1032 /* We don't know if the job can handle CTRL-C itself or not, this
1033 * may kill the shell instead of killing the command running in the
1034 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001035 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001036#endif
1037
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001038 if (c == (termkey == 0 ? Ctrl_W : termkey))
1039 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001040 int prev_c = c;
1041
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001042#ifdef FEAT_CMDL_INFO
1043 if (add_to_showcmd(c))
1044 out_flush();
1045#endif
1046 c = term_vgetc();
1047#ifdef FEAT_CMDL_INFO
1048 clear_showcmd();
1049#endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001050 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001051 /* job finished while waiting for a character */
1052 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001053
1054 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001055 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001056 /* "CTRL-W .": send CTRL-W to the job */
1057 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001058 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001059 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001060 {
1061 term_enter_terminal_mode();
1062 return FAIL;
1063 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001064 else if (c == '"')
1065 {
1066 term_paste_register(prev_c);
1067 continue;
1068 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001069 else if (termkey == 0 || c != termkey)
1070 {
1071 stuffcharReadbuff(Ctrl_W);
1072 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001073 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001074 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001075 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001076 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1077 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001078 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001079 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001080}
1081
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001082/*
1083 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001084 * This updates the title and status, but does not close the vter, because
1085 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001086 */
1087 void
1088term_job_ended(job_T *job)
1089{
1090 term_T *term;
1091 int did_one = FALSE;
1092
1093 for (term = first_term; term != NULL; term = term->tl_next)
1094 if (term->tl_job == job)
1095 {
1096 vim_free(term->tl_title);
1097 term->tl_title = NULL;
1098 vim_free(term->tl_status_text);
1099 term->tl_status_text = NULL;
1100 redraw_buf_and_status_later(term->tl_buffer, VALID);
1101 did_one = TRUE;
1102 }
1103 if (did_one)
1104 redraw_statuslines();
1105 if (curbuf->b_term != NULL)
1106 {
1107 if (curbuf->b_term->tl_job == job)
1108 maketitle();
1109 update_cursor(curbuf->b_term, TRUE);
1110 }
1111}
1112
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001113 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001114may_toggle_cursor(term_T *term)
1115{
1116 if (curbuf == term->tl_buffer)
1117 {
1118 if (term->tl_cursor_visible)
1119 cursor_on();
1120 else
1121 cursor_off();
1122 }
1123}
1124
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001125 static int
1126handle_damage(VTermRect rect, void *user)
1127{
1128 term_T *term = (term_T *)user;
1129
1130 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1131 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1132 redraw_buf_later(term->tl_buffer, NOT_VALID);
1133 return 1;
1134}
1135
1136 static int
1137handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1138{
1139 term_T *term = (term_T *)user;
1140
1141 /* TODO */
1142 redraw_buf_later(term->tl_buffer, NOT_VALID);
1143 return 1;
1144}
1145
1146 static int
1147handle_movecursor(
1148 VTermPos pos,
1149 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001150 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001151 void *user)
1152{
1153 term_T *term = (term_T *)user;
1154 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001155
1156 term->tl_cursor_pos = pos;
1157 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001158
1159 FOR_ALL_WINDOWS(wp)
1160 {
1161 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001162 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001163 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001164 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001165 {
1166 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001167 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001168 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001169
1170 return 1;
1171}
1172
Bram Moolenaar21554412017-07-24 21:44:43 +02001173 static int
1174handle_settermprop(
1175 VTermProp prop,
1176 VTermValue *value,
1177 void *user)
1178{
1179 term_T *term = (term_T *)user;
1180
1181 switch (prop)
1182 {
1183 case VTERM_PROP_TITLE:
1184 vim_free(term->tl_title);
1185 term->tl_title = vim_strsave((char_u *)value->string);
1186 vim_free(term->tl_status_text);
1187 term->tl_status_text = NULL;
1188 if (term == curbuf->b_term)
1189 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001190 break;
1191
1192 case VTERM_PROP_CURSORVISIBLE:
1193 term->tl_cursor_visible = value->boolean;
1194 may_toggle_cursor(term);
1195 out_flush();
1196 break;
1197
Bram Moolenaar21554412017-07-24 21:44:43 +02001198 default:
1199 break;
1200 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001201 /* Always return 1, otherwise vterm doesn't store the value internally. */
1202 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001203}
1204
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001205/*
1206 * The job running in the terminal resized the terminal.
1207 */
1208 static int
1209handle_resize(int rows, int cols, void *user)
1210{
1211 term_T *term = (term_T *)user;
1212 win_T *wp;
1213
1214 term->tl_rows = rows;
1215 term->tl_cols = cols;
1216 FOR_ALL_WINDOWS(wp)
1217 {
1218 if (wp->w_buffer == term->tl_buffer)
1219 {
1220 win_setheight_win(rows, wp);
1221 win_setwidth_win(cols, wp);
1222 }
1223 }
1224
1225 redraw_buf_later(term->tl_buffer, NOT_VALID);
1226 return 1;
1227}
1228
Bram Moolenaard85f2712017-07-28 21:51:57 +02001229/*
1230 * Handle a line that is pushed off the top of the screen.
1231 */
1232 static int
1233handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1234{
1235 term_T *term = (term_T *)user;
1236
1237 /* TODO: Limit the number of lines that are stored. */
1238 /* TODO: put the text in the buffer. */
1239 if (ga_grow(&term->tl_scrollback, 1) == OK)
1240 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001241 VTermScreenCell *p = NULL;
1242 int len = 0;
1243 int i;
1244 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001245
1246 /* do not store empty cells at the end */
1247 for (i = 0; i < cols; ++i)
1248 if (cells[i].chars[0] != 0)
1249 len = i + 1;
1250
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001251 if (len > 0)
1252 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001253 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001254 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001255
1256 line = (sb_line_T *)term->tl_scrollback.ga_data
1257 + term->tl_scrollback.ga_len;
1258 line->sb_cols = len;
1259 line->sb_cells = p;
1260 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001261 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001262
1263 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001264 }
1265 return 0; /* ignored */
1266}
1267
Bram Moolenaar21554412017-07-24 21:44:43 +02001268static VTermScreenCallbacks screen_callbacks = {
1269 handle_damage, /* damage */
1270 handle_moverect, /* moverect */
1271 handle_movecursor, /* movecursor */
1272 handle_settermprop, /* settermprop */
1273 NULL, /* bell */
1274 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001275 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001276 NULL /* sb_popline */
1277};
1278
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001279/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001280 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001281 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001282 */
1283 void
1284term_channel_closed(channel_T *ch)
1285{
1286 term_T *term;
1287 int did_one = FALSE;
1288
1289 for (term = first_term; term != NULL; term = term->tl_next)
1290 if (term->tl_job == ch->ch_job)
1291 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001292 term->tl_channel_closed = TRUE;
1293
Bram Moolenaard85f2712017-07-28 21:51:57 +02001294 vim_free(term->tl_title);
1295 term->tl_title = NULL;
1296 vim_free(term->tl_status_text);
1297 term->tl_status_text = NULL;
1298
Bram Moolenaar423802d2017-07-30 16:52:24 +02001299 /* Unless in Terminal-Normal mode: clear the vterm. */
1300 if (!term->tl_terminal_mode)
1301 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001302
1303 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1304 did_one = TRUE;
1305 }
1306 if (did_one)
1307 {
1308 redraw_statuslines();
1309
1310 /* Need to break out of vgetc(). */
1311 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001312 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001313
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001314 term = curbuf->b_term;
1315 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001316 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001317 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001318 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001319 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001320 }
1321 }
1322}
1323
1324/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001325 * Reverse engineer the RGB value into a cterm color index.
1326 * First color is 1. Return 0 if no match found.
1327 */
1328 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001329color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001330{
1331 int red = color->red;
1332 int blue = color->blue;
1333 int green = color->green;
1334
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001335 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001336 if (red == 0)
1337 {
1338 if (green == 0)
1339 {
1340 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001341 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001342 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001343 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001344 }
1345 else if (green == 224)
1346 {
1347 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001348 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001349 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001350 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001351 }
1352 }
1353 else if (red == 224)
1354 {
1355 if (green == 0)
1356 {
1357 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001358 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001359 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001360 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001361 }
1362 else if (green == 224)
1363 {
1364 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001365 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001366 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001367 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001368 }
1369 }
1370 else if (red == 128)
1371 {
1372 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001373 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001374 }
1375 else if (red == 255)
1376 {
1377 if (green == 64)
1378 {
1379 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001380 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001381 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001382 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001383 }
1384 else if (green == 255)
1385 {
1386 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001387 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001388 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001389 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001390 }
1391 }
1392 else if (red == 64)
1393 {
1394 if (green == 64)
1395 {
1396 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001397 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001398 }
1399 else if (green == 255)
1400 {
1401 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001402 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001403 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001404 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001405 }
1406 }
1407 if (t_colors >= 256)
1408 {
1409 if (red == blue && red == green)
1410 {
1411 /* 24-color greyscale */
1412 static int cutoff[23] = {
1413 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1414 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1415 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1416 int i;
1417
1418 for (i = 0; i < 23; ++i)
1419 if (red < cutoff[i])
1420 return i + 233;
1421 return 256;
1422 }
1423
1424 /* 216-color cube */
1425 return 17 + ((red + 25) / 0x33) * 36
1426 + ((green + 25) / 0x33) * 6
1427 + (blue + 25) / 0x33;
1428 }
1429 return 0;
1430}
1431
1432/*
1433 * Convert the attributes of a vterm cell into an attribute index.
1434 */
1435 static int
1436cell2attr(VTermScreenCell *cell)
1437{
1438 int attr = 0;
1439
1440 if (cell->attrs.bold)
1441 attr |= HL_BOLD;
1442 if (cell->attrs.underline)
1443 attr |= HL_UNDERLINE;
1444 if (cell->attrs.italic)
1445 attr |= HL_ITALIC;
1446 if (cell->attrs.strike)
1447 attr |= HL_STANDOUT;
1448 if (cell->attrs.reverse)
1449 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001450
1451#ifdef FEAT_GUI
1452 if (gui.in_use)
1453 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001454 guicolor_T fg, bg;
1455
1456 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1457 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1458 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001459 }
1460 else
1461#endif
1462#ifdef FEAT_TERMGUICOLORS
1463 if (p_tgc)
1464 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001465 guicolor_T fg, bg;
1466
1467 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1468 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1469
1470 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001471 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001472 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001473#endif
1474 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001475 int bold = MAYBE;
1476 int fg = color2index(&cell->fg, TRUE, &bold);
1477 int bg = color2index(&cell->bg, FALSE, &bold);
1478
1479 /* with 8 colors set the bold attribute to get a bright foreground */
1480 if (bold == TRUE)
1481 attr |= HL_BOLD;
1482 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001483 }
1484 return 0;
1485}
1486
1487/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001488 * Called to update the window that contains a terminal.
1489 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001490 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001491 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001492term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001493{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001494 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001495 VTerm *vterm;
1496 VTermScreen *screen;
1497 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001498 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001499
Bram Moolenaar423802d2017-07-30 16:52:24 +02001500 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001501 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001502
Bram Moolenaard85f2712017-07-28 21:51:57 +02001503 vterm = term->tl_vterm;
1504 screen = vterm_obtain_screen(vterm);
1505 state = vterm_obtain_state(vterm);
1506
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001507 /*
1508 * If the window was resized a redraw will be triggered and we get here.
1509 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1510 */
1511 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1512 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001513 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001514 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1515 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1516 win_T *twp;
1517
1518 FOR_ALL_WINDOWS(twp)
1519 {
1520 /* When more than one window shows the same terminal, use the
1521 * smallest size. */
1522 if (twp->w_buffer == term->tl_buffer)
1523 {
1524 if (!term->tl_rows_fixed && rows > twp->w_height)
1525 rows = twp->w_height;
1526 if (!term->tl_cols_fixed && cols > twp->w_width)
1527 cols = twp->w_width;
1528 }
1529 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001530
1531 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001532 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001533 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001534 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001535 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001536
1537 /* The cursor may have been moved when resizing. */
1538 vterm_state_get_cursorpos(state, &pos);
1539 position_cursor(wp, &pos);
1540
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001541 /* TODO: Only redraw what changed. */
1542 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001543 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001544 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001545 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001546
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001547 if (pos.row < term->tl_rows)
1548 {
1549 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001550 {
1551 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001552 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001553
Bram Moolenaareeac6772017-07-23 15:48:37 +02001554 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1555 vim_memset(&cell, 0, sizeof(cell));
1556
1557 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001558 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001559 if (c == NUL)
1560 {
1561 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001562#if defined(FEAT_MBYTE)
1563 if (enc_utf8)
1564 ScreenLinesUC[off] = NUL;
1565#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001566 }
1567 else
1568 {
1569#if defined(FEAT_MBYTE)
1570 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001571 {
1572 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001573 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001574 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001575 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001576 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001577 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001578 if (enc_utf8)
1579 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001580 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001581#else
1582 ScreenLines[off] = c;
1583#endif
1584 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001585 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001586
1587 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001588 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001589 if (cell.width == 2)
1590 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001591 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001592#if defined(FEAT_MBYTE)
1593 if (enc_utf8)
1594 ScreenLinesUC[off] = NUL;
1595#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001596 ++pos.col;
1597 ++off;
1598 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001599 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001600 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001601 else
1602 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001603
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001604 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1605 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001606 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001607
1608 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001609}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001610
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001611/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001612 * Return TRUE if "wp" is a terminal window where the job has finished.
1613 */
1614 int
1615term_is_finished(buf_T *buf)
1616{
1617 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1618}
1619
1620/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001621 * Return TRUE if "wp" is a terminal window where the job has finished or we
1622 * are in Terminal-Normal mode.
1623 */
1624 int
1625term_show_buffer(buf_T *buf)
1626{
1627 term_T *term = buf->b_term;
1628
1629 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1630}
1631
1632/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001633 * The current buffer is going to be changed. If there is terminal
1634 * highlighting remove it now.
1635 */
1636 void
1637term_change_in_curbuf(void)
1638{
1639 term_T *term = curbuf->b_term;
1640
1641 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1642 {
1643 free_scrollback(term);
1644 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001645
1646 /* The buffer is now like a normal buffer, it cannot be easily
1647 * abandoned when changed. */
1648 set_string_option_direct((char_u *)"buftype", -1,
1649 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001650 }
1651}
1652
1653/*
1654 * Get the screen attribute for a position in the buffer.
1655 */
1656 int
1657term_get_attr(buf_T *buf, linenr_T lnum, int col)
1658{
1659 term_T *term = buf->b_term;
1660 sb_line_T *line;
1661
Bram Moolenaar70229f92017-07-29 16:01:53 +02001662 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001663 return 0;
1664 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1665 if (col >= line->sb_cols)
1666 return 0;
1667 return cell2attr(line->sb_cells + col);
1668}
1669
1670/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001671 * Create a new vterm and initialize it.
1672 */
1673 static void
1674create_vterm(term_T *term, int rows, int cols)
1675{
1676 VTerm *vterm;
1677 VTermScreen *screen;
1678
1679 vterm = vterm_new(rows, cols);
1680 term->tl_vterm = vterm;
1681 screen = vterm_obtain_screen(vterm);
1682 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1683 /* TODO: depends on 'encoding'. */
1684 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001685
1686 /* Vterm uses a default black background. Set it to white when
1687 * 'background' is "light". */
1688 if (*p_bg == 'l')
1689 {
1690 VTermColor fg, bg;
1691
1692 fg.red = fg.green = fg.blue = 0;
1693 bg.red = bg.green = bg.blue = 255;
1694 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1695 }
1696
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001697 /* Required to initialize most things. */
1698 vterm_screen_reset(screen, 1 /* hard */);
1699}
1700
Bram Moolenaar21554412017-07-24 21:44:43 +02001701/*
1702 * Return the text to show for the buffer name and status.
1703 */
1704 char_u *
1705term_get_status_text(term_T *term)
1706{
1707 if (term->tl_status_text == NULL)
1708 {
1709 char_u *txt;
1710 size_t len;
1711
Bram Moolenaar423802d2017-07-30 16:52:24 +02001712 if (term->tl_terminal_mode)
1713 {
1714 if (term_job_running(term))
1715 txt = (char_u *)_("Terminal");
1716 else
1717 txt = (char_u *)_("Terminal-finished");
1718 }
1719 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001720 txt = term->tl_title;
1721 else if (term_job_running(term))
1722 txt = (char_u *)_("running");
1723 else
1724 txt = (char_u *)_("finished");
1725 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001726 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001727 if (term->tl_status_text != NULL)
1728 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1729 term->tl_buffer->b_fname, txt);
1730 }
1731 return term->tl_status_text;
1732}
1733
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001734/*
1735 * Mark references in jobs of terminals.
1736 */
1737 int
1738set_ref_in_term(int copyID)
1739{
1740 int abort = FALSE;
1741 term_T *term;
1742 typval_T tv;
1743
1744 for (term = first_term; term != NULL; term = term->tl_next)
1745 if (term->tl_job != NULL)
1746 {
1747 tv.v_type = VAR_JOB;
1748 tv.vval.v_job = term->tl_job;
1749 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1750 }
1751 return abort;
1752}
1753
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001754/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001755 * Get the buffer from the first argument in "argvars".
1756 * Returns NULL when the buffer is not for a terminal window.
1757 */
1758 static buf_T *
1759term_get_buf(typval_T *argvars)
1760{
1761 buf_T *buf;
1762
1763 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1764 ++emsg_off;
1765 buf = get_buf_tv(&argvars[0], FALSE);
1766 --emsg_off;
1767 if (buf == NULL || buf->b_term == NULL)
1768 return NULL;
1769 return buf;
1770}
1771
1772/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001773 * "term_getattr(attr, name)" function
1774 */
1775 void
1776f_term_getattr(typval_T *argvars, typval_T *rettv)
1777{
1778 int attr;
1779 size_t i;
1780 char_u *name;
1781
1782 static struct {
1783 char *name;
1784 int attr;
1785 } attrs[] = {
1786 {"bold", HL_BOLD},
1787 {"italic", HL_ITALIC},
1788 {"underline", HL_UNDERLINE},
1789 {"strike", HL_STANDOUT},
1790 {"reverse", HL_INVERSE},
1791 };
1792
1793 attr = get_tv_number(&argvars[0]);
1794 name = get_tv_string_chk(&argvars[1]);
1795 if (name == NULL)
1796 return;
1797
1798 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1799 if (STRCMP(name, attrs[i].name) == 0)
1800 {
1801 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1802 break;
1803 }
1804}
1805
1806/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001807 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001808 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001809 void
1810f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001811{
Bram Moolenaar97870002017-07-30 18:28:38 +02001812 buf_T *buf = term_get_buf(argvars);
1813 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001814
Bram Moolenaar97870002017-07-30 18:28:38 +02001815 if (rettv_list_alloc(rettv) == FAIL)
1816 return;
1817 if (buf == NULL)
1818 return;
1819
1820 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001821 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1822 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001823 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001824}
1825
1826/*
1827 * "term_getjob(buf)" function
1828 */
1829 void
1830f_term_getjob(typval_T *argvars, typval_T *rettv)
1831{
1832 buf_T *buf = term_get_buf(argvars);
1833
1834 rettv->v_type = VAR_JOB;
1835 rettv->vval.v_job = NULL;
1836 if (buf == NULL)
1837 return;
1838
1839 rettv->vval.v_job = buf->b_term->tl_job;
1840 if (rettv->vval.v_job != NULL)
1841 ++rettv->vval.v_job->jv_refcount;
1842}
1843
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001844 static int
1845get_row_number(typval_T *tv, term_T *term)
1846{
1847 if (tv->v_type == VAR_STRING
1848 && tv->vval.v_string != NULL
1849 && STRCMP(tv->vval.v_string, ".") == 0)
1850 return term->tl_cursor_pos.row;
1851 return (int)get_tv_number(tv) - 1;
1852}
1853
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001854/*
1855 * "term_getline(buf, row)" function
1856 */
1857 void
1858f_term_getline(typval_T *argvars, typval_T *rettv)
1859{
1860 buf_T *buf = term_get_buf(argvars);
1861 term_T *term;
1862 int row;
1863
1864 rettv->v_type = VAR_STRING;
1865 if (buf == NULL)
1866 return;
1867 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001868 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001869
1870 if (term->tl_vterm == NULL)
1871 {
1872 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1873
1874 /* vterm is finished, get the text from the buffer */
1875 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1876 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1877 }
1878 else
1879 {
1880 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1881 VTermRect rect;
1882 int len;
1883 char_u *p;
1884
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001885 if (row < 0 || row >= term->tl_rows)
1886 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001887 len = term->tl_cols * MB_MAXBYTES + 1;
1888 p = alloc(len);
1889 if (p == NULL)
1890 return;
1891 rettv->vval.v_string = p;
1892
1893 rect.start_col = 0;
1894 rect.end_col = term->tl_cols;
1895 rect.start_row = row;
1896 rect.end_row = row + 1;
1897 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1898 }
1899}
1900
1901/*
1902 * "term_getsize(buf)" function
1903 */
1904 void
1905f_term_getsize(typval_T *argvars, typval_T *rettv)
1906{
1907 buf_T *buf = term_get_buf(argvars);
1908 list_T *l;
1909
1910 if (rettv_list_alloc(rettv) == FAIL)
1911 return;
1912 if (buf == NULL)
1913 return;
1914
1915 l = rettv->vval.v_list;
1916 list_append_number(l, buf->b_term->tl_rows);
1917 list_append_number(l, buf->b_term->tl_cols);
1918}
1919
1920/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001921 * "term_getstatus(buf)" function
1922 */
1923 void
1924f_term_getstatus(typval_T *argvars, typval_T *rettv)
1925{
1926 buf_T *buf = term_get_buf(argvars);
1927 term_T *term;
1928 char_u val[100];
1929
1930 rettv->v_type = VAR_STRING;
1931 if (buf == NULL)
1932 return;
1933 term = buf->b_term;
1934
1935 if (term_job_running(term))
1936 STRCPY(val, "running");
1937 else
1938 STRCPY(val, "finished");
1939 if (term->tl_terminal_mode)
1940 STRCAT(val, ",terminal");
1941 rettv->vval.v_string = vim_strsave(val);
1942}
1943
1944/*
1945 * "term_gettitle(buf)" function
1946 */
1947 void
1948f_term_gettitle(typval_T *argvars, typval_T *rettv)
1949{
1950 buf_T *buf = term_get_buf(argvars);
1951
1952 rettv->v_type = VAR_STRING;
1953 if (buf == NULL)
1954 return;
1955
1956 if (buf->b_term->tl_title != NULL)
1957 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1958}
1959
1960/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001961 * "term_gettty(buf)" function
1962 */
1963 void
1964f_term_gettty(typval_T *argvars, typval_T *rettv)
1965{
1966 buf_T *buf = term_get_buf(argvars);
1967 char_u *p;
1968
1969 rettv->v_type = VAR_STRING;
1970 if (buf == NULL)
1971 return;
1972 if (buf->b_term->tl_job != NULL)
1973 p = buf->b_term->tl_job->jv_tty_name;
1974 else
1975 p = buf->b_term->tl_tty_name;
1976 if (p != NULL)
1977 rettv->vval.v_string = vim_strsave(p);
1978}
1979
1980/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001981 * "term_list()" function
1982 */
1983 void
1984f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1985{
1986 term_T *tp;
1987 list_T *l;
1988
1989 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1990 return;
1991
1992 l = rettv->vval.v_list;
1993 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1994 if (tp != NULL && tp->tl_buffer != NULL)
1995 if (list_append_number(l,
1996 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1997 return;
1998}
1999
2000/*
2001 * "term_scrape(buf, row)" function
2002 */
2003 void
2004f_term_scrape(typval_T *argvars, typval_T *rettv)
2005{
2006 buf_T *buf = term_get_buf(argvars);
2007 VTermScreen *screen = NULL;
2008 VTermPos pos;
2009 list_T *l;
2010 term_T *term;
2011
2012 if (rettv_list_alloc(rettv) == FAIL)
2013 return;
2014 if (buf == NULL)
2015 return;
2016 term = buf->b_term;
2017 if (term->tl_vterm != NULL)
2018 screen = vterm_obtain_screen(term->tl_vterm);
2019
2020 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002021 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002022 for (pos.col = 0; pos.col < term->tl_cols; )
2023 {
2024 dict_T *dcell;
2025 VTermScreenCell cell;
2026 char_u rgb[8];
2027 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2028 int off = 0;
2029 int i;
2030
2031 if (screen == NULL)
2032 {
2033 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2034 sb_line_T *line;
2035
2036 /* vterm has finished, get the cell from scrollback */
2037 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2038 break;
2039 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2040 if (pos.col >= line->sb_cols)
2041 break;
2042 cell = line->sb_cells[pos.col];
2043 }
2044 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2045 break;
2046 dcell = dict_alloc();
2047 list_append_dict(l, dcell);
2048
2049 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2050 {
2051 if (cell.chars[i] == 0)
2052 break;
2053 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2054 }
2055 mbs[off] = NUL;
2056 dict_add_nr_str(dcell, "chars", 0, mbs);
2057
2058 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2059 cell.fg.red, cell.fg.green, cell.fg.blue);
2060 dict_add_nr_str(dcell, "fg", 0, rgb);
2061 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2062 cell.bg.red, cell.bg.green, cell.bg.blue);
2063 dict_add_nr_str(dcell, "bg", 0, rgb);
2064
2065 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2066 dict_add_nr_str(dcell, "width", cell.width, NULL);
2067
2068 ++pos.col;
2069 if (cell.width == 2)
2070 ++pos.col;
2071 }
2072}
2073
2074/*
2075 * "term_sendkeys(buf, keys)" function
2076 */
2077 void
2078f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2079{
2080 buf_T *buf = term_get_buf(argvars);
2081 char_u *msg;
2082 term_T *term;
2083
2084 rettv->v_type = VAR_UNKNOWN;
2085 if (buf == NULL)
2086 return;
2087
2088 msg = get_tv_string_chk(&argvars[1]);
2089 if (msg == NULL)
2090 return;
2091 term = buf->b_term;
2092 if (term->tl_vterm == NULL)
2093 return;
2094
2095 while (*msg != NUL)
2096 {
2097 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2098 msg += MB_PTR2LEN(msg);
2099 }
2100
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002101 if (!term->tl_terminal_mode)
2102 {
2103 /* TODO: only update once in a while. */
2104 update_screen(0);
2105 if (buf == curbuf)
2106 update_cursor(term, TRUE);
2107 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002108}
2109
2110/*
2111 * "term_start(command, options)" function
2112 */
2113 void
2114f_term_start(typval_T *argvars, typval_T *rettv)
2115{
2116 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002117 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002118
2119 if (cmd == NULL)
2120 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002121 init_job_options(&opt);
2122 /* TODO: allow more job options */
2123 if (argvars[1].v_type != VAR_UNKNOWN
2124 && get_job_options(&argvars[1], &opt,
2125 JO_TIMEOUT_ALL + JO_STOPONEXIT
2126 + JO_EXIT_CB + JO_CLOSE_CALLBACK) == FAIL)
2127 return;
2128
2129 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002130
2131 if (curbuf->b_term != NULL)
2132 rettv->vval.v_number = curbuf->b_fnum;
2133}
2134
2135/*
2136 * "term_wait" function
2137 */
2138 void
2139f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2140{
2141 buf_T *buf = term_get_buf(argvars);
2142
2143 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002144 {
2145 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002146 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002147 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002148 if (buf->b_term->tl_job == NULL)
2149 {
2150 ch_log(NULL, "term_wait(): no job to wait for");
2151 return;
2152 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002153
2154 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002155 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002156 {
2157 /* The job is dead, keep reading channel I/O until the channel is
2158 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002159 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002160 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2161 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002162 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002163 parse_queued_messages();
2164 ui_delay(10L, FALSE);
2165 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002166 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002167 parse_queued_messages();
2168 }
2169 else
2170 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002171 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002172 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002173
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002174 /* Wait for 10 msec for any channel I/O. */
2175 /* TODO: use delay from optional argument */
2176 ui_delay(10L, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002177 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002178
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002179 /* Flushing messages on channels is hopefully sufficient.
2180 * TODO: is there a better way? */
2181 parse_queued_messages();
2182 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002183}
2184
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002185# ifdef WIN3264
2186
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002187/**************************************
2188 * 2. MS-Windows implementation.
2189 */
2190
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002191#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2192#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2193
Bram Moolenaar8a773062017-07-24 22:29:21 +02002194void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002195void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002196void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002197BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2198void (*winpty_config_set_initial_size)(void*, int, int);
2199LPCWSTR (*winpty_conin_name)(void*);
2200LPCWSTR (*winpty_conout_name)(void*);
2201LPCWSTR (*winpty_conerr_name)(void*);
2202void (*winpty_free)(void*);
2203void (*winpty_config_free)(void*);
2204void (*winpty_spawn_config_free)(void*);
2205void (*winpty_error_free)(void*);
2206LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002207BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002208HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002209
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002210#define WINPTY_DLL "winpty.dll"
2211
2212static HINSTANCE hWinPtyDLL = NULL;
2213
2214 int
2215dyn_winpty_init(void)
2216{
2217 int i;
2218 static struct
2219 {
2220 char *name;
2221 FARPROC *ptr;
2222 } winpty_entry[] =
2223 {
2224 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2225 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2226 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2227 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2228 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2229 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2230 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2231 {"winpty_free", (FARPROC*)&winpty_free},
2232 {"winpty_open", (FARPROC*)&winpty_open},
2233 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2234 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2235 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2236 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002237 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002238 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002239 {NULL, NULL}
2240 };
2241
2242 /* No need to initialize twice. */
2243 if (hWinPtyDLL)
2244 return 1;
2245 /* Load winpty.dll */
2246 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2247 if (!hWinPtyDLL)
2248 {
2249 EMSG2(_(e_loadlib), WINPTY_DLL);
2250 return 0;
2251 }
2252 for (i = 0; winpty_entry[i].name != NULL
2253 && winpty_entry[i].ptr != NULL; ++i)
2254 {
2255 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2256 winpty_entry[i].name)) == NULL)
2257 {
2258 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2259 return 0;
2260 }
2261 }
2262
2263 return 1;
2264}
2265
2266/*
2267 * Create a new terminal of "rows" by "cols" cells.
2268 * Store a reference in "term".
2269 * Return OK or FAIL.
2270 */
2271 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002272term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002273{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002274 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002275 channel_T *channel = NULL;
2276 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002277 DWORD error;
2278 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2279 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002280 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002281 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002282
2283 if (!dyn_winpty_init())
2284 return FAIL;
2285
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002286 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002287 if (p == NULL)
2288 return FAIL;
2289
2290 job = job_alloc();
2291 if (job == NULL)
2292 goto failed;
2293
2294 channel = add_channel();
2295 if (channel == NULL)
2296 goto failed;
2297
2298 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2299 if (term->tl_winpty_config == NULL)
2300 goto failed;
2301
2302 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2303 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2304 if (term->tl_winpty == NULL)
2305 goto failed;
2306
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002307 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002308 spawn_config = winpty_spawn_config_new(
2309 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2310 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2311 NULL,
2312 p,
2313 NULL,
2314 NULL,
2315 &winpty_err);
2316 if (spawn_config == NULL)
2317 goto failed;
2318
2319 channel = add_channel();
2320 if (channel == NULL)
2321 goto failed;
2322
2323 job = job_alloc();
2324 if (job == NULL)
2325 goto failed;
2326
2327 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2328 &child_thread_handle, &error, &winpty_err))
2329 goto failed;
2330
2331 channel_set_pipes(channel,
2332 (sock_T) CreateFileW(
2333 winpty_conin_name(term->tl_winpty),
2334 GENERIC_WRITE, 0, NULL,
2335 OPEN_EXISTING, 0, NULL),
2336 (sock_T) CreateFileW(
2337 winpty_conout_name(term->tl_winpty),
2338 GENERIC_READ, 0, NULL,
2339 OPEN_EXISTING, 0, NULL),
2340 (sock_T) CreateFileW(
2341 winpty_conerr_name(term->tl_winpty),
2342 GENERIC_READ, 0, NULL,
2343 OPEN_EXISTING, 0, NULL));
2344
2345 jo = CreateJobObject(NULL, NULL);
2346 if (jo == NULL)
2347 goto failed;
2348
2349 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002350 {
2351 /* Failed, switch the way to terminate process with TerminateProcess. */
2352 CloseHandle(jo);
2353 jo = NULL;
2354 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002355
2356 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002357 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002358
2359 create_vterm(term, rows, cols);
2360
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002361 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002362 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002363
2364 job->jv_channel = channel;
2365 job->jv_proc_info.hProcess = child_process_handle;
2366 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2367 job->jv_job_object = jo;
2368 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002369 sprintf(buf, "winpty://%lu",
2370 GetProcessId(winpty_agent_process(term->tl_winpty)));
2371 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002372 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002373 term->tl_job = job;
2374
2375 return OK;
2376
2377failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002378 if (spawn_config != NULL)
2379 winpty_spawn_config_free(spawn_config);
2380 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002381 if (channel != NULL)
2382 channel_clear(channel);
2383 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002384 {
2385 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002386 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002387 }
2388 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002389 if (jo != NULL)
2390 CloseHandle(jo);
2391 if (term->tl_winpty != NULL)
2392 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002393 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002394 if (term->tl_winpty_config != NULL)
2395 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002396 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002397 if (winpty_err != NULL)
2398 {
2399 char_u *msg = utf16_to_enc(
2400 (short_u *)winpty_error_msg(winpty_err), NULL);
2401
2402 EMSG(msg);
2403 winpty_error_free(winpty_err);
2404 }
2405 return FAIL;
2406}
2407
2408/*
2409 * Free the terminal emulator part of "term".
2410 */
2411 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002412term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002413{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002414 if (term->tl_winpty != NULL)
2415 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002416 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002417 if (term->tl_winpty_config != NULL)
2418 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002419 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002420 if (term->tl_vterm != NULL)
2421 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002422 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002423}
2424
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002425/*
2426 * Request size to terminal.
2427 */
2428 static void
2429term_report_winsize(term_T *term, int rows, int cols)
2430{
2431 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2432}
2433
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002434# else
2435
2436/**************************************
2437 * 3. Unix-like implementation.
2438 */
2439
2440/*
2441 * Create a new terminal of "rows" by "cols" cells.
2442 * Start job for "cmd".
2443 * Store the pointers in "term".
2444 * Return OK or FAIL.
2445 */
2446 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002447term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002448{
2449 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002450
2451 create_vterm(term, rows, cols);
2452
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002453 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002454 argvars[0].v_type = VAR_STRING;
2455 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002456
2457 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002458 if (term->tl_job != NULL)
2459 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002460
Bram Moolenaar61a66052017-07-22 18:39:00 +02002461 return term->tl_job != NULL
2462 && term->tl_job->jv_channel != NULL
2463 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002464}
2465
2466/*
2467 * Free the terminal emulator part of "term".
2468 */
2469 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002470term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002471{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002472 if (term->tl_vterm != NULL)
2473 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002474 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002475}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002476
2477/*
2478 * Request size to terminal.
2479 */
2480 static void
2481term_report_winsize(term_T *term, int rows, int cols)
2482{
2483 /* Use an ioctl() to report the new window size to the job. */
2484 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2485 {
2486 int fd = -1;
2487 int part;
2488
2489 for (part = PART_OUT; part < PART_COUNT; ++part)
2490 {
2491 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2492 if (isatty(fd))
2493 break;
2494 }
2495 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2496 mch_stop_job(term->tl_job, (char_u *)"winch");
2497 }
2498}
2499
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002500# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002501
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002502#endif /* FEAT_TERMINAL */