blob: 9c165337bed9275cd3be8f63541367adb0fe0a99 [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 Moolenaar94053a52017-08-01 21:44:33 +020039 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaard8dc1792017-08-03 11:55:21 +020040 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar423802d2017-07-30 16:52:24 +020041 * - in bash mouse clicks are inserting characters.
42 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020043 * - add argument to term_wait() for waiting time.
Bram Moolenaard85f2712017-07-28 21:51:57 +020044 * - For the scrollback buffer store lines in the buffer, only attributes in
45 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020046 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020048 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
49 * job finishes).
50 * - add option values to the command:
51 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020052 * - support different cursor shapes, colors and attributes
53 * - make term_getcursor() return type (none/block/bar/underline) and
54 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020055 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020056 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020057 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020058 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020059 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020060 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020061 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
62 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020064 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020065 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020067 * - implement "term" for job_start(): more job options when starting a
68 * terminal.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020069 * - support ":term NONE" to open a terminal with a pty but not running a job
70 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020071 * - if the job in the terminal does not support the mouse, we can use the
72 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020073 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
74 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020075 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020076 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020077 * - Copy text in the vterm to the Vim buffer once in a while, so that
78 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079 */
80
81#include "vim.h"
82
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020083#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020084
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020085#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020086# define MIN(x,y) (x < y ? x : y)
87# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020088#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020089
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020090#include "libvterm/include/vterm.h"
91
Bram Moolenaard85f2712017-07-28 21:51:57 +020092typedef struct sb_line_S {
93 int sb_cols; /* can differ per line */
94 VTermScreenCell *sb_cells; /* allocated */
95} sb_line_T;
96
Bram Moolenaare4f25e42017-07-07 11:54:15 +020097/* typedef term_T in structs.h */
98struct terminal_S {
99 term_T *tl_next;
100
Bram Moolenaar423802d2017-07-30 16:52:24 +0200101 VTerm *tl_vterm;
102 job_T *tl_job;
103 buf_T *tl_buffer;
104
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200105 /* used when tl_job is NULL and only a pty was created */
106 int tl_tty_fd;
107 char_u *tl_tty_name;
108
Bram Moolenaar423802d2017-07-30 16:52:24 +0200109 int tl_terminal_mode;
110 int tl_channel_closed;
111
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200112#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200113 void *tl_winpty_config;
114 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200115#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200116
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200117 /* last known vterm size */
118 int tl_rows;
119 int tl_cols;
120 /* vterm size does not follow window size */
121 int tl_rows_fixed;
122 int tl_cols_fixed;
123
Bram Moolenaar21554412017-07-24 21:44:43 +0200124 char_u *tl_title; /* NULL or allocated */
125 char_u *tl_status_text; /* NULL or allocated */
126
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200127 /* Range of screen rows to update. Zero based. */
128 int tl_dirty_row_start; /* -1 if nothing dirty */
129 int tl_dirty_row_end; /* row below last one to update */
130
Bram Moolenaard85f2712017-07-28 21:51:57 +0200131 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200132 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200133
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200134 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200135 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200136};
137
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200138/*
139 * List of all active terminals.
140 */
141static term_T *first_term = NULL;
142
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200143
144#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
145#define KEY_BUF_LEN 200
146
147/*
148 * Functions with separate implementation for MS-Windows and Unix-like systems.
149 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200150static 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 +0200151static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200152static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200153
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200154/**************************************
155 * 1. Generic code for all systems.
156 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200157
158/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200159 * Determine the terminal size from 'termsize' and the current window.
160 * Assumes term->tl_rows and term->tl_cols are zero.
161 */
162 static void
163set_term_and_win_size(term_T *term)
164{
165 if (*curwin->w_p_tms != NUL)
166 {
167 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
168
169 term->tl_rows = atoi((char *)curwin->w_p_tms);
170 term->tl_cols = atoi((char *)p);
171 }
172 if (term->tl_rows == 0)
173 term->tl_rows = curwin->w_height;
174 else
175 {
176 win_setheight_win(term->tl_rows, curwin);
177 term->tl_rows_fixed = TRUE;
178 }
179 if (term->tl_cols == 0)
180 term->tl_cols = curwin->w_width;
181 else
182 {
183 win_setwidth_win(term->tl_cols, curwin);
184 term->tl_cols_fixed = TRUE;
185 }
186}
187
188/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200189 * Initialize job options for a terminal job.
190 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200191 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200192 static void
193init_job_options(jobopt_T *opt)
194{
195 clear_job_options(opt);
196
197 opt->jo_mode = MODE_RAW;
198 opt->jo_out_mode = MODE_RAW;
199 opt->jo_err_mode = MODE_RAW;
200 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
201
202 opt->jo_io[PART_OUT] = JIO_BUFFER;
203 opt->jo_io[PART_ERR] = JIO_BUFFER;
204 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
205
206 opt->jo_modifiable[PART_OUT] = 0;
207 opt->jo_modifiable[PART_ERR] = 0;
208 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
209
210 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
211}
212
213/*
214 * Set job options mandatory for a terminal job.
215 */
216 static void
217setup_job_options(jobopt_T *opt, int rows, int cols)
218{
219 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
220 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
221 opt->jo_pty = TRUE;
222 opt->jo_term_rows = rows;
223 opt->jo_term_cols = cols;
224}
225
226 static void
227term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200228{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200229 exarg_T split_ea;
230 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200231 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200232
233 if (check_restricted() || check_secure())
234 return;
235
236 term = (term_T *)alloc_clear(sizeof(term_T));
237 if (term == NULL)
238 return;
239 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200240 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200241 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200242
243 /* Open a new window or tab. */
244 vim_memset(&split_ea, 0, sizeof(split_ea));
245 split_ea.cmdidx = CMD_new;
246 split_ea.cmd = (char_u *)"new";
247 split_ea.arg = (char_u *)"";
248 ex_splitview(&split_ea);
249 if (curwin == old_curwin)
250 {
251 /* split failed */
252 vim_free(term);
253 return;
254 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200255 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200256 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200257
258 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200259 term->tl_next = first_term;
260 first_term = term;
261
Bram Moolenaar293424c2017-07-26 23:11:01 +0200262 if (cmd == NULL || *cmd == NUL)
263 cmd = p_sh;
264
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200265 {
266 int i;
267 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200268 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200269
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200270 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200271 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200272 /* Prepend a ! to the command name to avoid the buffer name equals
273 * the executable, otherwise ":w!" would overwrite it. */
274 if (i == 0)
275 vim_snprintf((char *)p, len, "!%s", cmd);
276 else
277 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200278 if (buflist_findname(p) == NULL)
279 {
280 curbuf->b_ffname = p;
281 break;
282 }
283 }
284 }
285 curbuf->b_fname = curbuf->b_ffname;
286
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200287 /* Mark the buffer as not modifiable. It can only be made modifiable after
288 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200289 curbuf->b_p_ma = FALSE;
290 set_string_option_direct((char_u *)"buftype", -1,
291 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200292
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200293 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200294 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200295
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200296 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200297 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200298 {
299 /* store the size we ended up with */
300 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200301 }
302 else
303 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200304 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200305
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200306 /* Wiping out the buffer will also close the window and call
307 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200308 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200309 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200310}
311
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200312/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200313 * ":terminal": open a terminal window and execute a job in it.
314 */
315 void
316ex_terminal(exarg_T *eap)
317{
318 jobopt_T opt;
319
320 init_job_options(&opt);
321 /* TODO: get options from before the command */
322
323 term_start(eap->arg, &opt);
324}
325
326/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200327 * Free the scrollback buffer for "term".
328 */
329 static void
330free_scrollback(term_T *term)
331{
332 int i;
333
334 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
335 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
336 ga_clear(&term->tl_scrollback);
337}
338
339/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200340 * Free a terminal and everything it refers to.
341 * Kills the job if there is one.
342 * Called when wiping out a buffer.
343 */
344 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200345free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200346{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200347 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200348 term_T *tp;
349
350 if (term == NULL)
351 return;
352 if (first_term == term)
353 first_term = term->tl_next;
354 else
355 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
356 if (tp->tl_next == term)
357 {
358 tp->tl_next = term->tl_next;
359 break;
360 }
361
362 if (term->tl_job != NULL)
363 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200364 if (term->tl_job->jv_status != JOB_ENDED
365 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200366 job_stop(term->tl_job, NULL, "kill");
367 job_unref(term->tl_job);
368 }
369
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200370 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200371
372 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200373 vim_free(term->tl_title);
374 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200375 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200376 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200377}
378
379/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200380 * Write job output "msg[len]" to the vterm.
381 */
382 static void
383term_write_job_output(term_T *term, char_u *msg, size_t len)
384{
385 VTerm *vterm = term->tl_vterm;
386 char_u *p;
387 size_t done;
388 size_t len_now;
389
390 for (done = 0; done < len; done += len_now)
391 {
392 for (p = msg + done; p < msg + len; )
393 {
394 if (*p == NL)
395 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200396 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200397 }
398 len_now = p - msg - done;
399 vterm_input_write(vterm, (char *)msg + done, len_now);
400 if (p < msg + len && *p == NL)
401 {
402 /* Convert NL to CR-NL, that appears to work best. */
403 vterm_input_write(vterm, "\r\n", 2);
404 ++len_now;
405 }
406 }
407
408 /* this invokes the damage callbacks */
409 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
410}
411
Bram Moolenaar1c844932017-07-24 23:36:41 +0200412 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200413update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200414{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200415 if (term->tl_terminal_mode)
416 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200417 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200418 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200419 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200420 if (term->tl_cursor_visible)
421 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200422 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200423#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200424 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200425 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200426#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200427 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200428}
429
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200430/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200431 * Invoked when "msg" output from a job was received. Write it to the terminal
432 * of "buffer".
433 */
434 void
435write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
436{
437 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200438 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200439
Bram Moolenaard85f2712017-07-28 21:51:57 +0200440 if (term->tl_vterm == NULL)
441 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200442 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200443 return;
444 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200445 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200446 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200447
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200448 if (!term->tl_terminal_mode)
449 {
450 /* TODO: only update once in a while. */
451 update_screen(0);
452 update_cursor(term, TRUE);
453 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200454}
455
456/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200457 * Send a mouse position and click to the vterm
458 */
459 static int
460term_send_mouse(VTerm *vterm, int button, int pressed)
461{
462 VTermModifier mod = VTERM_MOD_NONE;
463
464 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
465 mouse_col - W_WINCOL(curwin), mod);
466 vterm_mouse_button(vterm, button, pressed, mod);
467 return TRUE;
468}
469
470/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200471 * Convert typed key "c" into bytes to send to the job.
472 * Return the number of bytes in "buf".
473 */
474 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200475term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200476{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200477 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200478 VTermKey key = VTERM_KEY_NONE;
479 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200480 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200481
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200482 switch (c)
483 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200484 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200485 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200486 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
487 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200488 case K_DEL: key = VTERM_KEY_DEL; break;
489 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200490 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
491 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200492 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200493 case K_S_END: mod = VTERM_MOD_SHIFT;
494 key = VTERM_KEY_END; break;
495 case K_C_END: mod = VTERM_MOD_CTRL;
496 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200497 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
498 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
499 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
500 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
501 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
502 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
503 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
504 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
505 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
506 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
507 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
508 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
509 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200510 case K_S_HOME: mod = VTERM_MOD_SHIFT;
511 key = VTERM_KEY_HOME; break;
512 case K_C_HOME: mod = VTERM_MOD_CTRL;
513 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200514 case K_INS: key = VTERM_KEY_INS; break;
515 case K_K0: key = VTERM_KEY_KP_0; break;
516 case K_K1: key = VTERM_KEY_KP_1; break;
517 case K_K2: key = VTERM_KEY_KP_2; break;
518 case K_K3: key = VTERM_KEY_KP_3; break;
519 case K_K4: key = VTERM_KEY_KP_4; break;
520 case K_K5: key = VTERM_KEY_KP_5; break;
521 case K_K6: key = VTERM_KEY_KP_6; break;
522 case K_K7: key = VTERM_KEY_KP_7; break;
523 case K_K8: key = VTERM_KEY_KP_8; break;
524 case K_K9: key = VTERM_KEY_KP_9; break;
525 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
526 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
527 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
528 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
529 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
530 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
531 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
532 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
533 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
534 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
535 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
536 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
537 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200538 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
539 key = VTERM_KEY_LEFT; break;
540 case K_C_LEFT: mod = VTERM_MOD_CTRL;
541 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200542 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
543 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
544 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200545 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
546 key = VTERM_KEY_RIGHT; break;
547 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
548 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200549 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200550 case K_S_UP: mod = VTERM_MOD_SHIFT;
551 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200552 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200553
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200554 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
555 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
556 case K_MOUSELEFT: /* TODO */ return 0;
557 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200558
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200559 case K_LEFTMOUSE:
560 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
561 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
562 case K_LEFTRELEASE:
563 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
564 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
565 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
566 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
567 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
568 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
569 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
570 case K_X1MOUSE: /* TODO */ return 0;
571 case K_X1DRAG: /* TODO */ return 0;
572 case K_X1RELEASE: /* TODO */ return 0;
573 case K_X2MOUSE: /* TODO */ return 0;
574 case K_X2DRAG: /* TODO */ return 0;
575 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200576
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200577 case K_IGNORE: return 0;
578 case K_NOP: return 0;
579 case K_UNDO: return 0;
580 case K_HELP: return 0;
581 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
582 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
583 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
584 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
585 case K_SELECT: return 0;
586#ifdef FEAT_GUI
587 case K_VER_SCROLLBAR: return 0;
588 case K_HOR_SCROLLBAR: return 0;
589#endif
590#ifdef FEAT_GUI_TABLINE
591 case K_TABLINE: return 0;
592 case K_TABMENU: return 0;
593#endif
594#ifdef FEAT_NETBEANS_INTG
595 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
596#endif
597#ifdef FEAT_DND
598 case K_DROP: return 0;
599#endif
600#ifdef FEAT_AUTOCMD
601 case K_CURSORHOLD: return 0;
602#endif
603 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
604 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200605 }
606
607 /*
608 * Convert special keys to vterm keys:
609 * - Write keys to vterm: vterm_keyboard_key()
610 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200611 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200612 */
613 if (key != VTERM_KEY_NONE)
614 /* Special key, let vterm convert it. */
615 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200616 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200617 /* Normal character, let vterm convert it. */
618 vterm_keyboard_unichar(vterm, c, mod);
619
620 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200621 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200622}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200623
Bram Moolenaar938783d2017-07-16 20:13:26 +0200624/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200625 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200626 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200627 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200628term_job_running(term_T *term)
629{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200630 /* Also consider the job finished when the channel is closed, to avoid a
631 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200632 return term != NULL
633 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200634 && term->tl_job->jv_status == JOB_STARTED
635 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200636}
637
638/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200639 * Add the last line of the scrollback buffer to the buffer in the window.
640 */
641 static void
642add_scrollback_line_to_buffer(term_T *term)
643{
644 linenr_T lnum = term->tl_scrollback.ga_len - 1;
645 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
646 garray_T ga;
647 int c;
648 int col;
649 int i;
650
651 ga_init2(&ga, 1, 100);
652 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
653 {
654 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
655 goto failed;
656 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
657 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
658 (char_u *)ga.ga_data + ga.ga_len);
659 }
660 if (ga_grow(&ga, 1) == FAIL)
661 goto failed;
662 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
663 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
664
665 if (lnum == 0)
666 {
667 /* Delete the empty line that was in the empty buffer. */
668 curbuf = term->tl_buffer;
669 ml_delete(2, FALSE);
670 curbuf = curwin->w_buffer;
671 }
672
673failed:
674 ga_clear(&ga);
675}
676
677/*
678 * Add the current lines of the terminal to scrollback and to the buffer.
679 * Called after the job has ended and when switching to Terminal mode.
680 */
681 static void
682move_terminal_to_buffer(term_T *term)
683{
684 win_T *wp;
685 int len;
686 int lines_skipped = 0;
687 VTermPos pos;
688 VTermScreenCell cell;
689 VTermScreenCell *p;
690 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
691
692 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
693 {
694 len = 0;
695 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
696 if (vterm_screen_get_cell(screen, pos, &cell) != 0
697 && cell.chars[0] != NUL)
698 len = pos.col + 1;
699
700 if (len == 0)
701 ++lines_skipped;
702 else
703 {
704 while (lines_skipped > 0)
705 {
706 /* Line was skipped, add an empty line. */
707 --lines_skipped;
708 if (ga_grow(&term->tl_scrollback, 1) == OK)
709 {
710 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
711 + term->tl_scrollback.ga_len;
712
713 line->sb_cols = 0;
714 line->sb_cells = NULL;
715 ++term->tl_scrollback.ga_len;
716
717 add_scrollback_line_to_buffer(term);
718 }
719 }
720
721 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
722 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
723 {
724 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
725 + term->tl_scrollback.ga_len;
726
727 for (pos.col = 0; pos.col < len; ++pos.col)
728 {
729 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
730 vim_memset(p + pos.col, 0, sizeof(cell));
731 else
732 p[pos.col] = cell;
733 }
734 line->sb_cols = len;
735 line->sb_cells = p;
736 ++term->tl_scrollback.ga_len;
737
738 add_scrollback_line_to_buffer(term);
739 }
740 else
741 vim_free(p);
742 }
743 }
744
745 FOR_ALL_WINDOWS(wp)
746 {
747 if (wp->w_buffer == term->tl_buffer)
748 {
749 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
750 wp->w_cursor.col = 0;
751 wp->w_valid = 0;
752 redraw_win_later(wp, NOT_VALID);
753 }
754 }
755}
756
757 static void
758set_terminal_mode(term_T *term, int on)
759{
760 term->tl_terminal_mode = on;
761 vim_free(term->tl_status_text);
762 term->tl_status_text = NULL;
763 if (term->tl_buffer == curbuf)
764 maketitle();
765}
766
767/*
768 * Called after the job if finished and Terminal mode is not active:
769 * Move the vterm contents into the scrollback buffer and free the vterm.
770 */
771 static void
772cleanup_vterm(term_T *term)
773{
774 move_terminal_to_buffer(term);
775 term_free_vterm(term);
776 set_terminal_mode(term, FALSE);
777}
778
779/*
780 * Switch from sending keys to the job to Terminal-Normal mode.
781 * Suspends updating the terminal window.
782 */
783 static void
784term_enter_terminal_mode()
785{
786 term_T *term = curbuf->b_term;
787
788 /* Append the current terminal contents to the buffer. */
789 move_terminal_to_buffer(term);
790
791 set_terminal_mode(term, TRUE);
792}
793
794/*
795 * Returns TRUE if the current window contains a terminal and we are in
796 * Terminal-Normal mode.
797 */
798 int
799term_in_terminal_mode()
800{
801 term_T *term = curbuf->b_term;
802
803 return term != NULL && term->tl_terminal_mode;
804}
805
806/*
807 * Switch from Terminal-Normal mode to sending keys to the job.
808 * Restores updating the terminal window.
809 */
810 void
811term_leave_terminal_mode()
812{
813 term_T *term = curbuf->b_term;
814 sb_line_T *line;
815 garray_T *gap;
816
817 /* Remove the terminal contents from the scrollback and the buffer. */
818 gap = &term->tl_scrollback;
819 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
820 {
821 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
822 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
823 vim_free(line->sb_cells);
824 --gap->ga_len;
825 if (gap->ga_len == 0)
826 break;
827 }
828 check_cursor();
829
830 set_terminal_mode(term, FALSE);
831
832 if (term->tl_channel_closed)
833 cleanup_vterm(term);
834 redraw_buf_and_status_later(curbuf, NOT_VALID);
835}
836
837/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200838 * Get a key from the user without mapping.
839 * TODO: use terminal mode mappings.
840 */
841 static int
842term_vgetc()
843{
844 int c;
845
846 ++no_mapping;
847 ++allow_keys;
848 got_int = FALSE;
849 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200850 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200851 --no_mapping;
852 --allow_keys;
853 return c;
854}
855
856/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200857 * Send keys to terminal.
858 */
859 static int
860send_keys_to_term(term_T *term, int c, int typed)
861{
862 char msg[KEY_BUF_LEN];
863 size_t len;
864 static int mouse_was_outside = FALSE;
865 int dragging_outside = FALSE;
866
867 /* Catch keys that need to be handled as in Normal mode. */
868 switch (c)
869 {
870 case NUL:
871 case K_ZERO:
872 if (typed)
873 stuffcharReadbuff(c);
874 return FAIL;
875
876 case K_IGNORE:
877 return FAIL;
878
879 case K_LEFTDRAG:
880 case K_MIDDLEDRAG:
881 case K_RIGHTDRAG:
882 case K_X1DRAG:
883 case K_X2DRAG:
884 dragging_outside = mouse_was_outside;
885 /* FALLTHROUGH */
886 case K_LEFTMOUSE:
887 case K_LEFTMOUSE_NM:
888 case K_LEFTRELEASE:
889 case K_LEFTRELEASE_NM:
890 case K_MIDDLEMOUSE:
891 case K_MIDDLERELEASE:
892 case K_RIGHTMOUSE:
893 case K_RIGHTRELEASE:
894 case K_X1MOUSE:
895 case K_X1RELEASE:
896 case K_X2MOUSE:
897 case K_X2RELEASE:
898 if (mouse_row < W_WINROW(curwin)
899 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
900 || mouse_col < W_WINCOL(curwin)
901 || mouse_col >= W_ENDCOL(curwin)
902 || dragging_outside)
903 {
904 /* click outside the current window */
905 if (typed)
906 {
907 stuffcharReadbuff(c);
908 mouse_was_outside = TRUE;
909 }
910 return FAIL;
911 }
912 }
913 if (typed)
914 mouse_was_outside = FALSE;
915
916 /* Convert the typed key to a sequence of bytes for the job. */
917 len = term_convert_key(term, c, msg);
918 if (len > 0)
919 /* TODO: if FAIL is returned, stop? */
920 channel_send(term->tl_job->jv_channel, PART_IN,
921 (char_u *)msg, (int)len, NULL);
922
923 return OK;
924}
925
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200926 static void
927position_cursor(win_T *wp, VTermPos *pos)
928{
929 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
930 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
931 wp->w_valid |= (VALID_WCOL|VALID_WROW);
932}
933
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200934/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200935 * Handle CTRL-W "": send register contents to the job.
936 */
937 static void
938term_paste_register(int prev_c UNUSED)
939{
940 int c;
941 list_T *l;
942 listitem_T *item;
943 long reglen = 0;
944 int type;
945
946#ifdef FEAT_CMDL_INFO
947 if (add_to_showcmd(prev_c))
948 if (add_to_showcmd('"'))
949 out_flush();
950#endif
951 c = term_vgetc();
952#ifdef FEAT_CMDL_INFO
953 clear_showcmd();
954#endif
955
956 /* CTRL-W "= prompt for expression to evaluate. */
957 if (c == '=' && get_expr_register() != '=')
958 return;
959
960 l = (list_T *)get_reg_contents(c, GREG_LIST);
961 if (l != NULL)
962 {
963 type = get_reg_type(c, &reglen);
964 for (item = l->lv_first; item != NULL; item = item->li_next)
965 {
966 char_u *s = get_tv_string(&item->li_tv);
967
968 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
969 s, STRLEN(s), NULL);
970 if (item->li_next != NULL || type == MLINE)
971 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
972 (char_u *)"\r", 1, NULL);
973 }
974 list_free(l);
975 }
976}
977
978/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200979 * Returns TRUE if the current window contains a terminal and we are sending
980 * keys to the job.
981 */
982 int
983term_use_loop()
984{
985 term_T *term = curbuf->b_term;
986
987 return term != NULL
988 && !term->tl_terminal_mode
989 && term->tl_vterm != NULL
990 && term_job_running(term);
991}
992
993/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200994 * Wait for input and send it to the job.
995 * Return when the start of a CTRL-W command is typed or anything else that
996 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200997 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
998 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200999 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001000 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001001terminal_loop(void)
1002{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001003 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001004 int termkey = 0;
1005
1006 if (*curwin->w_p_tk != NUL)
1007 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001008 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001009
1010 for (;;)
1011 {
1012 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001013 /* Repeat redrawing in case a message is received while redrawing. */
1014 while (curwin->w_redr_type != 0)
1015 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001016 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001017
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001018 c = term_vgetc();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001019 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001020 /* job finished while waiting for a character */
1021 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001022
Bram Moolenaarfae42832017-08-01 22:24:26 +02001023#ifdef UNIX
1024 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1025#endif
1026#ifdef WIN3264
1027 if (c == Ctrl_C)
1028 /* We don't know if the job can handle CTRL-C itself or not, this
1029 * may kill the shell instead of killing the command running in the
1030 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001031 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001032#endif
1033
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001034 if (c == (termkey == 0 ? Ctrl_W : termkey))
1035 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001036 int prev_c = c;
1037
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001038#ifdef FEAT_CMDL_INFO
1039 if (add_to_showcmd(c))
1040 out_flush();
1041#endif
1042 c = term_vgetc();
1043#ifdef FEAT_CMDL_INFO
1044 clear_showcmd();
1045#endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001046 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001047 /* job finished while waiting for a character */
1048 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001049
1050 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001051 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001052 /* "CTRL-W .": send CTRL-W to the job */
1053 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001054 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001055 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001056 {
1057 term_enter_terminal_mode();
1058 return FAIL;
1059 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001060 else if (c == '"')
1061 {
1062 term_paste_register(prev_c);
1063 continue;
1064 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001065 else if (termkey == 0 || c != termkey)
1066 {
1067 stuffcharReadbuff(Ctrl_W);
1068 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001069 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001070 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001071 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001072 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1073 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001074 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001075 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001076}
1077
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001078/*
1079 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001080 * This updates the title and status, but does not close the vter, because
1081 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001082 */
1083 void
1084term_job_ended(job_T *job)
1085{
1086 term_T *term;
1087 int did_one = FALSE;
1088
1089 for (term = first_term; term != NULL; term = term->tl_next)
1090 if (term->tl_job == job)
1091 {
1092 vim_free(term->tl_title);
1093 term->tl_title = NULL;
1094 vim_free(term->tl_status_text);
1095 term->tl_status_text = NULL;
1096 redraw_buf_and_status_later(term->tl_buffer, VALID);
1097 did_one = TRUE;
1098 }
1099 if (did_one)
1100 redraw_statuslines();
1101 if (curbuf->b_term != NULL)
1102 {
1103 if (curbuf->b_term->tl_job == job)
1104 maketitle();
1105 update_cursor(curbuf->b_term, TRUE);
1106 }
1107}
1108
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001109 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001110may_toggle_cursor(term_T *term)
1111{
1112 if (curbuf == term->tl_buffer)
1113 {
1114 if (term->tl_cursor_visible)
1115 cursor_on();
1116 else
1117 cursor_off();
1118 }
1119}
1120
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001121 static int
1122handle_damage(VTermRect rect, void *user)
1123{
1124 term_T *term = (term_T *)user;
1125
1126 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1127 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1128 redraw_buf_later(term->tl_buffer, NOT_VALID);
1129 return 1;
1130}
1131
1132 static int
1133handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1134{
1135 term_T *term = (term_T *)user;
1136
1137 /* TODO */
1138 redraw_buf_later(term->tl_buffer, NOT_VALID);
1139 return 1;
1140}
1141
1142 static int
1143handle_movecursor(
1144 VTermPos pos,
1145 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001146 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001147 void *user)
1148{
1149 term_T *term = (term_T *)user;
1150 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001151
1152 term->tl_cursor_pos = pos;
1153 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001154
1155 FOR_ALL_WINDOWS(wp)
1156 {
1157 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001158 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001159 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001160 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001161 {
1162 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001163 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001164 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001165
1166 return 1;
1167}
1168
Bram Moolenaar21554412017-07-24 21:44:43 +02001169 static int
1170handle_settermprop(
1171 VTermProp prop,
1172 VTermValue *value,
1173 void *user)
1174{
1175 term_T *term = (term_T *)user;
1176
1177 switch (prop)
1178 {
1179 case VTERM_PROP_TITLE:
1180 vim_free(term->tl_title);
1181 term->tl_title = vim_strsave((char_u *)value->string);
1182 vim_free(term->tl_status_text);
1183 term->tl_status_text = NULL;
1184 if (term == curbuf->b_term)
1185 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001186 break;
1187
1188 case VTERM_PROP_CURSORVISIBLE:
1189 term->tl_cursor_visible = value->boolean;
1190 may_toggle_cursor(term);
1191 out_flush();
1192 break;
1193
Bram Moolenaar21554412017-07-24 21:44:43 +02001194 default:
1195 break;
1196 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001197 /* Always return 1, otherwise vterm doesn't store the value internally. */
1198 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001199}
1200
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001201/*
1202 * The job running in the terminal resized the terminal.
1203 */
1204 static int
1205handle_resize(int rows, int cols, void *user)
1206{
1207 term_T *term = (term_T *)user;
1208 win_T *wp;
1209
1210 term->tl_rows = rows;
1211 term->tl_cols = cols;
1212 FOR_ALL_WINDOWS(wp)
1213 {
1214 if (wp->w_buffer == term->tl_buffer)
1215 {
1216 win_setheight_win(rows, wp);
1217 win_setwidth_win(cols, wp);
1218 }
1219 }
1220
1221 redraw_buf_later(term->tl_buffer, NOT_VALID);
1222 return 1;
1223}
1224
Bram Moolenaard85f2712017-07-28 21:51:57 +02001225/*
1226 * Handle a line that is pushed off the top of the screen.
1227 */
1228 static int
1229handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1230{
1231 term_T *term = (term_T *)user;
1232
1233 /* TODO: Limit the number of lines that are stored. */
1234 /* TODO: put the text in the buffer. */
1235 if (ga_grow(&term->tl_scrollback, 1) == OK)
1236 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001237 VTermScreenCell *p = NULL;
1238 int len = 0;
1239 int i;
1240 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001241
1242 /* do not store empty cells at the end */
1243 for (i = 0; i < cols; ++i)
1244 if (cells[i].chars[0] != 0)
1245 len = i + 1;
1246
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001247 if (len > 0)
1248 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001249 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001250 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001251
1252 line = (sb_line_T *)term->tl_scrollback.ga_data
1253 + term->tl_scrollback.ga_len;
1254 line->sb_cols = len;
1255 line->sb_cells = p;
1256 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001257 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001258
1259 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001260 }
1261 return 0; /* ignored */
1262}
1263
Bram Moolenaar21554412017-07-24 21:44:43 +02001264static VTermScreenCallbacks screen_callbacks = {
1265 handle_damage, /* damage */
1266 handle_moverect, /* moverect */
1267 handle_movecursor, /* movecursor */
1268 handle_settermprop, /* settermprop */
1269 NULL, /* bell */
1270 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001271 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001272 NULL /* sb_popline */
1273};
1274
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001275/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001276 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001277 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001278 */
1279 void
1280term_channel_closed(channel_T *ch)
1281{
1282 term_T *term;
1283 int did_one = FALSE;
1284
1285 for (term = first_term; term != NULL; term = term->tl_next)
1286 if (term->tl_job == ch->ch_job)
1287 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001288 term->tl_channel_closed = TRUE;
1289
Bram Moolenaard85f2712017-07-28 21:51:57 +02001290 vim_free(term->tl_title);
1291 term->tl_title = NULL;
1292 vim_free(term->tl_status_text);
1293 term->tl_status_text = NULL;
1294
Bram Moolenaar423802d2017-07-30 16:52:24 +02001295 /* Unless in Terminal-Normal mode: clear the vterm. */
1296 if (!term->tl_terminal_mode)
1297 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001298
1299 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1300 did_one = TRUE;
1301 }
1302 if (did_one)
1303 {
1304 redraw_statuslines();
1305
1306 /* Need to break out of vgetc(). */
1307 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001308 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001309
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001310 term = curbuf->b_term;
1311 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001312 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001313 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001314 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001315 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001316 }
1317 }
1318}
1319
1320/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001321 * Reverse engineer the RGB value into a cterm color index.
1322 * First color is 1. Return 0 if no match found.
1323 */
1324 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001325color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001326{
1327 int red = color->red;
1328 int blue = color->blue;
1329 int green = color->green;
1330
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001331 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001332 if (red == 0)
1333 {
1334 if (green == 0)
1335 {
1336 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001337 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001338 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001339 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001340 }
1341 else if (green == 224)
1342 {
1343 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001344 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001345 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001346 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001347 }
1348 }
1349 else if (red == 224)
1350 {
1351 if (green == 0)
1352 {
1353 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001354 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001355 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001356 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001357 }
1358 else if (green == 224)
1359 {
1360 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001361 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001362 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001363 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001364 }
1365 }
1366 else if (red == 128)
1367 {
1368 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001369 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001370 }
1371 else if (red == 255)
1372 {
1373 if (green == 64)
1374 {
1375 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001376 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001377 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001378 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001379 }
1380 else if (green == 255)
1381 {
1382 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001383 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001384 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001385 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001386 }
1387 }
1388 else if (red == 64)
1389 {
1390 if (green == 64)
1391 {
1392 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001393 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001394 }
1395 else if (green == 255)
1396 {
1397 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001398 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001399 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001400 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001401 }
1402 }
1403 if (t_colors >= 256)
1404 {
1405 if (red == blue && red == green)
1406 {
1407 /* 24-color greyscale */
1408 static int cutoff[23] = {
1409 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1410 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1411 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1412 int i;
1413
1414 for (i = 0; i < 23; ++i)
1415 if (red < cutoff[i])
1416 return i + 233;
1417 return 256;
1418 }
1419
1420 /* 216-color cube */
1421 return 17 + ((red + 25) / 0x33) * 36
1422 + ((green + 25) / 0x33) * 6
1423 + (blue + 25) / 0x33;
1424 }
1425 return 0;
1426}
1427
1428/*
1429 * Convert the attributes of a vterm cell into an attribute index.
1430 */
1431 static int
1432cell2attr(VTermScreenCell *cell)
1433{
1434 int attr = 0;
1435
1436 if (cell->attrs.bold)
1437 attr |= HL_BOLD;
1438 if (cell->attrs.underline)
1439 attr |= HL_UNDERLINE;
1440 if (cell->attrs.italic)
1441 attr |= HL_ITALIC;
1442 if (cell->attrs.strike)
1443 attr |= HL_STANDOUT;
1444 if (cell->attrs.reverse)
1445 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001446
1447#ifdef FEAT_GUI
1448 if (gui.in_use)
1449 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001450 guicolor_T fg, bg;
1451
1452 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1453 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1454 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001455 }
1456 else
1457#endif
1458#ifdef FEAT_TERMGUICOLORS
1459 if (p_tgc)
1460 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001461 guicolor_T fg, bg;
1462
1463 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1464 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1465
1466 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001467 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001468 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001469#endif
1470 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001471 int bold = MAYBE;
1472 int fg = color2index(&cell->fg, TRUE, &bold);
1473 int bg = color2index(&cell->bg, FALSE, &bold);
1474
1475 /* with 8 colors set the bold attribute to get a bright foreground */
1476 if (bold == TRUE)
1477 attr |= HL_BOLD;
1478 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001479 }
1480 return 0;
1481}
1482
1483/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001484 * Called to update the window that contains a terminal.
1485 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001486 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001487 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001488term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001489{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001490 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001491 VTerm *vterm;
1492 VTermScreen *screen;
1493 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001494 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001495
Bram Moolenaar423802d2017-07-30 16:52:24 +02001496 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001497 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001498
Bram Moolenaard85f2712017-07-28 21:51:57 +02001499 vterm = term->tl_vterm;
1500 screen = vterm_obtain_screen(vterm);
1501 state = vterm_obtain_state(vterm);
1502
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001503 /*
1504 * If the window was resized a redraw will be triggered and we get here.
1505 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1506 */
1507 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1508 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001509 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001510 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1511 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1512 win_T *twp;
1513
1514 FOR_ALL_WINDOWS(twp)
1515 {
1516 /* When more than one window shows the same terminal, use the
1517 * smallest size. */
1518 if (twp->w_buffer == term->tl_buffer)
1519 {
1520 if (!term->tl_rows_fixed && rows > twp->w_height)
1521 rows = twp->w_height;
1522 if (!term->tl_cols_fixed && cols > twp->w_width)
1523 cols = twp->w_width;
1524 }
1525 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001526
1527 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001528 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001529 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001530 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001531 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001532
1533 /* The cursor may have been moved when resizing. */
1534 vterm_state_get_cursorpos(state, &pos);
1535 position_cursor(wp, &pos);
1536
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001537 /* TODO: Only redraw what changed. */
1538 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001539 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001540 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001541 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001542
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001543 if (pos.row < term->tl_rows)
1544 {
1545 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001546 {
1547 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001548 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001549
Bram Moolenaareeac6772017-07-23 15:48:37 +02001550 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1551 vim_memset(&cell, 0, sizeof(cell));
1552
1553 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001554 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001555 if (c == NUL)
1556 {
1557 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001558#if defined(FEAT_MBYTE)
1559 if (enc_utf8)
1560 ScreenLinesUC[off] = NUL;
1561#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001562 }
1563 else
1564 {
1565#if defined(FEAT_MBYTE)
1566 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001567 {
1568 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001569 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001570 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001571 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001572 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001573 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001574 if (enc_utf8)
1575 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001576 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001577#else
1578 ScreenLines[off] = c;
1579#endif
1580 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001581 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001582
1583 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001584 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001585 if (cell.width == 2)
1586 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001587 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001588#if defined(FEAT_MBYTE)
1589 if (enc_utf8)
1590 ScreenLinesUC[off] = NUL;
1591#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001592 ++pos.col;
1593 ++off;
1594 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001595 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001596 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001597 else
1598 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001599
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001600 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1601 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001602 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001603
1604 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001605}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001606
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001607/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001608 * Return TRUE if "wp" is a terminal window where the job has finished.
1609 */
1610 int
1611term_is_finished(buf_T *buf)
1612{
1613 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1614}
1615
1616/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001617 * Return TRUE if "wp" is a terminal window where the job has finished or we
1618 * are in Terminal-Normal mode.
1619 */
1620 int
1621term_show_buffer(buf_T *buf)
1622{
1623 term_T *term = buf->b_term;
1624
1625 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1626}
1627
1628/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001629 * The current buffer is going to be changed. If there is terminal
1630 * highlighting remove it now.
1631 */
1632 void
1633term_change_in_curbuf(void)
1634{
1635 term_T *term = curbuf->b_term;
1636
1637 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1638 {
1639 free_scrollback(term);
1640 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001641
1642 /* The buffer is now like a normal buffer, it cannot be easily
1643 * abandoned when changed. */
1644 set_string_option_direct((char_u *)"buftype", -1,
1645 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001646 }
1647}
1648
1649/*
1650 * Get the screen attribute for a position in the buffer.
1651 */
1652 int
1653term_get_attr(buf_T *buf, linenr_T lnum, int col)
1654{
1655 term_T *term = buf->b_term;
1656 sb_line_T *line;
1657
Bram Moolenaar70229f92017-07-29 16:01:53 +02001658 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001659 return 0;
1660 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1661 if (col >= line->sb_cols)
1662 return 0;
1663 return cell2attr(line->sb_cells + col);
1664}
1665
1666/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001667 * Create a new vterm and initialize it.
1668 */
1669 static void
1670create_vterm(term_T *term, int rows, int cols)
1671{
1672 VTerm *vterm;
1673 VTermScreen *screen;
1674
1675 vterm = vterm_new(rows, cols);
1676 term->tl_vterm = vterm;
1677 screen = vterm_obtain_screen(vterm);
1678 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1679 /* TODO: depends on 'encoding'. */
1680 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001681
1682 /* Vterm uses a default black background. Set it to white when
1683 * 'background' is "light". */
1684 if (*p_bg == 'l')
1685 {
1686 VTermColor fg, bg;
1687
1688 fg.red = fg.green = fg.blue = 0;
1689 bg.red = bg.green = bg.blue = 255;
1690 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1691 }
1692
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001693 /* Required to initialize most things. */
1694 vterm_screen_reset(screen, 1 /* hard */);
1695}
1696
Bram Moolenaar21554412017-07-24 21:44:43 +02001697/*
1698 * Return the text to show for the buffer name and status.
1699 */
1700 char_u *
1701term_get_status_text(term_T *term)
1702{
1703 if (term->tl_status_text == NULL)
1704 {
1705 char_u *txt;
1706 size_t len;
1707
Bram Moolenaar423802d2017-07-30 16:52:24 +02001708 if (term->tl_terminal_mode)
1709 {
1710 if (term_job_running(term))
1711 txt = (char_u *)_("Terminal");
1712 else
1713 txt = (char_u *)_("Terminal-finished");
1714 }
1715 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001716 txt = term->tl_title;
1717 else if (term_job_running(term))
1718 txt = (char_u *)_("running");
1719 else
1720 txt = (char_u *)_("finished");
1721 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001722 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001723 if (term->tl_status_text != NULL)
1724 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1725 term->tl_buffer->b_fname, txt);
1726 }
1727 return term->tl_status_text;
1728}
1729
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001730/*
1731 * Mark references in jobs of terminals.
1732 */
1733 int
1734set_ref_in_term(int copyID)
1735{
1736 int abort = FALSE;
1737 term_T *term;
1738 typval_T tv;
1739
1740 for (term = first_term; term != NULL; term = term->tl_next)
1741 if (term->tl_job != NULL)
1742 {
1743 tv.v_type = VAR_JOB;
1744 tv.vval.v_job = term->tl_job;
1745 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1746 }
1747 return abort;
1748}
1749
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001750/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001751 * Get the buffer from the first argument in "argvars".
1752 * Returns NULL when the buffer is not for a terminal window.
1753 */
1754 static buf_T *
1755term_get_buf(typval_T *argvars)
1756{
1757 buf_T *buf;
1758
1759 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1760 ++emsg_off;
1761 buf = get_buf_tv(&argvars[0], FALSE);
1762 --emsg_off;
1763 if (buf == NULL || buf->b_term == NULL)
1764 return NULL;
1765 return buf;
1766}
1767
1768/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001769 * "term_getattr(attr, name)" function
1770 */
1771 void
1772f_term_getattr(typval_T *argvars, typval_T *rettv)
1773{
1774 int attr;
1775 size_t i;
1776 char_u *name;
1777
1778 static struct {
1779 char *name;
1780 int attr;
1781 } attrs[] = {
1782 {"bold", HL_BOLD},
1783 {"italic", HL_ITALIC},
1784 {"underline", HL_UNDERLINE},
1785 {"strike", HL_STANDOUT},
1786 {"reverse", HL_INVERSE},
1787 };
1788
1789 attr = get_tv_number(&argvars[0]);
1790 name = get_tv_string_chk(&argvars[1]);
1791 if (name == NULL)
1792 return;
1793
1794 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1795 if (STRCMP(name, attrs[i].name) == 0)
1796 {
1797 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1798 break;
1799 }
1800}
1801
1802/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001803 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001804 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001805 void
1806f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001807{
Bram Moolenaar97870002017-07-30 18:28:38 +02001808 buf_T *buf = term_get_buf(argvars);
1809 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001810
Bram Moolenaar97870002017-07-30 18:28:38 +02001811 if (rettv_list_alloc(rettv) == FAIL)
1812 return;
1813 if (buf == NULL)
1814 return;
1815
1816 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001817 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1818 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001819 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001820}
1821
1822/*
1823 * "term_getjob(buf)" function
1824 */
1825 void
1826f_term_getjob(typval_T *argvars, typval_T *rettv)
1827{
1828 buf_T *buf = term_get_buf(argvars);
1829
1830 rettv->v_type = VAR_JOB;
1831 rettv->vval.v_job = NULL;
1832 if (buf == NULL)
1833 return;
1834
1835 rettv->vval.v_job = buf->b_term->tl_job;
1836 if (rettv->vval.v_job != NULL)
1837 ++rettv->vval.v_job->jv_refcount;
1838}
1839
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001840 static int
1841get_row_number(typval_T *tv, term_T *term)
1842{
1843 if (tv->v_type == VAR_STRING
1844 && tv->vval.v_string != NULL
1845 && STRCMP(tv->vval.v_string, ".") == 0)
1846 return term->tl_cursor_pos.row;
1847 return (int)get_tv_number(tv) - 1;
1848}
1849
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001850/*
1851 * "term_getline(buf, row)" function
1852 */
1853 void
1854f_term_getline(typval_T *argvars, typval_T *rettv)
1855{
1856 buf_T *buf = term_get_buf(argvars);
1857 term_T *term;
1858 int row;
1859
1860 rettv->v_type = VAR_STRING;
1861 if (buf == NULL)
1862 return;
1863 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001864 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001865
1866 if (term->tl_vterm == NULL)
1867 {
1868 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1869
1870 /* vterm is finished, get the text from the buffer */
1871 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1872 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1873 }
1874 else
1875 {
1876 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1877 VTermRect rect;
1878 int len;
1879 char_u *p;
1880
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001881 if (row < 0 || row >= term->tl_rows)
1882 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001883 len = term->tl_cols * MB_MAXBYTES + 1;
1884 p = alloc(len);
1885 if (p == NULL)
1886 return;
1887 rettv->vval.v_string = p;
1888
1889 rect.start_col = 0;
1890 rect.end_col = term->tl_cols;
1891 rect.start_row = row;
1892 rect.end_row = row + 1;
1893 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1894 }
1895}
1896
1897/*
1898 * "term_getsize(buf)" function
1899 */
1900 void
1901f_term_getsize(typval_T *argvars, typval_T *rettv)
1902{
1903 buf_T *buf = term_get_buf(argvars);
1904 list_T *l;
1905
1906 if (rettv_list_alloc(rettv) == FAIL)
1907 return;
1908 if (buf == NULL)
1909 return;
1910
1911 l = rettv->vval.v_list;
1912 list_append_number(l, buf->b_term->tl_rows);
1913 list_append_number(l, buf->b_term->tl_cols);
1914}
1915
1916/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001917 * "term_getstatus(buf)" function
1918 */
1919 void
1920f_term_getstatus(typval_T *argvars, typval_T *rettv)
1921{
1922 buf_T *buf = term_get_buf(argvars);
1923 term_T *term;
1924 char_u val[100];
1925
1926 rettv->v_type = VAR_STRING;
1927 if (buf == NULL)
1928 return;
1929 term = buf->b_term;
1930
1931 if (term_job_running(term))
1932 STRCPY(val, "running");
1933 else
1934 STRCPY(val, "finished");
1935 if (term->tl_terminal_mode)
1936 STRCAT(val, ",terminal");
1937 rettv->vval.v_string = vim_strsave(val);
1938}
1939
1940/*
1941 * "term_gettitle(buf)" function
1942 */
1943 void
1944f_term_gettitle(typval_T *argvars, typval_T *rettv)
1945{
1946 buf_T *buf = term_get_buf(argvars);
1947
1948 rettv->v_type = VAR_STRING;
1949 if (buf == NULL)
1950 return;
1951
1952 if (buf->b_term->tl_title != NULL)
1953 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1954}
1955
1956/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001957 * "term_gettty(buf)" function
1958 */
1959 void
1960f_term_gettty(typval_T *argvars, typval_T *rettv)
1961{
1962 buf_T *buf = term_get_buf(argvars);
1963 char_u *p;
1964
1965 rettv->v_type = VAR_STRING;
1966 if (buf == NULL)
1967 return;
1968 if (buf->b_term->tl_job != NULL)
1969 p = buf->b_term->tl_job->jv_tty_name;
1970 else
1971 p = buf->b_term->tl_tty_name;
1972 if (p != NULL)
1973 rettv->vval.v_string = vim_strsave(p);
1974}
1975
1976/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001977 * "term_list()" function
1978 */
1979 void
1980f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1981{
1982 term_T *tp;
1983 list_T *l;
1984
1985 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1986 return;
1987
1988 l = rettv->vval.v_list;
1989 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1990 if (tp != NULL && tp->tl_buffer != NULL)
1991 if (list_append_number(l,
1992 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1993 return;
1994}
1995
1996/*
1997 * "term_scrape(buf, row)" function
1998 */
1999 void
2000f_term_scrape(typval_T *argvars, typval_T *rettv)
2001{
2002 buf_T *buf = term_get_buf(argvars);
2003 VTermScreen *screen = NULL;
2004 VTermPos pos;
2005 list_T *l;
2006 term_T *term;
2007
2008 if (rettv_list_alloc(rettv) == FAIL)
2009 return;
2010 if (buf == NULL)
2011 return;
2012 term = buf->b_term;
2013 if (term->tl_vterm != NULL)
2014 screen = vterm_obtain_screen(term->tl_vterm);
2015
2016 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002017 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002018 for (pos.col = 0; pos.col < term->tl_cols; )
2019 {
2020 dict_T *dcell;
2021 VTermScreenCell cell;
2022 char_u rgb[8];
2023 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2024 int off = 0;
2025 int i;
2026
2027 if (screen == NULL)
2028 {
2029 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2030 sb_line_T *line;
2031
2032 /* vterm has finished, get the cell from scrollback */
2033 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2034 break;
2035 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2036 if (pos.col >= line->sb_cols)
2037 break;
2038 cell = line->sb_cells[pos.col];
2039 }
2040 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2041 break;
2042 dcell = dict_alloc();
2043 list_append_dict(l, dcell);
2044
2045 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2046 {
2047 if (cell.chars[i] == 0)
2048 break;
2049 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2050 }
2051 mbs[off] = NUL;
2052 dict_add_nr_str(dcell, "chars", 0, mbs);
2053
2054 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2055 cell.fg.red, cell.fg.green, cell.fg.blue);
2056 dict_add_nr_str(dcell, "fg", 0, rgb);
2057 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2058 cell.bg.red, cell.bg.green, cell.bg.blue);
2059 dict_add_nr_str(dcell, "bg", 0, rgb);
2060
2061 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2062 dict_add_nr_str(dcell, "width", cell.width, NULL);
2063
2064 ++pos.col;
2065 if (cell.width == 2)
2066 ++pos.col;
2067 }
2068}
2069
2070/*
2071 * "term_sendkeys(buf, keys)" function
2072 */
2073 void
2074f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2075{
2076 buf_T *buf = term_get_buf(argvars);
2077 char_u *msg;
2078 term_T *term;
2079
2080 rettv->v_type = VAR_UNKNOWN;
2081 if (buf == NULL)
2082 return;
2083
2084 msg = get_tv_string_chk(&argvars[1]);
2085 if (msg == NULL)
2086 return;
2087 term = buf->b_term;
2088 if (term->tl_vterm == NULL)
2089 return;
2090
2091 while (*msg != NUL)
2092 {
2093 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2094 msg += MB_PTR2LEN(msg);
2095 }
2096
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002097 if (!term->tl_terminal_mode)
2098 {
2099 /* TODO: only update once in a while. */
2100 update_screen(0);
2101 if (buf == curbuf)
2102 update_cursor(term, TRUE);
2103 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002104}
2105
2106/*
2107 * "term_start(command, options)" function
2108 */
2109 void
2110f_term_start(typval_T *argvars, typval_T *rettv)
2111{
2112 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002113 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002114
2115 if (cmd == NULL)
2116 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002117 init_job_options(&opt);
2118 /* TODO: allow more job options */
2119 if (argvars[1].v_type != VAR_UNKNOWN
2120 && get_job_options(&argvars[1], &opt,
2121 JO_TIMEOUT_ALL + JO_STOPONEXIT
2122 + JO_EXIT_CB + JO_CLOSE_CALLBACK) == FAIL)
2123 return;
2124
2125 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002126
2127 if (curbuf->b_term != NULL)
2128 rettv->vval.v_number = curbuf->b_fnum;
2129}
2130
2131/*
2132 * "term_wait" function
2133 */
2134 void
2135f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2136{
2137 buf_T *buf = term_get_buf(argvars);
2138
2139 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002140 {
2141 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002142 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002143 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002144 if (buf->b_term->tl_job == NULL)
2145 {
2146 ch_log(NULL, "term_wait(): no job to wait for");
2147 return;
2148 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002149
2150 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002151 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002152 {
2153 /* The job is dead, keep reading channel I/O until the channel is
2154 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002155 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002156 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2157 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002158 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002159 parse_queued_messages();
2160 ui_delay(10L, FALSE);
2161 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002162 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002163 parse_queued_messages();
2164 }
2165 else
2166 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002167 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002168 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002169
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002170 /* Wait for 10 msec for any channel I/O. */
2171 /* TODO: use delay from optional argument */
2172 ui_delay(10L, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002173 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002174
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002175 /* Flushing messages on channels is hopefully sufficient.
2176 * TODO: is there a better way? */
2177 parse_queued_messages();
2178 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002179}
2180
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002181# ifdef WIN3264
2182
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002183/**************************************
2184 * 2. MS-Windows implementation.
2185 */
2186
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002187#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2188#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2189
Bram Moolenaar8a773062017-07-24 22:29:21 +02002190void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002191void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002192void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002193BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2194void (*winpty_config_set_initial_size)(void*, int, int);
2195LPCWSTR (*winpty_conin_name)(void*);
2196LPCWSTR (*winpty_conout_name)(void*);
2197LPCWSTR (*winpty_conerr_name)(void*);
2198void (*winpty_free)(void*);
2199void (*winpty_config_free)(void*);
2200void (*winpty_spawn_config_free)(void*);
2201void (*winpty_error_free)(void*);
2202LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002203BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002204
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002205#define WINPTY_DLL "winpty.dll"
2206
2207static HINSTANCE hWinPtyDLL = NULL;
2208
2209 int
2210dyn_winpty_init(void)
2211{
2212 int i;
2213 static struct
2214 {
2215 char *name;
2216 FARPROC *ptr;
2217 } winpty_entry[] =
2218 {
2219 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2220 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2221 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2222 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2223 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2224 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2225 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2226 {"winpty_free", (FARPROC*)&winpty_free},
2227 {"winpty_open", (FARPROC*)&winpty_open},
2228 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2229 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2230 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2231 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002232 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002233 {NULL, NULL}
2234 };
2235
2236 /* No need to initialize twice. */
2237 if (hWinPtyDLL)
2238 return 1;
2239 /* Load winpty.dll */
2240 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2241 if (!hWinPtyDLL)
2242 {
2243 EMSG2(_(e_loadlib), WINPTY_DLL);
2244 return 0;
2245 }
2246 for (i = 0; winpty_entry[i].name != NULL
2247 && winpty_entry[i].ptr != NULL; ++i)
2248 {
2249 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2250 winpty_entry[i].name)) == NULL)
2251 {
2252 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2253 return 0;
2254 }
2255 }
2256
2257 return 1;
2258}
2259
2260/*
2261 * Create a new terminal of "rows" by "cols" cells.
2262 * Store a reference in "term".
2263 * Return OK or FAIL.
2264 */
2265 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002266term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002267{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002268 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002269 channel_T *channel = NULL;
2270 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002271 DWORD error;
2272 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2273 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002274 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002275
2276 if (!dyn_winpty_init())
2277 return FAIL;
2278
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002279 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002280 if (p == NULL)
2281 return FAIL;
2282
2283 job = job_alloc();
2284 if (job == NULL)
2285 goto failed;
2286
2287 channel = add_channel();
2288 if (channel == NULL)
2289 goto failed;
2290
2291 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2292 if (term->tl_winpty_config == NULL)
2293 goto failed;
2294
2295 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2296 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2297 if (term->tl_winpty == NULL)
2298 goto failed;
2299
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002300 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002301 spawn_config = winpty_spawn_config_new(
2302 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2303 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2304 NULL,
2305 p,
2306 NULL,
2307 NULL,
2308 &winpty_err);
2309 if (spawn_config == NULL)
2310 goto failed;
2311
2312 channel = add_channel();
2313 if (channel == NULL)
2314 goto failed;
2315
2316 job = job_alloc();
2317 if (job == NULL)
2318 goto failed;
2319
2320 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2321 &child_thread_handle, &error, &winpty_err))
2322 goto failed;
2323
2324 channel_set_pipes(channel,
2325 (sock_T) CreateFileW(
2326 winpty_conin_name(term->tl_winpty),
2327 GENERIC_WRITE, 0, NULL,
2328 OPEN_EXISTING, 0, NULL),
2329 (sock_T) CreateFileW(
2330 winpty_conout_name(term->tl_winpty),
2331 GENERIC_READ, 0, NULL,
2332 OPEN_EXISTING, 0, NULL),
2333 (sock_T) CreateFileW(
2334 winpty_conerr_name(term->tl_winpty),
2335 GENERIC_READ, 0, NULL,
2336 OPEN_EXISTING, 0, NULL));
2337
2338 jo = CreateJobObject(NULL, NULL);
2339 if (jo == NULL)
2340 goto failed;
2341
2342 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002343 {
2344 /* Failed, switch the way to terminate process with TerminateProcess. */
2345 CloseHandle(jo);
2346 jo = NULL;
2347 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002348
2349 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002350 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002351
2352 create_vterm(term, rows, cols);
2353
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002354 channel_set_job(channel, job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002355
2356 job->jv_channel = channel;
2357 job->jv_proc_info.hProcess = child_process_handle;
2358 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2359 job->jv_job_object = jo;
2360 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002361 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002362 term->tl_job = job;
2363
2364 return OK;
2365
2366failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002367 if (spawn_config != NULL)
2368 winpty_spawn_config_free(spawn_config);
2369 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002370 if (channel != NULL)
2371 channel_clear(channel);
2372 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002373 {
2374 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002375 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002376 }
2377 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002378 if (jo != NULL)
2379 CloseHandle(jo);
2380 if (term->tl_winpty != NULL)
2381 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002382 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002383 if (term->tl_winpty_config != NULL)
2384 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002385 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002386 if (winpty_err != NULL)
2387 {
2388 char_u *msg = utf16_to_enc(
2389 (short_u *)winpty_error_msg(winpty_err), NULL);
2390
2391 EMSG(msg);
2392 winpty_error_free(winpty_err);
2393 }
2394 return FAIL;
2395}
2396
2397/*
2398 * Free the terminal emulator part of "term".
2399 */
2400 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002401term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002402{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002403 if (term->tl_winpty != NULL)
2404 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002405 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002406 if (term->tl_winpty_config != NULL)
2407 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002408 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002409 if (term->tl_vterm != NULL)
2410 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002411 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002412}
2413
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002414/*
2415 * Request size to terminal.
2416 */
2417 static void
2418term_report_winsize(term_T *term, int rows, int cols)
2419{
2420 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2421}
2422
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002423# else
2424
2425/**************************************
2426 * 3. Unix-like implementation.
2427 */
2428
2429/*
2430 * Create a new terminal of "rows" by "cols" cells.
2431 * Start job for "cmd".
2432 * Store the pointers in "term".
2433 * Return OK or FAIL.
2434 */
2435 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002436term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002437{
2438 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002439
2440 create_vterm(term, rows, cols);
2441
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002442 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002443 argvars[0].v_type = VAR_STRING;
2444 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002445
2446 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002447 if (term->tl_job != NULL)
2448 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002449
Bram Moolenaar61a66052017-07-22 18:39:00 +02002450 return term->tl_job != NULL
2451 && term->tl_job->jv_channel != NULL
2452 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002453}
2454
2455/*
2456 * Free the terminal emulator part of "term".
2457 */
2458 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002459term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002460{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002461 if (term->tl_vterm != NULL)
2462 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002463 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002464}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002465
2466/*
2467 * Request size to terminal.
2468 */
2469 static void
2470term_report_winsize(term_T *term, int rows, int cols)
2471{
2472 /* Use an ioctl() to report the new window size to the job. */
2473 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2474 {
2475 int fd = -1;
2476 int part;
2477
2478 for (part = PART_OUT; part < PART_COUNT; ++part)
2479 {
2480 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2481 if (isatty(fd))
2482 break;
2483 }
2484 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2485 mch_stop_job(term->tl_job, (char_u *)"winch");
2486 }
2487}
2488
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002489# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002490
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002491#endif /* FEAT_TERMINAL */