blob: abdb50b30dfafecd03a681d6e45c0a308c522fd0 [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. */
632 return term->tl_job != NULL
633 && term->tl_job->jv_status == JOB_STARTED
634 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200635}
636
637/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200638 * Add the last line of the scrollback buffer to the buffer in the window.
639 */
640 static void
641add_scrollback_line_to_buffer(term_T *term)
642{
643 linenr_T lnum = term->tl_scrollback.ga_len - 1;
644 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
645 garray_T ga;
646 int c;
647 int col;
648 int i;
649
650 ga_init2(&ga, 1, 100);
651 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
652 {
653 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
654 goto failed;
655 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
656 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
657 (char_u *)ga.ga_data + ga.ga_len);
658 }
659 if (ga_grow(&ga, 1) == FAIL)
660 goto failed;
661 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
662 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
663
664 if (lnum == 0)
665 {
666 /* Delete the empty line that was in the empty buffer. */
667 curbuf = term->tl_buffer;
668 ml_delete(2, FALSE);
669 curbuf = curwin->w_buffer;
670 }
671
672failed:
673 ga_clear(&ga);
674}
675
676/*
677 * Add the current lines of the terminal to scrollback and to the buffer.
678 * Called after the job has ended and when switching to Terminal mode.
679 */
680 static void
681move_terminal_to_buffer(term_T *term)
682{
683 win_T *wp;
684 int len;
685 int lines_skipped = 0;
686 VTermPos pos;
687 VTermScreenCell cell;
688 VTermScreenCell *p;
689 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
690
691 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
692 {
693 len = 0;
694 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
695 if (vterm_screen_get_cell(screen, pos, &cell) != 0
696 && cell.chars[0] != NUL)
697 len = pos.col + 1;
698
699 if (len == 0)
700 ++lines_skipped;
701 else
702 {
703 while (lines_skipped > 0)
704 {
705 /* Line was skipped, add an empty line. */
706 --lines_skipped;
707 if (ga_grow(&term->tl_scrollback, 1) == OK)
708 {
709 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
710 + term->tl_scrollback.ga_len;
711
712 line->sb_cols = 0;
713 line->sb_cells = NULL;
714 ++term->tl_scrollback.ga_len;
715
716 add_scrollback_line_to_buffer(term);
717 }
718 }
719
720 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
721 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
722 {
723 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
724 + term->tl_scrollback.ga_len;
725
726 for (pos.col = 0; pos.col < len; ++pos.col)
727 {
728 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
729 vim_memset(p + pos.col, 0, sizeof(cell));
730 else
731 p[pos.col] = cell;
732 }
733 line->sb_cols = len;
734 line->sb_cells = p;
735 ++term->tl_scrollback.ga_len;
736
737 add_scrollback_line_to_buffer(term);
738 }
739 else
740 vim_free(p);
741 }
742 }
743
744 FOR_ALL_WINDOWS(wp)
745 {
746 if (wp->w_buffer == term->tl_buffer)
747 {
748 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
749 wp->w_cursor.col = 0;
750 wp->w_valid = 0;
751 redraw_win_later(wp, NOT_VALID);
752 }
753 }
754}
755
756 static void
757set_terminal_mode(term_T *term, int on)
758{
759 term->tl_terminal_mode = on;
760 vim_free(term->tl_status_text);
761 term->tl_status_text = NULL;
762 if (term->tl_buffer == curbuf)
763 maketitle();
764}
765
766/*
767 * Called after the job if finished and Terminal mode is not active:
768 * Move the vterm contents into the scrollback buffer and free the vterm.
769 */
770 static void
771cleanup_vterm(term_T *term)
772{
773 move_terminal_to_buffer(term);
774 term_free_vterm(term);
775 set_terminal_mode(term, FALSE);
776}
777
778/*
779 * Switch from sending keys to the job to Terminal-Normal mode.
780 * Suspends updating the terminal window.
781 */
782 static void
783term_enter_terminal_mode()
784{
785 term_T *term = curbuf->b_term;
786
787 /* Append the current terminal contents to the buffer. */
788 move_terminal_to_buffer(term);
789
790 set_terminal_mode(term, TRUE);
791}
792
793/*
794 * Returns TRUE if the current window contains a terminal and we are in
795 * Terminal-Normal mode.
796 */
797 int
798term_in_terminal_mode()
799{
800 term_T *term = curbuf->b_term;
801
802 return term != NULL && term->tl_terminal_mode;
803}
804
805/*
806 * Switch from Terminal-Normal mode to sending keys to the job.
807 * Restores updating the terminal window.
808 */
809 void
810term_leave_terminal_mode()
811{
812 term_T *term = curbuf->b_term;
813 sb_line_T *line;
814 garray_T *gap;
815
816 /* Remove the terminal contents from the scrollback and the buffer. */
817 gap = &term->tl_scrollback;
818 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
819 {
820 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
821 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
822 vim_free(line->sb_cells);
823 --gap->ga_len;
824 if (gap->ga_len == 0)
825 break;
826 }
827 check_cursor();
828
829 set_terminal_mode(term, FALSE);
830
831 if (term->tl_channel_closed)
832 cleanup_vterm(term);
833 redraw_buf_and_status_later(curbuf, NOT_VALID);
834}
835
836/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200837 * Get a key from the user without mapping.
838 * TODO: use terminal mode mappings.
839 */
840 static int
841term_vgetc()
842{
843 int c;
844
845 ++no_mapping;
846 ++allow_keys;
847 got_int = FALSE;
848 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200849 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200850 --no_mapping;
851 --allow_keys;
852 return c;
853}
854
855/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200856 * Send keys to terminal.
857 */
858 static int
859send_keys_to_term(term_T *term, int c, int typed)
860{
861 char msg[KEY_BUF_LEN];
862 size_t len;
863 static int mouse_was_outside = FALSE;
864 int dragging_outside = FALSE;
865
866 /* Catch keys that need to be handled as in Normal mode. */
867 switch (c)
868 {
869 case NUL:
870 case K_ZERO:
871 if (typed)
872 stuffcharReadbuff(c);
873 return FAIL;
874
875 case K_IGNORE:
876 return FAIL;
877
878 case K_LEFTDRAG:
879 case K_MIDDLEDRAG:
880 case K_RIGHTDRAG:
881 case K_X1DRAG:
882 case K_X2DRAG:
883 dragging_outside = mouse_was_outside;
884 /* FALLTHROUGH */
885 case K_LEFTMOUSE:
886 case K_LEFTMOUSE_NM:
887 case K_LEFTRELEASE:
888 case K_LEFTRELEASE_NM:
889 case K_MIDDLEMOUSE:
890 case K_MIDDLERELEASE:
891 case K_RIGHTMOUSE:
892 case K_RIGHTRELEASE:
893 case K_X1MOUSE:
894 case K_X1RELEASE:
895 case K_X2MOUSE:
896 case K_X2RELEASE:
897 if (mouse_row < W_WINROW(curwin)
898 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
899 || mouse_col < W_WINCOL(curwin)
900 || mouse_col >= W_ENDCOL(curwin)
901 || dragging_outside)
902 {
903 /* click outside the current window */
904 if (typed)
905 {
906 stuffcharReadbuff(c);
907 mouse_was_outside = TRUE;
908 }
909 return FAIL;
910 }
911 }
912 if (typed)
913 mouse_was_outside = FALSE;
914
915 /* Convert the typed key to a sequence of bytes for the job. */
916 len = term_convert_key(term, c, msg);
917 if (len > 0)
918 /* TODO: if FAIL is returned, stop? */
919 channel_send(term->tl_job->jv_channel, PART_IN,
920 (char_u *)msg, (int)len, NULL);
921
922 return OK;
923}
924
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200925 static void
926position_cursor(win_T *wp, VTermPos *pos)
927{
928 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
929 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
930 wp->w_valid |= (VALID_WCOL|VALID_WROW);
931}
932
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200933/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200934 * Handle CTRL-W "": send register contents to the job.
935 */
936 static void
937term_paste_register(int prev_c UNUSED)
938{
939 int c;
940 list_T *l;
941 listitem_T *item;
942 long reglen = 0;
943 int type;
944
945#ifdef FEAT_CMDL_INFO
946 if (add_to_showcmd(prev_c))
947 if (add_to_showcmd('"'))
948 out_flush();
949#endif
950 c = term_vgetc();
951#ifdef FEAT_CMDL_INFO
952 clear_showcmd();
953#endif
954
955 /* CTRL-W "= prompt for expression to evaluate. */
956 if (c == '=' && get_expr_register() != '=')
957 return;
958
959 l = (list_T *)get_reg_contents(c, GREG_LIST);
960 if (l != NULL)
961 {
962 type = get_reg_type(c, &reglen);
963 for (item = l->lv_first; item != NULL; item = item->li_next)
964 {
965 char_u *s = get_tv_string(&item->li_tv);
966
967 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
968 s, STRLEN(s), NULL);
969 if (item->li_next != NULL || type == MLINE)
970 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
971 (char_u *)"\r", 1, NULL);
972 }
973 list_free(l);
974 }
975}
976
977/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200978 * Returns TRUE if the current window contains a terminal and we are sending
979 * keys to the job.
980 */
981 int
982term_use_loop()
983{
984 term_T *term = curbuf->b_term;
985
986 return term != NULL
987 && !term->tl_terminal_mode
988 && term->tl_vterm != NULL
989 && term_job_running(term);
990}
991
992/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200993 * Wait for input and send it to the job.
994 * Return when the start of a CTRL-W command is typed or anything else that
995 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200996 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
997 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200998 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200999 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001000terminal_loop(void)
1001{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001002 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001003 int termkey = 0;
1004
1005 if (*curwin->w_p_tk != NUL)
1006 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001007 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001008
1009 for (;;)
1010 {
1011 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001012 /* Repeat redrawing in case a message is received while redrawing. */
1013 while (curwin->w_redr_type != 0)
1014 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001015 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001016
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001017 c = term_vgetc();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001018 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001019 /* job finished while waiting for a character */
1020 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001021
Bram Moolenaarfae42832017-08-01 22:24:26 +02001022#ifdef UNIX
1023 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1024#endif
1025#ifdef WIN3264
1026 if (c == Ctrl_C)
1027 /* We don't know if the job can handle CTRL-C itself or not, this
1028 * may kill the shell instead of killing the command running in the
1029 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001030 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001031#endif
1032
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001033 if (c == (termkey == 0 ? Ctrl_W : termkey))
1034 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001035 int prev_c = c;
1036
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001037#ifdef FEAT_CMDL_INFO
1038 if (add_to_showcmd(c))
1039 out_flush();
1040#endif
1041 c = term_vgetc();
1042#ifdef FEAT_CMDL_INFO
1043 clear_showcmd();
1044#endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001045 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001046 /* job finished while waiting for a character */
1047 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001048
1049 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001050 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001051 /* "CTRL-W .": send CTRL-W to the job */
1052 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001053 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001054 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001055 {
1056 term_enter_terminal_mode();
1057 return FAIL;
1058 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001059 else if (c == '"')
1060 {
1061 term_paste_register(prev_c);
1062 continue;
1063 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001064 else if (termkey == 0 || c != termkey)
1065 {
1066 stuffcharReadbuff(Ctrl_W);
1067 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001068 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001069 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001070 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001071 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1072 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001073 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001074 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001075}
1076
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001077/*
1078 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001079 * This updates the title and status, but does not close the vter, because
1080 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001081 */
1082 void
1083term_job_ended(job_T *job)
1084{
1085 term_T *term;
1086 int did_one = FALSE;
1087
1088 for (term = first_term; term != NULL; term = term->tl_next)
1089 if (term->tl_job == job)
1090 {
1091 vim_free(term->tl_title);
1092 term->tl_title = NULL;
1093 vim_free(term->tl_status_text);
1094 term->tl_status_text = NULL;
1095 redraw_buf_and_status_later(term->tl_buffer, VALID);
1096 did_one = TRUE;
1097 }
1098 if (did_one)
1099 redraw_statuslines();
1100 if (curbuf->b_term != NULL)
1101 {
1102 if (curbuf->b_term->tl_job == job)
1103 maketitle();
1104 update_cursor(curbuf->b_term, TRUE);
1105 }
1106}
1107
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001108 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001109may_toggle_cursor(term_T *term)
1110{
1111 if (curbuf == term->tl_buffer)
1112 {
1113 if (term->tl_cursor_visible)
1114 cursor_on();
1115 else
1116 cursor_off();
1117 }
1118}
1119
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001120 static int
1121handle_damage(VTermRect rect, void *user)
1122{
1123 term_T *term = (term_T *)user;
1124
1125 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1126 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1127 redraw_buf_later(term->tl_buffer, NOT_VALID);
1128 return 1;
1129}
1130
1131 static int
1132handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1133{
1134 term_T *term = (term_T *)user;
1135
1136 /* TODO */
1137 redraw_buf_later(term->tl_buffer, NOT_VALID);
1138 return 1;
1139}
1140
1141 static int
1142handle_movecursor(
1143 VTermPos pos,
1144 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001145 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001146 void *user)
1147{
1148 term_T *term = (term_T *)user;
1149 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001150
1151 term->tl_cursor_pos = pos;
1152 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001153
1154 FOR_ALL_WINDOWS(wp)
1155 {
1156 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001157 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001158 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001159 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001160 {
1161 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001162 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001163 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001164
1165 return 1;
1166}
1167
Bram Moolenaar21554412017-07-24 21:44:43 +02001168 static int
1169handle_settermprop(
1170 VTermProp prop,
1171 VTermValue *value,
1172 void *user)
1173{
1174 term_T *term = (term_T *)user;
1175
1176 switch (prop)
1177 {
1178 case VTERM_PROP_TITLE:
1179 vim_free(term->tl_title);
1180 term->tl_title = vim_strsave((char_u *)value->string);
1181 vim_free(term->tl_status_text);
1182 term->tl_status_text = NULL;
1183 if (term == curbuf->b_term)
1184 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001185 break;
1186
1187 case VTERM_PROP_CURSORVISIBLE:
1188 term->tl_cursor_visible = value->boolean;
1189 may_toggle_cursor(term);
1190 out_flush();
1191 break;
1192
Bram Moolenaar21554412017-07-24 21:44:43 +02001193 default:
1194 break;
1195 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001196 /* Always return 1, otherwise vterm doesn't store the value internally. */
1197 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001198}
1199
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001200/*
1201 * The job running in the terminal resized the terminal.
1202 */
1203 static int
1204handle_resize(int rows, int cols, void *user)
1205{
1206 term_T *term = (term_T *)user;
1207 win_T *wp;
1208
1209 term->tl_rows = rows;
1210 term->tl_cols = cols;
1211 FOR_ALL_WINDOWS(wp)
1212 {
1213 if (wp->w_buffer == term->tl_buffer)
1214 {
1215 win_setheight_win(rows, wp);
1216 win_setwidth_win(cols, wp);
1217 }
1218 }
1219
1220 redraw_buf_later(term->tl_buffer, NOT_VALID);
1221 return 1;
1222}
1223
Bram Moolenaard85f2712017-07-28 21:51:57 +02001224/*
1225 * Handle a line that is pushed off the top of the screen.
1226 */
1227 static int
1228handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1229{
1230 term_T *term = (term_T *)user;
1231
1232 /* TODO: Limit the number of lines that are stored. */
1233 /* TODO: put the text in the buffer. */
1234 if (ga_grow(&term->tl_scrollback, 1) == OK)
1235 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001236 VTermScreenCell *p = NULL;
1237 int len = 0;
1238 int i;
1239 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001240
1241 /* do not store empty cells at the end */
1242 for (i = 0; i < cols; ++i)
1243 if (cells[i].chars[0] != 0)
1244 len = i + 1;
1245
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001246 if (len > 0)
1247 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001248 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001249 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001250
1251 line = (sb_line_T *)term->tl_scrollback.ga_data
1252 + term->tl_scrollback.ga_len;
1253 line->sb_cols = len;
1254 line->sb_cells = p;
1255 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001256 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001257
1258 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001259 }
1260 return 0; /* ignored */
1261}
1262
Bram Moolenaar21554412017-07-24 21:44:43 +02001263static VTermScreenCallbacks screen_callbacks = {
1264 handle_damage, /* damage */
1265 handle_moverect, /* moverect */
1266 handle_movecursor, /* movecursor */
1267 handle_settermprop, /* settermprop */
1268 NULL, /* bell */
1269 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001270 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001271 NULL /* sb_popline */
1272};
1273
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001274/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001275 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001276 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001277 */
1278 void
1279term_channel_closed(channel_T *ch)
1280{
1281 term_T *term;
1282 int did_one = FALSE;
1283
1284 for (term = first_term; term != NULL; term = term->tl_next)
1285 if (term->tl_job == ch->ch_job)
1286 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001287 term->tl_channel_closed = TRUE;
1288
Bram Moolenaard85f2712017-07-28 21:51:57 +02001289 vim_free(term->tl_title);
1290 term->tl_title = NULL;
1291 vim_free(term->tl_status_text);
1292 term->tl_status_text = NULL;
1293
Bram Moolenaar423802d2017-07-30 16:52:24 +02001294 /* Unless in Terminal-Normal mode: clear the vterm. */
1295 if (!term->tl_terminal_mode)
1296 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001297
1298 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1299 did_one = TRUE;
1300 }
1301 if (did_one)
1302 {
1303 redraw_statuslines();
1304
1305 /* Need to break out of vgetc(). */
1306 ins_char_typebuf(K_IGNORE);
1307
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001308 term = curbuf->b_term;
1309 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001310 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001311 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001312 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001313 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001314 }
1315 }
1316}
1317
1318/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001319 * Reverse engineer the RGB value into a cterm color index.
1320 * First color is 1. Return 0 if no match found.
1321 */
1322 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001323color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001324{
1325 int red = color->red;
1326 int blue = color->blue;
1327 int green = color->green;
1328
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001329 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001330 if (red == 0)
1331 {
1332 if (green == 0)
1333 {
1334 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001335 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001336 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001337 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001338 }
1339 else if (green == 224)
1340 {
1341 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001342 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001343 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001344 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001345 }
1346 }
1347 else if (red == 224)
1348 {
1349 if (green == 0)
1350 {
1351 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001352 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001353 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001354 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001355 }
1356 else if (green == 224)
1357 {
1358 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001359 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001360 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001361 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001362 }
1363 }
1364 else if (red == 128)
1365 {
1366 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001367 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001368 }
1369 else if (red == 255)
1370 {
1371 if (green == 64)
1372 {
1373 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001374 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001375 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001376 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001377 }
1378 else if (green == 255)
1379 {
1380 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001381 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001382 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001383 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001384 }
1385 }
1386 else if (red == 64)
1387 {
1388 if (green == 64)
1389 {
1390 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001391 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001392 }
1393 else if (green == 255)
1394 {
1395 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001396 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001397 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001398 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001399 }
1400 }
1401 if (t_colors >= 256)
1402 {
1403 if (red == blue && red == green)
1404 {
1405 /* 24-color greyscale */
1406 static int cutoff[23] = {
1407 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1408 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1409 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1410 int i;
1411
1412 for (i = 0; i < 23; ++i)
1413 if (red < cutoff[i])
1414 return i + 233;
1415 return 256;
1416 }
1417
1418 /* 216-color cube */
1419 return 17 + ((red + 25) / 0x33) * 36
1420 + ((green + 25) / 0x33) * 6
1421 + (blue + 25) / 0x33;
1422 }
1423 return 0;
1424}
1425
1426/*
1427 * Convert the attributes of a vterm cell into an attribute index.
1428 */
1429 static int
1430cell2attr(VTermScreenCell *cell)
1431{
1432 int attr = 0;
1433
1434 if (cell->attrs.bold)
1435 attr |= HL_BOLD;
1436 if (cell->attrs.underline)
1437 attr |= HL_UNDERLINE;
1438 if (cell->attrs.italic)
1439 attr |= HL_ITALIC;
1440 if (cell->attrs.strike)
1441 attr |= HL_STANDOUT;
1442 if (cell->attrs.reverse)
1443 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001444
1445#ifdef FEAT_GUI
1446 if (gui.in_use)
1447 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001448 guicolor_T fg, bg;
1449
1450 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1451 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1452 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001453 }
1454 else
1455#endif
1456#ifdef FEAT_TERMGUICOLORS
1457 if (p_tgc)
1458 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001459 guicolor_T fg, bg;
1460
1461 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1462 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1463
1464 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001465 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001466 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001467#endif
1468 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001469 int bold = MAYBE;
1470 int fg = color2index(&cell->fg, TRUE, &bold);
1471 int bg = color2index(&cell->bg, FALSE, &bold);
1472
1473 /* with 8 colors set the bold attribute to get a bright foreground */
1474 if (bold == TRUE)
1475 attr |= HL_BOLD;
1476 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001477 }
1478 return 0;
1479}
1480
1481/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001482 * Called to update the window that contains a terminal.
1483 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001484 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001485 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001486term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001487{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001488 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001489 VTerm *vterm;
1490 VTermScreen *screen;
1491 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001492 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001493
Bram Moolenaar423802d2017-07-30 16:52:24 +02001494 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001495 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001496
Bram Moolenaard85f2712017-07-28 21:51:57 +02001497 vterm = term->tl_vterm;
1498 screen = vterm_obtain_screen(vterm);
1499 state = vterm_obtain_state(vterm);
1500
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001501 /*
1502 * If the window was resized a redraw will be triggered and we get here.
1503 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1504 */
1505 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1506 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001507 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001508 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1509 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1510 win_T *twp;
1511
1512 FOR_ALL_WINDOWS(twp)
1513 {
1514 /* When more than one window shows the same terminal, use the
1515 * smallest size. */
1516 if (twp->w_buffer == term->tl_buffer)
1517 {
1518 if (!term->tl_rows_fixed && rows > twp->w_height)
1519 rows = twp->w_height;
1520 if (!term->tl_cols_fixed && cols > twp->w_width)
1521 cols = twp->w_width;
1522 }
1523 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001524
1525 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001526 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001527 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001528 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001529 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001530
1531 /* The cursor may have been moved when resizing. */
1532 vterm_state_get_cursorpos(state, &pos);
1533 position_cursor(wp, &pos);
1534
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001535 /* TODO: Only redraw what changed. */
1536 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001537 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001538 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001539 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001540
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001541 if (pos.row < term->tl_rows)
1542 {
1543 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001544 {
1545 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001546 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001547
Bram Moolenaareeac6772017-07-23 15:48:37 +02001548 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1549 vim_memset(&cell, 0, sizeof(cell));
1550
1551 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001552 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001553 if (c == NUL)
1554 {
1555 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001556#if defined(FEAT_MBYTE)
1557 if (enc_utf8)
1558 ScreenLinesUC[off] = NUL;
1559#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001560 }
1561 else
1562 {
1563#if defined(FEAT_MBYTE)
1564 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001565 {
1566 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001567 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001568 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001569 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001570 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001571 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001572 if (enc_utf8)
1573 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001574 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001575#else
1576 ScreenLines[off] = c;
1577#endif
1578 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001579 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001580
1581 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001582 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001583 if (cell.width == 2)
1584 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001585 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001586#if defined(FEAT_MBYTE)
1587 if (enc_utf8)
1588 ScreenLinesUC[off] = NUL;
1589#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001590 ++pos.col;
1591 ++off;
1592 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001593 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001594 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001595 else
1596 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001597
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001598 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1599 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001600 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001601
1602 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001603}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001604
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001605/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001606 * Return TRUE if "wp" is a terminal window where the job has finished.
1607 */
1608 int
1609term_is_finished(buf_T *buf)
1610{
1611 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1612}
1613
1614/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001615 * Return TRUE if "wp" is a terminal window where the job has finished or we
1616 * are in Terminal-Normal mode.
1617 */
1618 int
1619term_show_buffer(buf_T *buf)
1620{
1621 term_T *term = buf->b_term;
1622
1623 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1624}
1625
1626/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001627 * The current buffer is going to be changed. If there is terminal
1628 * highlighting remove it now.
1629 */
1630 void
1631term_change_in_curbuf(void)
1632{
1633 term_T *term = curbuf->b_term;
1634
1635 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1636 {
1637 free_scrollback(term);
1638 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001639
1640 /* The buffer is now like a normal buffer, it cannot be easily
1641 * abandoned when changed. */
1642 set_string_option_direct((char_u *)"buftype", -1,
1643 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001644 }
1645}
1646
1647/*
1648 * Get the screen attribute for a position in the buffer.
1649 */
1650 int
1651term_get_attr(buf_T *buf, linenr_T lnum, int col)
1652{
1653 term_T *term = buf->b_term;
1654 sb_line_T *line;
1655
Bram Moolenaar70229f92017-07-29 16:01:53 +02001656 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001657 return 0;
1658 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1659 if (col >= line->sb_cols)
1660 return 0;
1661 return cell2attr(line->sb_cells + col);
1662}
1663
1664/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001665 * Create a new vterm and initialize it.
1666 */
1667 static void
1668create_vterm(term_T *term, int rows, int cols)
1669{
1670 VTerm *vterm;
1671 VTermScreen *screen;
1672
1673 vterm = vterm_new(rows, cols);
1674 term->tl_vterm = vterm;
1675 screen = vterm_obtain_screen(vterm);
1676 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1677 /* TODO: depends on 'encoding'. */
1678 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001679
1680 /* Vterm uses a default black background. Set it to white when
1681 * 'background' is "light". */
1682 if (*p_bg == 'l')
1683 {
1684 VTermColor fg, bg;
1685
1686 fg.red = fg.green = fg.blue = 0;
1687 bg.red = bg.green = bg.blue = 255;
1688 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1689 }
1690
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001691 /* Required to initialize most things. */
1692 vterm_screen_reset(screen, 1 /* hard */);
1693}
1694
Bram Moolenaar21554412017-07-24 21:44:43 +02001695/*
1696 * Return the text to show for the buffer name and status.
1697 */
1698 char_u *
1699term_get_status_text(term_T *term)
1700{
1701 if (term->tl_status_text == NULL)
1702 {
1703 char_u *txt;
1704 size_t len;
1705
Bram Moolenaar423802d2017-07-30 16:52:24 +02001706 if (term->tl_terminal_mode)
1707 {
1708 if (term_job_running(term))
1709 txt = (char_u *)_("Terminal");
1710 else
1711 txt = (char_u *)_("Terminal-finished");
1712 }
1713 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001714 txt = term->tl_title;
1715 else if (term_job_running(term))
1716 txt = (char_u *)_("running");
1717 else
1718 txt = (char_u *)_("finished");
1719 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001720 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001721 if (term->tl_status_text != NULL)
1722 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1723 term->tl_buffer->b_fname, txt);
1724 }
1725 return term->tl_status_text;
1726}
1727
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001728/*
1729 * Mark references in jobs of terminals.
1730 */
1731 int
1732set_ref_in_term(int copyID)
1733{
1734 int abort = FALSE;
1735 term_T *term;
1736 typval_T tv;
1737
1738 for (term = first_term; term != NULL; term = term->tl_next)
1739 if (term->tl_job != NULL)
1740 {
1741 tv.v_type = VAR_JOB;
1742 tv.vval.v_job = term->tl_job;
1743 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1744 }
1745 return abort;
1746}
1747
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001748/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001749 * Get the buffer from the first argument in "argvars".
1750 * Returns NULL when the buffer is not for a terminal window.
1751 */
1752 static buf_T *
1753term_get_buf(typval_T *argvars)
1754{
1755 buf_T *buf;
1756
1757 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1758 ++emsg_off;
1759 buf = get_buf_tv(&argvars[0], FALSE);
1760 --emsg_off;
1761 if (buf == NULL || buf->b_term == NULL)
1762 return NULL;
1763 return buf;
1764}
1765
1766/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001767 * "term_getattr(attr, name)" function
1768 */
1769 void
1770f_term_getattr(typval_T *argvars, typval_T *rettv)
1771{
1772 int attr;
1773 size_t i;
1774 char_u *name;
1775
1776 static struct {
1777 char *name;
1778 int attr;
1779 } attrs[] = {
1780 {"bold", HL_BOLD},
1781 {"italic", HL_ITALIC},
1782 {"underline", HL_UNDERLINE},
1783 {"strike", HL_STANDOUT},
1784 {"reverse", HL_INVERSE},
1785 };
1786
1787 attr = get_tv_number(&argvars[0]);
1788 name = get_tv_string_chk(&argvars[1]);
1789 if (name == NULL)
1790 return;
1791
1792 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1793 if (STRCMP(name, attrs[i].name) == 0)
1794 {
1795 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1796 break;
1797 }
1798}
1799
1800/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001801 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001802 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001803 void
1804f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001805{
Bram Moolenaar97870002017-07-30 18:28:38 +02001806 buf_T *buf = term_get_buf(argvars);
1807 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001808
Bram Moolenaar97870002017-07-30 18:28:38 +02001809 if (rettv_list_alloc(rettv) == FAIL)
1810 return;
1811 if (buf == NULL)
1812 return;
1813
1814 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001815 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1816 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001817 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001818}
1819
1820/*
1821 * "term_getjob(buf)" function
1822 */
1823 void
1824f_term_getjob(typval_T *argvars, typval_T *rettv)
1825{
1826 buf_T *buf = term_get_buf(argvars);
1827
1828 rettv->v_type = VAR_JOB;
1829 rettv->vval.v_job = NULL;
1830 if (buf == NULL)
1831 return;
1832
1833 rettv->vval.v_job = buf->b_term->tl_job;
1834 if (rettv->vval.v_job != NULL)
1835 ++rettv->vval.v_job->jv_refcount;
1836}
1837
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001838 static int
1839get_row_number(typval_T *tv, term_T *term)
1840{
1841 if (tv->v_type == VAR_STRING
1842 && tv->vval.v_string != NULL
1843 && STRCMP(tv->vval.v_string, ".") == 0)
1844 return term->tl_cursor_pos.row;
1845 return (int)get_tv_number(tv) - 1;
1846}
1847
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001848/*
1849 * "term_getline(buf, row)" function
1850 */
1851 void
1852f_term_getline(typval_T *argvars, typval_T *rettv)
1853{
1854 buf_T *buf = term_get_buf(argvars);
1855 term_T *term;
1856 int row;
1857
1858 rettv->v_type = VAR_STRING;
1859 if (buf == NULL)
1860 return;
1861 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001862 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001863
1864 if (term->tl_vterm == NULL)
1865 {
1866 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1867
1868 /* vterm is finished, get the text from the buffer */
1869 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1870 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1871 }
1872 else
1873 {
1874 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1875 VTermRect rect;
1876 int len;
1877 char_u *p;
1878
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001879 if (row < 0 || row >= term->tl_rows)
1880 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001881 len = term->tl_cols * MB_MAXBYTES + 1;
1882 p = alloc(len);
1883 if (p == NULL)
1884 return;
1885 rettv->vval.v_string = p;
1886
1887 rect.start_col = 0;
1888 rect.end_col = term->tl_cols;
1889 rect.start_row = row;
1890 rect.end_row = row + 1;
1891 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1892 }
1893}
1894
1895/*
1896 * "term_getsize(buf)" function
1897 */
1898 void
1899f_term_getsize(typval_T *argvars, typval_T *rettv)
1900{
1901 buf_T *buf = term_get_buf(argvars);
1902 list_T *l;
1903
1904 if (rettv_list_alloc(rettv) == FAIL)
1905 return;
1906 if (buf == NULL)
1907 return;
1908
1909 l = rettv->vval.v_list;
1910 list_append_number(l, buf->b_term->tl_rows);
1911 list_append_number(l, buf->b_term->tl_cols);
1912}
1913
1914/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001915 * "term_getstatus(buf)" function
1916 */
1917 void
1918f_term_getstatus(typval_T *argvars, typval_T *rettv)
1919{
1920 buf_T *buf = term_get_buf(argvars);
1921 term_T *term;
1922 char_u val[100];
1923
1924 rettv->v_type = VAR_STRING;
1925 if (buf == NULL)
1926 return;
1927 term = buf->b_term;
1928
1929 if (term_job_running(term))
1930 STRCPY(val, "running");
1931 else
1932 STRCPY(val, "finished");
1933 if (term->tl_terminal_mode)
1934 STRCAT(val, ",terminal");
1935 rettv->vval.v_string = vim_strsave(val);
1936}
1937
1938/*
1939 * "term_gettitle(buf)" function
1940 */
1941 void
1942f_term_gettitle(typval_T *argvars, typval_T *rettv)
1943{
1944 buf_T *buf = term_get_buf(argvars);
1945
1946 rettv->v_type = VAR_STRING;
1947 if (buf == NULL)
1948 return;
1949
1950 if (buf->b_term->tl_title != NULL)
1951 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1952}
1953
1954/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001955 * "term_gettty(buf)" function
1956 */
1957 void
1958f_term_gettty(typval_T *argvars, typval_T *rettv)
1959{
1960 buf_T *buf = term_get_buf(argvars);
1961 char_u *p;
1962
1963 rettv->v_type = VAR_STRING;
1964 if (buf == NULL)
1965 return;
1966 if (buf->b_term->tl_job != NULL)
1967 p = buf->b_term->tl_job->jv_tty_name;
1968 else
1969 p = buf->b_term->tl_tty_name;
1970 if (p != NULL)
1971 rettv->vval.v_string = vim_strsave(p);
1972}
1973
1974/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001975 * "term_list()" function
1976 */
1977 void
1978f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1979{
1980 term_T *tp;
1981 list_T *l;
1982
1983 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1984 return;
1985
1986 l = rettv->vval.v_list;
1987 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1988 if (tp != NULL && tp->tl_buffer != NULL)
1989 if (list_append_number(l,
1990 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1991 return;
1992}
1993
1994/*
1995 * "term_scrape(buf, row)" function
1996 */
1997 void
1998f_term_scrape(typval_T *argvars, typval_T *rettv)
1999{
2000 buf_T *buf = term_get_buf(argvars);
2001 VTermScreen *screen = NULL;
2002 VTermPos pos;
2003 list_T *l;
2004 term_T *term;
2005
2006 if (rettv_list_alloc(rettv) == FAIL)
2007 return;
2008 if (buf == NULL)
2009 return;
2010 term = buf->b_term;
2011 if (term->tl_vterm != NULL)
2012 screen = vterm_obtain_screen(term->tl_vterm);
2013
2014 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002015 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002016 for (pos.col = 0; pos.col < term->tl_cols; )
2017 {
2018 dict_T *dcell;
2019 VTermScreenCell cell;
2020 char_u rgb[8];
2021 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2022 int off = 0;
2023 int i;
2024
2025 if (screen == NULL)
2026 {
2027 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2028 sb_line_T *line;
2029
2030 /* vterm has finished, get the cell from scrollback */
2031 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2032 break;
2033 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2034 if (pos.col >= line->sb_cols)
2035 break;
2036 cell = line->sb_cells[pos.col];
2037 }
2038 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2039 break;
2040 dcell = dict_alloc();
2041 list_append_dict(l, dcell);
2042
2043 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2044 {
2045 if (cell.chars[i] == 0)
2046 break;
2047 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2048 }
2049 mbs[off] = NUL;
2050 dict_add_nr_str(dcell, "chars", 0, mbs);
2051
2052 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2053 cell.fg.red, cell.fg.green, cell.fg.blue);
2054 dict_add_nr_str(dcell, "fg", 0, rgb);
2055 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2056 cell.bg.red, cell.bg.green, cell.bg.blue);
2057 dict_add_nr_str(dcell, "bg", 0, rgb);
2058
2059 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2060 dict_add_nr_str(dcell, "width", cell.width, NULL);
2061
2062 ++pos.col;
2063 if (cell.width == 2)
2064 ++pos.col;
2065 }
2066}
2067
2068/*
2069 * "term_sendkeys(buf, keys)" function
2070 */
2071 void
2072f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2073{
2074 buf_T *buf = term_get_buf(argvars);
2075 char_u *msg;
2076 term_T *term;
2077
2078 rettv->v_type = VAR_UNKNOWN;
2079 if (buf == NULL)
2080 return;
2081
2082 msg = get_tv_string_chk(&argvars[1]);
2083 if (msg == NULL)
2084 return;
2085 term = buf->b_term;
2086 if (term->tl_vterm == NULL)
2087 return;
2088
2089 while (*msg != NUL)
2090 {
2091 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2092 msg += MB_PTR2LEN(msg);
2093 }
2094
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002095 if (!term->tl_terminal_mode)
2096 {
2097 /* TODO: only update once in a while. */
2098 update_screen(0);
2099 if (buf == curbuf)
2100 update_cursor(term, TRUE);
2101 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002102}
2103
2104/*
2105 * "term_start(command, options)" function
2106 */
2107 void
2108f_term_start(typval_T *argvars, typval_T *rettv)
2109{
2110 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002111 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002112
2113 if (cmd == NULL)
2114 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002115 init_job_options(&opt);
2116 /* TODO: allow more job options */
2117 if (argvars[1].v_type != VAR_UNKNOWN
2118 && get_job_options(&argvars[1], &opt,
2119 JO_TIMEOUT_ALL + JO_STOPONEXIT
2120 + JO_EXIT_CB + JO_CLOSE_CALLBACK) == FAIL)
2121 return;
2122
2123 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002124
2125 if (curbuf->b_term != NULL)
2126 rettv->vval.v_number = curbuf->b_fnum;
2127}
2128
2129/*
2130 * "term_wait" function
2131 */
2132 void
2133f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2134{
2135 buf_T *buf = term_get_buf(argvars);
2136
2137 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002138 {
2139 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002140 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002141 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002142
2143 /* Get the job status, this will detect a job that finished. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002144 if (buf->b_term->tl_job == NULL
2145 || STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
2146 {
2147 /* The job is dead, keep reading channel I/O until the channel is
2148 * closed. */
2149 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2150 {
2151 mch_char_avail();
2152 parse_queued_messages();
2153 ui_delay(10L, FALSE);
2154 }
2155 mch_char_avail();
2156 parse_queued_messages();
2157 }
2158 else
2159 {
2160 mch_char_avail();
2161 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002162
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002163 /* Wait for 10 msec for any channel I/O. */
2164 /* TODO: use delay from optional argument */
2165 ui_delay(10L, TRUE);
2166 mch_char_avail();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002167
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002168 /* Flushing messages on channels is hopefully sufficient.
2169 * TODO: is there a better way? */
2170 parse_queued_messages();
2171 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002172}
2173
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002174# ifdef WIN3264
2175
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002176/**************************************
2177 * 2. MS-Windows implementation.
2178 */
2179
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002180#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2181#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2182
Bram Moolenaar8a773062017-07-24 22:29:21 +02002183void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002184void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002185void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002186BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2187void (*winpty_config_set_initial_size)(void*, int, int);
2188LPCWSTR (*winpty_conin_name)(void*);
2189LPCWSTR (*winpty_conout_name)(void*);
2190LPCWSTR (*winpty_conerr_name)(void*);
2191void (*winpty_free)(void*);
2192void (*winpty_config_free)(void*);
2193void (*winpty_spawn_config_free)(void*);
2194void (*winpty_error_free)(void*);
2195LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002196BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002197
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002198#define WINPTY_DLL "winpty.dll"
2199
2200static HINSTANCE hWinPtyDLL = NULL;
2201
2202 int
2203dyn_winpty_init(void)
2204{
2205 int i;
2206 static struct
2207 {
2208 char *name;
2209 FARPROC *ptr;
2210 } winpty_entry[] =
2211 {
2212 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2213 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2214 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2215 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2216 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2217 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2218 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2219 {"winpty_free", (FARPROC*)&winpty_free},
2220 {"winpty_open", (FARPROC*)&winpty_open},
2221 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2222 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2223 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2224 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002225 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002226 {NULL, NULL}
2227 };
2228
2229 /* No need to initialize twice. */
2230 if (hWinPtyDLL)
2231 return 1;
2232 /* Load winpty.dll */
2233 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2234 if (!hWinPtyDLL)
2235 {
2236 EMSG2(_(e_loadlib), WINPTY_DLL);
2237 return 0;
2238 }
2239 for (i = 0; winpty_entry[i].name != NULL
2240 && winpty_entry[i].ptr != NULL; ++i)
2241 {
2242 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2243 winpty_entry[i].name)) == NULL)
2244 {
2245 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2246 return 0;
2247 }
2248 }
2249
2250 return 1;
2251}
2252
2253/*
2254 * Create a new terminal of "rows" by "cols" cells.
2255 * Store a reference in "term".
2256 * Return OK or FAIL.
2257 */
2258 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002259term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002260{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002261 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002262 channel_T *channel = NULL;
2263 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002264 DWORD error;
2265 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2266 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002267 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002268
2269 if (!dyn_winpty_init())
2270 return FAIL;
2271
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002272 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002273 if (p == NULL)
2274 return FAIL;
2275
2276 job = job_alloc();
2277 if (job == NULL)
2278 goto failed;
2279
2280 channel = add_channel();
2281 if (channel == NULL)
2282 goto failed;
2283
2284 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2285 if (term->tl_winpty_config == NULL)
2286 goto failed;
2287
2288 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2289 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2290 if (term->tl_winpty == NULL)
2291 goto failed;
2292
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002293 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002294 spawn_config = winpty_spawn_config_new(
2295 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2296 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2297 NULL,
2298 p,
2299 NULL,
2300 NULL,
2301 &winpty_err);
2302 if (spawn_config == NULL)
2303 goto failed;
2304
2305 channel = add_channel();
2306 if (channel == NULL)
2307 goto failed;
2308
2309 job = job_alloc();
2310 if (job == NULL)
2311 goto failed;
2312
2313 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2314 &child_thread_handle, &error, &winpty_err))
2315 goto failed;
2316
2317 channel_set_pipes(channel,
2318 (sock_T) CreateFileW(
2319 winpty_conin_name(term->tl_winpty),
2320 GENERIC_WRITE, 0, NULL,
2321 OPEN_EXISTING, 0, NULL),
2322 (sock_T) CreateFileW(
2323 winpty_conout_name(term->tl_winpty),
2324 GENERIC_READ, 0, NULL,
2325 OPEN_EXISTING, 0, NULL),
2326 (sock_T) CreateFileW(
2327 winpty_conerr_name(term->tl_winpty),
2328 GENERIC_READ, 0, NULL,
2329 OPEN_EXISTING, 0, NULL));
2330
2331 jo = CreateJobObject(NULL, NULL);
2332 if (jo == NULL)
2333 goto failed;
2334
2335 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002336 {
2337 /* Failed, switch the way to terminate process with TerminateProcess. */
2338 CloseHandle(jo);
2339 jo = NULL;
2340 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002341
2342 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002343 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002344
2345 create_vterm(term, rows, cols);
2346
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002347 channel_set_job(channel, job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002348
2349 job->jv_channel = channel;
2350 job->jv_proc_info.hProcess = child_process_handle;
2351 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2352 job->jv_job_object = jo;
2353 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002354 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002355 term->tl_job = job;
2356
2357 return OK;
2358
2359failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002360 if (spawn_config != NULL)
2361 winpty_spawn_config_free(spawn_config);
2362 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002363 if (channel != NULL)
2364 channel_clear(channel);
2365 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002366 {
2367 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002368 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002369 }
2370 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002371 if (jo != NULL)
2372 CloseHandle(jo);
2373 if (term->tl_winpty != NULL)
2374 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002375 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002376 if (term->tl_winpty_config != NULL)
2377 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002378 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002379 if (winpty_err != NULL)
2380 {
2381 char_u *msg = utf16_to_enc(
2382 (short_u *)winpty_error_msg(winpty_err), NULL);
2383
2384 EMSG(msg);
2385 winpty_error_free(winpty_err);
2386 }
2387 return FAIL;
2388}
2389
2390/*
2391 * Free the terminal emulator part of "term".
2392 */
2393 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002394term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002395{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002396 if (term->tl_winpty != NULL)
2397 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002398 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002399 if (term->tl_winpty_config != NULL)
2400 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002401 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002402 if (term->tl_vterm != NULL)
2403 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002404 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002405}
2406
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002407/*
2408 * Request size to terminal.
2409 */
2410 static void
2411term_report_winsize(term_T *term, int rows, int cols)
2412{
2413 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2414}
2415
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002416# else
2417
2418/**************************************
2419 * 3. Unix-like implementation.
2420 */
2421
2422/*
2423 * Create a new terminal of "rows" by "cols" cells.
2424 * Start job for "cmd".
2425 * Store the pointers in "term".
2426 * Return OK or FAIL.
2427 */
2428 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002429term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002430{
2431 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002432
2433 create_vterm(term, rows, cols);
2434
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002435 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002436 argvars[0].v_type = VAR_STRING;
2437 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002438
2439 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002440 if (term->tl_job != NULL)
2441 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002442
Bram Moolenaar61a66052017-07-22 18:39:00 +02002443 return term->tl_job != NULL
2444 && term->tl_job->jv_channel != NULL
2445 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002446}
2447
2448/*
2449 * Free the terminal emulator part of "term".
2450 */
2451 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002452term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002453{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002454 if (term->tl_vterm != NULL)
2455 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002456 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002457}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002458
2459/*
2460 * Request size to terminal.
2461 */
2462 static void
2463term_report_winsize(term_T *term, int rows, int cols)
2464{
2465 /* Use an ioctl() to report the new window size to the job. */
2466 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2467 {
2468 int fd = -1;
2469 int part;
2470
2471 for (part = PART_OUT; part < PART_COUNT; ++part)
2472 {
2473 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2474 if (isatty(fd))
2475 break;
2476 }
2477 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2478 mch_stop_job(term->tl_job, (char_u *)"winch");
2479 }
2480}
2481
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002482# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002483
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002484#endif /* FEAT_TERMINAL */