blob: b289f6c994d2768fba40ba101cf11f833e2b1044 [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 Moolenaar12d93ee2017-07-30 19:02:02 +020054 * - support different cursor shapes, colors and attributes
55 * - make term_getcursor() return type (none/block/bar/underline) and
56 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020057 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020058 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020059 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020060 * - add 't' to mode()
Bram Moolenaar938783d2017-07-16 20:13:26 +020061 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaard85f2712017-07-28 21:51:57 +020062 * - when closing window and job has ended, make buffer hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020063 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020064 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020066 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020067 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020068 * - implement "term" for job_start(): more job options when starting a
69 * terminal.
Bram Moolenaar423802d2017-07-30 16:52:24 +020070 * - if the job in the terminal does not support the mouse, we can use the
71 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020072 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
73 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020074 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020075 */
76
77#include "vim.h"
78
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020079#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020080
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020081#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020082# define MIN(x,y) (x < y ? x : y)
83# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020084#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020085
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020086#include "libvterm/include/vterm.h"
87
Bram Moolenaard85f2712017-07-28 21:51:57 +020088typedef struct sb_line_S {
89 int sb_cols; /* can differ per line */
90 VTermScreenCell *sb_cells; /* allocated */
91} sb_line_T;
92
Bram Moolenaare4f25e42017-07-07 11:54:15 +020093/* typedef term_T in structs.h */
94struct terminal_S {
95 term_T *tl_next;
96
Bram Moolenaar423802d2017-07-30 16:52:24 +020097 VTerm *tl_vterm;
98 job_T *tl_job;
99 buf_T *tl_buffer;
100
101 int tl_terminal_mode;
102 int tl_channel_closed;
103
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200104#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200105 void *tl_winpty_config;
106 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200107#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200108
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200109 /* last known vterm size */
110 int tl_rows;
111 int tl_cols;
112 /* vterm size does not follow window size */
113 int tl_rows_fixed;
114 int tl_cols_fixed;
115
Bram Moolenaar21554412017-07-24 21:44:43 +0200116 char_u *tl_title; /* NULL or allocated */
117 char_u *tl_status_text; /* NULL or allocated */
118
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200119 /* Range of screen rows to update. Zero based. */
120 int tl_dirty_row_start; /* -1 if nothing dirty */
121 int tl_dirty_row_end; /* row below last one to update */
122
Bram Moolenaard85f2712017-07-28 21:51:57 +0200123 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200124 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200125
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200126 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200127 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200128};
129
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200130/*
131 * List of all active terminals.
132 */
133static term_T *first_term = NULL;
134
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200135
136#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
137#define KEY_BUF_LEN 200
138
139/*
140 * Functions with separate implementation for MS-Windows and Unix-like systems.
141 */
142static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200143static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200144static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200145
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200146/**************************************
147 * 1. Generic code for all systems.
148 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200149
150/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200151 * Determine the terminal size from 'termsize' and the current window.
152 * Assumes term->tl_rows and term->tl_cols are zero.
153 */
154 static void
155set_term_and_win_size(term_T *term)
156{
157 if (*curwin->w_p_tms != NUL)
158 {
159 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
160
161 term->tl_rows = atoi((char *)curwin->w_p_tms);
162 term->tl_cols = atoi((char *)p);
163 }
164 if (term->tl_rows == 0)
165 term->tl_rows = curwin->w_height;
166 else
167 {
168 win_setheight_win(term->tl_rows, curwin);
169 term->tl_rows_fixed = TRUE;
170 }
171 if (term->tl_cols == 0)
172 term->tl_cols = curwin->w_width;
173 else
174 {
175 win_setwidth_win(term->tl_cols, curwin);
176 term->tl_cols_fixed = TRUE;
177 }
178}
179
180/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200181 * ":terminal": open a terminal window and execute a job in it.
182 */
183 void
184ex_terminal(exarg_T *eap)
185{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200186 exarg_T split_ea;
187 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200188 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200189 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200190
191 if (check_restricted() || check_secure())
192 return;
193
194 term = (term_T *)alloc_clear(sizeof(term_T));
195 if (term == NULL)
196 return;
197 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200198 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200199 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200200
201 /* Open a new window or tab. */
202 vim_memset(&split_ea, 0, sizeof(split_ea));
203 split_ea.cmdidx = CMD_new;
204 split_ea.cmd = (char_u *)"new";
205 split_ea.arg = (char_u *)"";
206 ex_splitview(&split_ea);
207 if (curwin == old_curwin)
208 {
209 /* split failed */
210 vim_free(term);
211 return;
212 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200213 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200214 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200215
216 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200217 term->tl_next = first_term;
218 first_term = term;
219
Bram Moolenaar293424c2017-07-26 23:11:01 +0200220 if (cmd == NULL || *cmd == NUL)
221 cmd = p_sh;
222
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200223 if (buflist_findname(cmd) == NULL)
224 curbuf->b_ffname = vim_strsave(cmd);
225 else
226 {
227 int i;
228 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200229 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200230
231 for (i = 1; p != NULL; ++i)
232 {
233 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
234 if (buflist_findname(p) == NULL)
235 {
236 curbuf->b_ffname = p;
237 break;
238 }
239 }
240 }
241 curbuf->b_fname = curbuf->b_ffname;
242
243 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
244 curbuf->b_changed = TRUE;
245 curbuf->b_p_ma = FALSE;
246 set_string_option_direct((char_u *)"buftype", -1,
247 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200249 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200251 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200252 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200253 {
254 /* store the size we ended up with */
255 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200256 }
257 else
258 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200259 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200260
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200261 /* Wiping out the buffer will also close the window and call
262 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200263 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200264 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200265
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200266 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200267}
268
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200269/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200270 * Free the scrollback buffer for "term".
271 */
272 static void
273free_scrollback(term_T *term)
274{
275 int i;
276
277 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
278 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
279 ga_clear(&term->tl_scrollback);
280}
281
282/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200283 * Free a terminal and everything it refers to.
284 * Kills the job if there is one.
285 * Called when wiping out a buffer.
286 */
287 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200288free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200289{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200290 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200291 term_T *tp;
292
293 if (term == NULL)
294 return;
295 if (first_term == term)
296 first_term = term->tl_next;
297 else
298 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
299 if (tp->tl_next == term)
300 {
301 tp->tl_next = term->tl_next;
302 break;
303 }
304
305 if (term->tl_job != NULL)
306 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200307 if (term->tl_job->jv_status != JOB_ENDED
308 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200309 job_stop(term->tl_job, NULL, "kill");
310 job_unref(term->tl_job);
311 }
312
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200313 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200314
315 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200316 vim_free(term->tl_title);
317 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200318 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200319 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200320}
321
322/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200323 * Write job output "msg[len]" to the vterm.
324 */
325 static void
326term_write_job_output(term_T *term, char_u *msg, size_t len)
327{
328 VTerm *vterm = term->tl_vterm;
329 char_u *p;
330 size_t done;
331 size_t len_now;
332
333 for (done = 0; done < len; done += len_now)
334 {
335 for (p = msg + done; p < msg + len; )
336 {
337 if (*p == NL)
338 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200339 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200340 }
341 len_now = p - msg - done;
342 vterm_input_write(vterm, (char *)msg + done, len_now);
343 if (p < msg + len && *p == NL)
344 {
345 /* Convert NL to CR-NL, that appears to work best. */
346 vterm_input_write(vterm, "\r\n", 2);
347 ++len_now;
348 }
349 }
350
351 /* this invokes the damage callbacks */
352 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
353}
354
Bram Moolenaar1c844932017-07-24 23:36:41 +0200355 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200356update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200357{
Bram Moolenaar1c844932017-07-24 23:36:41 +0200358 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200359 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200360 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200361 if (term->tl_cursor_visible)
362 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200363 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200364#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200365 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200366 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200367#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200368 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200369}
370
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200371/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200372 * Invoked when "msg" output from a job was received. Write it to the terminal
373 * of "buffer".
374 */
375 void
376write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
377{
378 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200379 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200380
Bram Moolenaard85f2712017-07-28 21:51:57 +0200381 if (term->tl_vterm == NULL)
382 {
383 ch_logn(channel, "NOT writing %d bytes to terminal", (int)len);
384 return;
385 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200386 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200387 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200388
389 /* TODO: only update once in a while. */
390 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200391 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200392}
393
394/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200395 * Send a mouse position and click to the vterm
396 */
397 static int
398term_send_mouse(VTerm *vterm, int button, int pressed)
399{
400 VTermModifier mod = VTERM_MOD_NONE;
401
402 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
403 mouse_col - W_WINCOL(curwin), mod);
404 vterm_mouse_button(vterm, button, pressed, mod);
405 return TRUE;
406}
407
408/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200409 * Convert typed key "c" into bytes to send to the job.
410 * Return the number of bytes in "buf".
411 */
412 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200413term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200414{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200415 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200416 VTermKey key = VTERM_KEY_NONE;
417 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200418 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200419
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200420 switch (c)
421 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200422 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200423 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200424 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
425 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200426 case K_DEL: key = VTERM_KEY_DEL; break;
427 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200428 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
429 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200430 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200431 case K_S_END: mod = VTERM_MOD_SHIFT;
432 key = VTERM_KEY_END; break;
433 case K_C_END: mod = VTERM_MOD_CTRL;
434 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200435 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
436 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
437 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
438 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
439 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
440 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
441 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
442 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
443 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
444 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
445 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
446 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
447 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200448 case K_S_HOME: mod = VTERM_MOD_SHIFT;
449 key = VTERM_KEY_HOME; break;
450 case K_C_HOME: mod = VTERM_MOD_CTRL;
451 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200452 case K_INS: key = VTERM_KEY_INS; break;
453 case K_K0: key = VTERM_KEY_KP_0; break;
454 case K_K1: key = VTERM_KEY_KP_1; break;
455 case K_K2: key = VTERM_KEY_KP_2; break;
456 case K_K3: key = VTERM_KEY_KP_3; break;
457 case K_K4: key = VTERM_KEY_KP_4; break;
458 case K_K5: key = VTERM_KEY_KP_5; break;
459 case K_K6: key = VTERM_KEY_KP_6; break;
460 case K_K7: key = VTERM_KEY_KP_7; break;
461 case K_K8: key = VTERM_KEY_KP_8; break;
462 case K_K9: key = VTERM_KEY_KP_9; break;
463 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
464 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
465 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
466 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
467 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
468 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
469 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
470 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
471 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
472 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
473 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
474 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
475 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200476 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
477 key = VTERM_KEY_LEFT; break;
478 case K_C_LEFT: mod = VTERM_MOD_CTRL;
479 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200480 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
481 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
482 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200483 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
484 key = VTERM_KEY_RIGHT; break;
485 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
486 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200487 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200488 case K_S_UP: mod = VTERM_MOD_SHIFT;
489 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200490 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200491
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200492 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
493 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
494 case K_MOUSELEFT: /* TODO */ return 0;
495 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200496
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200497 case K_LEFTMOUSE:
498 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
499 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
500 case K_LEFTRELEASE:
501 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
502 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
503 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
504 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
505 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
506 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
507 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
508 case K_X1MOUSE: /* TODO */ return 0;
509 case K_X1DRAG: /* TODO */ return 0;
510 case K_X1RELEASE: /* TODO */ return 0;
511 case K_X2MOUSE: /* TODO */ return 0;
512 case K_X2DRAG: /* TODO */ return 0;
513 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200514
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200515 case K_IGNORE: return 0;
516 case K_NOP: return 0;
517 case K_UNDO: return 0;
518 case K_HELP: return 0;
519 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
520 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
521 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
522 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
523 case K_SELECT: return 0;
524#ifdef FEAT_GUI
525 case K_VER_SCROLLBAR: return 0;
526 case K_HOR_SCROLLBAR: return 0;
527#endif
528#ifdef FEAT_GUI_TABLINE
529 case K_TABLINE: return 0;
530 case K_TABMENU: return 0;
531#endif
532#ifdef FEAT_NETBEANS_INTG
533 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
534#endif
535#ifdef FEAT_DND
536 case K_DROP: return 0;
537#endif
538#ifdef FEAT_AUTOCMD
539 case K_CURSORHOLD: return 0;
540#endif
541 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
542 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200543 }
544
545 /*
546 * Convert special keys to vterm keys:
547 * - Write keys to vterm: vterm_keyboard_key()
548 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200549 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200550 */
551 if (key != VTERM_KEY_NONE)
552 /* Special key, let vterm convert it. */
553 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200554 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200555 /* Normal character, let vterm convert it. */
556 vterm_keyboard_unichar(vterm, c, mod);
557
558 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200559 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200560}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200561
Bram Moolenaar938783d2017-07-16 20:13:26 +0200562/*
Bram Moolenaard85f2712017-07-28 21:51:57 +0200563 * Return TRUE if the job for "buf" is still running.
564 */
565 static int
566term_job_running(term_T *term)
567{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200568 /* Also consider the job finished when the channel is closed, to avoid a
569 * race condition when updating the title. */
570 return term->tl_job != NULL
571 && term->tl_job->jv_status == JOB_STARTED
572 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200573}
574
575/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200576 * Add the last line of the scrollback buffer to the buffer in the window.
577 */
578 static void
579add_scrollback_line_to_buffer(term_T *term)
580{
581 linenr_T lnum = term->tl_scrollback.ga_len - 1;
582 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
583 garray_T ga;
584 int c;
585 int col;
586 int i;
587
588 ga_init2(&ga, 1, 100);
589 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
590 {
591 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
592 goto failed;
593 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
594 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
595 (char_u *)ga.ga_data + ga.ga_len);
596 }
597 if (ga_grow(&ga, 1) == FAIL)
598 goto failed;
599 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
600 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
601
602 if (lnum == 0)
603 {
604 /* Delete the empty line that was in the empty buffer. */
605 curbuf = term->tl_buffer;
606 ml_delete(2, FALSE);
607 curbuf = curwin->w_buffer;
608 }
609
610failed:
611 ga_clear(&ga);
612}
613
614/*
615 * Add the current lines of the terminal to scrollback and to the buffer.
616 * Called after the job has ended and when switching to Terminal mode.
617 */
618 static void
619move_terminal_to_buffer(term_T *term)
620{
621 win_T *wp;
622 int len;
623 int lines_skipped = 0;
624 VTermPos pos;
625 VTermScreenCell cell;
626 VTermScreenCell *p;
627 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
628
629 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
630 {
631 len = 0;
632 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
633 if (vterm_screen_get_cell(screen, pos, &cell) != 0
634 && cell.chars[0] != NUL)
635 len = pos.col + 1;
636
637 if (len == 0)
638 ++lines_skipped;
639 else
640 {
641 while (lines_skipped > 0)
642 {
643 /* Line was skipped, add an empty line. */
644 --lines_skipped;
645 if (ga_grow(&term->tl_scrollback, 1) == OK)
646 {
647 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
648 + term->tl_scrollback.ga_len;
649
650 line->sb_cols = 0;
651 line->sb_cells = NULL;
652 ++term->tl_scrollback.ga_len;
653
654 add_scrollback_line_to_buffer(term);
655 }
656 }
657
658 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
659 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
660 {
661 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
662 + term->tl_scrollback.ga_len;
663
664 for (pos.col = 0; pos.col < len; ++pos.col)
665 {
666 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
667 vim_memset(p + pos.col, 0, sizeof(cell));
668 else
669 p[pos.col] = cell;
670 }
671 line->sb_cols = len;
672 line->sb_cells = p;
673 ++term->tl_scrollback.ga_len;
674
675 add_scrollback_line_to_buffer(term);
676 }
677 else
678 vim_free(p);
679 }
680 }
681
682 FOR_ALL_WINDOWS(wp)
683 {
684 if (wp->w_buffer == term->tl_buffer)
685 {
686 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
687 wp->w_cursor.col = 0;
688 wp->w_valid = 0;
689 redraw_win_later(wp, NOT_VALID);
690 }
691 }
692}
693
694 static void
695set_terminal_mode(term_T *term, int on)
696{
697 term->tl_terminal_mode = on;
698 vim_free(term->tl_status_text);
699 term->tl_status_text = NULL;
700 if (term->tl_buffer == curbuf)
701 maketitle();
702}
703
704/*
705 * Called after the job if finished and Terminal mode is not active:
706 * Move the vterm contents into the scrollback buffer and free the vterm.
707 */
708 static void
709cleanup_vterm(term_T *term)
710{
711 move_terminal_to_buffer(term);
712 term_free_vterm(term);
713 set_terminal_mode(term, FALSE);
714}
715
716/*
717 * Switch from sending keys to the job to Terminal-Normal mode.
718 * Suspends updating the terminal window.
719 */
720 static void
721term_enter_terminal_mode()
722{
723 term_T *term = curbuf->b_term;
724
725 /* Append the current terminal contents to the buffer. */
726 move_terminal_to_buffer(term);
727
728 set_terminal_mode(term, TRUE);
729}
730
731/*
732 * Returns TRUE if the current window contains a terminal and we are in
733 * Terminal-Normal mode.
734 */
735 int
736term_in_terminal_mode()
737{
738 term_T *term = curbuf->b_term;
739
740 return term != NULL && term->tl_terminal_mode;
741}
742
743/*
744 * Switch from Terminal-Normal mode to sending keys to the job.
745 * Restores updating the terminal window.
746 */
747 void
748term_leave_terminal_mode()
749{
750 term_T *term = curbuf->b_term;
751 sb_line_T *line;
752 garray_T *gap;
753
754 /* Remove the terminal contents from the scrollback and the buffer. */
755 gap = &term->tl_scrollback;
756 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
757 {
758 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
759 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
760 vim_free(line->sb_cells);
761 --gap->ga_len;
762 if (gap->ga_len == 0)
763 break;
764 }
765 check_cursor();
766
767 set_terminal_mode(term, FALSE);
768
769 if (term->tl_channel_closed)
770 cleanup_vterm(term);
771 redraw_buf_and_status_later(curbuf, NOT_VALID);
772}
773
774/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200775 * Get a key from the user without mapping.
776 * TODO: use terminal mode mappings.
777 */
778 static int
779term_vgetc()
780{
781 int c;
782
783 ++no_mapping;
784 ++allow_keys;
785 got_int = FALSE;
786 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200787 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200788 --no_mapping;
789 --allow_keys;
790 return c;
791}
792
793/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200794 * Send keys to terminal.
795 */
796 static int
797send_keys_to_term(term_T *term, int c, int typed)
798{
799 char msg[KEY_BUF_LEN];
800 size_t len;
801 static int mouse_was_outside = FALSE;
802 int dragging_outside = FALSE;
803
804 /* Catch keys that need to be handled as in Normal mode. */
805 switch (c)
806 {
807 case NUL:
808 case K_ZERO:
809 if (typed)
810 stuffcharReadbuff(c);
811 return FAIL;
812
813 case K_IGNORE:
814 return FAIL;
815
816 case K_LEFTDRAG:
817 case K_MIDDLEDRAG:
818 case K_RIGHTDRAG:
819 case K_X1DRAG:
820 case K_X2DRAG:
821 dragging_outside = mouse_was_outside;
822 /* FALLTHROUGH */
823 case K_LEFTMOUSE:
824 case K_LEFTMOUSE_NM:
825 case K_LEFTRELEASE:
826 case K_LEFTRELEASE_NM:
827 case K_MIDDLEMOUSE:
828 case K_MIDDLERELEASE:
829 case K_RIGHTMOUSE:
830 case K_RIGHTRELEASE:
831 case K_X1MOUSE:
832 case K_X1RELEASE:
833 case K_X2MOUSE:
834 case K_X2RELEASE:
835 if (mouse_row < W_WINROW(curwin)
836 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
837 || mouse_col < W_WINCOL(curwin)
838 || mouse_col >= W_ENDCOL(curwin)
839 || dragging_outside)
840 {
841 /* click outside the current window */
842 if (typed)
843 {
844 stuffcharReadbuff(c);
845 mouse_was_outside = TRUE;
846 }
847 return FAIL;
848 }
849 }
850 if (typed)
851 mouse_was_outside = FALSE;
852
853 /* Convert the typed key to a sequence of bytes for the job. */
854 len = term_convert_key(term, c, msg);
855 if (len > 0)
856 /* TODO: if FAIL is returned, stop? */
857 channel_send(term->tl_job->jv_channel, PART_IN,
858 (char_u *)msg, (int)len, NULL);
859
860 return OK;
861}
862
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200863 static void
864position_cursor(win_T *wp, VTermPos *pos)
865{
866 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
867 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
868 wp->w_valid |= (VALID_WCOL|VALID_WROW);
869}
870
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200871/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200872 * Returns TRUE if the current window contains a terminal and we are sending
873 * keys to the job.
874 */
875 int
876term_use_loop()
877{
878 term_T *term = curbuf->b_term;
879
880 return term != NULL
881 && !term->tl_terminal_mode
882 && term->tl_vterm != NULL
883 && term_job_running(term);
884}
885
886/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200887 * Wait for input and send it to the job.
888 * Return when the start of a CTRL-W command is typed or anything else that
889 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200890 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
891 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200892 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200893 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200894terminal_loop(void)
895{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200896 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200897 int termkey = 0;
898
899 if (*curwin->w_p_tk != NUL)
900 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200901 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200902
903 for (;;)
904 {
905 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200906 /* Repeat redrawing in case a message is received while redrawing. */
907 while (curwin->w_redr_type != 0)
908 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200909 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200910
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200911 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200912 if (curbuf->b_term->tl_vterm == NULL
913 || !term_job_running(curbuf->b_term))
914 /* job finished while waiting for a character */
915 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200916
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200917 if (c == (termkey == 0 ? Ctrl_W : termkey))
918 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200919#ifdef FEAT_CMDL_INFO
920 if (add_to_showcmd(c))
921 out_flush();
922#endif
923 c = term_vgetc();
924#ifdef FEAT_CMDL_INFO
925 clear_showcmd();
926#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200927 if (curbuf->b_term->tl_vterm == NULL
928 || !term_job_running(curbuf->b_term))
929 /* job finished while waiting for a character */
930 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200931
932 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200933 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200934 /* "CTRL-W .": send CTRL-W to the job */
935 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200936 }
937 else if (termkey == 0 && c == 'N')
938 {
939 term_enter_terminal_mode();
940 return FAIL;
941 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200942 else if (termkey == 0 || c != termkey)
943 {
944 stuffcharReadbuff(Ctrl_W);
945 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200946 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200947 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200948 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200949 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
950 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200951 }
Bram Moolenaard85f2712017-07-28 21:51:57 +0200952 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200953}
954
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200955/*
956 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200957 * This updates the title and status, but does not close the vter, because
958 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200959 */
960 void
961term_job_ended(job_T *job)
962{
963 term_T *term;
964 int did_one = FALSE;
965
966 for (term = first_term; term != NULL; term = term->tl_next)
967 if (term->tl_job == job)
968 {
969 vim_free(term->tl_title);
970 term->tl_title = NULL;
971 vim_free(term->tl_status_text);
972 term->tl_status_text = NULL;
973 redraw_buf_and_status_later(term->tl_buffer, VALID);
974 did_one = TRUE;
975 }
976 if (did_one)
977 redraw_statuslines();
978 if (curbuf->b_term != NULL)
979 {
980 if (curbuf->b_term->tl_job == job)
981 maketitle();
982 update_cursor(curbuf->b_term, TRUE);
983 }
984}
985
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200986 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200987may_toggle_cursor(term_T *term)
988{
989 if (curbuf == term->tl_buffer)
990 {
991 if (term->tl_cursor_visible)
992 cursor_on();
993 else
994 cursor_off();
995 }
996}
997
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200998 static int
999handle_damage(VTermRect rect, void *user)
1000{
1001 term_T *term = (term_T *)user;
1002
1003 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1004 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1005 redraw_buf_later(term->tl_buffer, NOT_VALID);
1006 return 1;
1007}
1008
1009 static int
1010handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1011{
1012 term_T *term = (term_T *)user;
1013
1014 /* TODO */
1015 redraw_buf_later(term->tl_buffer, NOT_VALID);
1016 return 1;
1017}
1018
1019 static int
1020handle_movecursor(
1021 VTermPos pos,
1022 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001023 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001024 void *user)
1025{
1026 term_T *term = (term_T *)user;
1027 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001028
1029 term->tl_cursor_pos = pos;
1030 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001031
1032 FOR_ALL_WINDOWS(wp)
1033 {
1034 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001035 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001036 }
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001037 if (term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001038 {
1039 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001040 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001041 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001042
1043 return 1;
1044}
1045
Bram Moolenaar21554412017-07-24 21:44:43 +02001046 static int
1047handle_settermprop(
1048 VTermProp prop,
1049 VTermValue *value,
1050 void *user)
1051{
1052 term_T *term = (term_T *)user;
1053
1054 switch (prop)
1055 {
1056 case VTERM_PROP_TITLE:
1057 vim_free(term->tl_title);
1058 term->tl_title = vim_strsave((char_u *)value->string);
1059 vim_free(term->tl_status_text);
1060 term->tl_status_text = NULL;
1061 if (term == curbuf->b_term)
1062 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001063 break;
1064
1065 case VTERM_PROP_CURSORVISIBLE:
1066 term->tl_cursor_visible = value->boolean;
1067 may_toggle_cursor(term);
1068 out_flush();
1069 break;
1070
Bram Moolenaar21554412017-07-24 21:44:43 +02001071 default:
1072 break;
1073 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001074 /* Always return 1, otherwise vterm doesn't store the value internally. */
1075 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001076}
1077
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001078/*
1079 * The job running in the terminal resized the terminal.
1080 */
1081 static int
1082handle_resize(int rows, int cols, void *user)
1083{
1084 term_T *term = (term_T *)user;
1085 win_T *wp;
1086
1087 term->tl_rows = rows;
1088 term->tl_cols = cols;
1089 FOR_ALL_WINDOWS(wp)
1090 {
1091 if (wp->w_buffer == term->tl_buffer)
1092 {
1093 win_setheight_win(rows, wp);
1094 win_setwidth_win(cols, wp);
1095 }
1096 }
1097
1098 redraw_buf_later(term->tl_buffer, NOT_VALID);
1099 return 1;
1100}
1101
Bram Moolenaard85f2712017-07-28 21:51:57 +02001102/*
1103 * Handle a line that is pushed off the top of the screen.
1104 */
1105 static int
1106handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1107{
1108 term_T *term = (term_T *)user;
1109
1110 /* TODO: Limit the number of lines that are stored. */
1111 /* TODO: put the text in the buffer. */
1112 if (ga_grow(&term->tl_scrollback, 1) == OK)
1113 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001114 VTermScreenCell *p = NULL;
1115 int len = 0;
1116 int i;
1117 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001118
1119 /* do not store empty cells at the end */
1120 for (i = 0; i < cols; ++i)
1121 if (cells[i].chars[0] != 0)
1122 len = i + 1;
1123
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001124 if (len > 0)
1125 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001126 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001127 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001128
1129 line = (sb_line_T *)term->tl_scrollback.ga_data
1130 + term->tl_scrollback.ga_len;
1131 line->sb_cols = len;
1132 line->sb_cells = p;
1133 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001134 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001135
1136 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001137 }
1138 return 0; /* ignored */
1139}
1140
Bram Moolenaar21554412017-07-24 21:44:43 +02001141static VTermScreenCallbacks screen_callbacks = {
1142 handle_damage, /* damage */
1143 handle_moverect, /* moverect */
1144 handle_movecursor, /* movecursor */
1145 handle_settermprop, /* settermprop */
1146 NULL, /* bell */
1147 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001148 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001149 NULL /* sb_popline */
1150};
1151
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001152/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001153 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001154 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001155 */
1156 void
1157term_channel_closed(channel_T *ch)
1158{
1159 term_T *term;
1160 int did_one = FALSE;
1161
1162 for (term = first_term; term != NULL; term = term->tl_next)
1163 if (term->tl_job == ch->ch_job)
1164 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001165 term->tl_channel_closed = TRUE;
1166
Bram Moolenaard85f2712017-07-28 21:51:57 +02001167 vim_free(term->tl_title);
1168 term->tl_title = NULL;
1169 vim_free(term->tl_status_text);
1170 term->tl_status_text = NULL;
1171
Bram Moolenaar423802d2017-07-30 16:52:24 +02001172 /* Unless in Terminal-Normal mode: clear the vterm. */
1173 if (!term->tl_terminal_mode)
1174 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001175
1176 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1177 did_one = TRUE;
1178 }
1179 if (did_one)
1180 {
1181 redraw_statuslines();
1182
1183 /* Need to break out of vgetc(). */
1184 ins_char_typebuf(K_IGNORE);
1185
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001186 term = curbuf->b_term;
1187 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001188 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001189 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001190 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001191 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001192 }
1193 }
1194}
1195
1196/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001197 * Reverse engineer the RGB value into a cterm color index.
1198 * First color is 1. Return 0 if no match found.
1199 */
1200 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001201color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001202{
1203 int red = color->red;
1204 int blue = color->blue;
1205 int green = color->green;
1206
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001207 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001208 if (red == 0)
1209 {
1210 if (green == 0)
1211 {
1212 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001213 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001214 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001215 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001216 }
1217 else if (green == 224)
1218 {
1219 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001220 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001221 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001222 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001223 }
1224 }
1225 else if (red == 224)
1226 {
1227 if (green == 0)
1228 {
1229 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001230 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001231 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001232 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001233 }
1234 else if (green == 224)
1235 {
1236 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001237 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001238 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001239 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001240 }
1241 }
1242 else if (red == 128)
1243 {
1244 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001245 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001246 }
1247 else if (red == 255)
1248 {
1249 if (green == 64)
1250 {
1251 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001252 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001253 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001254 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001255 }
1256 else if (green == 255)
1257 {
1258 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001259 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001260 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001261 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001262 }
1263 }
1264 else if (red == 64)
1265 {
1266 if (green == 64)
1267 {
1268 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001269 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001270 }
1271 else if (green == 255)
1272 {
1273 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001274 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001275 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001276 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001277 }
1278 }
1279 if (t_colors >= 256)
1280 {
1281 if (red == blue && red == green)
1282 {
1283 /* 24-color greyscale */
1284 static int cutoff[23] = {
1285 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1286 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1287 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1288 int i;
1289
1290 for (i = 0; i < 23; ++i)
1291 if (red < cutoff[i])
1292 return i + 233;
1293 return 256;
1294 }
1295
1296 /* 216-color cube */
1297 return 17 + ((red + 25) / 0x33) * 36
1298 + ((green + 25) / 0x33) * 6
1299 + (blue + 25) / 0x33;
1300 }
1301 return 0;
1302}
1303
1304/*
1305 * Convert the attributes of a vterm cell into an attribute index.
1306 */
1307 static int
1308cell2attr(VTermScreenCell *cell)
1309{
1310 int attr = 0;
1311
1312 if (cell->attrs.bold)
1313 attr |= HL_BOLD;
1314 if (cell->attrs.underline)
1315 attr |= HL_UNDERLINE;
1316 if (cell->attrs.italic)
1317 attr |= HL_ITALIC;
1318 if (cell->attrs.strike)
1319 attr |= HL_STANDOUT;
1320 if (cell->attrs.reverse)
1321 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001322
1323#ifdef FEAT_GUI
1324 if (gui.in_use)
1325 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001326 guicolor_T fg, bg;
1327
1328 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1329 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1330 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001331 }
1332 else
1333#endif
1334#ifdef FEAT_TERMGUICOLORS
1335 if (p_tgc)
1336 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001337 guicolor_T fg, bg;
1338
1339 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1340 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1341
1342 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001343 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001344 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001345#endif
1346 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001347 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1348 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001349 }
1350 return 0;
1351}
1352
1353/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001354 * Called to update the window that contains a terminal.
1355 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001356 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001357 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001358term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001359{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001360 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001361 VTerm *vterm;
1362 VTermScreen *screen;
1363 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001364 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001365
Bram Moolenaar423802d2017-07-30 16:52:24 +02001366 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001367 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001368
Bram Moolenaard85f2712017-07-28 21:51:57 +02001369 vterm = term->tl_vterm;
1370 screen = vterm_obtain_screen(vterm);
1371 state = vterm_obtain_state(vterm);
1372
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001373 /*
1374 * If the window was resized a redraw will be triggered and we get here.
1375 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1376 */
1377 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1378 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001379 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001380 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1381 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1382 win_T *twp;
1383
1384 FOR_ALL_WINDOWS(twp)
1385 {
1386 /* When more than one window shows the same terminal, use the
1387 * smallest size. */
1388 if (twp->w_buffer == term->tl_buffer)
1389 {
1390 if (!term->tl_rows_fixed && rows > twp->w_height)
1391 rows = twp->w_height;
1392 if (!term->tl_cols_fixed && cols > twp->w_width)
1393 cols = twp->w_width;
1394 }
1395 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001396
1397 vterm_set_size(vterm, rows, cols);
1398 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1399 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001400 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001401 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001402
1403 /* The cursor may have been moved when resizing. */
1404 vterm_state_get_cursorpos(state, &pos);
1405 position_cursor(wp, &pos);
1406
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001407 /* TODO: Only redraw what changed. */
1408 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001409 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001410 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001411 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001412
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001413 if (pos.row < term->tl_rows)
1414 {
1415 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001416 {
1417 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001418 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001419
Bram Moolenaareeac6772017-07-23 15:48:37 +02001420 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1421 vim_memset(&cell, 0, sizeof(cell));
1422
1423 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001424 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001425 if (c == NUL)
1426 {
1427 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001428#if defined(FEAT_MBYTE)
1429 if (enc_utf8)
1430 ScreenLinesUC[off] = NUL;
1431#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001432 }
1433 else
1434 {
1435#if defined(FEAT_MBYTE)
1436 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001437 {
1438 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001439 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001440 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001441 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001442 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001443 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001444 if (enc_utf8)
1445 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001446 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001447#else
1448 ScreenLines[off] = c;
1449#endif
1450 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001451 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001452
1453 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001454 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001455 if (cell.width == 2)
1456 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001457 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001458#if defined(FEAT_MBYTE)
1459 if (enc_utf8)
1460 ScreenLinesUC[off] = NUL;
1461#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001462 ++pos.col;
1463 ++off;
1464 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001465 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001466 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001467 else
1468 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001469
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001470 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1471 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001472 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001473
1474 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001475}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001476
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001477/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001478 * Return TRUE if "wp" is a terminal window where the job has finished.
1479 */
1480 int
1481term_is_finished(buf_T *buf)
1482{
1483 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1484}
1485
1486/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001487 * Return TRUE if "wp" is a terminal window where the job has finished or we
1488 * are in Terminal-Normal mode.
1489 */
1490 int
1491term_show_buffer(buf_T *buf)
1492{
1493 term_T *term = buf->b_term;
1494
1495 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1496}
1497
1498/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001499 * The current buffer is going to be changed. If there is terminal
1500 * highlighting remove it now.
1501 */
1502 void
1503term_change_in_curbuf(void)
1504{
1505 term_T *term = curbuf->b_term;
1506
1507 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1508 {
1509 free_scrollback(term);
1510 redraw_buf_later(term->tl_buffer, NOT_VALID);
1511 }
1512}
1513
1514/*
1515 * Get the screen attribute for a position in the buffer.
1516 */
1517 int
1518term_get_attr(buf_T *buf, linenr_T lnum, int col)
1519{
1520 term_T *term = buf->b_term;
1521 sb_line_T *line;
1522
Bram Moolenaar70229f92017-07-29 16:01:53 +02001523 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001524 return 0;
1525 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1526 if (col >= line->sb_cols)
1527 return 0;
1528 return cell2attr(line->sb_cells + col);
1529}
1530
1531/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001532 * Set job options common for Unix and MS-Windows.
1533 */
1534 static void
1535setup_job_options(jobopt_T *opt, int rows, int cols)
1536{
1537 clear_job_options(opt);
1538 opt->jo_mode = MODE_RAW;
1539 opt->jo_out_mode = MODE_RAW;
1540 opt->jo_err_mode = MODE_RAW;
1541 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001542
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001543 opt->jo_io[PART_OUT] = JIO_BUFFER;
1544 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001545 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1546
1547 opt->jo_modifiable[PART_OUT] = 0;
1548 opt->jo_modifiable[PART_ERR] = 0;
1549 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1550
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001551 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1552 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001553 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001554 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1555
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001556 opt->jo_term_rows = rows;
1557 opt->jo_term_cols = cols;
1558}
1559
1560/*
1561 * Create a new vterm and initialize it.
1562 */
1563 static void
1564create_vterm(term_T *term, int rows, int cols)
1565{
1566 VTerm *vterm;
1567 VTermScreen *screen;
1568
1569 vterm = vterm_new(rows, cols);
1570 term->tl_vterm = vterm;
1571 screen = vterm_obtain_screen(vterm);
1572 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1573 /* TODO: depends on 'encoding'. */
1574 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001575
1576 /* Vterm uses a default black background. Set it to white when
1577 * 'background' is "light". */
1578 if (*p_bg == 'l')
1579 {
1580 VTermColor fg, bg;
1581
1582 fg.red = fg.green = fg.blue = 0;
1583 bg.red = bg.green = bg.blue = 255;
1584 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1585 }
1586
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001587 /* Required to initialize most things. */
1588 vterm_screen_reset(screen, 1 /* hard */);
1589}
1590
Bram Moolenaar21554412017-07-24 21:44:43 +02001591/*
1592 * Return the text to show for the buffer name and status.
1593 */
1594 char_u *
1595term_get_status_text(term_T *term)
1596{
1597 if (term->tl_status_text == NULL)
1598 {
1599 char_u *txt;
1600 size_t len;
1601
Bram Moolenaar423802d2017-07-30 16:52:24 +02001602 if (term->tl_terminal_mode)
1603 {
1604 if (term_job_running(term))
1605 txt = (char_u *)_("Terminal");
1606 else
1607 txt = (char_u *)_("Terminal-finished");
1608 }
1609 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001610 txt = term->tl_title;
1611 else if (term_job_running(term))
1612 txt = (char_u *)_("running");
1613 else
1614 txt = (char_u *)_("finished");
1615 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001616 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001617 if (term->tl_status_text != NULL)
1618 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1619 term->tl_buffer->b_fname, txt);
1620 }
1621 return term->tl_status_text;
1622}
1623
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001624/*
1625 * Mark references in jobs of terminals.
1626 */
1627 int
1628set_ref_in_term(int copyID)
1629{
1630 int abort = FALSE;
1631 term_T *term;
1632 typval_T tv;
1633
1634 for (term = first_term; term != NULL; term = term->tl_next)
1635 if (term->tl_job != NULL)
1636 {
1637 tv.v_type = VAR_JOB;
1638 tv.vval.v_job = term->tl_job;
1639 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1640 }
1641 return abort;
1642}
1643
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001644/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001645 * Get the buffer from the first argument in "argvars".
1646 * Returns NULL when the buffer is not for a terminal window.
1647 */
1648 static buf_T *
1649term_get_buf(typval_T *argvars)
1650{
1651 buf_T *buf;
1652
1653 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1654 ++emsg_off;
1655 buf = get_buf_tv(&argvars[0], FALSE);
1656 --emsg_off;
1657 if (buf == NULL || buf->b_term == NULL)
1658 return NULL;
1659 return buf;
1660}
1661
1662/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001663 * "term_getattr(attr, name)" function
1664 */
1665 void
1666f_term_getattr(typval_T *argvars, typval_T *rettv)
1667{
1668 int attr;
1669 size_t i;
1670 char_u *name;
1671
1672 static struct {
1673 char *name;
1674 int attr;
1675 } attrs[] = {
1676 {"bold", HL_BOLD},
1677 {"italic", HL_ITALIC},
1678 {"underline", HL_UNDERLINE},
1679 {"strike", HL_STANDOUT},
1680 {"reverse", HL_INVERSE},
1681 };
1682
1683 attr = get_tv_number(&argvars[0]);
1684 name = get_tv_string_chk(&argvars[1]);
1685 if (name == NULL)
1686 return;
1687
1688 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1689 if (STRCMP(name, attrs[i].name) == 0)
1690 {
1691 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1692 break;
1693 }
1694}
1695
1696/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001697 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001698 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001699 void
1700f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001701{
Bram Moolenaar97870002017-07-30 18:28:38 +02001702 buf_T *buf = term_get_buf(argvars);
1703 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001704
Bram Moolenaar97870002017-07-30 18:28:38 +02001705 if (rettv_list_alloc(rettv) == FAIL)
1706 return;
1707 if (buf == NULL)
1708 return;
1709
1710 l = rettv->vval.v_list;
1711 list_append_number(l, buf->b_term->tl_cursor_pos.row);
1712 list_append_number(l, buf->b_term->tl_cursor_pos.col);
1713 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001714}
1715
1716/*
1717 * "term_getjob(buf)" function
1718 */
1719 void
1720f_term_getjob(typval_T *argvars, typval_T *rettv)
1721{
1722 buf_T *buf = term_get_buf(argvars);
1723
1724 rettv->v_type = VAR_JOB;
1725 rettv->vval.v_job = NULL;
1726 if (buf == NULL)
1727 return;
1728
1729 rettv->vval.v_job = buf->b_term->tl_job;
1730 if (rettv->vval.v_job != NULL)
1731 ++rettv->vval.v_job->jv_refcount;
1732}
1733
1734/*
1735 * "term_getline(buf, row)" function
1736 */
1737 void
1738f_term_getline(typval_T *argvars, typval_T *rettv)
1739{
1740 buf_T *buf = term_get_buf(argvars);
1741 term_T *term;
1742 int row;
1743
1744 rettv->v_type = VAR_STRING;
1745 if (buf == NULL)
1746 return;
1747 term = buf->b_term;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001748 if (argvars[1].v_type == VAR_UNKNOWN)
1749 row = term->tl_cursor_pos.row;
1750 else
1751 row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001752
1753 if (term->tl_vterm == NULL)
1754 {
1755 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1756
1757 /* vterm is finished, get the text from the buffer */
1758 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1759 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1760 }
1761 else
1762 {
1763 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1764 VTermRect rect;
1765 int len;
1766 char_u *p;
1767
1768 len = term->tl_cols * MB_MAXBYTES + 1;
1769 p = alloc(len);
1770 if (p == NULL)
1771 return;
1772 rettv->vval.v_string = p;
1773
1774 rect.start_col = 0;
1775 rect.end_col = term->tl_cols;
1776 rect.start_row = row;
1777 rect.end_row = row + 1;
1778 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1779 }
1780}
1781
1782/*
1783 * "term_getsize(buf)" function
1784 */
1785 void
1786f_term_getsize(typval_T *argvars, typval_T *rettv)
1787{
1788 buf_T *buf = term_get_buf(argvars);
1789 list_T *l;
1790
1791 if (rettv_list_alloc(rettv) == FAIL)
1792 return;
1793 if (buf == NULL)
1794 return;
1795
1796 l = rettv->vval.v_list;
1797 list_append_number(l, buf->b_term->tl_rows);
1798 list_append_number(l, buf->b_term->tl_cols);
1799}
1800
1801/*
1802 * "term_list()" function
1803 */
1804 void
1805f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1806{
1807 term_T *tp;
1808 list_T *l;
1809
1810 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1811 return;
1812
1813 l = rettv->vval.v_list;
1814 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1815 if (tp != NULL && tp->tl_buffer != NULL)
1816 if (list_append_number(l,
1817 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1818 return;
1819}
1820
1821/*
1822 * "term_scrape(buf, row)" function
1823 */
1824 void
1825f_term_scrape(typval_T *argvars, typval_T *rettv)
1826{
1827 buf_T *buf = term_get_buf(argvars);
1828 VTermScreen *screen = NULL;
1829 VTermPos pos;
1830 list_T *l;
1831 term_T *term;
1832
1833 if (rettv_list_alloc(rettv) == FAIL)
1834 return;
1835 if (buf == NULL)
1836 return;
1837 term = buf->b_term;
1838 if (term->tl_vterm != NULL)
1839 screen = vterm_obtain_screen(term->tl_vterm);
1840
1841 l = rettv->vval.v_list;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001842 if (argvars[1].v_type == VAR_UNKNOWN)
1843 pos.row = term->tl_cursor_pos.row;
1844 else
1845 pos.row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001846 for (pos.col = 0; pos.col < term->tl_cols; )
1847 {
1848 dict_T *dcell;
1849 VTermScreenCell cell;
1850 char_u rgb[8];
1851 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1852 int off = 0;
1853 int i;
1854
1855 if (screen == NULL)
1856 {
1857 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1858 sb_line_T *line;
1859
1860 /* vterm has finished, get the cell from scrollback */
1861 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1862 break;
1863 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1864 if (pos.col >= line->sb_cols)
1865 break;
1866 cell = line->sb_cells[pos.col];
1867 }
1868 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1869 break;
1870 dcell = dict_alloc();
1871 list_append_dict(l, dcell);
1872
1873 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1874 {
1875 if (cell.chars[i] == 0)
1876 break;
1877 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1878 }
1879 mbs[off] = NUL;
1880 dict_add_nr_str(dcell, "chars", 0, mbs);
1881
1882 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1883 cell.fg.red, cell.fg.green, cell.fg.blue);
1884 dict_add_nr_str(dcell, "fg", 0, rgb);
1885 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1886 cell.bg.red, cell.bg.green, cell.bg.blue);
1887 dict_add_nr_str(dcell, "bg", 0, rgb);
1888
1889 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
1890 dict_add_nr_str(dcell, "width", cell.width, NULL);
1891
1892 ++pos.col;
1893 if (cell.width == 2)
1894 ++pos.col;
1895 }
1896}
1897
1898/*
1899 * "term_sendkeys(buf, keys)" function
1900 */
1901 void
1902f_term_sendkeys(typval_T *argvars, typval_T *rettv)
1903{
1904 buf_T *buf = term_get_buf(argvars);
1905 char_u *msg;
1906 term_T *term;
1907
1908 rettv->v_type = VAR_UNKNOWN;
1909 if (buf == NULL)
1910 return;
1911
1912 msg = get_tv_string_chk(&argvars[1]);
1913 if (msg == NULL)
1914 return;
1915 term = buf->b_term;
1916 if (term->tl_vterm == NULL)
1917 return;
1918
1919 while (*msg != NUL)
1920 {
1921 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
1922 msg += MB_PTR2LEN(msg);
1923 }
1924
1925 /* TODO: only update once in a while. */
1926 update_screen(0);
1927 if (buf == curbuf)
1928 update_cursor(term, TRUE);
1929}
1930
1931/*
1932 * "term_start(command, options)" function
1933 */
1934 void
1935f_term_start(typval_T *argvars, typval_T *rettv)
1936{
1937 char_u *cmd = get_tv_string_chk(&argvars[0]);
1938 exarg_T ea;
1939
1940 if (cmd == NULL)
1941 return;
1942 ea.arg = cmd;
1943 ex_terminal(&ea);
1944
1945 if (curbuf->b_term != NULL)
1946 rettv->vval.v_number = curbuf->b_fnum;
1947}
1948
1949/*
1950 * "term_wait" function
1951 */
1952 void
1953f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
1954{
1955 buf_T *buf = term_get_buf(argvars);
1956
1957 if (buf == NULL)
1958 return;
1959
1960 /* Get the job status, this will detect a job that finished. */
1961 if (buf->b_term->tl_job != NULL)
1962 (void)job_status(buf->b_term->tl_job);
1963
1964 /* Check for any pending channel I/O. */
1965 vpeekc_any();
1966 ui_delay(10L, FALSE);
1967
1968 /* Flushing messages on channels is hopefully sufficient.
1969 * TODO: is there a better way? */
1970 parse_queued_messages();
1971}
1972
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001973# ifdef WIN3264
1974
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001975/**************************************
1976 * 2. MS-Windows implementation.
1977 */
1978
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001979#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1980#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1981
Bram Moolenaar8a773062017-07-24 22:29:21 +02001982void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001983void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001984void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001985BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1986void (*winpty_config_set_initial_size)(void*, int, int);
1987LPCWSTR (*winpty_conin_name)(void*);
1988LPCWSTR (*winpty_conout_name)(void*);
1989LPCWSTR (*winpty_conerr_name)(void*);
1990void (*winpty_free)(void*);
1991void (*winpty_config_free)(void*);
1992void (*winpty_spawn_config_free)(void*);
1993void (*winpty_error_free)(void*);
1994LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001995BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001996
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001997#define WINPTY_DLL "winpty.dll"
1998
1999static HINSTANCE hWinPtyDLL = NULL;
2000
2001 int
2002dyn_winpty_init(void)
2003{
2004 int i;
2005 static struct
2006 {
2007 char *name;
2008 FARPROC *ptr;
2009 } winpty_entry[] =
2010 {
2011 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2012 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2013 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2014 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2015 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2016 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2017 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2018 {"winpty_free", (FARPROC*)&winpty_free},
2019 {"winpty_open", (FARPROC*)&winpty_open},
2020 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2021 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2022 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2023 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002024 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002025 {NULL, NULL}
2026 };
2027
2028 /* No need to initialize twice. */
2029 if (hWinPtyDLL)
2030 return 1;
2031 /* Load winpty.dll */
2032 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2033 if (!hWinPtyDLL)
2034 {
2035 EMSG2(_(e_loadlib), WINPTY_DLL);
2036 return 0;
2037 }
2038 for (i = 0; winpty_entry[i].name != NULL
2039 && winpty_entry[i].ptr != NULL; ++i)
2040 {
2041 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2042 winpty_entry[i].name)) == NULL)
2043 {
2044 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2045 return 0;
2046 }
2047 }
2048
2049 return 1;
2050}
2051
2052/*
2053 * Create a new terminal of "rows" by "cols" cells.
2054 * Store a reference in "term".
2055 * Return OK or FAIL.
2056 */
2057 static int
2058term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2059{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002060 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002061 channel_T *channel = NULL;
2062 job_T *job = NULL;
2063 jobopt_T opt;
2064 DWORD error;
2065 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2066 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002067 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002068
2069 if (!dyn_winpty_init())
2070 return FAIL;
2071
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002072 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002073 if (p == NULL)
2074 return FAIL;
2075
2076 job = job_alloc();
2077 if (job == NULL)
2078 goto failed;
2079
2080 channel = add_channel();
2081 if (channel == NULL)
2082 goto failed;
2083
2084 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2085 if (term->tl_winpty_config == NULL)
2086 goto failed;
2087
2088 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2089 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2090 if (term->tl_winpty == NULL)
2091 goto failed;
2092
2093 spawn_config = winpty_spawn_config_new(
2094 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2095 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2096 NULL,
2097 p,
2098 NULL,
2099 NULL,
2100 &winpty_err);
2101 if (spawn_config == NULL)
2102 goto failed;
2103
2104 channel = add_channel();
2105 if (channel == NULL)
2106 goto failed;
2107
2108 job = job_alloc();
2109 if (job == NULL)
2110 goto failed;
2111
2112 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2113 &child_thread_handle, &error, &winpty_err))
2114 goto failed;
2115
2116 channel_set_pipes(channel,
2117 (sock_T) CreateFileW(
2118 winpty_conin_name(term->tl_winpty),
2119 GENERIC_WRITE, 0, NULL,
2120 OPEN_EXISTING, 0, NULL),
2121 (sock_T) CreateFileW(
2122 winpty_conout_name(term->tl_winpty),
2123 GENERIC_READ, 0, NULL,
2124 OPEN_EXISTING, 0, NULL),
2125 (sock_T) CreateFileW(
2126 winpty_conerr_name(term->tl_winpty),
2127 GENERIC_READ, 0, NULL,
2128 OPEN_EXISTING, 0, NULL));
2129
2130 jo = CreateJobObject(NULL, NULL);
2131 if (jo == NULL)
2132 goto failed;
2133
2134 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002135 {
2136 /* Failed, switch the way to terminate process with TerminateProcess. */
2137 CloseHandle(jo);
2138 jo = NULL;
2139 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002140
2141 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002142 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002143
2144 create_vterm(term, rows, cols);
2145
2146 setup_job_options(&opt, rows, cols);
2147 channel_set_job(channel, job, &opt);
2148
2149 job->jv_channel = channel;
2150 job->jv_proc_info.hProcess = child_process_handle;
2151 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2152 job->jv_job_object = jo;
2153 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002154 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002155 term->tl_job = job;
2156
2157 return OK;
2158
2159failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002160 if (spawn_config != NULL)
2161 winpty_spawn_config_free(spawn_config);
2162 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002163 if (channel != NULL)
2164 channel_clear(channel);
2165 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002166 {
2167 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002168 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002169 }
2170 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002171 if (jo != NULL)
2172 CloseHandle(jo);
2173 if (term->tl_winpty != NULL)
2174 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002175 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002176 if (term->tl_winpty_config != NULL)
2177 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002178 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002179 if (winpty_err != NULL)
2180 {
2181 char_u *msg = utf16_to_enc(
2182 (short_u *)winpty_error_msg(winpty_err), NULL);
2183
2184 EMSG(msg);
2185 winpty_error_free(winpty_err);
2186 }
2187 return FAIL;
2188}
2189
2190/*
2191 * Free the terminal emulator part of "term".
2192 */
2193 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002194term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002195{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002196 if (term->tl_winpty != NULL)
2197 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002198 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002199 if (term->tl_winpty_config != NULL)
2200 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002201 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002202 if (term->tl_vterm != NULL)
2203 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002204 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002205}
2206
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002207/*
2208 * Request size to terminal.
2209 */
2210 static void
2211term_report_winsize(term_T *term, int rows, int cols)
2212{
2213 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2214}
2215
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002216# else
2217
2218/**************************************
2219 * 3. Unix-like implementation.
2220 */
2221
2222/*
2223 * Create a new terminal of "rows" by "cols" cells.
2224 * Start job for "cmd".
2225 * Store the pointers in "term".
2226 * Return OK or FAIL.
2227 */
2228 static int
2229term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2230{
2231 typval_T argvars[2];
2232 jobopt_T opt;
2233
2234 create_vterm(term, rows, cols);
2235
2236 argvars[0].v_type = VAR_STRING;
2237 argvars[0].vval.v_string = cmd;
2238 setup_job_options(&opt, rows, cols);
2239 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002240 if (term->tl_job != NULL)
2241 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002242
Bram Moolenaar61a66052017-07-22 18:39:00 +02002243 return term->tl_job != NULL
2244 && term->tl_job->jv_channel != NULL
2245 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002246}
2247
2248/*
2249 * Free the terminal emulator part of "term".
2250 */
2251 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002252term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002253{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002254 if (term->tl_vterm != NULL)
2255 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002256 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002257}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002258
2259/*
2260 * Request size to terminal.
2261 */
2262 static void
2263term_report_winsize(term_T *term, int rows, int cols)
2264{
2265 /* Use an ioctl() to report the new window size to the job. */
2266 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2267 {
2268 int fd = -1;
2269 int part;
2270
2271 for (part = PART_OUT; part < PART_COUNT; ++part)
2272 {
2273 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2274 if (isatty(fd))
2275 break;
2276 }
2277 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2278 mch_stop_job(term->tl_job, (char_u *)"winch");
2279 }
2280}
2281
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002282# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002283
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002284#endif /* FEAT_TERMINAL */