blob: f526108c59d0a7e78e0f413dc472c5c7cabc586a [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 Moolenaare4f25e42017-07-07 11:54:15 +0200123 pos_T tl_cursor;
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
860/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200861 * Returns TRUE if the current window contains a terminal and we are sending
862 * keys to the job.
863 */
864 int
865term_use_loop()
866{
867 term_T *term = curbuf->b_term;
868
869 return term != NULL
870 && !term->tl_terminal_mode
871 && term->tl_vterm != NULL
872 && term_job_running(term);
873}
874
875/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200876 * Wait for input and send it to the job.
877 * Return when the start of a CTRL-W command is typed or anything else that
878 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200879 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
880 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200881 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200882 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200883terminal_loop(void)
884{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200885 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200886 int termkey = 0;
887
888 if (*curwin->w_p_tk != NUL)
889 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200890
891 for (;;)
892 {
893 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200894 /* Repeat redrawing in case a message is received while redrawing. */
895 while (curwin->w_redr_type != 0)
896 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200897 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200898
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200899 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200900 if (curbuf->b_term->tl_vterm == NULL
901 || !term_job_running(curbuf->b_term))
902 /* job finished while waiting for a character */
903 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200904
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200905 if (c == (termkey == 0 ? Ctrl_W : termkey))
906 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200907#ifdef FEAT_CMDL_INFO
908 if (add_to_showcmd(c))
909 out_flush();
910#endif
911 c = term_vgetc();
912#ifdef FEAT_CMDL_INFO
913 clear_showcmd();
914#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200915 if (curbuf->b_term->tl_vterm == NULL
916 || !term_job_running(curbuf->b_term))
917 /* job finished while waiting for a character */
918 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200919
920 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200921 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200922 /* "CTRL-W .": send CTRL-W to the job */
923 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200924 }
925 else if (termkey == 0 && c == 'N')
926 {
927 term_enter_terminal_mode();
928 return FAIL;
929 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200930 else if (termkey == 0 || c != termkey)
931 {
932 stuffcharReadbuff(Ctrl_W);
933 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200934 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200935 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200936 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200937 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
938 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200939 }
Bram Moolenaard85f2712017-07-28 21:51:57 +0200940 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200941}
942
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200943/*
944 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200945 * This updates the title and status, but does not close the vter, because
946 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200947 */
948 void
949term_job_ended(job_T *job)
950{
951 term_T *term;
952 int did_one = FALSE;
953
954 for (term = first_term; term != NULL; term = term->tl_next)
955 if (term->tl_job == job)
956 {
957 vim_free(term->tl_title);
958 term->tl_title = NULL;
959 vim_free(term->tl_status_text);
960 term->tl_status_text = NULL;
961 redraw_buf_and_status_later(term->tl_buffer, VALID);
962 did_one = TRUE;
963 }
964 if (did_one)
965 redraw_statuslines();
966 if (curbuf->b_term != NULL)
967 {
968 if (curbuf->b_term->tl_job == job)
969 maketitle();
970 update_cursor(curbuf->b_term, TRUE);
971 }
972}
973
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200974 static void
975position_cursor(win_T *wp, VTermPos *pos)
976{
977 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
978 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
979}
980
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200981 static void
982may_toggle_cursor(term_T *term)
983{
984 if (curbuf == term->tl_buffer)
985 {
986 if (term->tl_cursor_visible)
987 cursor_on();
988 else
989 cursor_off();
990 }
991}
992
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200993 static int
994handle_damage(VTermRect rect, void *user)
995{
996 term_T *term = (term_T *)user;
997
998 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
999 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1000 redraw_buf_later(term->tl_buffer, NOT_VALID);
1001 return 1;
1002}
1003
1004 static int
1005handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1006{
1007 term_T *term = (term_T *)user;
1008
1009 /* TODO */
1010 redraw_buf_later(term->tl_buffer, NOT_VALID);
1011 return 1;
1012}
1013
1014 static int
1015handle_movecursor(
1016 VTermPos pos,
1017 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001018 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001019 void *user)
1020{
1021 term_T *term = (term_T *)user;
1022 win_T *wp;
1023 int is_current = FALSE;
1024
1025 FOR_ALL_WINDOWS(wp)
1026 {
1027 if (wp->w_buffer == term->tl_buffer)
1028 {
1029 position_cursor(wp, &pos);
1030 if (wp == curwin)
1031 is_current = TRUE;
1032 }
1033 }
1034
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001035 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001036 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001037 {
1038 may_toggle_cursor(term);
1039 update_cursor(term, TRUE);
1040 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001041
1042 return 1;
1043}
1044
Bram Moolenaar21554412017-07-24 21:44:43 +02001045 static int
1046handle_settermprop(
1047 VTermProp prop,
1048 VTermValue *value,
1049 void *user)
1050{
1051 term_T *term = (term_T *)user;
1052
1053 switch (prop)
1054 {
1055 case VTERM_PROP_TITLE:
1056 vim_free(term->tl_title);
1057 term->tl_title = vim_strsave((char_u *)value->string);
1058 vim_free(term->tl_status_text);
1059 term->tl_status_text = NULL;
1060 if (term == curbuf->b_term)
1061 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001062 break;
1063
1064 case VTERM_PROP_CURSORVISIBLE:
1065 term->tl_cursor_visible = value->boolean;
1066 may_toggle_cursor(term);
1067 out_flush();
1068 break;
1069
Bram Moolenaar21554412017-07-24 21:44:43 +02001070 default:
1071 break;
1072 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001073 /* Always return 1, otherwise vterm doesn't store the value internally. */
1074 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001075}
1076
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001077/*
1078 * The job running in the terminal resized the terminal.
1079 */
1080 static int
1081handle_resize(int rows, int cols, void *user)
1082{
1083 term_T *term = (term_T *)user;
1084 win_T *wp;
1085
1086 term->tl_rows = rows;
1087 term->tl_cols = cols;
1088 FOR_ALL_WINDOWS(wp)
1089 {
1090 if (wp->w_buffer == term->tl_buffer)
1091 {
1092 win_setheight_win(rows, wp);
1093 win_setwidth_win(cols, wp);
1094 }
1095 }
1096
1097 redraw_buf_later(term->tl_buffer, NOT_VALID);
1098 return 1;
1099}
1100
Bram Moolenaard85f2712017-07-28 21:51:57 +02001101/*
1102 * Handle a line that is pushed off the top of the screen.
1103 */
1104 static int
1105handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1106{
1107 term_T *term = (term_T *)user;
1108
1109 /* TODO: Limit the number of lines that are stored. */
1110 /* TODO: put the text in the buffer. */
1111 if (ga_grow(&term->tl_scrollback, 1) == OK)
1112 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001113 VTermScreenCell *p = NULL;
1114 int len = 0;
1115 int i;
1116 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001117
1118 /* do not store empty cells at the end */
1119 for (i = 0; i < cols; ++i)
1120 if (cells[i].chars[0] != 0)
1121 len = i + 1;
1122
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001123 if (len > 0)
1124 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001125 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001126 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001127
1128 line = (sb_line_T *)term->tl_scrollback.ga_data
1129 + term->tl_scrollback.ga_len;
1130 line->sb_cols = len;
1131 line->sb_cells = p;
1132 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001133 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001134
1135 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001136 }
1137 return 0; /* ignored */
1138}
1139
Bram Moolenaar21554412017-07-24 21:44:43 +02001140static VTermScreenCallbacks screen_callbacks = {
1141 handle_damage, /* damage */
1142 handle_moverect, /* moverect */
1143 handle_movecursor, /* movecursor */
1144 handle_settermprop, /* settermprop */
1145 NULL, /* bell */
1146 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001147 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001148 NULL /* sb_popline */
1149};
1150
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001151/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001152 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001153 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001154 */
1155 void
1156term_channel_closed(channel_T *ch)
1157{
1158 term_T *term;
1159 int did_one = FALSE;
1160
1161 for (term = first_term; term != NULL; term = term->tl_next)
1162 if (term->tl_job == ch->ch_job)
1163 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001164 term->tl_channel_closed = TRUE;
1165
Bram Moolenaard85f2712017-07-28 21:51:57 +02001166 vim_free(term->tl_title);
1167 term->tl_title = NULL;
1168 vim_free(term->tl_status_text);
1169 term->tl_status_text = NULL;
1170
Bram Moolenaar423802d2017-07-30 16:52:24 +02001171 /* Unless in Terminal-Normal mode: clear the vterm. */
1172 if (!term->tl_terminal_mode)
1173 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001174
1175 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1176 did_one = TRUE;
1177 }
1178 if (did_one)
1179 {
1180 redraw_statuslines();
1181
1182 /* Need to break out of vgetc(). */
1183 ins_char_typebuf(K_IGNORE);
1184
1185 if (curbuf->b_term != NULL)
1186 {
1187 if (curbuf->b_term->tl_job == ch->ch_job)
1188 maketitle();
1189 update_cursor(curbuf->b_term, TRUE);
1190 }
1191 }
1192}
1193
1194/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001195 * Reverse engineer the RGB value into a cterm color index.
1196 * First color is 1. Return 0 if no match found.
1197 */
1198 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001199color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001200{
1201 int red = color->red;
1202 int blue = color->blue;
1203 int green = color->green;
1204
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001205 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001206 if (red == 0)
1207 {
1208 if (green == 0)
1209 {
1210 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001211 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001212 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001213 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001214 }
1215 else if (green == 224)
1216 {
1217 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001218 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001219 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001220 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001221 }
1222 }
1223 else if (red == 224)
1224 {
1225 if (green == 0)
1226 {
1227 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001228 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001229 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001230 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001231 }
1232 else if (green == 224)
1233 {
1234 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001235 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001236 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001237 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001238 }
1239 }
1240 else if (red == 128)
1241 {
1242 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001243 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001244 }
1245 else if (red == 255)
1246 {
1247 if (green == 64)
1248 {
1249 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001250 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001251 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001252 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001253 }
1254 else if (green == 255)
1255 {
1256 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001257 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001258 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001259 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001260 }
1261 }
1262 else if (red == 64)
1263 {
1264 if (green == 64)
1265 {
1266 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001267 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001268 }
1269 else if (green == 255)
1270 {
1271 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001272 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001273 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001274 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001275 }
1276 }
1277 if (t_colors >= 256)
1278 {
1279 if (red == blue && red == green)
1280 {
1281 /* 24-color greyscale */
1282 static int cutoff[23] = {
1283 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1284 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1285 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1286 int i;
1287
1288 for (i = 0; i < 23; ++i)
1289 if (red < cutoff[i])
1290 return i + 233;
1291 return 256;
1292 }
1293
1294 /* 216-color cube */
1295 return 17 + ((red + 25) / 0x33) * 36
1296 + ((green + 25) / 0x33) * 6
1297 + (blue + 25) / 0x33;
1298 }
1299 return 0;
1300}
1301
1302/*
1303 * Convert the attributes of a vterm cell into an attribute index.
1304 */
1305 static int
1306cell2attr(VTermScreenCell *cell)
1307{
1308 int attr = 0;
1309
1310 if (cell->attrs.bold)
1311 attr |= HL_BOLD;
1312 if (cell->attrs.underline)
1313 attr |= HL_UNDERLINE;
1314 if (cell->attrs.italic)
1315 attr |= HL_ITALIC;
1316 if (cell->attrs.strike)
1317 attr |= HL_STANDOUT;
1318 if (cell->attrs.reverse)
1319 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001320
1321#ifdef FEAT_GUI
1322 if (gui.in_use)
1323 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001324 guicolor_T fg, bg;
1325
1326 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1327 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1328 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001329 }
1330 else
1331#endif
1332#ifdef FEAT_TERMGUICOLORS
1333 if (p_tgc)
1334 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001335 guicolor_T fg, bg;
1336
1337 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1338 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1339
1340 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001341 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001342 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001343#endif
1344 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001345 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1346 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001347 }
1348 return 0;
1349}
1350
1351/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001352 * Called to update the window that contains a terminal.
1353 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001354 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001355 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001356term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001357{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001358 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001359 VTerm *vterm;
1360 VTermScreen *screen;
1361 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001362 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001363
Bram Moolenaar423802d2017-07-30 16:52:24 +02001364 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001365 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001366
Bram Moolenaard85f2712017-07-28 21:51:57 +02001367 vterm = term->tl_vterm;
1368 screen = vterm_obtain_screen(vterm);
1369 state = vterm_obtain_state(vterm);
1370
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001371 /*
1372 * If the window was resized a redraw will be triggered and we get here.
1373 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1374 */
1375 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1376 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001377 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001378 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1379 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1380 win_T *twp;
1381
1382 FOR_ALL_WINDOWS(twp)
1383 {
1384 /* When more than one window shows the same terminal, use the
1385 * smallest size. */
1386 if (twp->w_buffer == term->tl_buffer)
1387 {
1388 if (!term->tl_rows_fixed && rows > twp->w_height)
1389 rows = twp->w_height;
1390 if (!term->tl_cols_fixed && cols > twp->w_width)
1391 cols = twp->w_width;
1392 }
1393 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001394
1395 vterm_set_size(vterm, rows, cols);
1396 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1397 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001398 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001399 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001400
1401 /* The cursor may have been moved when resizing. */
1402 vterm_state_get_cursorpos(state, &pos);
1403 position_cursor(wp, &pos);
1404
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001405 /* TODO: Only redraw what changed. */
1406 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001407 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001408 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001409 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001410
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001411 if (pos.row < term->tl_rows)
1412 {
1413 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001414 {
1415 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001416 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001417
Bram Moolenaareeac6772017-07-23 15:48:37 +02001418 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1419 vim_memset(&cell, 0, sizeof(cell));
1420
1421 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001422 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001423 if (c == NUL)
1424 {
1425 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001426#if defined(FEAT_MBYTE)
1427 if (enc_utf8)
1428 ScreenLinesUC[off] = NUL;
1429#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001430 }
1431 else
1432 {
1433#if defined(FEAT_MBYTE)
1434 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001435 {
1436 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001437 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001438 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001439 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001440 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001441 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001442 if (enc_utf8)
1443 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001444 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001445#else
1446 ScreenLines[off] = c;
1447#endif
1448 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001449 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001450
1451 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001452 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001453 if (cell.width == 2)
1454 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001455 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001456#if defined(FEAT_MBYTE)
1457 if (enc_utf8)
1458 ScreenLinesUC[off] = NUL;
1459#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001460 ++pos.col;
1461 ++off;
1462 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001463 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001464 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001465 else
1466 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001467
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001468 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1469 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001470 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001471
1472 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001473}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001474
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001475/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001476 * Return TRUE if "wp" is a terminal window where the job has finished.
1477 */
1478 int
1479term_is_finished(buf_T *buf)
1480{
1481 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1482}
1483
1484/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001485 * Return TRUE if "wp" is a terminal window where the job has finished or we
1486 * are in Terminal-Normal mode.
1487 */
1488 int
1489term_show_buffer(buf_T *buf)
1490{
1491 term_T *term = buf->b_term;
1492
1493 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1494}
1495
1496/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001497 * The current buffer is going to be changed. If there is terminal
1498 * highlighting remove it now.
1499 */
1500 void
1501term_change_in_curbuf(void)
1502{
1503 term_T *term = curbuf->b_term;
1504
1505 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1506 {
1507 free_scrollback(term);
1508 redraw_buf_later(term->tl_buffer, NOT_VALID);
1509 }
1510}
1511
1512/*
1513 * Get the screen attribute for a position in the buffer.
1514 */
1515 int
1516term_get_attr(buf_T *buf, linenr_T lnum, int col)
1517{
1518 term_T *term = buf->b_term;
1519 sb_line_T *line;
1520
Bram Moolenaar70229f92017-07-29 16:01:53 +02001521 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001522 return 0;
1523 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1524 if (col >= line->sb_cols)
1525 return 0;
1526 return cell2attr(line->sb_cells + col);
1527}
1528
1529/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001530 * Set job options common for Unix and MS-Windows.
1531 */
1532 static void
1533setup_job_options(jobopt_T *opt, int rows, int cols)
1534{
1535 clear_job_options(opt);
1536 opt->jo_mode = MODE_RAW;
1537 opt->jo_out_mode = MODE_RAW;
1538 opt->jo_err_mode = MODE_RAW;
1539 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001540
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001541 opt->jo_io[PART_OUT] = JIO_BUFFER;
1542 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001543 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1544
1545 opt->jo_modifiable[PART_OUT] = 0;
1546 opt->jo_modifiable[PART_ERR] = 0;
1547 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1548
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001549 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1550 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001551 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001552 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1553
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001554 opt->jo_term_rows = rows;
1555 opt->jo_term_cols = cols;
1556}
1557
1558/*
1559 * Create a new vterm and initialize it.
1560 */
1561 static void
1562create_vterm(term_T *term, int rows, int cols)
1563{
1564 VTerm *vterm;
1565 VTermScreen *screen;
1566
1567 vterm = vterm_new(rows, cols);
1568 term->tl_vterm = vterm;
1569 screen = vterm_obtain_screen(vterm);
1570 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1571 /* TODO: depends on 'encoding'. */
1572 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001573
1574 /* Vterm uses a default black background. Set it to white when
1575 * 'background' is "light". */
1576 if (*p_bg == 'l')
1577 {
1578 VTermColor fg, bg;
1579
1580 fg.red = fg.green = fg.blue = 0;
1581 bg.red = bg.green = bg.blue = 255;
1582 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1583 }
1584
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001585 /* Required to initialize most things. */
1586 vterm_screen_reset(screen, 1 /* hard */);
1587}
1588
Bram Moolenaar21554412017-07-24 21:44:43 +02001589/*
1590 * Return the text to show for the buffer name and status.
1591 */
1592 char_u *
1593term_get_status_text(term_T *term)
1594{
1595 if (term->tl_status_text == NULL)
1596 {
1597 char_u *txt;
1598 size_t len;
1599
Bram Moolenaar423802d2017-07-30 16:52:24 +02001600 if (term->tl_terminal_mode)
1601 {
1602 if (term_job_running(term))
1603 txt = (char_u *)_("Terminal");
1604 else
1605 txt = (char_u *)_("Terminal-finished");
1606 }
1607 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001608 txt = term->tl_title;
1609 else if (term_job_running(term))
1610 txt = (char_u *)_("running");
1611 else
1612 txt = (char_u *)_("finished");
1613 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001614 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001615 if (term->tl_status_text != NULL)
1616 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1617 term->tl_buffer->b_fname, txt);
1618 }
1619 return term->tl_status_text;
1620}
1621
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001622/*
1623 * Mark references in jobs of terminals.
1624 */
1625 int
1626set_ref_in_term(int copyID)
1627{
1628 int abort = FALSE;
1629 term_T *term;
1630 typval_T tv;
1631
1632 for (term = first_term; term != NULL; term = term->tl_next)
1633 if (term->tl_job != NULL)
1634 {
1635 tv.v_type = VAR_JOB;
1636 tv.vval.v_job = term->tl_job;
1637 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1638 }
1639 return abort;
1640}
1641
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001642/*
1643 * "term_getattr(attr, name)" function
1644 */
1645 void
1646f_term_getattr(typval_T *argvars, typval_T *rettv)
1647{
1648 int attr;
1649 size_t i;
1650 char_u *name;
1651
1652 static struct {
1653 char *name;
1654 int attr;
1655 } attrs[] = {
1656 {"bold", HL_BOLD},
1657 {"italic", HL_ITALIC},
1658 {"underline", HL_UNDERLINE},
1659 {"strike", HL_STANDOUT},
1660 {"reverse", HL_INVERSE},
1661 };
1662
1663 attr = get_tv_number(&argvars[0]);
1664 name = get_tv_string_chk(&argvars[1]);
1665 if (name == NULL)
1666 return;
1667
1668 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1669 if (STRCMP(name, attrs[i].name) == 0)
1670 {
1671 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1672 break;
1673 }
1674}
1675
1676/*
1677 * Get the buffer from the first argument in "argvars".
1678 * Returns NULL when the buffer is not for a terminal window.
1679 */
1680 static buf_T *
1681term_get_buf(typval_T *argvars)
1682{
1683 buf_T *buf;
1684
1685 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1686 ++emsg_off;
1687 buf = get_buf_tv(&argvars[0], FALSE);
1688 --emsg_off;
Bram Moolenaarf144a3f2017-07-30 18:02:12 +02001689 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001690 return NULL;
1691 return buf;
1692}
1693
1694/*
1695 * "term_getjob(buf)" function
1696 */
1697 void
1698f_term_getjob(typval_T *argvars, typval_T *rettv)
1699{
1700 buf_T *buf = term_get_buf(argvars);
1701
1702 rettv->v_type = VAR_JOB;
1703 rettv->vval.v_job = NULL;
1704 if (buf == NULL)
1705 return;
1706
1707 rettv->vval.v_job = buf->b_term->tl_job;
1708 if (rettv->vval.v_job != NULL)
1709 ++rettv->vval.v_job->jv_refcount;
1710}
1711
1712/*
1713 * "term_getline(buf, row)" function
1714 */
1715 void
1716f_term_getline(typval_T *argvars, typval_T *rettv)
1717{
1718 buf_T *buf = term_get_buf(argvars);
1719 term_T *term;
1720 int row;
1721
1722 rettv->v_type = VAR_STRING;
1723 if (buf == NULL)
1724 return;
1725 term = buf->b_term;
1726 row = (int)get_tv_number(&argvars[1]);
1727
1728 if (term->tl_vterm == NULL)
1729 {
1730 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1731
1732 /* vterm is finished, get the text from the buffer */
1733 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1734 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1735 }
1736 else
1737 {
1738 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1739 VTermRect rect;
1740 int len;
1741 char_u *p;
1742
1743 len = term->tl_cols * MB_MAXBYTES + 1;
1744 p = alloc(len);
1745 if (p == NULL)
1746 return;
1747 rettv->vval.v_string = p;
1748
1749 rect.start_col = 0;
1750 rect.end_col = term->tl_cols;
1751 rect.start_row = row;
1752 rect.end_row = row + 1;
1753 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1754 }
1755}
1756
1757/*
1758 * "term_getsize(buf)" function
1759 */
1760 void
1761f_term_getsize(typval_T *argvars, typval_T *rettv)
1762{
1763 buf_T *buf = term_get_buf(argvars);
1764 list_T *l;
1765
1766 if (rettv_list_alloc(rettv) == FAIL)
1767 return;
1768 if (buf == NULL)
1769 return;
1770
1771 l = rettv->vval.v_list;
1772 list_append_number(l, buf->b_term->tl_rows);
1773 list_append_number(l, buf->b_term->tl_cols);
1774}
1775
1776/*
1777 * "term_list()" function
1778 */
1779 void
1780f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1781{
1782 term_T *tp;
1783 list_T *l;
1784
1785 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1786 return;
1787
1788 l = rettv->vval.v_list;
1789 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1790 if (tp != NULL && tp->tl_buffer != NULL)
1791 if (list_append_number(l,
1792 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1793 return;
1794}
1795
1796/*
1797 * "term_scrape(buf, row)" function
1798 */
1799 void
1800f_term_scrape(typval_T *argvars, typval_T *rettv)
1801{
1802 buf_T *buf = term_get_buf(argvars);
1803 VTermScreen *screen = NULL;
1804 VTermPos pos;
1805 list_T *l;
1806 term_T *term;
1807
1808 if (rettv_list_alloc(rettv) == FAIL)
1809 return;
1810 if (buf == NULL)
1811 return;
1812 term = buf->b_term;
1813 if (term->tl_vterm != NULL)
1814 screen = vterm_obtain_screen(term->tl_vterm);
1815
1816 l = rettv->vval.v_list;
1817 pos.row = (int)get_tv_number(&argvars[1]);
1818 for (pos.col = 0; pos.col < term->tl_cols; )
1819 {
1820 dict_T *dcell;
1821 VTermScreenCell cell;
1822 char_u rgb[8];
1823 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1824 int off = 0;
1825 int i;
1826
1827 if (screen == NULL)
1828 {
1829 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1830 sb_line_T *line;
1831
1832 /* vterm has finished, get the cell from scrollback */
1833 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1834 break;
1835 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1836 if (pos.col >= line->sb_cols)
1837 break;
1838 cell = line->sb_cells[pos.col];
1839 }
1840 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1841 break;
1842 dcell = dict_alloc();
1843 list_append_dict(l, dcell);
1844
1845 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1846 {
1847 if (cell.chars[i] == 0)
1848 break;
1849 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1850 }
1851 mbs[off] = NUL;
1852 dict_add_nr_str(dcell, "chars", 0, mbs);
1853
1854 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1855 cell.fg.red, cell.fg.green, cell.fg.blue);
1856 dict_add_nr_str(dcell, "fg", 0, rgb);
1857 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1858 cell.bg.red, cell.bg.green, cell.bg.blue);
1859 dict_add_nr_str(dcell, "bg", 0, rgb);
1860
1861 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
1862 dict_add_nr_str(dcell, "width", cell.width, NULL);
1863
1864 ++pos.col;
1865 if (cell.width == 2)
1866 ++pos.col;
1867 }
1868}
1869
1870/*
1871 * "term_sendkeys(buf, keys)" function
1872 */
1873 void
1874f_term_sendkeys(typval_T *argvars, typval_T *rettv)
1875{
1876 buf_T *buf = term_get_buf(argvars);
1877 char_u *msg;
1878 term_T *term;
1879
1880 rettv->v_type = VAR_UNKNOWN;
1881 if (buf == NULL)
1882 return;
1883
1884 msg = get_tv_string_chk(&argvars[1]);
1885 if (msg == NULL)
1886 return;
1887 term = buf->b_term;
1888 if (term->tl_vterm == NULL)
1889 return;
1890
1891 while (*msg != NUL)
1892 {
1893 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
1894 msg += MB_PTR2LEN(msg);
1895 }
1896
1897 /* TODO: only update once in a while. */
1898 update_screen(0);
1899 if (buf == curbuf)
1900 update_cursor(term, TRUE);
1901}
1902
1903/*
1904 * "term_start(command, options)" function
1905 */
1906 void
1907f_term_start(typval_T *argvars, typval_T *rettv)
1908{
1909 char_u *cmd = get_tv_string_chk(&argvars[0]);
1910 exarg_T ea;
1911
1912 if (cmd == NULL)
1913 return;
1914 ea.arg = cmd;
1915 ex_terminal(&ea);
1916
1917 if (curbuf->b_term != NULL)
1918 rettv->vval.v_number = curbuf->b_fnum;
1919}
1920
1921/*
1922 * "term_wait" function
1923 */
1924 void
1925f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
1926{
1927 buf_T *buf = term_get_buf(argvars);
1928
1929 if (buf == NULL)
1930 return;
1931
1932 /* Get the job status, this will detect a job that finished. */
1933 if (buf->b_term->tl_job != NULL)
1934 (void)job_status(buf->b_term->tl_job);
1935
1936 /* Check for any pending channel I/O. */
1937 vpeekc_any();
1938 ui_delay(10L, FALSE);
1939
1940 /* Flushing messages on channels is hopefully sufficient.
1941 * TODO: is there a better way? */
1942 parse_queued_messages();
1943}
1944
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001945# ifdef WIN3264
1946
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001947/**************************************
1948 * 2. MS-Windows implementation.
1949 */
1950
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001951#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1952#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1953
Bram Moolenaar8a773062017-07-24 22:29:21 +02001954void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001955void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001956void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001957BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1958void (*winpty_config_set_initial_size)(void*, int, int);
1959LPCWSTR (*winpty_conin_name)(void*);
1960LPCWSTR (*winpty_conout_name)(void*);
1961LPCWSTR (*winpty_conerr_name)(void*);
1962void (*winpty_free)(void*);
1963void (*winpty_config_free)(void*);
1964void (*winpty_spawn_config_free)(void*);
1965void (*winpty_error_free)(void*);
1966LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001967BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001968
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001969#define WINPTY_DLL "winpty.dll"
1970
1971static HINSTANCE hWinPtyDLL = NULL;
1972
1973 int
1974dyn_winpty_init(void)
1975{
1976 int i;
1977 static struct
1978 {
1979 char *name;
1980 FARPROC *ptr;
1981 } winpty_entry[] =
1982 {
1983 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1984 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1985 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1986 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1987 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1988 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1989 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1990 {"winpty_free", (FARPROC*)&winpty_free},
1991 {"winpty_open", (FARPROC*)&winpty_open},
1992 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1993 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1994 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1995 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001996 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001997 {NULL, NULL}
1998 };
1999
2000 /* No need to initialize twice. */
2001 if (hWinPtyDLL)
2002 return 1;
2003 /* Load winpty.dll */
2004 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2005 if (!hWinPtyDLL)
2006 {
2007 EMSG2(_(e_loadlib), WINPTY_DLL);
2008 return 0;
2009 }
2010 for (i = 0; winpty_entry[i].name != NULL
2011 && winpty_entry[i].ptr != NULL; ++i)
2012 {
2013 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2014 winpty_entry[i].name)) == NULL)
2015 {
2016 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2017 return 0;
2018 }
2019 }
2020
2021 return 1;
2022}
2023
2024/*
2025 * Create a new terminal of "rows" by "cols" cells.
2026 * Store a reference in "term".
2027 * Return OK or FAIL.
2028 */
2029 static int
2030term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2031{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002032 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002033 channel_T *channel = NULL;
2034 job_T *job = NULL;
2035 jobopt_T opt;
2036 DWORD error;
2037 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2038 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002039 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002040
2041 if (!dyn_winpty_init())
2042 return FAIL;
2043
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002044 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002045 if (p == NULL)
2046 return FAIL;
2047
2048 job = job_alloc();
2049 if (job == NULL)
2050 goto failed;
2051
2052 channel = add_channel();
2053 if (channel == NULL)
2054 goto failed;
2055
2056 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2057 if (term->tl_winpty_config == NULL)
2058 goto failed;
2059
2060 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2061 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2062 if (term->tl_winpty == NULL)
2063 goto failed;
2064
2065 spawn_config = winpty_spawn_config_new(
2066 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2067 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2068 NULL,
2069 p,
2070 NULL,
2071 NULL,
2072 &winpty_err);
2073 if (spawn_config == NULL)
2074 goto failed;
2075
2076 channel = add_channel();
2077 if (channel == NULL)
2078 goto failed;
2079
2080 job = job_alloc();
2081 if (job == NULL)
2082 goto failed;
2083
2084 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2085 &child_thread_handle, &error, &winpty_err))
2086 goto failed;
2087
2088 channel_set_pipes(channel,
2089 (sock_T) CreateFileW(
2090 winpty_conin_name(term->tl_winpty),
2091 GENERIC_WRITE, 0, NULL,
2092 OPEN_EXISTING, 0, NULL),
2093 (sock_T) CreateFileW(
2094 winpty_conout_name(term->tl_winpty),
2095 GENERIC_READ, 0, NULL,
2096 OPEN_EXISTING, 0, NULL),
2097 (sock_T) CreateFileW(
2098 winpty_conerr_name(term->tl_winpty),
2099 GENERIC_READ, 0, NULL,
2100 OPEN_EXISTING, 0, NULL));
2101
2102 jo = CreateJobObject(NULL, NULL);
2103 if (jo == NULL)
2104 goto failed;
2105
2106 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002107 {
2108 /* Failed, switch the way to terminate process with TerminateProcess. */
2109 CloseHandle(jo);
2110 jo = NULL;
2111 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002112
2113 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002114 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002115
2116 create_vterm(term, rows, cols);
2117
2118 setup_job_options(&opt, rows, cols);
2119 channel_set_job(channel, job, &opt);
2120
2121 job->jv_channel = channel;
2122 job->jv_proc_info.hProcess = child_process_handle;
2123 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2124 job->jv_job_object = jo;
2125 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002126 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002127 term->tl_job = job;
2128
2129 return OK;
2130
2131failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002132 if (spawn_config != NULL)
2133 winpty_spawn_config_free(spawn_config);
2134 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002135 if (channel != NULL)
2136 channel_clear(channel);
2137 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002138 {
2139 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002140 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002141 }
2142 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002143 if (jo != NULL)
2144 CloseHandle(jo);
2145 if (term->tl_winpty != NULL)
2146 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002147 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002148 if (term->tl_winpty_config != NULL)
2149 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002150 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002151 if (winpty_err != NULL)
2152 {
2153 char_u *msg = utf16_to_enc(
2154 (short_u *)winpty_error_msg(winpty_err), NULL);
2155
2156 EMSG(msg);
2157 winpty_error_free(winpty_err);
2158 }
2159 return FAIL;
2160}
2161
2162/*
2163 * Free the terminal emulator part of "term".
2164 */
2165 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002166term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002167{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002168 if (term->tl_winpty != NULL)
2169 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002170 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002171 if (term->tl_winpty_config != NULL)
2172 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002173 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002174 if (term->tl_vterm != NULL)
2175 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002176 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002177}
2178
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002179/*
2180 * Request size to terminal.
2181 */
2182 static void
2183term_report_winsize(term_T *term, int rows, int cols)
2184{
2185 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2186}
2187
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002188# else
2189
2190/**************************************
2191 * 3. Unix-like implementation.
2192 */
2193
2194/*
2195 * Create a new terminal of "rows" by "cols" cells.
2196 * Start job for "cmd".
2197 * Store the pointers in "term".
2198 * Return OK or FAIL.
2199 */
2200 static int
2201term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2202{
2203 typval_T argvars[2];
2204 jobopt_T opt;
2205
2206 create_vterm(term, rows, cols);
2207
2208 argvars[0].v_type = VAR_STRING;
2209 argvars[0].vval.v_string = cmd;
2210 setup_job_options(&opt, rows, cols);
2211 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002212 if (term->tl_job != NULL)
2213 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002214
Bram Moolenaar61a66052017-07-22 18:39:00 +02002215 return term->tl_job != NULL
2216 && term->tl_job->jv_channel != NULL
2217 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002218}
2219
2220/*
2221 * Free the terminal emulator part of "term".
2222 */
2223 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002224term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002225{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002226 if (term->tl_vterm != NULL)
2227 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002228 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002229}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002230
2231/*
2232 * Request size to terminal.
2233 */
2234 static void
2235term_report_winsize(term_T *term, int rows, int cols)
2236{
2237 /* Use an ioctl() to report the new window size to the job. */
2238 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2239 {
2240 int fd = -1;
2241 int part;
2242
2243 for (part = PART_OUT; part < PART_COUNT; ++part)
2244 {
2245 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2246 if (isatty(fd))
2247 break;
2248 }
2249 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2250 mch_stop_job(term->tl_job, (char_u *)"winch");
2251 }
2252}
2253
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002254# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002255
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002256#endif /* FEAT_TERMINAL */