blob: be76161e512ee1e1042c52c63f27083880356622 [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020022 * this library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020035 * When the job ends the text is put in a buffer. Redrawing then happens from
36 * that buffer, attributes come from the scrollback buffer tl_scrollback.
37 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020038 * TODO:
Bram Moolenaar423802d2017-07-30 16:52:24 +020039 * - Problem with statusline (Zyx, Christian)
40 * - Make CTRL-W "" paste register content to the job?
41 * - in bash mouse clicks are inserting characters.
42 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaard85f2712017-07-28 21:51:57 +020043 * - For the scrollback buffer store lines in the buffer, only attributes in
44 * tl_scrollback.
Bram Moolenaar423802d2017-07-30 16:52:24 +020045 * - Add term_status(): "" if not a terminal, "running" if job running,
46 * "finished" if finished, "running,vim" when job is running and in
47 * Terminal mode, "running,vim,pending" when job output is pending.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020048 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020049 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020050 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
51 * job finishes).
52 * - add option values to the command:
53 * :term <24x80> <close> vim notes.txt
Bram Moolenaard85f2712017-07-28 21:51:57 +020054 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020055 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020056 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020057 * - add 't' to mode()
Bram Moolenaar938783d2017-07-16 20:13:26 +020058 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaard85f2712017-07-28 21:51:57 +020059 * - when closing window and job has ended, make buffer hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020060 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020061 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020062 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020063 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020064 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020065 * - implement "term" for job_start(): more job options when starting a
66 * terminal.
Bram Moolenaar423802d2017-07-30 16:52:24 +020067 * - if the job in the terminal does not support the mouse, we can use the
68 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020069 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
70 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020071 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020072 */
73
74#include "vim.h"
75
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020076#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020077
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020078#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079# define MIN(x,y) (x < y ? x : y)
80# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020081#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020083#include "libvterm/include/vterm.h"
84
Bram Moolenaard85f2712017-07-28 21:51:57 +020085typedef struct sb_line_S {
86 int sb_cols; /* can differ per line */
87 VTermScreenCell *sb_cells; /* allocated */
88} sb_line_T;
89
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090/* typedef term_T in structs.h */
91struct terminal_S {
92 term_T *tl_next;
93
Bram Moolenaar423802d2017-07-30 16:52:24 +020094 VTerm *tl_vterm;
95 job_T *tl_job;
96 buf_T *tl_buffer;
97
98 int tl_terminal_mode;
99 int tl_channel_closed;
100
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200101#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200102 void *tl_winpty_config;
103 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200104#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200105
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200106 /* last known vterm size */
107 int tl_rows;
108 int tl_cols;
109 /* vterm size does not follow window size */
110 int tl_rows_fixed;
111 int tl_cols_fixed;
112
Bram Moolenaar21554412017-07-24 21:44:43 +0200113 char_u *tl_title; /* NULL or allocated */
114 char_u *tl_status_text; /* NULL or allocated */
115
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200116 /* Range of screen rows to update. Zero based. */
117 int tl_dirty_row_start; /* -1 if nothing dirty */
118 int tl_dirty_row_end; /* row below last one to update */
119
Bram Moolenaard85f2712017-07-28 21:51:57 +0200120 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200121 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200122
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200123 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200124 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125};
126
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200127/*
128 * List of all active terminals.
129 */
130static term_T *first_term = NULL;
131
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200132
133#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
134#define KEY_BUF_LEN 200
135
136/*
137 * Functions with separate implementation for MS-Windows and Unix-like systems.
138 */
139static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200140static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200141static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200142
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200143/**************************************
144 * 1. Generic code for all systems.
145 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200146
147/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200148 * Determine the terminal size from 'termsize' and the current window.
149 * Assumes term->tl_rows and term->tl_cols are zero.
150 */
151 static void
152set_term_and_win_size(term_T *term)
153{
154 if (*curwin->w_p_tms != NUL)
155 {
156 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
157
158 term->tl_rows = atoi((char *)curwin->w_p_tms);
159 term->tl_cols = atoi((char *)p);
160 }
161 if (term->tl_rows == 0)
162 term->tl_rows = curwin->w_height;
163 else
164 {
165 win_setheight_win(term->tl_rows, curwin);
166 term->tl_rows_fixed = TRUE;
167 }
168 if (term->tl_cols == 0)
169 term->tl_cols = curwin->w_width;
170 else
171 {
172 win_setwidth_win(term->tl_cols, curwin);
173 term->tl_cols_fixed = TRUE;
174 }
175}
176
177/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200178 * ":terminal": open a terminal window and execute a job in it.
179 */
180 void
181ex_terminal(exarg_T *eap)
182{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200183 exarg_T split_ea;
184 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200185 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200186 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200187
188 if (check_restricted() || check_secure())
189 return;
190
191 term = (term_T *)alloc_clear(sizeof(term_T));
192 if (term == NULL)
193 return;
194 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200195 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200196 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200197
198 /* Open a new window or tab. */
199 vim_memset(&split_ea, 0, sizeof(split_ea));
200 split_ea.cmdidx = CMD_new;
201 split_ea.cmd = (char_u *)"new";
202 split_ea.arg = (char_u *)"";
203 ex_splitview(&split_ea);
204 if (curwin == old_curwin)
205 {
206 /* split failed */
207 vim_free(term);
208 return;
209 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200210 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200211 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200212
213 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200214 term->tl_next = first_term;
215 first_term = term;
216
Bram Moolenaar293424c2017-07-26 23:11:01 +0200217 if (cmd == NULL || *cmd == NUL)
218 cmd = p_sh;
219
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200220 if (buflist_findname(cmd) == NULL)
221 curbuf->b_ffname = vim_strsave(cmd);
222 else
223 {
224 int i;
225 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200226 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200227
228 for (i = 1; p != NULL; ++i)
229 {
230 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
231 if (buflist_findname(p) == NULL)
232 {
233 curbuf->b_ffname = p;
234 break;
235 }
236 }
237 }
238 curbuf->b_fname = curbuf->b_ffname;
239
240 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
241 curbuf->b_changed = TRUE;
242 curbuf->b_p_ma = FALSE;
243 set_string_option_direct((char_u *)"buftype", -1,
244 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200245
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200246 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200248 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200249 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200250 {
251 /* store the size we ended up with */
252 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200253 }
254 else
255 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200256 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200257
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200258 /* Wiping out the buffer will also close the window and call
259 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200260 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200261 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200262
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200263 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264}
265
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200266/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200267 * Free the scrollback buffer for "term".
268 */
269 static void
270free_scrollback(term_T *term)
271{
272 int i;
273
274 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
275 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
276 ga_clear(&term->tl_scrollback);
277}
278
279/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200280 * Free a terminal and everything it refers to.
281 * Kills the job if there is one.
282 * Called when wiping out a buffer.
283 */
284 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200285free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200286{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200287 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200288 term_T *tp;
289
290 if (term == NULL)
291 return;
292 if (first_term == term)
293 first_term = term->tl_next;
294 else
295 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
296 if (tp->tl_next == term)
297 {
298 tp->tl_next = term->tl_next;
299 break;
300 }
301
302 if (term->tl_job != NULL)
303 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200304 if (term->tl_job->jv_status != JOB_ENDED
305 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200306 job_stop(term->tl_job, NULL, "kill");
307 job_unref(term->tl_job);
308 }
309
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200310 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200311
312 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200313 vim_free(term->tl_title);
314 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200315 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200316 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200317}
318
319/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200320 * Write job output "msg[len]" to the vterm.
321 */
322 static void
323term_write_job_output(term_T *term, char_u *msg, size_t len)
324{
325 VTerm *vterm = term->tl_vterm;
326 char_u *p;
327 size_t done;
328 size_t len_now;
329
330 for (done = 0; done < len; done += len_now)
331 {
332 for (p = msg + done; p < msg + len; )
333 {
334 if (*p == NL)
335 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200336 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200337 }
338 len_now = p - msg - done;
339 vterm_input_write(vterm, (char *)msg + done, len_now);
340 if (p < msg + len && *p == NL)
341 {
342 /* Convert NL to CR-NL, that appears to work best. */
343 vterm_input_write(vterm, "\r\n", 2);
344 ++len_now;
345 }
346 }
347
348 /* this invokes the damage callbacks */
349 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
350}
351
Bram Moolenaar1c844932017-07-24 23:36:41 +0200352 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200353update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200354{
Bram Moolenaar1c844932017-07-24 23:36:41 +0200355 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200356 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200357 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200358 if (term->tl_cursor_visible)
359 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200360 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200361#ifdef FEAT_GUI
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200362 if (gui.in_use && term->tl_cursor_visible)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200363 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200364#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200365 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200366}
367
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200368/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200369 * Invoked when "msg" output from a job was received. Write it to the terminal
370 * of "buffer".
371 */
372 void
373write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
374{
375 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200376 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200377
Bram Moolenaard85f2712017-07-28 21:51:57 +0200378 if (term->tl_vterm == NULL)
379 {
380 ch_logn(channel, "NOT writing %d bytes to terminal", (int)len);
381 return;
382 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200383 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200384 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200385
386 /* TODO: only update once in a while. */
387 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200388 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200389}
390
391/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200392 * Send a mouse position and click to the vterm
393 */
394 static int
395term_send_mouse(VTerm *vterm, int button, int pressed)
396{
397 VTermModifier mod = VTERM_MOD_NONE;
398
399 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
400 mouse_col - W_WINCOL(curwin), mod);
401 vterm_mouse_button(vterm, button, pressed, mod);
402 return TRUE;
403}
404
405/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200406 * Convert typed key "c" into bytes to send to the job.
407 * Return the number of bytes in "buf".
408 */
409 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200410term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200411{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200412 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200413 VTermKey key = VTERM_KEY_NONE;
414 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200415 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200416
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200417 switch (c)
418 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200419 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200420 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200421 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
422 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200423 case K_DEL: key = VTERM_KEY_DEL; break;
424 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200425 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
426 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200427 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200428 case K_S_END: mod = VTERM_MOD_SHIFT;
429 key = VTERM_KEY_END; break;
430 case K_C_END: mod = VTERM_MOD_CTRL;
431 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200432 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
433 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
434 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
435 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
436 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
437 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
438 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
439 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
440 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
441 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
442 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
443 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
444 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200445 case K_S_HOME: mod = VTERM_MOD_SHIFT;
446 key = VTERM_KEY_HOME; break;
447 case K_C_HOME: mod = VTERM_MOD_CTRL;
448 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200449 case K_INS: key = VTERM_KEY_INS; break;
450 case K_K0: key = VTERM_KEY_KP_0; break;
451 case K_K1: key = VTERM_KEY_KP_1; break;
452 case K_K2: key = VTERM_KEY_KP_2; break;
453 case K_K3: key = VTERM_KEY_KP_3; break;
454 case K_K4: key = VTERM_KEY_KP_4; break;
455 case K_K5: key = VTERM_KEY_KP_5; break;
456 case K_K6: key = VTERM_KEY_KP_6; break;
457 case K_K7: key = VTERM_KEY_KP_7; break;
458 case K_K8: key = VTERM_KEY_KP_8; break;
459 case K_K9: key = VTERM_KEY_KP_9; break;
460 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
461 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
462 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
463 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
464 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
465 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
466 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
467 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
468 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
469 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
470 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
471 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
472 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200473 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
474 key = VTERM_KEY_LEFT; break;
475 case K_C_LEFT: mod = VTERM_MOD_CTRL;
476 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200477 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
478 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
479 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200480 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
481 key = VTERM_KEY_RIGHT; break;
482 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
483 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200484 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200485 case K_S_UP: mod = VTERM_MOD_SHIFT;
486 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200487 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200488
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200489 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
490 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
491 case K_MOUSELEFT: /* TODO */ return 0;
492 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200493
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200494 case K_LEFTMOUSE:
495 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
496 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
497 case K_LEFTRELEASE:
498 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
499 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
500 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
501 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
502 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
503 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
504 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
505 case K_X1MOUSE: /* TODO */ return 0;
506 case K_X1DRAG: /* TODO */ return 0;
507 case K_X1RELEASE: /* TODO */ return 0;
508 case K_X2MOUSE: /* TODO */ return 0;
509 case K_X2DRAG: /* TODO */ return 0;
510 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200511
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200512 case K_IGNORE: return 0;
513 case K_NOP: return 0;
514 case K_UNDO: return 0;
515 case K_HELP: return 0;
516 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
517 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
518 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
519 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
520 case K_SELECT: return 0;
521#ifdef FEAT_GUI
522 case K_VER_SCROLLBAR: return 0;
523 case K_HOR_SCROLLBAR: return 0;
524#endif
525#ifdef FEAT_GUI_TABLINE
526 case K_TABLINE: return 0;
527 case K_TABMENU: return 0;
528#endif
529#ifdef FEAT_NETBEANS_INTG
530 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
531#endif
532#ifdef FEAT_DND
533 case K_DROP: return 0;
534#endif
535#ifdef FEAT_AUTOCMD
536 case K_CURSORHOLD: return 0;
537#endif
538 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
539 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200540 }
541
542 /*
543 * Convert special keys to vterm keys:
544 * - Write keys to vterm: vterm_keyboard_key()
545 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200546 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200547 */
548 if (key != VTERM_KEY_NONE)
549 /* Special key, let vterm convert it. */
550 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200551 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200552 /* Normal character, let vterm convert it. */
553 vterm_keyboard_unichar(vterm, c, mod);
554
555 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200556 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200557}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200558
Bram Moolenaar938783d2017-07-16 20:13:26 +0200559/*
Bram Moolenaard85f2712017-07-28 21:51:57 +0200560 * Return TRUE if the job for "buf" is still running.
561 */
562 static int
563term_job_running(term_T *term)
564{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200565 /* Also consider the job finished when the channel is closed, to avoid a
566 * race condition when updating the title. */
567 return term->tl_job != NULL
568 && term->tl_job->jv_status == JOB_STARTED
569 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200570}
571
572/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200573 * Add the last line of the scrollback buffer to the buffer in the window.
574 */
575 static void
576add_scrollback_line_to_buffer(term_T *term)
577{
578 linenr_T lnum = term->tl_scrollback.ga_len - 1;
579 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
580 garray_T ga;
581 int c;
582 int col;
583 int i;
584
585 ga_init2(&ga, 1, 100);
586 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
587 {
588 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
589 goto failed;
590 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
591 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
592 (char_u *)ga.ga_data + ga.ga_len);
593 }
594 if (ga_grow(&ga, 1) == FAIL)
595 goto failed;
596 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
597 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
598
599 if (lnum == 0)
600 {
601 /* Delete the empty line that was in the empty buffer. */
602 curbuf = term->tl_buffer;
603 ml_delete(2, FALSE);
604 curbuf = curwin->w_buffer;
605 }
606
607failed:
608 ga_clear(&ga);
609}
610
611/*
612 * Add the current lines of the terminal to scrollback and to the buffer.
613 * Called after the job has ended and when switching to Terminal mode.
614 */
615 static void
616move_terminal_to_buffer(term_T *term)
617{
618 win_T *wp;
619 int len;
620 int lines_skipped = 0;
621 VTermPos pos;
622 VTermScreenCell cell;
623 VTermScreenCell *p;
624 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
625
626 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
627 {
628 len = 0;
629 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
630 if (vterm_screen_get_cell(screen, pos, &cell) != 0
631 && cell.chars[0] != NUL)
632 len = pos.col + 1;
633
634 if (len == 0)
635 ++lines_skipped;
636 else
637 {
638 while (lines_skipped > 0)
639 {
640 /* Line was skipped, add an empty line. */
641 --lines_skipped;
642 if (ga_grow(&term->tl_scrollback, 1) == OK)
643 {
644 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
645 + term->tl_scrollback.ga_len;
646
647 line->sb_cols = 0;
648 line->sb_cells = NULL;
649 ++term->tl_scrollback.ga_len;
650
651 add_scrollback_line_to_buffer(term);
652 }
653 }
654
655 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
656 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
657 {
658 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
659 + term->tl_scrollback.ga_len;
660
661 for (pos.col = 0; pos.col < len; ++pos.col)
662 {
663 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
664 vim_memset(p + pos.col, 0, sizeof(cell));
665 else
666 p[pos.col] = cell;
667 }
668 line->sb_cols = len;
669 line->sb_cells = p;
670 ++term->tl_scrollback.ga_len;
671
672 add_scrollback_line_to_buffer(term);
673 }
674 else
675 vim_free(p);
676 }
677 }
678
679 FOR_ALL_WINDOWS(wp)
680 {
681 if (wp->w_buffer == term->tl_buffer)
682 {
683 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
684 wp->w_cursor.col = 0;
685 wp->w_valid = 0;
686 redraw_win_later(wp, NOT_VALID);
687 }
688 }
689}
690
691 static void
692set_terminal_mode(term_T *term, int on)
693{
694 term->tl_terminal_mode = on;
695 vim_free(term->tl_status_text);
696 term->tl_status_text = NULL;
697 if (term->tl_buffer == curbuf)
698 maketitle();
699}
700
701/*
702 * Called after the job if finished and Terminal mode is not active:
703 * Move the vterm contents into the scrollback buffer and free the vterm.
704 */
705 static void
706cleanup_vterm(term_T *term)
707{
708 move_terminal_to_buffer(term);
709 term_free_vterm(term);
710 set_terminal_mode(term, FALSE);
711}
712
713/*
714 * Switch from sending keys to the job to Terminal-Normal mode.
715 * Suspends updating the terminal window.
716 */
717 static void
718term_enter_terminal_mode()
719{
720 term_T *term = curbuf->b_term;
721
722 /* Append the current terminal contents to the buffer. */
723 move_terminal_to_buffer(term);
724
725 set_terminal_mode(term, TRUE);
726}
727
728/*
729 * Returns TRUE if the current window contains a terminal and we are in
730 * Terminal-Normal mode.
731 */
732 int
733term_in_terminal_mode()
734{
735 term_T *term = curbuf->b_term;
736
737 return term != NULL && term->tl_terminal_mode;
738}
739
740/*
741 * Switch from Terminal-Normal mode to sending keys to the job.
742 * Restores updating the terminal window.
743 */
744 void
745term_leave_terminal_mode()
746{
747 term_T *term = curbuf->b_term;
748 sb_line_T *line;
749 garray_T *gap;
750
751 /* Remove the terminal contents from the scrollback and the buffer. */
752 gap = &term->tl_scrollback;
753 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
754 {
755 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
756 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
757 vim_free(line->sb_cells);
758 --gap->ga_len;
759 if (gap->ga_len == 0)
760 break;
761 }
762 check_cursor();
763
764 set_terminal_mode(term, FALSE);
765
766 if (term->tl_channel_closed)
767 cleanup_vterm(term);
768 redraw_buf_and_status_later(curbuf, NOT_VALID);
769}
770
771/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200772 * Get a key from the user without mapping.
773 * TODO: use terminal mode mappings.
774 */
775 static int
776term_vgetc()
777{
778 int c;
779
780 ++no_mapping;
781 ++allow_keys;
782 got_int = FALSE;
783 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200784 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200785 --no_mapping;
786 --allow_keys;
787 return c;
788}
789
790/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200791 * Send keys to terminal.
792 */
793 static int
794send_keys_to_term(term_T *term, int c, int typed)
795{
796 char msg[KEY_BUF_LEN];
797 size_t len;
798 static int mouse_was_outside = FALSE;
799 int dragging_outside = FALSE;
800
801 /* Catch keys that need to be handled as in Normal mode. */
802 switch (c)
803 {
804 case NUL:
805 case K_ZERO:
806 if (typed)
807 stuffcharReadbuff(c);
808 return FAIL;
809
810 case K_IGNORE:
811 return FAIL;
812
813 case K_LEFTDRAG:
814 case K_MIDDLEDRAG:
815 case K_RIGHTDRAG:
816 case K_X1DRAG:
817 case K_X2DRAG:
818 dragging_outside = mouse_was_outside;
819 /* FALLTHROUGH */
820 case K_LEFTMOUSE:
821 case K_LEFTMOUSE_NM:
822 case K_LEFTRELEASE:
823 case K_LEFTRELEASE_NM:
824 case K_MIDDLEMOUSE:
825 case K_MIDDLERELEASE:
826 case K_RIGHTMOUSE:
827 case K_RIGHTRELEASE:
828 case K_X1MOUSE:
829 case K_X1RELEASE:
830 case K_X2MOUSE:
831 case K_X2RELEASE:
832 if (mouse_row < W_WINROW(curwin)
833 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
834 || mouse_col < W_WINCOL(curwin)
835 || mouse_col >= W_ENDCOL(curwin)
836 || dragging_outside)
837 {
838 /* click outside the current window */
839 if (typed)
840 {
841 stuffcharReadbuff(c);
842 mouse_was_outside = TRUE;
843 }
844 return FAIL;
845 }
846 }
847 if (typed)
848 mouse_was_outside = FALSE;
849
850 /* Convert the typed key to a sequence of bytes for the job. */
851 len = term_convert_key(term, c, msg);
852 if (len > 0)
853 /* TODO: if FAIL is returned, stop? */
854 channel_send(term->tl_job->jv_channel, PART_IN,
855 (char_u *)msg, (int)len, NULL);
856
857 return OK;
858}
859
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200860 static void
861position_cursor(win_T *wp, VTermPos *pos)
862{
863 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
864 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
865 wp->w_valid |= (VALID_WCOL|VALID_WROW);
866}
867
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200868/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200869 * Returns TRUE if the current window contains a terminal and we are sending
870 * keys to the job.
871 */
872 int
873term_use_loop()
874{
875 term_T *term = curbuf->b_term;
876
877 return term != NULL
878 && !term->tl_terminal_mode
879 && term->tl_vterm != NULL
880 && term_job_running(term);
881}
882
883/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200884 * Wait for input and send it to the job.
885 * Return when the start of a CTRL-W command is typed or anything else that
886 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200887 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
888 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200889 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200890 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200891terminal_loop(void)
892{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200893 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200894 int termkey = 0;
895
896 if (*curwin->w_p_tk != NUL)
897 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200898 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200899
900 for (;;)
901 {
902 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200903 /* Repeat redrawing in case a message is received while redrawing. */
904 while (curwin->w_redr_type != 0)
905 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200906 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200907
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200908 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200909 if (curbuf->b_term->tl_vterm == NULL
910 || !term_job_running(curbuf->b_term))
911 /* job finished while waiting for a character */
912 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200913
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200914 if (c == (termkey == 0 ? Ctrl_W : termkey))
915 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200916#ifdef FEAT_CMDL_INFO
917 if (add_to_showcmd(c))
918 out_flush();
919#endif
920 c = term_vgetc();
921#ifdef FEAT_CMDL_INFO
922 clear_showcmd();
923#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200924 if (curbuf->b_term->tl_vterm == NULL
925 || !term_job_running(curbuf->b_term))
926 /* job finished while waiting for a character */
927 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200928
929 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200930 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200931 /* "CTRL-W .": send CTRL-W to the job */
932 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200933 }
934 else if (termkey == 0 && c == 'N')
935 {
936 term_enter_terminal_mode();
937 return FAIL;
938 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200939 else if (termkey == 0 || c != termkey)
940 {
941 stuffcharReadbuff(Ctrl_W);
942 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200943 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200944 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200945 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200946 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
947 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200948 }
Bram Moolenaard85f2712017-07-28 21:51:57 +0200949 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200950}
951
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200952/*
953 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200954 * This updates the title and status, but does not close the vter, because
955 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200956 */
957 void
958term_job_ended(job_T *job)
959{
960 term_T *term;
961 int did_one = FALSE;
962
963 for (term = first_term; term != NULL; term = term->tl_next)
964 if (term->tl_job == job)
965 {
966 vim_free(term->tl_title);
967 term->tl_title = NULL;
968 vim_free(term->tl_status_text);
969 term->tl_status_text = NULL;
970 redraw_buf_and_status_later(term->tl_buffer, VALID);
971 did_one = TRUE;
972 }
973 if (did_one)
974 redraw_statuslines();
975 if (curbuf->b_term != NULL)
976 {
977 if (curbuf->b_term->tl_job == job)
978 maketitle();
979 update_cursor(curbuf->b_term, TRUE);
980 }
981}
982
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200983 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200984may_toggle_cursor(term_T *term)
985{
986 if (curbuf == term->tl_buffer)
987 {
988 if (term->tl_cursor_visible)
989 cursor_on();
990 else
991 cursor_off();
992 }
993}
994
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200995 static int
996handle_damage(VTermRect rect, void *user)
997{
998 term_T *term = (term_T *)user;
999
1000 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1001 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1002 redraw_buf_later(term->tl_buffer, NOT_VALID);
1003 return 1;
1004}
1005
1006 static int
1007handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1008{
1009 term_T *term = (term_T *)user;
1010
1011 /* TODO */
1012 redraw_buf_later(term->tl_buffer, NOT_VALID);
1013 return 1;
1014}
1015
1016 static int
1017handle_movecursor(
1018 VTermPos pos,
1019 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001020 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001021 void *user)
1022{
1023 term_T *term = (term_T *)user;
1024 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001025
1026 term->tl_cursor_pos = pos;
1027 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001028
1029 FOR_ALL_WINDOWS(wp)
1030 {
1031 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001032 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001033 }
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001034 if (term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001035 {
1036 may_toggle_cursor(term);
1037 update_cursor(term, TRUE);
1038 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001039
1040 return 1;
1041}
1042
Bram Moolenaar21554412017-07-24 21:44:43 +02001043 static int
1044handle_settermprop(
1045 VTermProp prop,
1046 VTermValue *value,
1047 void *user)
1048{
1049 term_T *term = (term_T *)user;
1050
1051 switch (prop)
1052 {
1053 case VTERM_PROP_TITLE:
1054 vim_free(term->tl_title);
1055 term->tl_title = vim_strsave((char_u *)value->string);
1056 vim_free(term->tl_status_text);
1057 term->tl_status_text = NULL;
1058 if (term == curbuf->b_term)
1059 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001060 break;
1061
1062 case VTERM_PROP_CURSORVISIBLE:
1063 term->tl_cursor_visible = value->boolean;
1064 may_toggle_cursor(term);
1065 out_flush();
1066 break;
1067
Bram Moolenaar21554412017-07-24 21:44:43 +02001068 default:
1069 break;
1070 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001071 /* Always return 1, otherwise vterm doesn't store the value internally. */
1072 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001073}
1074
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001075/*
1076 * The job running in the terminal resized the terminal.
1077 */
1078 static int
1079handle_resize(int rows, int cols, void *user)
1080{
1081 term_T *term = (term_T *)user;
1082 win_T *wp;
1083
1084 term->tl_rows = rows;
1085 term->tl_cols = cols;
1086 FOR_ALL_WINDOWS(wp)
1087 {
1088 if (wp->w_buffer == term->tl_buffer)
1089 {
1090 win_setheight_win(rows, wp);
1091 win_setwidth_win(cols, wp);
1092 }
1093 }
1094
1095 redraw_buf_later(term->tl_buffer, NOT_VALID);
1096 return 1;
1097}
1098
Bram Moolenaard85f2712017-07-28 21:51:57 +02001099/*
1100 * Handle a line that is pushed off the top of the screen.
1101 */
1102 static int
1103handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1104{
1105 term_T *term = (term_T *)user;
1106
1107 /* TODO: Limit the number of lines that are stored. */
1108 /* TODO: put the text in the buffer. */
1109 if (ga_grow(&term->tl_scrollback, 1) == OK)
1110 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001111 VTermScreenCell *p = NULL;
1112 int len = 0;
1113 int i;
1114 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001115
1116 /* do not store empty cells at the end */
1117 for (i = 0; i < cols; ++i)
1118 if (cells[i].chars[0] != 0)
1119 len = i + 1;
1120
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001121 if (len > 0)
1122 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001123 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001124 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001125
1126 line = (sb_line_T *)term->tl_scrollback.ga_data
1127 + term->tl_scrollback.ga_len;
1128 line->sb_cols = len;
1129 line->sb_cells = p;
1130 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001131 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001132
1133 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001134 }
1135 return 0; /* ignored */
1136}
1137
Bram Moolenaar21554412017-07-24 21:44:43 +02001138static VTermScreenCallbacks screen_callbacks = {
1139 handle_damage, /* damage */
1140 handle_moverect, /* moverect */
1141 handle_movecursor, /* movecursor */
1142 handle_settermprop, /* settermprop */
1143 NULL, /* bell */
1144 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001145 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001146 NULL /* sb_popline */
1147};
1148
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001149/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001150 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001151 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001152 */
1153 void
1154term_channel_closed(channel_T *ch)
1155{
1156 term_T *term;
1157 int did_one = FALSE;
1158
1159 for (term = first_term; term != NULL; term = term->tl_next)
1160 if (term->tl_job == ch->ch_job)
1161 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001162 term->tl_channel_closed = TRUE;
1163
Bram Moolenaard85f2712017-07-28 21:51:57 +02001164 vim_free(term->tl_title);
1165 term->tl_title = NULL;
1166 vim_free(term->tl_status_text);
1167 term->tl_status_text = NULL;
1168
Bram Moolenaar423802d2017-07-30 16:52:24 +02001169 /* Unless in Terminal-Normal mode: clear the vterm. */
1170 if (!term->tl_terminal_mode)
1171 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001172
1173 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1174 did_one = TRUE;
1175 }
1176 if (did_one)
1177 {
1178 redraw_statuslines();
1179
1180 /* Need to break out of vgetc(). */
1181 ins_char_typebuf(K_IGNORE);
1182
1183 if (curbuf->b_term != NULL)
1184 {
1185 if (curbuf->b_term->tl_job == ch->ch_job)
1186 maketitle();
1187 update_cursor(curbuf->b_term, TRUE);
1188 }
1189 }
1190}
1191
1192/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001193 * Reverse engineer the RGB value into a cterm color index.
1194 * First color is 1. Return 0 if no match found.
1195 */
1196 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001197color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001198{
1199 int red = color->red;
1200 int blue = color->blue;
1201 int green = color->green;
1202
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001203 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001204 if (red == 0)
1205 {
1206 if (green == 0)
1207 {
1208 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001209 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001210 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001211 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001212 }
1213 else if (green == 224)
1214 {
1215 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001216 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001217 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001218 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001219 }
1220 }
1221 else if (red == 224)
1222 {
1223 if (green == 0)
1224 {
1225 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001226 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001227 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001228 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001229 }
1230 else if (green == 224)
1231 {
1232 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001233 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001234 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001235 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001236 }
1237 }
1238 else if (red == 128)
1239 {
1240 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001241 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001242 }
1243 else if (red == 255)
1244 {
1245 if (green == 64)
1246 {
1247 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001248 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001249 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001250 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001251 }
1252 else if (green == 255)
1253 {
1254 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001255 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001256 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001257 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001258 }
1259 }
1260 else if (red == 64)
1261 {
1262 if (green == 64)
1263 {
1264 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001265 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001266 }
1267 else if (green == 255)
1268 {
1269 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001270 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001271 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001272 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001273 }
1274 }
1275 if (t_colors >= 256)
1276 {
1277 if (red == blue && red == green)
1278 {
1279 /* 24-color greyscale */
1280 static int cutoff[23] = {
1281 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1282 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1283 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1284 int i;
1285
1286 for (i = 0; i < 23; ++i)
1287 if (red < cutoff[i])
1288 return i + 233;
1289 return 256;
1290 }
1291
1292 /* 216-color cube */
1293 return 17 + ((red + 25) / 0x33) * 36
1294 + ((green + 25) / 0x33) * 6
1295 + (blue + 25) / 0x33;
1296 }
1297 return 0;
1298}
1299
1300/*
1301 * Convert the attributes of a vterm cell into an attribute index.
1302 */
1303 static int
1304cell2attr(VTermScreenCell *cell)
1305{
1306 int attr = 0;
1307
1308 if (cell->attrs.bold)
1309 attr |= HL_BOLD;
1310 if (cell->attrs.underline)
1311 attr |= HL_UNDERLINE;
1312 if (cell->attrs.italic)
1313 attr |= HL_ITALIC;
1314 if (cell->attrs.strike)
1315 attr |= HL_STANDOUT;
1316 if (cell->attrs.reverse)
1317 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001318
1319#ifdef FEAT_GUI
1320 if (gui.in_use)
1321 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001322 guicolor_T fg, bg;
1323
1324 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1325 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1326 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001327 }
1328 else
1329#endif
1330#ifdef FEAT_TERMGUICOLORS
1331 if (p_tgc)
1332 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001333 guicolor_T fg, bg;
1334
1335 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1336 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1337
1338 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001339 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001340 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001341#endif
1342 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001343 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1344 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001345 }
1346 return 0;
1347}
1348
1349/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001350 * Called to update the window that contains a terminal.
1351 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001352 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001353 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001354term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001355{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001356 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001357 VTerm *vterm;
1358 VTermScreen *screen;
1359 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001360 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001361
Bram Moolenaar423802d2017-07-30 16:52:24 +02001362 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001363 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001364
Bram Moolenaard85f2712017-07-28 21:51:57 +02001365 vterm = term->tl_vterm;
1366 screen = vterm_obtain_screen(vterm);
1367 state = vterm_obtain_state(vterm);
1368
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001369 /*
1370 * If the window was resized a redraw will be triggered and we get here.
1371 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1372 */
1373 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1374 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001375 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001376 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1377 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1378 win_T *twp;
1379
1380 FOR_ALL_WINDOWS(twp)
1381 {
1382 /* When more than one window shows the same terminal, use the
1383 * smallest size. */
1384 if (twp->w_buffer == term->tl_buffer)
1385 {
1386 if (!term->tl_rows_fixed && rows > twp->w_height)
1387 rows = twp->w_height;
1388 if (!term->tl_cols_fixed && cols > twp->w_width)
1389 cols = twp->w_width;
1390 }
1391 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001392
1393 vterm_set_size(vterm, rows, cols);
1394 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1395 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001396 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001397 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001398
1399 /* The cursor may have been moved when resizing. */
1400 vterm_state_get_cursorpos(state, &pos);
1401 position_cursor(wp, &pos);
1402
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001403 /* TODO: Only redraw what changed. */
1404 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001405 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001406 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001407 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001408
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001409 if (pos.row < term->tl_rows)
1410 {
1411 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001412 {
1413 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001414 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001415
Bram Moolenaareeac6772017-07-23 15:48:37 +02001416 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1417 vim_memset(&cell, 0, sizeof(cell));
1418
1419 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001420 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001421 if (c == NUL)
1422 {
1423 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001424#if defined(FEAT_MBYTE)
1425 if (enc_utf8)
1426 ScreenLinesUC[off] = NUL;
1427#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001428 }
1429 else
1430 {
1431#if defined(FEAT_MBYTE)
1432 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001433 {
1434 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001435 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001436 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001437 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001438 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001439 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001440 if (enc_utf8)
1441 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001442 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001443#else
1444 ScreenLines[off] = c;
1445#endif
1446 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001447 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001448
1449 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001450 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001451 if (cell.width == 2)
1452 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001453 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001454#if defined(FEAT_MBYTE)
1455 if (enc_utf8)
1456 ScreenLinesUC[off] = NUL;
1457#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001458 ++pos.col;
1459 ++off;
1460 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001461 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001462 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001463 else
1464 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001465
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001466 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1467 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001468 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001469
1470 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001471}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001472
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001473/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001474 * Return TRUE if "wp" is a terminal window where the job has finished.
1475 */
1476 int
1477term_is_finished(buf_T *buf)
1478{
1479 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1480}
1481
1482/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001483 * Return TRUE if "wp" is a terminal window where the job has finished or we
1484 * are in Terminal-Normal mode.
1485 */
1486 int
1487term_show_buffer(buf_T *buf)
1488{
1489 term_T *term = buf->b_term;
1490
1491 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1492}
1493
1494/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001495 * The current buffer is going to be changed. If there is terminal
1496 * highlighting remove it now.
1497 */
1498 void
1499term_change_in_curbuf(void)
1500{
1501 term_T *term = curbuf->b_term;
1502
1503 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1504 {
1505 free_scrollback(term);
1506 redraw_buf_later(term->tl_buffer, NOT_VALID);
1507 }
1508}
1509
1510/*
1511 * Get the screen attribute for a position in the buffer.
1512 */
1513 int
1514term_get_attr(buf_T *buf, linenr_T lnum, int col)
1515{
1516 term_T *term = buf->b_term;
1517 sb_line_T *line;
1518
Bram Moolenaar70229f92017-07-29 16:01:53 +02001519 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001520 return 0;
1521 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1522 if (col >= line->sb_cols)
1523 return 0;
1524 return cell2attr(line->sb_cells + col);
1525}
1526
1527/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001528 * Set job options common for Unix and MS-Windows.
1529 */
1530 static void
1531setup_job_options(jobopt_T *opt, int rows, int cols)
1532{
1533 clear_job_options(opt);
1534 opt->jo_mode = MODE_RAW;
1535 opt->jo_out_mode = MODE_RAW;
1536 opt->jo_err_mode = MODE_RAW;
1537 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001538
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001539 opt->jo_io[PART_OUT] = JIO_BUFFER;
1540 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001541 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1542
1543 opt->jo_modifiable[PART_OUT] = 0;
1544 opt->jo_modifiable[PART_ERR] = 0;
1545 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1546
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001547 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1548 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001549 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001550 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1551
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001552 opt->jo_term_rows = rows;
1553 opt->jo_term_cols = cols;
1554}
1555
1556/*
1557 * Create a new vterm and initialize it.
1558 */
1559 static void
1560create_vterm(term_T *term, int rows, int cols)
1561{
1562 VTerm *vterm;
1563 VTermScreen *screen;
1564
1565 vterm = vterm_new(rows, cols);
1566 term->tl_vterm = vterm;
1567 screen = vterm_obtain_screen(vterm);
1568 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1569 /* TODO: depends on 'encoding'. */
1570 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001571
1572 /* Vterm uses a default black background. Set it to white when
1573 * 'background' is "light". */
1574 if (*p_bg == 'l')
1575 {
1576 VTermColor fg, bg;
1577
1578 fg.red = fg.green = fg.blue = 0;
1579 bg.red = bg.green = bg.blue = 255;
1580 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1581 }
1582
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001583 /* Required to initialize most things. */
1584 vterm_screen_reset(screen, 1 /* hard */);
1585}
1586
Bram Moolenaar21554412017-07-24 21:44:43 +02001587/*
1588 * Return the text to show for the buffer name and status.
1589 */
1590 char_u *
1591term_get_status_text(term_T *term)
1592{
1593 if (term->tl_status_text == NULL)
1594 {
1595 char_u *txt;
1596 size_t len;
1597
Bram Moolenaar423802d2017-07-30 16:52:24 +02001598 if (term->tl_terminal_mode)
1599 {
1600 if (term_job_running(term))
1601 txt = (char_u *)_("Terminal");
1602 else
1603 txt = (char_u *)_("Terminal-finished");
1604 }
1605 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001606 txt = term->tl_title;
1607 else if (term_job_running(term))
1608 txt = (char_u *)_("running");
1609 else
1610 txt = (char_u *)_("finished");
1611 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001612 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001613 if (term->tl_status_text != NULL)
1614 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1615 term->tl_buffer->b_fname, txt);
1616 }
1617 return term->tl_status_text;
1618}
1619
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001620/*
1621 * Mark references in jobs of terminals.
1622 */
1623 int
1624set_ref_in_term(int copyID)
1625{
1626 int abort = FALSE;
1627 term_T *term;
1628 typval_T tv;
1629
1630 for (term = first_term; term != NULL; term = term->tl_next)
1631 if (term->tl_job != NULL)
1632 {
1633 tv.v_type = VAR_JOB;
1634 tv.vval.v_job = term->tl_job;
1635 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1636 }
1637 return abort;
1638}
1639
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001640/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001641 * Get the buffer from the first argument in "argvars".
1642 * Returns NULL when the buffer is not for a terminal window.
1643 */
1644 static buf_T *
1645term_get_buf(typval_T *argvars)
1646{
1647 buf_T *buf;
1648
1649 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1650 ++emsg_off;
1651 buf = get_buf_tv(&argvars[0], FALSE);
1652 --emsg_off;
1653 if (buf == NULL || buf->b_term == NULL)
1654 return NULL;
1655 return buf;
1656}
1657
1658/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001659 * "term_getattr(attr, name)" function
1660 */
1661 void
1662f_term_getattr(typval_T *argvars, typval_T *rettv)
1663{
1664 int attr;
1665 size_t i;
1666 char_u *name;
1667
1668 static struct {
1669 char *name;
1670 int attr;
1671 } attrs[] = {
1672 {"bold", HL_BOLD},
1673 {"italic", HL_ITALIC},
1674 {"underline", HL_UNDERLINE},
1675 {"strike", HL_STANDOUT},
1676 {"reverse", HL_INVERSE},
1677 };
1678
1679 attr = get_tv_number(&argvars[0]);
1680 name = get_tv_string_chk(&argvars[1]);
1681 if (name == NULL)
1682 return;
1683
1684 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1685 if (STRCMP(name, attrs[i].name) == 0)
1686 {
1687 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1688 break;
1689 }
1690}
1691
1692/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001693 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001694 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001695 void
1696f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001697{
Bram Moolenaar97870002017-07-30 18:28:38 +02001698 buf_T *buf = term_get_buf(argvars);
1699 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001700
Bram Moolenaar97870002017-07-30 18:28:38 +02001701 if (rettv_list_alloc(rettv) == FAIL)
1702 return;
1703 if (buf == NULL)
1704 return;
1705
1706 l = rettv->vval.v_list;
1707 list_append_number(l, buf->b_term->tl_cursor_pos.row);
1708 list_append_number(l, buf->b_term->tl_cursor_pos.col);
1709 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001710}
1711
1712/*
1713 * "term_getjob(buf)" function
1714 */
1715 void
1716f_term_getjob(typval_T *argvars, typval_T *rettv)
1717{
1718 buf_T *buf = term_get_buf(argvars);
1719
1720 rettv->v_type = VAR_JOB;
1721 rettv->vval.v_job = NULL;
1722 if (buf == NULL)
1723 return;
1724
1725 rettv->vval.v_job = buf->b_term->tl_job;
1726 if (rettv->vval.v_job != NULL)
1727 ++rettv->vval.v_job->jv_refcount;
1728}
1729
1730/*
1731 * "term_getline(buf, row)" function
1732 */
1733 void
1734f_term_getline(typval_T *argvars, typval_T *rettv)
1735{
1736 buf_T *buf = term_get_buf(argvars);
1737 term_T *term;
1738 int row;
1739
1740 rettv->v_type = VAR_STRING;
1741 if (buf == NULL)
1742 return;
1743 term = buf->b_term;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001744 if (argvars[1].v_type == VAR_UNKNOWN)
1745 row = term->tl_cursor_pos.row;
1746 else
1747 row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001748
1749 if (term->tl_vterm == NULL)
1750 {
1751 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1752
1753 /* vterm is finished, get the text from the buffer */
1754 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1755 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1756 }
1757 else
1758 {
1759 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1760 VTermRect rect;
1761 int len;
1762 char_u *p;
1763
1764 len = term->tl_cols * MB_MAXBYTES + 1;
1765 p = alloc(len);
1766 if (p == NULL)
1767 return;
1768 rettv->vval.v_string = p;
1769
1770 rect.start_col = 0;
1771 rect.end_col = term->tl_cols;
1772 rect.start_row = row;
1773 rect.end_row = row + 1;
1774 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1775 }
1776}
1777
1778/*
1779 * "term_getsize(buf)" function
1780 */
1781 void
1782f_term_getsize(typval_T *argvars, typval_T *rettv)
1783{
1784 buf_T *buf = term_get_buf(argvars);
1785 list_T *l;
1786
1787 if (rettv_list_alloc(rettv) == FAIL)
1788 return;
1789 if (buf == NULL)
1790 return;
1791
1792 l = rettv->vval.v_list;
1793 list_append_number(l, buf->b_term->tl_rows);
1794 list_append_number(l, buf->b_term->tl_cols);
1795}
1796
1797/*
1798 * "term_list()" function
1799 */
1800 void
1801f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1802{
1803 term_T *tp;
1804 list_T *l;
1805
1806 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1807 return;
1808
1809 l = rettv->vval.v_list;
1810 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1811 if (tp != NULL && tp->tl_buffer != NULL)
1812 if (list_append_number(l,
1813 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1814 return;
1815}
1816
1817/*
1818 * "term_scrape(buf, row)" function
1819 */
1820 void
1821f_term_scrape(typval_T *argvars, typval_T *rettv)
1822{
1823 buf_T *buf = term_get_buf(argvars);
1824 VTermScreen *screen = NULL;
1825 VTermPos pos;
1826 list_T *l;
1827 term_T *term;
1828
1829 if (rettv_list_alloc(rettv) == FAIL)
1830 return;
1831 if (buf == NULL)
1832 return;
1833 term = buf->b_term;
1834 if (term->tl_vterm != NULL)
1835 screen = vterm_obtain_screen(term->tl_vterm);
1836
1837 l = rettv->vval.v_list;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001838 if (argvars[1].v_type == VAR_UNKNOWN)
1839 pos.row = term->tl_cursor_pos.row;
1840 else
1841 pos.row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001842 for (pos.col = 0; pos.col < term->tl_cols; )
1843 {
1844 dict_T *dcell;
1845 VTermScreenCell cell;
1846 char_u rgb[8];
1847 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1848 int off = 0;
1849 int i;
1850
1851 if (screen == NULL)
1852 {
1853 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1854 sb_line_T *line;
1855
1856 /* vterm has finished, get the cell from scrollback */
1857 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1858 break;
1859 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1860 if (pos.col >= line->sb_cols)
1861 break;
1862 cell = line->sb_cells[pos.col];
1863 }
1864 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1865 break;
1866 dcell = dict_alloc();
1867 list_append_dict(l, dcell);
1868
1869 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1870 {
1871 if (cell.chars[i] == 0)
1872 break;
1873 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1874 }
1875 mbs[off] = NUL;
1876 dict_add_nr_str(dcell, "chars", 0, mbs);
1877
1878 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1879 cell.fg.red, cell.fg.green, cell.fg.blue);
1880 dict_add_nr_str(dcell, "fg", 0, rgb);
1881 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1882 cell.bg.red, cell.bg.green, cell.bg.blue);
1883 dict_add_nr_str(dcell, "bg", 0, rgb);
1884
1885 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
1886 dict_add_nr_str(dcell, "width", cell.width, NULL);
1887
1888 ++pos.col;
1889 if (cell.width == 2)
1890 ++pos.col;
1891 }
1892}
1893
1894/*
1895 * "term_sendkeys(buf, keys)" function
1896 */
1897 void
1898f_term_sendkeys(typval_T *argvars, typval_T *rettv)
1899{
1900 buf_T *buf = term_get_buf(argvars);
1901 char_u *msg;
1902 term_T *term;
1903
1904 rettv->v_type = VAR_UNKNOWN;
1905 if (buf == NULL)
1906 return;
1907
1908 msg = get_tv_string_chk(&argvars[1]);
1909 if (msg == NULL)
1910 return;
1911 term = buf->b_term;
1912 if (term->tl_vterm == NULL)
1913 return;
1914
1915 while (*msg != NUL)
1916 {
1917 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
1918 msg += MB_PTR2LEN(msg);
1919 }
1920
1921 /* TODO: only update once in a while. */
1922 update_screen(0);
1923 if (buf == curbuf)
1924 update_cursor(term, TRUE);
1925}
1926
1927/*
1928 * "term_start(command, options)" function
1929 */
1930 void
1931f_term_start(typval_T *argvars, typval_T *rettv)
1932{
1933 char_u *cmd = get_tv_string_chk(&argvars[0]);
1934 exarg_T ea;
1935
1936 if (cmd == NULL)
1937 return;
1938 ea.arg = cmd;
1939 ex_terminal(&ea);
1940
1941 if (curbuf->b_term != NULL)
1942 rettv->vval.v_number = curbuf->b_fnum;
1943}
1944
1945/*
1946 * "term_wait" function
1947 */
1948 void
1949f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
1950{
1951 buf_T *buf = term_get_buf(argvars);
1952
1953 if (buf == NULL)
1954 return;
1955
1956 /* Get the job status, this will detect a job that finished. */
1957 if (buf->b_term->tl_job != NULL)
1958 (void)job_status(buf->b_term->tl_job);
1959
1960 /* Check for any pending channel I/O. */
1961 vpeekc_any();
1962 ui_delay(10L, FALSE);
1963
1964 /* Flushing messages on channels is hopefully sufficient.
1965 * TODO: is there a better way? */
1966 parse_queued_messages();
1967}
1968
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001969# ifdef WIN3264
1970
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001971/**************************************
1972 * 2. MS-Windows implementation.
1973 */
1974
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001975#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1976#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1977
Bram Moolenaar8a773062017-07-24 22:29:21 +02001978void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001979void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001980void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001981BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1982void (*winpty_config_set_initial_size)(void*, int, int);
1983LPCWSTR (*winpty_conin_name)(void*);
1984LPCWSTR (*winpty_conout_name)(void*);
1985LPCWSTR (*winpty_conerr_name)(void*);
1986void (*winpty_free)(void*);
1987void (*winpty_config_free)(void*);
1988void (*winpty_spawn_config_free)(void*);
1989void (*winpty_error_free)(void*);
1990LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001991BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001992
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001993#define WINPTY_DLL "winpty.dll"
1994
1995static HINSTANCE hWinPtyDLL = NULL;
1996
1997 int
1998dyn_winpty_init(void)
1999{
2000 int i;
2001 static struct
2002 {
2003 char *name;
2004 FARPROC *ptr;
2005 } winpty_entry[] =
2006 {
2007 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2008 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2009 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2010 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2011 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2012 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2013 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2014 {"winpty_free", (FARPROC*)&winpty_free},
2015 {"winpty_open", (FARPROC*)&winpty_open},
2016 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2017 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2018 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2019 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002020 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002021 {NULL, NULL}
2022 };
2023
2024 /* No need to initialize twice. */
2025 if (hWinPtyDLL)
2026 return 1;
2027 /* Load winpty.dll */
2028 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2029 if (!hWinPtyDLL)
2030 {
2031 EMSG2(_(e_loadlib), WINPTY_DLL);
2032 return 0;
2033 }
2034 for (i = 0; winpty_entry[i].name != NULL
2035 && winpty_entry[i].ptr != NULL; ++i)
2036 {
2037 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2038 winpty_entry[i].name)) == NULL)
2039 {
2040 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2041 return 0;
2042 }
2043 }
2044
2045 return 1;
2046}
2047
2048/*
2049 * Create a new terminal of "rows" by "cols" cells.
2050 * Store a reference in "term".
2051 * Return OK or FAIL.
2052 */
2053 static int
2054term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2055{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002056 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002057 channel_T *channel = NULL;
2058 job_T *job = NULL;
2059 jobopt_T opt;
2060 DWORD error;
2061 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2062 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002063 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002064
2065 if (!dyn_winpty_init())
2066 return FAIL;
2067
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002068 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002069 if (p == NULL)
2070 return FAIL;
2071
2072 job = job_alloc();
2073 if (job == NULL)
2074 goto failed;
2075
2076 channel = add_channel();
2077 if (channel == NULL)
2078 goto failed;
2079
2080 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2081 if (term->tl_winpty_config == NULL)
2082 goto failed;
2083
2084 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2085 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2086 if (term->tl_winpty == NULL)
2087 goto failed;
2088
2089 spawn_config = winpty_spawn_config_new(
2090 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2091 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2092 NULL,
2093 p,
2094 NULL,
2095 NULL,
2096 &winpty_err);
2097 if (spawn_config == NULL)
2098 goto failed;
2099
2100 channel = add_channel();
2101 if (channel == NULL)
2102 goto failed;
2103
2104 job = job_alloc();
2105 if (job == NULL)
2106 goto failed;
2107
2108 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2109 &child_thread_handle, &error, &winpty_err))
2110 goto failed;
2111
2112 channel_set_pipes(channel,
2113 (sock_T) CreateFileW(
2114 winpty_conin_name(term->tl_winpty),
2115 GENERIC_WRITE, 0, NULL,
2116 OPEN_EXISTING, 0, NULL),
2117 (sock_T) CreateFileW(
2118 winpty_conout_name(term->tl_winpty),
2119 GENERIC_READ, 0, NULL,
2120 OPEN_EXISTING, 0, NULL),
2121 (sock_T) CreateFileW(
2122 winpty_conerr_name(term->tl_winpty),
2123 GENERIC_READ, 0, NULL,
2124 OPEN_EXISTING, 0, NULL));
2125
2126 jo = CreateJobObject(NULL, NULL);
2127 if (jo == NULL)
2128 goto failed;
2129
2130 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002131 {
2132 /* Failed, switch the way to terminate process with TerminateProcess. */
2133 CloseHandle(jo);
2134 jo = NULL;
2135 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002136
2137 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002138 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002139
2140 create_vterm(term, rows, cols);
2141
2142 setup_job_options(&opt, rows, cols);
2143 channel_set_job(channel, job, &opt);
2144
2145 job->jv_channel = channel;
2146 job->jv_proc_info.hProcess = child_process_handle;
2147 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2148 job->jv_job_object = jo;
2149 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002150 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002151 term->tl_job = job;
2152
2153 return OK;
2154
2155failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002156 if (spawn_config != NULL)
2157 winpty_spawn_config_free(spawn_config);
2158 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002159 if (channel != NULL)
2160 channel_clear(channel);
2161 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002162 {
2163 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002164 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002165 }
2166 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002167 if (jo != NULL)
2168 CloseHandle(jo);
2169 if (term->tl_winpty != NULL)
2170 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002171 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002172 if (term->tl_winpty_config != NULL)
2173 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002174 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002175 if (winpty_err != NULL)
2176 {
2177 char_u *msg = utf16_to_enc(
2178 (short_u *)winpty_error_msg(winpty_err), NULL);
2179
2180 EMSG(msg);
2181 winpty_error_free(winpty_err);
2182 }
2183 return FAIL;
2184}
2185
2186/*
2187 * Free the terminal emulator part of "term".
2188 */
2189 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002190term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002191{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002192 if (term->tl_winpty != NULL)
2193 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002194 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002195 if (term->tl_winpty_config != NULL)
2196 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002197 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002198 if (term->tl_vterm != NULL)
2199 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002200 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002201}
2202
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002203/*
2204 * Request size to terminal.
2205 */
2206 static void
2207term_report_winsize(term_T *term, int rows, int cols)
2208{
2209 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2210}
2211
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002212# else
2213
2214/**************************************
2215 * 3. Unix-like implementation.
2216 */
2217
2218/*
2219 * Create a new terminal of "rows" by "cols" cells.
2220 * Start job for "cmd".
2221 * Store the pointers in "term".
2222 * Return OK or FAIL.
2223 */
2224 static int
2225term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2226{
2227 typval_T argvars[2];
2228 jobopt_T opt;
2229
2230 create_vterm(term, rows, cols);
2231
2232 argvars[0].v_type = VAR_STRING;
2233 argvars[0].vval.v_string = cmd;
2234 setup_job_options(&opt, rows, cols);
2235 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002236 if (term->tl_job != NULL)
2237 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002238
Bram Moolenaar61a66052017-07-22 18:39:00 +02002239 return term->tl_job != NULL
2240 && term->tl_job->jv_channel != NULL
2241 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002242}
2243
2244/*
2245 * Free the terminal emulator part of "term".
2246 */
2247 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002248term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002249{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002250 if (term->tl_vterm != NULL)
2251 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002252 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002253}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002254
2255/*
2256 * Request size to terminal.
2257 */
2258 static void
2259term_report_winsize(term_T *term, int rows, int cols)
2260{
2261 /* Use an ioctl() to report the new window size to the job. */
2262 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2263 {
2264 int fd = -1;
2265 int part;
2266
2267 for (part = PART_OUT; part < PART_COUNT; ++part)
2268 {
2269 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2270 if (isatty(fd))
2271 break;
2272 }
2273 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2274 mch_stop_job(term->tl_job, (char_u *)"winch");
2275 }
2276}
2277
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002278# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002279
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002280#endif /* FEAT_TERMINAL */