blob: b06feffc2fbba5bb8988a67dc095249fc3d9918f [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);
1308
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001309 term = curbuf->b_term;
1310 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001311 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001312 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001313 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001314 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001315 }
1316 }
1317}
1318
1319/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001320 * Reverse engineer the RGB value into a cterm color index.
1321 * First color is 1. Return 0 if no match found.
1322 */
1323 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001324color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001325{
1326 int red = color->red;
1327 int blue = color->blue;
1328 int green = color->green;
1329
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001330 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001331 if (red == 0)
1332 {
1333 if (green == 0)
1334 {
1335 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001336 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001337 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001338 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001339 }
1340 else if (green == 224)
1341 {
1342 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001343 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001344 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001345 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001346 }
1347 }
1348 else if (red == 224)
1349 {
1350 if (green == 0)
1351 {
1352 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001353 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001354 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001355 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001356 }
1357 else if (green == 224)
1358 {
1359 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001360 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001361 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001362 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001363 }
1364 }
1365 else if (red == 128)
1366 {
1367 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001368 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001369 }
1370 else if (red == 255)
1371 {
1372 if (green == 64)
1373 {
1374 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001375 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001376 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001377 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001378 }
1379 else if (green == 255)
1380 {
1381 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001382 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001383 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001384 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001385 }
1386 }
1387 else if (red == 64)
1388 {
1389 if (green == 64)
1390 {
1391 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001392 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001393 }
1394 else if (green == 255)
1395 {
1396 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001397 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001398 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001399 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001400 }
1401 }
1402 if (t_colors >= 256)
1403 {
1404 if (red == blue && red == green)
1405 {
1406 /* 24-color greyscale */
1407 static int cutoff[23] = {
1408 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1409 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1410 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1411 int i;
1412
1413 for (i = 0; i < 23; ++i)
1414 if (red < cutoff[i])
1415 return i + 233;
1416 return 256;
1417 }
1418
1419 /* 216-color cube */
1420 return 17 + ((red + 25) / 0x33) * 36
1421 + ((green + 25) / 0x33) * 6
1422 + (blue + 25) / 0x33;
1423 }
1424 return 0;
1425}
1426
1427/*
1428 * Convert the attributes of a vterm cell into an attribute index.
1429 */
1430 static int
1431cell2attr(VTermScreenCell *cell)
1432{
1433 int attr = 0;
1434
1435 if (cell->attrs.bold)
1436 attr |= HL_BOLD;
1437 if (cell->attrs.underline)
1438 attr |= HL_UNDERLINE;
1439 if (cell->attrs.italic)
1440 attr |= HL_ITALIC;
1441 if (cell->attrs.strike)
1442 attr |= HL_STANDOUT;
1443 if (cell->attrs.reverse)
1444 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001445
1446#ifdef FEAT_GUI
1447 if (gui.in_use)
1448 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001449 guicolor_T fg, bg;
1450
1451 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1452 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1453 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001454 }
1455 else
1456#endif
1457#ifdef FEAT_TERMGUICOLORS
1458 if (p_tgc)
1459 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001460 guicolor_T fg, bg;
1461
1462 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1463 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1464
1465 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001466 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001467 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001468#endif
1469 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001470 int bold = MAYBE;
1471 int fg = color2index(&cell->fg, TRUE, &bold);
1472 int bg = color2index(&cell->bg, FALSE, &bold);
1473
1474 /* with 8 colors set the bold attribute to get a bright foreground */
1475 if (bold == TRUE)
1476 attr |= HL_BOLD;
1477 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001478 }
1479 return 0;
1480}
1481
1482/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001483 * Called to update the window that contains a terminal.
1484 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001485 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001486 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001487term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001488{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001489 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001490 VTerm *vterm;
1491 VTermScreen *screen;
1492 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001493 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001494
Bram Moolenaar423802d2017-07-30 16:52:24 +02001495 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001496 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001497
Bram Moolenaard85f2712017-07-28 21:51:57 +02001498 vterm = term->tl_vterm;
1499 screen = vterm_obtain_screen(vterm);
1500 state = vterm_obtain_state(vterm);
1501
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001502 /*
1503 * If the window was resized a redraw will be triggered and we get here.
1504 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1505 */
1506 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1507 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001508 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001509 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1510 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1511 win_T *twp;
1512
1513 FOR_ALL_WINDOWS(twp)
1514 {
1515 /* When more than one window shows the same terminal, use the
1516 * smallest size. */
1517 if (twp->w_buffer == term->tl_buffer)
1518 {
1519 if (!term->tl_rows_fixed && rows > twp->w_height)
1520 rows = twp->w_height;
1521 if (!term->tl_cols_fixed && cols > twp->w_width)
1522 cols = twp->w_width;
1523 }
1524 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001525
1526 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001527 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001528 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001529 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001530 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001531
1532 /* The cursor may have been moved when resizing. */
1533 vterm_state_get_cursorpos(state, &pos);
1534 position_cursor(wp, &pos);
1535
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001536 /* TODO: Only redraw what changed. */
1537 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001538 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001539 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001540 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001541
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001542 if (pos.row < term->tl_rows)
1543 {
1544 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001545 {
1546 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001547 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001548
Bram Moolenaareeac6772017-07-23 15:48:37 +02001549 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1550 vim_memset(&cell, 0, sizeof(cell));
1551
1552 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001553 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001554 if (c == NUL)
1555 {
1556 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001557#if defined(FEAT_MBYTE)
1558 if (enc_utf8)
1559 ScreenLinesUC[off] = NUL;
1560#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001561 }
1562 else
1563 {
1564#if defined(FEAT_MBYTE)
1565 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001566 {
1567 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001568 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001569 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001570 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001571 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001572 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001573 if (enc_utf8)
1574 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001575 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001576#else
1577 ScreenLines[off] = c;
1578#endif
1579 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001580 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001581
1582 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001583 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001584 if (cell.width == 2)
1585 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001586 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001587#if defined(FEAT_MBYTE)
1588 if (enc_utf8)
1589 ScreenLinesUC[off] = NUL;
1590#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001591 ++pos.col;
1592 ++off;
1593 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001594 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001595 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001596 else
1597 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001598
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001599 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1600 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001601 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001602
1603 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001604}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001605
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001606/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001607 * Return TRUE if "wp" is a terminal window where the job has finished.
1608 */
1609 int
1610term_is_finished(buf_T *buf)
1611{
1612 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1613}
1614
1615/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001616 * Return TRUE if "wp" is a terminal window where the job has finished or we
1617 * are in Terminal-Normal mode.
1618 */
1619 int
1620term_show_buffer(buf_T *buf)
1621{
1622 term_T *term = buf->b_term;
1623
1624 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1625}
1626
1627/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001628 * The current buffer is going to be changed. If there is terminal
1629 * highlighting remove it now.
1630 */
1631 void
1632term_change_in_curbuf(void)
1633{
1634 term_T *term = curbuf->b_term;
1635
1636 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1637 {
1638 free_scrollback(term);
1639 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001640
1641 /* The buffer is now like a normal buffer, it cannot be easily
1642 * abandoned when changed. */
1643 set_string_option_direct((char_u *)"buftype", -1,
1644 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001645 }
1646}
1647
1648/*
1649 * Get the screen attribute for a position in the buffer.
1650 */
1651 int
1652term_get_attr(buf_T *buf, linenr_T lnum, int col)
1653{
1654 term_T *term = buf->b_term;
1655 sb_line_T *line;
1656
Bram Moolenaar70229f92017-07-29 16:01:53 +02001657 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001658 return 0;
1659 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1660 if (col >= line->sb_cols)
1661 return 0;
1662 return cell2attr(line->sb_cells + col);
1663}
1664
1665/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001666 * Create a new vterm and initialize it.
1667 */
1668 static void
1669create_vterm(term_T *term, int rows, int cols)
1670{
1671 VTerm *vterm;
1672 VTermScreen *screen;
1673
1674 vterm = vterm_new(rows, cols);
1675 term->tl_vterm = vterm;
1676 screen = vterm_obtain_screen(vterm);
1677 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1678 /* TODO: depends on 'encoding'. */
1679 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001680
1681 /* Vterm uses a default black background. Set it to white when
1682 * 'background' is "light". */
1683 if (*p_bg == 'l')
1684 {
1685 VTermColor fg, bg;
1686
1687 fg.red = fg.green = fg.blue = 0;
1688 bg.red = bg.green = bg.blue = 255;
1689 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1690 }
1691
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001692 /* Required to initialize most things. */
1693 vterm_screen_reset(screen, 1 /* hard */);
1694}
1695
Bram Moolenaar21554412017-07-24 21:44:43 +02001696/*
1697 * Return the text to show for the buffer name and status.
1698 */
1699 char_u *
1700term_get_status_text(term_T *term)
1701{
1702 if (term->tl_status_text == NULL)
1703 {
1704 char_u *txt;
1705 size_t len;
1706
Bram Moolenaar423802d2017-07-30 16:52:24 +02001707 if (term->tl_terminal_mode)
1708 {
1709 if (term_job_running(term))
1710 txt = (char_u *)_("Terminal");
1711 else
1712 txt = (char_u *)_("Terminal-finished");
1713 }
1714 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001715 txt = term->tl_title;
1716 else if (term_job_running(term))
1717 txt = (char_u *)_("running");
1718 else
1719 txt = (char_u *)_("finished");
1720 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001721 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001722 if (term->tl_status_text != NULL)
1723 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1724 term->tl_buffer->b_fname, txt);
1725 }
1726 return term->tl_status_text;
1727}
1728
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001729/*
1730 * Mark references in jobs of terminals.
1731 */
1732 int
1733set_ref_in_term(int copyID)
1734{
1735 int abort = FALSE;
1736 term_T *term;
1737 typval_T tv;
1738
1739 for (term = first_term; term != NULL; term = term->tl_next)
1740 if (term->tl_job != NULL)
1741 {
1742 tv.v_type = VAR_JOB;
1743 tv.vval.v_job = term->tl_job;
1744 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1745 }
1746 return abort;
1747}
1748
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001749/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001750 * Get the buffer from the first argument in "argvars".
1751 * Returns NULL when the buffer is not for a terminal window.
1752 */
1753 static buf_T *
1754term_get_buf(typval_T *argvars)
1755{
1756 buf_T *buf;
1757
1758 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1759 ++emsg_off;
1760 buf = get_buf_tv(&argvars[0], FALSE);
1761 --emsg_off;
1762 if (buf == NULL || buf->b_term == NULL)
1763 return NULL;
1764 return buf;
1765}
1766
1767/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001768 * "term_getattr(attr, name)" function
1769 */
1770 void
1771f_term_getattr(typval_T *argvars, typval_T *rettv)
1772{
1773 int attr;
1774 size_t i;
1775 char_u *name;
1776
1777 static struct {
1778 char *name;
1779 int attr;
1780 } attrs[] = {
1781 {"bold", HL_BOLD},
1782 {"italic", HL_ITALIC},
1783 {"underline", HL_UNDERLINE},
1784 {"strike", HL_STANDOUT},
1785 {"reverse", HL_INVERSE},
1786 };
1787
1788 attr = get_tv_number(&argvars[0]);
1789 name = get_tv_string_chk(&argvars[1]);
1790 if (name == NULL)
1791 return;
1792
1793 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1794 if (STRCMP(name, attrs[i].name) == 0)
1795 {
1796 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1797 break;
1798 }
1799}
1800
1801/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001802 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001803 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001804 void
1805f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001806{
Bram Moolenaar97870002017-07-30 18:28:38 +02001807 buf_T *buf = term_get_buf(argvars);
1808 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001809
Bram Moolenaar97870002017-07-30 18:28:38 +02001810 if (rettv_list_alloc(rettv) == FAIL)
1811 return;
1812 if (buf == NULL)
1813 return;
1814
1815 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001816 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1817 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001818 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001819}
1820
1821/*
1822 * "term_getjob(buf)" function
1823 */
1824 void
1825f_term_getjob(typval_T *argvars, typval_T *rettv)
1826{
1827 buf_T *buf = term_get_buf(argvars);
1828
1829 rettv->v_type = VAR_JOB;
1830 rettv->vval.v_job = NULL;
1831 if (buf == NULL)
1832 return;
1833
1834 rettv->vval.v_job = buf->b_term->tl_job;
1835 if (rettv->vval.v_job != NULL)
1836 ++rettv->vval.v_job->jv_refcount;
1837}
1838
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001839 static int
1840get_row_number(typval_T *tv, term_T *term)
1841{
1842 if (tv->v_type == VAR_STRING
1843 && tv->vval.v_string != NULL
1844 && STRCMP(tv->vval.v_string, ".") == 0)
1845 return term->tl_cursor_pos.row;
1846 return (int)get_tv_number(tv) - 1;
1847}
1848
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001849/*
1850 * "term_getline(buf, row)" function
1851 */
1852 void
1853f_term_getline(typval_T *argvars, typval_T *rettv)
1854{
1855 buf_T *buf = term_get_buf(argvars);
1856 term_T *term;
1857 int row;
1858
1859 rettv->v_type = VAR_STRING;
1860 if (buf == NULL)
1861 return;
1862 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001863 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001864
1865 if (term->tl_vterm == NULL)
1866 {
1867 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1868
1869 /* vterm is finished, get the text from the buffer */
1870 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1871 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1872 }
1873 else
1874 {
1875 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1876 VTermRect rect;
1877 int len;
1878 char_u *p;
1879
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001880 if (row < 0 || row >= term->tl_rows)
1881 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001882 len = term->tl_cols * MB_MAXBYTES + 1;
1883 p = alloc(len);
1884 if (p == NULL)
1885 return;
1886 rettv->vval.v_string = p;
1887
1888 rect.start_col = 0;
1889 rect.end_col = term->tl_cols;
1890 rect.start_row = row;
1891 rect.end_row = row + 1;
1892 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1893 }
1894}
1895
1896/*
1897 * "term_getsize(buf)" function
1898 */
1899 void
1900f_term_getsize(typval_T *argvars, typval_T *rettv)
1901{
1902 buf_T *buf = term_get_buf(argvars);
1903 list_T *l;
1904
1905 if (rettv_list_alloc(rettv) == FAIL)
1906 return;
1907 if (buf == NULL)
1908 return;
1909
1910 l = rettv->vval.v_list;
1911 list_append_number(l, buf->b_term->tl_rows);
1912 list_append_number(l, buf->b_term->tl_cols);
1913}
1914
1915/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001916 * "term_getstatus(buf)" function
1917 */
1918 void
1919f_term_getstatus(typval_T *argvars, typval_T *rettv)
1920{
1921 buf_T *buf = term_get_buf(argvars);
1922 term_T *term;
1923 char_u val[100];
1924
1925 rettv->v_type = VAR_STRING;
1926 if (buf == NULL)
1927 return;
1928 term = buf->b_term;
1929
1930 if (term_job_running(term))
1931 STRCPY(val, "running");
1932 else
1933 STRCPY(val, "finished");
1934 if (term->tl_terminal_mode)
1935 STRCAT(val, ",terminal");
1936 rettv->vval.v_string = vim_strsave(val);
1937}
1938
1939/*
1940 * "term_gettitle(buf)" function
1941 */
1942 void
1943f_term_gettitle(typval_T *argvars, typval_T *rettv)
1944{
1945 buf_T *buf = term_get_buf(argvars);
1946
1947 rettv->v_type = VAR_STRING;
1948 if (buf == NULL)
1949 return;
1950
1951 if (buf->b_term->tl_title != NULL)
1952 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1953}
1954
1955/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001956 * "term_gettty(buf)" function
1957 */
1958 void
1959f_term_gettty(typval_T *argvars, typval_T *rettv)
1960{
1961 buf_T *buf = term_get_buf(argvars);
1962 char_u *p;
1963
1964 rettv->v_type = VAR_STRING;
1965 if (buf == NULL)
1966 return;
1967 if (buf->b_term->tl_job != NULL)
1968 p = buf->b_term->tl_job->jv_tty_name;
1969 else
1970 p = buf->b_term->tl_tty_name;
1971 if (p != NULL)
1972 rettv->vval.v_string = vim_strsave(p);
1973}
1974
1975/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001976 * "term_list()" function
1977 */
1978 void
1979f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1980{
1981 term_T *tp;
1982 list_T *l;
1983
1984 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1985 return;
1986
1987 l = rettv->vval.v_list;
1988 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1989 if (tp != NULL && tp->tl_buffer != NULL)
1990 if (list_append_number(l,
1991 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1992 return;
1993}
1994
1995/*
1996 * "term_scrape(buf, row)" function
1997 */
1998 void
1999f_term_scrape(typval_T *argvars, typval_T *rettv)
2000{
2001 buf_T *buf = term_get_buf(argvars);
2002 VTermScreen *screen = NULL;
2003 VTermPos pos;
2004 list_T *l;
2005 term_T *term;
2006
2007 if (rettv_list_alloc(rettv) == FAIL)
2008 return;
2009 if (buf == NULL)
2010 return;
2011 term = buf->b_term;
2012 if (term->tl_vterm != NULL)
2013 screen = vterm_obtain_screen(term->tl_vterm);
2014
2015 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002016 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002017 for (pos.col = 0; pos.col < term->tl_cols; )
2018 {
2019 dict_T *dcell;
2020 VTermScreenCell cell;
2021 char_u rgb[8];
2022 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2023 int off = 0;
2024 int i;
2025
2026 if (screen == NULL)
2027 {
2028 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2029 sb_line_T *line;
2030
2031 /* vterm has finished, get the cell from scrollback */
2032 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2033 break;
2034 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2035 if (pos.col >= line->sb_cols)
2036 break;
2037 cell = line->sb_cells[pos.col];
2038 }
2039 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2040 break;
2041 dcell = dict_alloc();
2042 list_append_dict(l, dcell);
2043
2044 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2045 {
2046 if (cell.chars[i] == 0)
2047 break;
2048 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2049 }
2050 mbs[off] = NUL;
2051 dict_add_nr_str(dcell, "chars", 0, mbs);
2052
2053 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2054 cell.fg.red, cell.fg.green, cell.fg.blue);
2055 dict_add_nr_str(dcell, "fg", 0, rgb);
2056 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2057 cell.bg.red, cell.bg.green, cell.bg.blue);
2058 dict_add_nr_str(dcell, "bg", 0, rgb);
2059
2060 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2061 dict_add_nr_str(dcell, "width", cell.width, NULL);
2062
2063 ++pos.col;
2064 if (cell.width == 2)
2065 ++pos.col;
2066 }
2067}
2068
2069/*
2070 * "term_sendkeys(buf, keys)" function
2071 */
2072 void
2073f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2074{
2075 buf_T *buf = term_get_buf(argvars);
2076 char_u *msg;
2077 term_T *term;
2078
2079 rettv->v_type = VAR_UNKNOWN;
2080 if (buf == NULL)
2081 return;
2082
2083 msg = get_tv_string_chk(&argvars[1]);
2084 if (msg == NULL)
2085 return;
2086 term = buf->b_term;
2087 if (term->tl_vterm == NULL)
2088 return;
2089
2090 while (*msg != NUL)
2091 {
2092 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2093 msg += MB_PTR2LEN(msg);
2094 }
2095
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002096 if (!term->tl_terminal_mode)
2097 {
2098 /* TODO: only update once in a while. */
2099 update_screen(0);
2100 if (buf == curbuf)
2101 update_cursor(term, TRUE);
2102 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002103}
2104
2105/*
2106 * "term_start(command, options)" function
2107 */
2108 void
2109f_term_start(typval_T *argvars, typval_T *rettv)
2110{
2111 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002112 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002113
2114 if (cmd == NULL)
2115 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002116 init_job_options(&opt);
2117 /* TODO: allow more job options */
2118 if (argvars[1].v_type != VAR_UNKNOWN
2119 && get_job_options(&argvars[1], &opt,
2120 JO_TIMEOUT_ALL + JO_STOPONEXIT
2121 + JO_EXIT_CB + JO_CLOSE_CALLBACK) == FAIL)
2122 return;
2123
2124 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002125
2126 if (curbuf->b_term != NULL)
2127 rettv->vval.v_number = curbuf->b_fnum;
2128}
2129
2130/*
2131 * "term_wait" function
2132 */
2133 void
2134f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2135{
2136 buf_T *buf = term_get_buf(argvars);
2137
2138 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002139 {
2140 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002141 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002142 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002143
2144 /* Get the job status, this will detect a job that finished. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002145 if (buf->b_term->tl_job == NULL
2146 || STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
2147 {
2148 /* The job is dead, keep reading channel I/O until the channel is
2149 * closed. */
2150 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2151 {
2152 mch_char_avail();
2153 parse_queued_messages();
2154 ui_delay(10L, FALSE);
2155 }
2156 mch_char_avail();
2157 parse_queued_messages();
2158 }
2159 else
2160 {
2161 mch_char_avail();
2162 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002163
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002164 /* Wait for 10 msec for any channel I/O. */
2165 /* TODO: use delay from optional argument */
2166 ui_delay(10L, TRUE);
2167 mch_char_avail();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002168
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002169 /* Flushing messages on channels is hopefully sufficient.
2170 * TODO: is there a better way? */
2171 parse_queued_messages();
2172 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002173}
2174
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002175# ifdef WIN3264
2176
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002177/**************************************
2178 * 2. MS-Windows implementation.
2179 */
2180
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002181#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2182#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2183
Bram Moolenaar8a773062017-07-24 22:29:21 +02002184void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002185void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002186void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002187BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2188void (*winpty_config_set_initial_size)(void*, int, int);
2189LPCWSTR (*winpty_conin_name)(void*);
2190LPCWSTR (*winpty_conout_name)(void*);
2191LPCWSTR (*winpty_conerr_name)(void*);
2192void (*winpty_free)(void*);
2193void (*winpty_config_free)(void*);
2194void (*winpty_spawn_config_free)(void*);
2195void (*winpty_error_free)(void*);
2196LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002197BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002198
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002199#define WINPTY_DLL "winpty.dll"
2200
2201static HINSTANCE hWinPtyDLL = NULL;
2202
2203 int
2204dyn_winpty_init(void)
2205{
2206 int i;
2207 static struct
2208 {
2209 char *name;
2210 FARPROC *ptr;
2211 } winpty_entry[] =
2212 {
2213 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2214 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2215 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2216 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2217 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2218 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2219 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2220 {"winpty_free", (FARPROC*)&winpty_free},
2221 {"winpty_open", (FARPROC*)&winpty_open},
2222 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2223 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2224 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2225 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002226 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002227 {NULL, NULL}
2228 };
2229
2230 /* No need to initialize twice. */
2231 if (hWinPtyDLL)
2232 return 1;
2233 /* Load winpty.dll */
2234 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2235 if (!hWinPtyDLL)
2236 {
2237 EMSG2(_(e_loadlib), WINPTY_DLL);
2238 return 0;
2239 }
2240 for (i = 0; winpty_entry[i].name != NULL
2241 && winpty_entry[i].ptr != NULL; ++i)
2242 {
2243 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2244 winpty_entry[i].name)) == NULL)
2245 {
2246 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2247 return 0;
2248 }
2249 }
2250
2251 return 1;
2252}
2253
2254/*
2255 * Create a new terminal of "rows" by "cols" cells.
2256 * Store a reference in "term".
2257 * Return OK or FAIL.
2258 */
2259 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002260term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002261{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002262 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002263 channel_T *channel = NULL;
2264 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002265 DWORD error;
2266 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2267 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002268 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002269
2270 if (!dyn_winpty_init())
2271 return FAIL;
2272
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002273 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002274 if (p == NULL)
2275 return FAIL;
2276
2277 job = job_alloc();
2278 if (job == NULL)
2279 goto failed;
2280
2281 channel = add_channel();
2282 if (channel == NULL)
2283 goto failed;
2284
2285 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2286 if (term->tl_winpty_config == NULL)
2287 goto failed;
2288
2289 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2290 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2291 if (term->tl_winpty == NULL)
2292 goto failed;
2293
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002294 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002295 spawn_config = winpty_spawn_config_new(
2296 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2297 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2298 NULL,
2299 p,
2300 NULL,
2301 NULL,
2302 &winpty_err);
2303 if (spawn_config == NULL)
2304 goto failed;
2305
2306 channel = add_channel();
2307 if (channel == NULL)
2308 goto failed;
2309
2310 job = job_alloc();
2311 if (job == NULL)
2312 goto failed;
2313
2314 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2315 &child_thread_handle, &error, &winpty_err))
2316 goto failed;
2317
2318 channel_set_pipes(channel,
2319 (sock_T) CreateFileW(
2320 winpty_conin_name(term->tl_winpty),
2321 GENERIC_WRITE, 0, NULL,
2322 OPEN_EXISTING, 0, NULL),
2323 (sock_T) CreateFileW(
2324 winpty_conout_name(term->tl_winpty),
2325 GENERIC_READ, 0, NULL,
2326 OPEN_EXISTING, 0, NULL),
2327 (sock_T) CreateFileW(
2328 winpty_conerr_name(term->tl_winpty),
2329 GENERIC_READ, 0, NULL,
2330 OPEN_EXISTING, 0, NULL));
2331
2332 jo = CreateJobObject(NULL, NULL);
2333 if (jo == NULL)
2334 goto failed;
2335
2336 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002337 {
2338 /* Failed, switch the way to terminate process with TerminateProcess. */
2339 CloseHandle(jo);
2340 jo = NULL;
2341 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002342
2343 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002344 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002345
2346 create_vterm(term, rows, cols);
2347
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002348 channel_set_job(channel, job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002349
2350 job->jv_channel = channel;
2351 job->jv_proc_info.hProcess = child_process_handle;
2352 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2353 job->jv_job_object = jo;
2354 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002355 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002356 term->tl_job = job;
2357
2358 return OK;
2359
2360failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002361 if (spawn_config != NULL)
2362 winpty_spawn_config_free(spawn_config);
2363 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002364 if (channel != NULL)
2365 channel_clear(channel);
2366 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002367 {
2368 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002369 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002370 }
2371 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002372 if (jo != NULL)
2373 CloseHandle(jo);
2374 if (term->tl_winpty != NULL)
2375 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002376 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002377 if (term->tl_winpty_config != NULL)
2378 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002379 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002380 if (winpty_err != NULL)
2381 {
2382 char_u *msg = utf16_to_enc(
2383 (short_u *)winpty_error_msg(winpty_err), NULL);
2384
2385 EMSG(msg);
2386 winpty_error_free(winpty_err);
2387 }
2388 return FAIL;
2389}
2390
2391/*
2392 * Free the terminal emulator part of "term".
2393 */
2394 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002395term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002396{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002397 if (term->tl_winpty != NULL)
2398 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002399 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002400 if (term->tl_winpty_config != NULL)
2401 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002402 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002403 if (term->tl_vterm != NULL)
2404 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002405 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002406}
2407
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002408/*
2409 * Request size to terminal.
2410 */
2411 static void
2412term_report_winsize(term_T *term, int rows, int cols)
2413{
2414 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2415}
2416
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002417# else
2418
2419/**************************************
2420 * 3. Unix-like implementation.
2421 */
2422
2423/*
2424 * Create a new terminal of "rows" by "cols" cells.
2425 * Start job for "cmd".
2426 * Store the pointers in "term".
2427 * Return OK or FAIL.
2428 */
2429 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002430term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002431{
2432 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002433
2434 create_vterm(term, rows, cols);
2435
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002436 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002437 argvars[0].v_type = VAR_STRING;
2438 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002439
2440 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002441 if (term->tl_job != NULL)
2442 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002443
Bram Moolenaar61a66052017-07-22 18:39:00 +02002444 return term->tl_job != NULL
2445 && term->tl_job->jv_channel != NULL
2446 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002447}
2448
2449/*
2450 * Free the terminal emulator part of "term".
2451 */
2452 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002453term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002454{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002455 if (term->tl_vterm != NULL)
2456 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002457 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002458}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002459
2460/*
2461 * Request size to terminal.
2462 */
2463 static void
2464term_report_winsize(term_T *term, int rows, int cols)
2465{
2466 /* Use an ioctl() to report the new window size to the job. */
2467 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2468 {
2469 int fd = -1;
2470 int part;
2471
2472 for (part = PART_OUT; part < PART_COUNT; ++part)
2473 {
2474 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2475 if (isatty(fd))
2476 break;
2477 }
2478 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2479 mch_stop_job(term->tl_job, (char_u *)"winch");
2480 }
2481}
2482
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002483# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002484
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002485#endif /* FEAT_TERMINAL */