blob: 45c6175708b31012a36803e8eff8f0066e25c4b7 [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
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;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001023
1024 term->tl_cursor_pos = pos;
1025 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001026
1027 FOR_ALL_WINDOWS(wp)
1028 {
1029 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001030 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001031 }
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001032 if (term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001033 {
1034 may_toggle_cursor(term);
1035 update_cursor(term, TRUE);
1036 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001037
1038 return 1;
1039}
1040
Bram Moolenaar21554412017-07-24 21:44:43 +02001041 static int
1042handle_settermprop(
1043 VTermProp prop,
1044 VTermValue *value,
1045 void *user)
1046{
1047 term_T *term = (term_T *)user;
1048
1049 switch (prop)
1050 {
1051 case VTERM_PROP_TITLE:
1052 vim_free(term->tl_title);
1053 term->tl_title = vim_strsave((char_u *)value->string);
1054 vim_free(term->tl_status_text);
1055 term->tl_status_text = NULL;
1056 if (term == curbuf->b_term)
1057 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001058 break;
1059
1060 case VTERM_PROP_CURSORVISIBLE:
1061 term->tl_cursor_visible = value->boolean;
1062 may_toggle_cursor(term);
1063 out_flush();
1064 break;
1065
Bram Moolenaar21554412017-07-24 21:44:43 +02001066 default:
1067 break;
1068 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001069 /* Always return 1, otherwise vterm doesn't store the value internally. */
1070 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001071}
1072
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001073/*
1074 * The job running in the terminal resized the terminal.
1075 */
1076 static int
1077handle_resize(int rows, int cols, void *user)
1078{
1079 term_T *term = (term_T *)user;
1080 win_T *wp;
1081
1082 term->tl_rows = rows;
1083 term->tl_cols = cols;
1084 FOR_ALL_WINDOWS(wp)
1085 {
1086 if (wp->w_buffer == term->tl_buffer)
1087 {
1088 win_setheight_win(rows, wp);
1089 win_setwidth_win(cols, wp);
1090 }
1091 }
1092
1093 redraw_buf_later(term->tl_buffer, NOT_VALID);
1094 return 1;
1095}
1096
Bram Moolenaard85f2712017-07-28 21:51:57 +02001097/*
1098 * Handle a line that is pushed off the top of the screen.
1099 */
1100 static int
1101handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1102{
1103 term_T *term = (term_T *)user;
1104
1105 /* TODO: Limit the number of lines that are stored. */
1106 /* TODO: put the text in the buffer. */
1107 if (ga_grow(&term->tl_scrollback, 1) == OK)
1108 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001109 VTermScreenCell *p = NULL;
1110 int len = 0;
1111 int i;
1112 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001113
1114 /* do not store empty cells at the end */
1115 for (i = 0; i < cols; ++i)
1116 if (cells[i].chars[0] != 0)
1117 len = i + 1;
1118
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001119 if (len > 0)
1120 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001121 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001122 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001123
1124 line = (sb_line_T *)term->tl_scrollback.ga_data
1125 + term->tl_scrollback.ga_len;
1126 line->sb_cols = len;
1127 line->sb_cells = p;
1128 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001129 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001130
1131 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001132 }
1133 return 0; /* ignored */
1134}
1135
Bram Moolenaar21554412017-07-24 21:44:43 +02001136static VTermScreenCallbacks screen_callbacks = {
1137 handle_damage, /* damage */
1138 handle_moverect, /* moverect */
1139 handle_movecursor, /* movecursor */
1140 handle_settermprop, /* settermprop */
1141 NULL, /* bell */
1142 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001143 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001144 NULL /* sb_popline */
1145};
1146
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001147/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001148 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001149 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001150 */
1151 void
1152term_channel_closed(channel_T *ch)
1153{
1154 term_T *term;
1155 int did_one = FALSE;
1156
1157 for (term = first_term; term != NULL; term = term->tl_next)
1158 if (term->tl_job == ch->ch_job)
1159 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001160 term->tl_channel_closed = TRUE;
1161
Bram Moolenaard85f2712017-07-28 21:51:57 +02001162 vim_free(term->tl_title);
1163 term->tl_title = NULL;
1164 vim_free(term->tl_status_text);
1165 term->tl_status_text = NULL;
1166
Bram Moolenaar423802d2017-07-30 16:52:24 +02001167 /* Unless in Terminal-Normal mode: clear the vterm. */
1168 if (!term->tl_terminal_mode)
1169 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001170
1171 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1172 did_one = TRUE;
1173 }
1174 if (did_one)
1175 {
1176 redraw_statuslines();
1177
1178 /* Need to break out of vgetc(). */
1179 ins_char_typebuf(K_IGNORE);
1180
1181 if (curbuf->b_term != NULL)
1182 {
1183 if (curbuf->b_term->tl_job == ch->ch_job)
1184 maketitle();
1185 update_cursor(curbuf->b_term, TRUE);
1186 }
1187 }
1188}
1189
1190/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001191 * Reverse engineer the RGB value into a cterm color index.
1192 * First color is 1. Return 0 if no match found.
1193 */
1194 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001195color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001196{
1197 int red = color->red;
1198 int blue = color->blue;
1199 int green = color->green;
1200
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001201 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001202 if (red == 0)
1203 {
1204 if (green == 0)
1205 {
1206 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001207 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001208 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001209 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001210 }
1211 else if (green == 224)
1212 {
1213 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001214 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001215 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001216 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001217 }
1218 }
1219 else if (red == 224)
1220 {
1221 if (green == 0)
1222 {
1223 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001224 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001225 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001226 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001227 }
1228 else if (green == 224)
1229 {
1230 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001231 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001232 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001233 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001234 }
1235 }
1236 else if (red == 128)
1237 {
1238 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001239 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001240 }
1241 else if (red == 255)
1242 {
1243 if (green == 64)
1244 {
1245 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001246 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001247 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001248 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001249 }
1250 else if (green == 255)
1251 {
1252 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001253 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001254 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001255 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001256 }
1257 }
1258 else if (red == 64)
1259 {
1260 if (green == 64)
1261 {
1262 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001263 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001264 }
1265 else if (green == 255)
1266 {
1267 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001268 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001269 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001270 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001271 }
1272 }
1273 if (t_colors >= 256)
1274 {
1275 if (red == blue && red == green)
1276 {
1277 /* 24-color greyscale */
1278 static int cutoff[23] = {
1279 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1280 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1281 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1282 int i;
1283
1284 for (i = 0; i < 23; ++i)
1285 if (red < cutoff[i])
1286 return i + 233;
1287 return 256;
1288 }
1289
1290 /* 216-color cube */
1291 return 17 + ((red + 25) / 0x33) * 36
1292 + ((green + 25) / 0x33) * 6
1293 + (blue + 25) / 0x33;
1294 }
1295 return 0;
1296}
1297
1298/*
1299 * Convert the attributes of a vterm cell into an attribute index.
1300 */
1301 static int
1302cell2attr(VTermScreenCell *cell)
1303{
1304 int attr = 0;
1305
1306 if (cell->attrs.bold)
1307 attr |= HL_BOLD;
1308 if (cell->attrs.underline)
1309 attr |= HL_UNDERLINE;
1310 if (cell->attrs.italic)
1311 attr |= HL_ITALIC;
1312 if (cell->attrs.strike)
1313 attr |= HL_STANDOUT;
1314 if (cell->attrs.reverse)
1315 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001316
1317#ifdef FEAT_GUI
1318 if (gui.in_use)
1319 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001320 guicolor_T fg, bg;
1321
1322 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1323 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1324 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001325 }
1326 else
1327#endif
1328#ifdef FEAT_TERMGUICOLORS
1329 if (p_tgc)
1330 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001331 guicolor_T fg, bg;
1332
1333 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1334 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1335
1336 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001337 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001338 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001339#endif
1340 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001341 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1342 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001343 }
1344 return 0;
1345}
1346
1347/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001348 * Called to update the window that contains a terminal.
1349 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001350 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001351 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001352term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001353{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001354 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001355 VTerm *vterm;
1356 VTermScreen *screen;
1357 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001358 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001359
Bram Moolenaar423802d2017-07-30 16:52:24 +02001360 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001361 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001362
Bram Moolenaard85f2712017-07-28 21:51:57 +02001363 vterm = term->tl_vterm;
1364 screen = vterm_obtain_screen(vterm);
1365 state = vterm_obtain_state(vterm);
1366
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001367 /*
1368 * If the window was resized a redraw will be triggered and we get here.
1369 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1370 */
1371 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1372 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001373 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001374 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1375 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1376 win_T *twp;
1377
1378 FOR_ALL_WINDOWS(twp)
1379 {
1380 /* When more than one window shows the same terminal, use the
1381 * smallest size. */
1382 if (twp->w_buffer == term->tl_buffer)
1383 {
1384 if (!term->tl_rows_fixed && rows > twp->w_height)
1385 rows = twp->w_height;
1386 if (!term->tl_cols_fixed && cols > twp->w_width)
1387 cols = twp->w_width;
1388 }
1389 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001390
1391 vterm_set_size(vterm, rows, cols);
1392 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1393 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001394 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001395 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001396
1397 /* The cursor may have been moved when resizing. */
1398 vterm_state_get_cursorpos(state, &pos);
1399 position_cursor(wp, &pos);
1400
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001401 /* TODO: Only redraw what changed. */
1402 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001403 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001404 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001405 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001406
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001407 if (pos.row < term->tl_rows)
1408 {
1409 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001410 {
1411 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001412 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001413
Bram Moolenaareeac6772017-07-23 15:48:37 +02001414 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1415 vim_memset(&cell, 0, sizeof(cell));
1416
1417 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001418 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001419 if (c == NUL)
1420 {
1421 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001422#if defined(FEAT_MBYTE)
1423 if (enc_utf8)
1424 ScreenLinesUC[off] = NUL;
1425#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001426 }
1427 else
1428 {
1429#if defined(FEAT_MBYTE)
1430 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001431 {
1432 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001433 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001434 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001435 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001436 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001437 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001438 if (enc_utf8)
1439 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001440 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001441#else
1442 ScreenLines[off] = c;
1443#endif
1444 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001445 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001446
1447 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001448 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001449 if (cell.width == 2)
1450 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001451 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001452#if defined(FEAT_MBYTE)
1453 if (enc_utf8)
1454 ScreenLinesUC[off] = NUL;
1455#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001456 ++pos.col;
1457 ++off;
1458 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001459 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001460 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001461 else
1462 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001463
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001464 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1465 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001466 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001467
1468 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001469}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001470
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001471/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001472 * Return TRUE if "wp" is a terminal window where the job has finished.
1473 */
1474 int
1475term_is_finished(buf_T *buf)
1476{
1477 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1478}
1479
1480/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001481 * Return TRUE if "wp" is a terminal window where the job has finished or we
1482 * are in Terminal-Normal mode.
1483 */
1484 int
1485term_show_buffer(buf_T *buf)
1486{
1487 term_T *term = buf->b_term;
1488
1489 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1490}
1491
1492/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001493 * The current buffer is going to be changed. If there is terminal
1494 * highlighting remove it now.
1495 */
1496 void
1497term_change_in_curbuf(void)
1498{
1499 term_T *term = curbuf->b_term;
1500
1501 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1502 {
1503 free_scrollback(term);
1504 redraw_buf_later(term->tl_buffer, NOT_VALID);
1505 }
1506}
1507
1508/*
1509 * Get the screen attribute for a position in the buffer.
1510 */
1511 int
1512term_get_attr(buf_T *buf, linenr_T lnum, int col)
1513{
1514 term_T *term = buf->b_term;
1515 sb_line_T *line;
1516
Bram Moolenaar70229f92017-07-29 16:01:53 +02001517 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001518 return 0;
1519 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1520 if (col >= line->sb_cols)
1521 return 0;
1522 return cell2attr(line->sb_cells + col);
1523}
1524
1525/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001526 * Set job options common for Unix and MS-Windows.
1527 */
1528 static void
1529setup_job_options(jobopt_T *opt, int rows, int cols)
1530{
1531 clear_job_options(opt);
1532 opt->jo_mode = MODE_RAW;
1533 opt->jo_out_mode = MODE_RAW;
1534 opt->jo_err_mode = MODE_RAW;
1535 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001536
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001537 opt->jo_io[PART_OUT] = JIO_BUFFER;
1538 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001539 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1540
1541 opt->jo_modifiable[PART_OUT] = 0;
1542 opt->jo_modifiable[PART_ERR] = 0;
1543 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1544
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001545 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1546 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001547 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001548 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1549
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001550 opt->jo_term_rows = rows;
1551 opt->jo_term_cols = cols;
1552}
1553
1554/*
1555 * Create a new vterm and initialize it.
1556 */
1557 static void
1558create_vterm(term_T *term, int rows, int cols)
1559{
1560 VTerm *vterm;
1561 VTermScreen *screen;
1562
1563 vterm = vterm_new(rows, cols);
1564 term->tl_vterm = vterm;
1565 screen = vterm_obtain_screen(vterm);
1566 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1567 /* TODO: depends on 'encoding'. */
1568 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001569
1570 /* Vterm uses a default black background. Set it to white when
1571 * 'background' is "light". */
1572 if (*p_bg == 'l')
1573 {
1574 VTermColor fg, bg;
1575
1576 fg.red = fg.green = fg.blue = 0;
1577 bg.red = bg.green = bg.blue = 255;
1578 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1579 }
1580
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001581 /* Required to initialize most things. */
1582 vterm_screen_reset(screen, 1 /* hard */);
1583}
1584
Bram Moolenaar21554412017-07-24 21:44:43 +02001585/*
1586 * Return the text to show for the buffer name and status.
1587 */
1588 char_u *
1589term_get_status_text(term_T *term)
1590{
1591 if (term->tl_status_text == NULL)
1592 {
1593 char_u *txt;
1594 size_t len;
1595
Bram Moolenaar423802d2017-07-30 16:52:24 +02001596 if (term->tl_terminal_mode)
1597 {
1598 if (term_job_running(term))
1599 txt = (char_u *)_("Terminal");
1600 else
1601 txt = (char_u *)_("Terminal-finished");
1602 }
1603 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001604 txt = term->tl_title;
1605 else if (term_job_running(term))
1606 txt = (char_u *)_("running");
1607 else
1608 txt = (char_u *)_("finished");
1609 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001610 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001611 if (term->tl_status_text != NULL)
1612 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1613 term->tl_buffer->b_fname, txt);
1614 }
1615 return term->tl_status_text;
1616}
1617
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001618/*
1619 * Mark references in jobs of terminals.
1620 */
1621 int
1622set_ref_in_term(int copyID)
1623{
1624 int abort = FALSE;
1625 term_T *term;
1626 typval_T tv;
1627
1628 for (term = first_term; term != NULL; term = term->tl_next)
1629 if (term->tl_job != NULL)
1630 {
1631 tv.v_type = VAR_JOB;
1632 tv.vval.v_job = term->tl_job;
1633 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1634 }
1635 return abort;
1636}
1637
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001638/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001639 * Get the buffer from the first argument in "argvars".
1640 * Returns NULL when the buffer is not for a terminal window.
1641 */
1642 static buf_T *
1643term_get_buf(typval_T *argvars)
1644{
1645 buf_T *buf;
1646
1647 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1648 ++emsg_off;
1649 buf = get_buf_tv(&argvars[0], FALSE);
1650 --emsg_off;
1651 if (buf == NULL || buf->b_term == NULL)
1652 return NULL;
1653 return buf;
1654}
1655
1656/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001657 * "term_getattr(attr, name)" function
1658 */
1659 void
1660f_term_getattr(typval_T *argvars, typval_T *rettv)
1661{
1662 int attr;
1663 size_t i;
1664 char_u *name;
1665
1666 static struct {
1667 char *name;
1668 int attr;
1669 } attrs[] = {
1670 {"bold", HL_BOLD},
1671 {"italic", HL_ITALIC},
1672 {"underline", HL_UNDERLINE},
1673 {"strike", HL_STANDOUT},
1674 {"reverse", HL_INVERSE},
1675 };
1676
1677 attr = get_tv_number(&argvars[0]);
1678 name = get_tv_string_chk(&argvars[1]);
1679 if (name == NULL)
1680 return;
1681
1682 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1683 if (STRCMP(name, attrs[i].name) == 0)
1684 {
1685 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1686 break;
1687 }
1688}
1689
1690/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001691 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001692 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001693 void
1694f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001695{
Bram Moolenaar97870002017-07-30 18:28:38 +02001696 buf_T *buf = term_get_buf(argvars);
1697 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001698
Bram Moolenaar97870002017-07-30 18:28:38 +02001699 if (rettv_list_alloc(rettv) == FAIL)
1700 return;
1701 if (buf == NULL)
1702 return;
1703
1704 l = rettv->vval.v_list;
1705 list_append_number(l, buf->b_term->tl_cursor_pos.row);
1706 list_append_number(l, buf->b_term->tl_cursor_pos.col);
1707 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001708}
1709
1710/*
1711 * "term_getjob(buf)" function
1712 */
1713 void
1714f_term_getjob(typval_T *argvars, typval_T *rettv)
1715{
1716 buf_T *buf = term_get_buf(argvars);
1717
1718 rettv->v_type = VAR_JOB;
1719 rettv->vval.v_job = NULL;
1720 if (buf == NULL)
1721 return;
1722
1723 rettv->vval.v_job = buf->b_term->tl_job;
1724 if (rettv->vval.v_job != NULL)
1725 ++rettv->vval.v_job->jv_refcount;
1726}
1727
1728/*
1729 * "term_getline(buf, row)" function
1730 */
1731 void
1732f_term_getline(typval_T *argvars, typval_T *rettv)
1733{
1734 buf_T *buf = term_get_buf(argvars);
1735 term_T *term;
1736 int row;
1737
1738 rettv->v_type = VAR_STRING;
1739 if (buf == NULL)
1740 return;
1741 term = buf->b_term;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001742 if (argvars[1].v_type == VAR_UNKNOWN)
1743 row = term->tl_cursor_pos.row;
1744 else
1745 row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001746
1747 if (term->tl_vterm == NULL)
1748 {
1749 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1750
1751 /* vterm is finished, get the text from the buffer */
1752 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1753 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1754 }
1755 else
1756 {
1757 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1758 VTermRect rect;
1759 int len;
1760 char_u *p;
1761
1762 len = term->tl_cols * MB_MAXBYTES + 1;
1763 p = alloc(len);
1764 if (p == NULL)
1765 return;
1766 rettv->vval.v_string = p;
1767
1768 rect.start_col = 0;
1769 rect.end_col = term->tl_cols;
1770 rect.start_row = row;
1771 rect.end_row = row + 1;
1772 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1773 }
1774}
1775
1776/*
1777 * "term_getsize(buf)" function
1778 */
1779 void
1780f_term_getsize(typval_T *argvars, typval_T *rettv)
1781{
1782 buf_T *buf = term_get_buf(argvars);
1783 list_T *l;
1784
1785 if (rettv_list_alloc(rettv) == FAIL)
1786 return;
1787 if (buf == NULL)
1788 return;
1789
1790 l = rettv->vval.v_list;
1791 list_append_number(l, buf->b_term->tl_rows);
1792 list_append_number(l, buf->b_term->tl_cols);
1793}
1794
1795/*
1796 * "term_list()" function
1797 */
1798 void
1799f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1800{
1801 term_T *tp;
1802 list_T *l;
1803
1804 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1805 return;
1806
1807 l = rettv->vval.v_list;
1808 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1809 if (tp != NULL && tp->tl_buffer != NULL)
1810 if (list_append_number(l,
1811 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1812 return;
1813}
1814
1815/*
1816 * "term_scrape(buf, row)" function
1817 */
1818 void
1819f_term_scrape(typval_T *argvars, typval_T *rettv)
1820{
1821 buf_T *buf = term_get_buf(argvars);
1822 VTermScreen *screen = NULL;
1823 VTermPos pos;
1824 list_T *l;
1825 term_T *term;
1826
1827 if (rettv_list_alloc(rettv) == FAIL)
1828 return;
1829 if (buf == NULL)
1830 return;
1831 term = buf->b_term;
1832 if (term->tl_vterm != NULL)
1833 screen = vterm_obtain_screen(term->tl_vterm);
1834
1835 l = rettv->vval.v_list;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001836 if (argvars[1].v_type == VAR_UNKNOWN)
1837 pos.row = term->tl_cursor_pos.row;
1838 else
1839 pos.row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001840 for (pos.col = 0; pos.col < term->tl_cols; )
1841 {
1842 dict_T *dcell;
1843 VTermScreenCell cell;
1844 char_u rgb[8];
1845 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1846 int off = 0;
1847 int i;
1848
1849 if (screen == NULL)
1850 {
1851 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1852 sb_line_T *line;
1853
1854 /* vterm has finished, get the cell from scrollback */
1855 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1856 break;
1857 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1858 if (pos.col >= line->sb_cols)
1859 break;
1860 cell = line->sb_cells[pos.col];
1861 }
1862 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1863 break;
1864 dcell = dict_alloc();
1865 list_append_dict(l, dcell);
1866
1867 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1868 {
1869 if (cell.chars[i] == 0)
1870 break;
1871 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1872 }
1873 mbs[off] = NUL;
1874 dict_add_nr_str(dcell, "chars", 0, mbs);
1875
1876 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1877 cell.fg.red, cell.fg.green, cell.fg.blue);
1878 dict_add_nr_str(dcell, "fg", 0, rgb);
1879 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1880 cell.bg.red, cell.bg.green, cell.bg.blue);
1881 dict_add_nr_str(dcell, "bg", 0, rgb);
1882
1883 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
1884 dict_add_nr_str(dcell, "width", cell.width, NULL);
1885
1886 ++pos.col;
1887 if (cell.width == 2)
1888 ++pos.col;
1889 }
1890}
1891
1892/*
1893 * "term_sendkeys(buf, keys)" function
1894 */
1895 void
1896f_term_sendkeys(typval_T *argvars, typval_T *rettv)
1897{
1898 buf_T *buf = term_get_buf(argvars);
1899 char_u *msg;
1900 term_T *term;
1901
1902 rettv->v_type = VAR_UNKNOWN;
1903 if (buf == NULL)
1904 return;
1905
1906 msg = get_tv_string_chk(&argvars[1]);
1907 if (msg == NULL)
1908 return;
1909 term = buf->b_term;
1910 if (term->tl_vterm == NULL)
1911 return;
1912
1913 while (*msg != NUL)
1914 {
1915 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
1916 msg += MB_PTR2LEN(msg);
1917 }
1918
1919 /* TODO: only update once in a while. */
1920 update_screen(0);
1921 if (buf == curbuf)
1922 update_cursor(term, TRUE);
1923}
1924
1925/*
1926 * "term_start(command, options)" function
1927 */
1928 void
1929f_term_start(typval_T *argvars, typval_T *rettv)
1930{
1931 char_u *cmd = get_tv_string_chk(&argvars[0]);
1932 exarg_T ea;
1933
1934 if (cmd == NULL)
1935 return;
1936 ea.arg = cmd;
1937 ex_terminal(&ea);
1938
1939 if (curbuf->b_term != NULL)
1940 rettv->vval.v_number = curbuf->b_fnum;
1941}
1942
1943/*
1944 * "term_wait" function
1945 */
1946 void
1947f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
1948{
1949 buf_T *buf = term_get_buf(argvars);
1950
1951 if (buf == NULL)
1952 return;
1953
1954 /* Get the job status, this will detect a job that finished. */
1955 if (buf->b_term->tl_job != NULL)
1956 (void)job_status(buf->b_term->tl_job);
1957
1958 /* Check for any pending channel I/O. */
1959 vpeekc_any();
1960 ui_delay(10L, FALSE);
1961
1962 /* Flushing messages on channels is hopefully sufficient.
1963 * TODO: is there a better way? */
1964 parse_queued_messages();
1965}
1966
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001967# ifdef WIN3264
1968
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001969/**************************************
1970 * 2. MS-Windows implementation.
1971 */
1972
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001973#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1974#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1975
Bram Moolenaar8a773062017-07-24 22:29:21 +02001976void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001977void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001978void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001979BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1980void (*winpty_config_set_initial_size)(void*, int, int);
1981LPCWSTR (*winpty_conin_name)(void*);
1982LPCWSTR (*winpty_conout_name)(void*);
1983LPCWSTR (*winpty_conerr_name)(void*);
1984void (*winpty_free)(void*);
1985void (*winpty_config_free)(void*);
1986void (*winpty_spawn_config_free)(void*);
1987void (*winpty_error_free)(void*);
1988LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001989BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001990
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001991#define WINPTY_DLL "winpty.dll"
1992
1993static HINSTANCE hWinPtyDLL = NULL;
1994
1995 int
1996dyn_winpty_init(void)
1997{
1998 int i;
1999 static struct
2000 {
2001 char *name;
2002 FARPROC *ptr;
2003 } winpty_entry[] =
2004 {
2005 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2006 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2007 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2008 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2009 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2010 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2011 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2012 {"winpty_free", (FARPROC*)&winpty_free},
2013 {"winpty_open", (FARPROC*)&winpty_open},
2014 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2015 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2016 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2017 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002018 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002019 {NULL, NULL}
2020 };
2021
2022 /* No need to initialize twice. */
2023 if (hWinPtyDLL)
2024 return 1;
2025 /* Load winpty.dll */
2026 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2027 if (!hWinPtyDLL)
2028 {
2029 EMSG2(_(e_loadlib), WINPTY_DLL);
2030 return 0;
2031 }
2032 for (i = 0; winpty_entry[i].name != NULL
2033 && winpty_entry[i].ptr != NULL; ++i)
2034 {
2035 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2036 winpty_entry[i].name)) == NULL)
2037 {
2038 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2039 return 0;
2040 }
2041 }
2042
2043 return 1;
2044}
2045
2046/*
2047 * Create a new terminal of "rows" by "cols" cells.
2048 * Store a reference in "term".
2049 * Return OK or FAIL.
2050 */
2051 static int
2052term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2053{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002054 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002055 channel_T *channel = NULL;
2056 job_T *job = NULL;
2057 jobopt_T opt;
2058 DWORD error;
2059 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2060 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002061 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002062
2063 if (!dyn_winpty_init())
2064 return FAIL;
2065
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002066 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002067 if (p == NULL)
2068 return FAIL;
2069
2070 job = job_alloc();
2071 if (job == NULL)
2072 goto failed;
2073
2074 channel = add_channel();
2075 if (channel == NULL)
2076 goto failed;
2077
2078 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2079 if (term->tl_winpty_config == NULL)
2080 goto failed;
2081
2082 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2083 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2084 if (term->tl_winpty == NULL)
2085 goto failed;
2086
2087 spawn_config = winpty_spawn_config_new(
2088 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2089 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2090 NULL,
2091 p,
2092 NULL,
2093 NULL,
2094 &winpty_err);
2095 if (spawn_config == NULL)
2096 goto failed;
2097
2098 channel = add_channel();
2099 if (channel == NULL)
2100 goto failed;
2101
2102 job = job_alloc();
2103 if (job == NULL)
2104 goto failed;
2105
2106 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2107 &child_thread_handle, &error, &winpty_err))
2108 goto failed;
2109
2110 channel_set_pipes(channel,
2111 (sock_T) CreateFileW(
2112 winpty_conin_name(term->tl_winpty),
2113 GENERIC_WRITE, 0, NULL,
2114 OPEN_EXISTING, 0, NULL),
2115 (sock_T) CreateFileW(
2116 winpty_conout_name(term->tl_winpty),
2117 GENERIC_READ, 0, NULL,
2118 OPEN_EXISTING, 0, NULL),
2119 (sock_T) CreateFileW(
2120 winpty_conerr_name(term->tl_winpty),
2121 GENERIC_READ, 0, NULL,
2122 OPEN_EXISTING, 0, NULL));
2123
2124 jo = CreateJobObject(NULL, NULL);
2125 if (jo == NULL)
2126 goto failed;
2127
2128 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002129 {
2130 /* Failed, switch the way to terminate process with TerminateProcess. */
2131 CloseHandle(jo);
2132 jo = NULL;
2133 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002134
2135 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002136 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002137
2138 create_vterm(term, rows, cols);
2139
2140 setup_job_options(&opt, rows, cols);
2141 channel_set_job(channel, job, &opt);
2142
2143 job->jv_channel = channel;
2144 job->jv_proc_info.hProcess = child_process_handle;
2145 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2146 job->jv_job_object = jo;
2147 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002148 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002149 term->tl_job = job;
2150
2151 return OK;
2152
2153failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002154 if (spawn_config != NULL)
2155 winpty_spawn_config_free(spawn_config);
2156 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002157 if (channel != NULL)
2158 channel_clear(channel);
2159 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002160 {
2161 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002162 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002163 }
2164 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002165 if (jo != NULL)
2166 CloseHandle(jo);
2167 if (term->tl_winpty != NULL)
2168 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002169 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002170 if (term->tl_winpty_config != NULL)
2171 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002172 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002173 if (winpty_err != NULL)
2174 {
2175 char_u *msg = utf16_to_enc(
2176 (short_u *)winpty_error_msg(winpty_err), NULL);
2177
2178 EMSG(msg);
2179 winpty_error_free(winpty_err);
2180 }
2181 return FAIL;
2182}
2183
2184/*
2185 * Free the terminal emulator part of "term".
2186 */
2187 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002188term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002189{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002190 if (term->tl_winpty != NULL)
2191 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002192 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002193 if (term->tl_winpty_config != NULL)
2194 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002195 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002196 if (term->tl_vterm != NULL)
2197 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002198 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002199}
2200
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002201/*
2202 * Request size to terminal.
2203 */
2204 static void
2205term_report_winsize(term_T *term, int rows, int cols)
2206{
2207 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2208}
2209
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002210# else
2211
2212/**************************************
2213 * 3. Unix-like implementation.
2214 */
2215
2216/*
2217 * Create a new terminal of "rows" by "cols" cells.
2218 * Start job for "cmd".
2219 * Store the pointers in "term".
2220 * Return OK or FAIL.
2221 */
2222 static int
2223term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2224{
2225 typval_T argvars[2];
2226 jobopt_T opt;
2227
2228 create_vterm(term, rows, cols);
2229
2230 argvars[0].v_type = VAR_STRING;
2231 argvars[0].vval.v_string = cmd;
2232 setup_job_options(&opt, rows, cols);
2233 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002234 if (term->tl_job != NULL)
2235 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002236
Bram Moolenaar61a66052017-07-22 18:39:00 +02002237 return term->tl_job != NULL
2238 && term->tl_job->jv_channel != NULL
2239 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002240}
2241
2242/*
2243 * Free the terminal emulator part of "term".
2244 */
2245 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002246term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002247{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002248 if (term->tl_vterm != NULL)
2249 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002250 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002251}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002252
2253/*
2254 * Request size to terminal.
2255 */
2256 static void
2257term_report_winsize(term_T *term, int rows, int cols)
2258{
2259 /* Use an ioctl() to report the new window size to the job. */
2260 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2261 {
2262 int fd = -1;
2263 int part;
2264
2265 for (part = PART_OUT; part < PART_COUNT; ++part)
2266 {
2267 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2268 if (isatty(fd))
2269 break;
2270 }
2271 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2272 mch_stop_job(term->tl_job, (char_u *)"winch");
2273 }
2274}
2275
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002276# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002277
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002278#endif /* FEAT_TERMINAL */