blob: d02e7f12c4bb649ee4697f0a581440de7f2d2f16 [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 Moolenaar63ecdda2017-07-28 22:29:35 +020039 * - Patch for functions: Yasuhiro Matsumoto, #1871
Bram Moolenaard85f2712017-07-28 21:51:57 +020040 * - For the scrollback buffer store lines in the buffer, only attributes in
41 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020042 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020043 * - Need an option or argument to drop the window+buffer right away, to be
44 * used for a shell or Vim.
Bram Moolenaard85f2712017-07-28 21:51:57 +020045 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaard85f2712017-07-28 21:51:57 +020046 * - do not store terminal buffer in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - add a character in :ls output
Bram Moolenaar938783d2017-07-16 20:13:26 +020048 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaard85f2712017-07-28 21:51:57 +020049 * - when closing window and job has ended, make buffer hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020050 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020051 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020052 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020053 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020054 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020055 * - implement "term" for job_start(): more job options when starting a
56 * terminal.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020057 * - implement term_list() list of buffers with a terminal
58 * - implement term_getsize(buf)
59 * - implement term_setsize(buf)
60 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
61 * - implement term_wait(buf) wait for screen to be updated
62 * - implement term_scrape(buf, row) inspect terminal screen
63 * - implement term_open(command, options) open terminal window
64 * - implement term_getjob(buf)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
66 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020067 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020068 */
69
70#include "vim.h"
71
72#ifdef FEAT_TERMINAL
73
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020074#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020075# define MIN(x,y) (x < y ? x : y)
76# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020077#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079#include "libvterm/include/vterm.h"
80
Bram Moolenaard85f2712017-07-28 21:51:57 +020081typedef struct sb_line_S {
82 int sb_cols; /* can differ per line */
83 VTermScreenCell *sb_cells; /* allocated */
84} sb_line_T;
85
Bram Moolenaare4f25e42017-07-07 11:54:15 +020086/* typedef term_T in structs.h */
87struct terminal_S {
88 term_T *tl_next;
89
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020090#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020091 void *tl_winpty_config;
92 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020093#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020094 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020095 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020096 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020097
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020098 /* last known vterm size */
99 int tl_rows;
100 int tl_cols;
101 /* vterm size does not follow window size */
102 int tl_rows_fixed;
103 int tl_cols_fixed;
104
Bram Moolenaar21554412017-07-24 21:44:43 +0200105 char_u *tl_title; /* NULL or allocated */
106 char_u *tl_status_text; /* NULL or allocated */
107
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200108 /* Range of screen rows to update. Zero based. */
109 int tl_dirty_row_start; /* -1 if nothing dirty */
110 int tl_dirty_row_end; /* row below last one to update */
111
Bram Moolenaard85f2712017-07-28 21:51:57 +0200112 garray_T tl_scrollback;
113
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200114 pos_T tl_cursor;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200115 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200116};
117
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200118/*
119 * List of all active terminals.
120 */
121static term_T *first_term = NULL;
122
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200123
124#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
125#define KEY_BUF_LEN 200
126
127/*
128 * Functions with separate implementation for MS-Windows and Unix-like systems.
129 */
130static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200131static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200132static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200133
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200134/**************************************
135 * 1. Generic code for all systems.
136 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200137
138/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200139 * Determine the terminal size from 'termsize' and the current window.
140 * Assumes term->tl_rows and term->tl_cols are zero.
141 */
142 static void
143set_term_and_win_size(term_T *term)
144{
145 if (*curwin->w_p_tms != NUL)
146 {
147 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
148
149 term->tl_rows = atoi((char *)curwin->w_p_tms);
150 term->tl_cols = atoi((char *)p);
151 }
152 if (term->tl_rows == 0)
153 term->tl_rows = curwin->w_height;
154 else
155 {
156 win_setheight_win(term->tl_rows, curwin);
157 term->tl_rows_fixed = TRUE;
158 }
159 if (term->tl_cols == 0)
160 term->tl_cols = curwin->w_width;
161 else
162 {
163 win_setwidth_win(term->tl_cols, curwin);
164 term->tl_cols_fixed = TRUE;
165 }
166}
167
168/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200169 * ":terminal": open a terminal window and execute a job in it.
170 */
171 void
172ex_terminal(exarg_T *eap)
173{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200174 exarg_T split_ea;
175 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200176 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200177 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200178
179 if (check_restricted() || check_secure())
180 return;
181
182 term = (term_T *)alloc_clear(sizeof(term_T));
183 if (term == NULL)
184 return;
185 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200186 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200187 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200188
189 /* Open a new window or tab. */
190 vim_memset(&split_ea, 0, sizeof(split_ea));
191 split_ea.cmdidx = CMD_new;
192 split_ea.cmd = (char_u *)"new";
193 split_ea.arg = (char_u *)"";
194 ex_splitview(&split_ea);
195 if (curwin == old_curwin)
196 {
197 /* split failed */
198 vim_free(term);
199 return;
200 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200201 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200202 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200203
204 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200205 term->tl_next = first_term;
206 first_term = term;
207
Bram Moolenaar293424c2017-07-26 23:11:01 +0200208 if (cmd == NULL || *cmd == NUL)
209 cmd = p_sh;
210
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200211 if (buflist_findname(cmd) == NULL)
212 curbuf->b_ffname = vim_strsave(cmd);
213 else
214 {
215 int i;
216 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200217 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200218
219 for (i = 1; p != NULL; ++i)
220 {
221 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
222 if (buflist_findname(p) == NULL)
223 {
224 curbuf->b_ffname = p;
225 break;
226 }
227 }
228 }
229 curbuf->b_fname = curbuf->b_ffname;
230
231 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
232 curbuf->b_changed = TRUE;
233 curbuf->b_p_ma = FALSE;
234 set_string_option_direct((char_u *)"buftype", -1,
235 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200236
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200237 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200238
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200239 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200240 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200241 {
242 /* store the size we ended up with */
243 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200244 }
245 else
246 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200247 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200248
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200249 /* Wiping out the buffer will also close the window and call
250 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200251 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200252 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200253
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200254 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200255}
256
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200257/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200258 * Free the scrollback buffer for "term".
259 */
260 static void
261free_scrollback(term_T *term)
262{
263 int i;
264
265 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
266 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
267 ga_clear(&term->tl_scrollback);
268}
269
270/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200271 * Free a terminal and everything it refers to.
272 * Kills the job if there is one.
273 * Called when wiping out a buffer.
274 */
275 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200276free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200277{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200278 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200279 term_T *tp;
280
281 if (term == NULL)
282 return;
283 if (first_term == term)
284 first_term = term->tl_next;
285 else
286 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
287 if (tp->tl_next == term)
288 {
289 tp->tl_next = term->tl_next;
290 break;
291 }
292
293 if (term->tl_job != NULL)
294 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200295 if (term->tl_job->jv_status != JOB_ENDED
296 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200297 job_stop(term->tl_job, NULL, "kill");
298 job_unref(term->tl_job);
299 }
300
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200301 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200302
303 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200304 vim_free(term->tl_title);
305 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200306 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200307 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200308}
309
310/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200311 * Write job output "msg[len]" to the vterm.
312 */
313 static void
314term_write_job_output(term_T *term, char_u *msg, size_t len)
315{
316 VTerm *vterm = term->tl_vterm;
317 char_u *p;
318 size_t done;
319 size_t len_now;
320
321 for (done = 0; done < len; done += len_now)
322 {
323 for (p = msg + done; p < msg + len; )
324 {
325 if (*p == NL)
326 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200327 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200328 }
329 len_now = p - msg - done;
330 vterm_input_write(vterm, (char *)msg + done, len_now);
331 if (p < msg + len && *p == NL)
332 {
333 /* Convert NL to CR-NL, that appears to work best. */
334 vterm_input_write(vterm, "\r\n", 2);
335 ++len_now;
336 }
337 }
338
339 /* this invokes the damage callbacks */
340 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
341}
342
Bram Moolenaar1c844932017-07-24 23:36:41 +0200343 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200344update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200345{
Bram Moolenaar1c844932017-07-24 23:36:41 +0200346 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200347 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200348 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200349 if (term->tl_cursor_visible)
350 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200351 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200352#ifdef FEAT_GUI
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200353 if (gui.in_use && term->tl_cursor_visible)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200354 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200355#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200356 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200357}
358
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200359/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200360 * Invoked when "msg" output from a job was received. Write it to the terminal
361 * of "buffer".
362 */
363 void
364write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
365{
366 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200367 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200368
Bram Moolenaard85f2712017-07-28 21:51:57 +0200369 if (term->tl_vterm == NULL)
370 {
371 ch_logn(channel, "NOT writing %d bytes to terminal", (int)len);
372 return;
373 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200374 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200375 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200376
377 /* TODO: only update once in a while. */
378 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200379 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200380}
381
382/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200383 * Convert typed key "c" into bytes to send to the job.
384 * Return the number of bytes in "buf".
385 */
386 static int
387term_convert_key(int c, char *buf)
388{
389 VTerm *vterm = curbuf->b_term->tl_vterm;
390 VTermKey key = VTERM_KEY_NONE;
391 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200392
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200393 switch (c)
394 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200395 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200396 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200397 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
398 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200399 case K_DEL: key = VTERM_KEY_DEL; break;
400 case K_DOWN: key = VTERM_KEY_DOWN; break;
401 case K_END: key = VTERM_KEY_END; break;
402 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
403 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
404 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
405 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
406 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
407 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
408 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
409 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
410 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
411 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
412 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
413 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
414 case K_HOME: key = VTERM_KEY_HOME; break;
415 case K_INS: key = VTERM_KEY_INS; break;
416 case K_K0: key = VTERM_KEY_KP_0; break;
417 case K_K1: key = VTERM_KEY_KP_1; break;
418 case K_K2: key = VTERM_KEY_KP_2; break;
419 case K_K3: key = VTERM_KEY_KP_3; break;
420 case K_K4: key = VTERM_KEY_KP_4; break;
421 case K_K5: key = VTERM_KEY_KP_5; break;
422 case K_K6: key = VTERM_KEY_KP_6; break;
423 case K_K7: key = VTERM_KEY_KP_7; break;
424 case K_K8: key = VTERM_KEY_KP_8; break;
425 case K_K9: key = VTERM_KEY_KP_9; break;
426 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
427 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
428 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
429 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
430 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
431 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
432 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
433 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
434 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
435 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
436 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
437 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
438 case K_LEFT: key = VTERM_KEY_LEFT; break;
439 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
440 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
441 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
442 case K_UP: key = VTERM_KEY_UP; break;
443 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200444
445 case K_MOUSEUP: /* TODO */ break;
446 case K_MOUSEDOWN: /* TODO */ break;
447 case K_MOUSELEFT: /* TODO */ break;
448 case K_MOUSERIGHT: /* TODO */ break;
449
450 case K_LEFTMOUSE: /* TODO */ break;
451 case K_LEFTMOUSE_NM: /* TODO */ break;
452 case K_LEFTDRAG: /* TODO */ break;
453 case K_LEFTRELEASE: /* TODO */ break;
454 case K_LEFTRELEASE_NM: /* TODO */ break;
455 case K_MIDDLEMOUSE: /* TODO */ break;
456 case K_MIDDLEDRAG: /* TODO */ break;
457 case K_MIDDLERELEASE: /* TODO */ break;
458 case K_RIGHTMOUSE: /* TODO */ break;
459 case K_RIGHTDRAG: /* TODO */ break;
460 case K_RIGHTRELEASE: /* TODO */ break;
461 case K_X1MOUSE: /* TODO */ break;
462 case K_X1DRAG: /* TODO */ break;
463 case K_X1RELEASE: /* TODO */ break;
464 case K_X2MOUSE: /* TODO */ break;
465 case K_X2DRAG: /* TODO */ break;
466 case K_X2RELEASE: /* TODO */ break;
467
468 /* TODO: handle all special keys and modifiers that terminal_loop()
469 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200470 }
471
472 /*
473 * Convert special keys to vterm keys:
474 * - Write keys to vterm: vterm_keyboard_key()
475 * - Write output to channel.
476 */
477 if (key != VTERM_KEY_NONE)
478 /* Special key, let vterm convert it. */
479 vterm_keyboard_key(vterm, key, mod);
480 else
481 /* Normal character, let vterm convert it. */
482 vterm_keyboard_unichar(vterm, c, mod);
483
484 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200485 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200486}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200487
Bram Moolenaar938783d2017-07-16 20:13:26 +0200488/*
Bram Moolenaard85f2712017-07-28 21:51:57 +0200489 * Return TRUE if the job for "buf" is still running.
490 */
491 static int
492term_job_running(term_T *term)
493{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200494 /* Also consider the job finished when the channel is closed, to avoid a
495 * race condition when updating the title. */
496 return term->tl_job != NULL
497 && term->tl_job->jv_status == JOB_STARTED
498 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200499}
500
501/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200502 * Get a key from the user without mapping.
503 * TODO: use terminal mode mappings.
504 */
505 static int
506term_vgetc()
507{
508 int c;
509
510 ++no_mapping;
511 ++allow_keys;
512 got_int = FALSE;
513 c = vgetc();
514 --no_mapping;
515 --allow_keys;
516 return c;
517}
518
519/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200520 * Wait for input and send it to the job.
521 * Return when the start of a CTRL-W command is typed or anything else that
522 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200523 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
524 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200525 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200526 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200527terminal_loop(void)
528{
529 char buf[KEY_BUF_LEN];
530 int c;
531 size_t len;
532 static int mouse_was_outside = FALSE;
533 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200534 int termkey = 0;
535
Bram Moolenaard85f2712017-07-28 21:51:57 +0200536 if (curbuf->b_term->tl_vterm == NULL || !term_job_running(curbuf->b_term))
537 /* job finished */
538 return OK;
539
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200540 if (*curwin->w_p_tk != NUL)
541 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200542
543 for (;;)
544 {
545 /* TODO: skip screen update when handling a sequence of keys. */
546 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200547 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200548 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200549 if (curbuf->b_term->tl_vterm == NULL
550 || !term_job_running(curbuf->b_term))
551 /* job finished while waiting for a character */
552 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200553
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200554 if (c == (termkey == 0 ? Ctrl_W : termkey))
555 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200556#ifdef FEAT_CMDL_INFO
557 if (add_to_showcmd(c))
558 out_flush();
559#endif
560 c = term_vgetc();
561#ifdef FEAT_CMDL_INFO
562 clear_showcmd();
563#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200564 if (curbuf->b_term->tl_vterm == NULL
565 || !term_job_running(curbuf->b_term))
566 /* job finished while waiting for a character */
567 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200568
569 if (termkey == 0 && c == '.')
570 /* "CTRL-W .": send CTRL-W to the job */
571 c = Ctrl_W;
572 else if (termkey == 0 || c != termkey)
573 {
574 stuffcharReadbuff(Ctrl_W);
575 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200576 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200577 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200578 }
579
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200580 /* Catch keys that need to be handled as in Normal mode. */
581 switch (c)
582 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200583 case NUL:
584 case K_ZERO:
585 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200586 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200587
588 case K_IGNORE: continue;
589
590 case K_LEFTDRAG:
591 case K_MIDDLEDRAG:
592 case K_RIGHTDRAG:
593 case K_X1DRAG:
594 case K_X2DRAG:
595 dragging_outside = mouse_was_outside;
596 /* FALLTHROUGH */
597 case K_LEFTMOUSE:
598 case K_LEFTMOUSE_NM:
599 case K_LEFTRELEASE:
600 case K_LEFTRELEASE_NM:
601 case K_MIDDLEMOUSE:
602 case K_MIDDLERELEASE:
603 case K_RIGHTMOUSE:
604 case K_RIGHTRELEASE:
605 case K_X1MOUSE:
606 case K_X1RELEASE:
607 case K_X2MOUSE:
608 case K_X2RELEASE:
609 if (mouse_row < W_WINROW(curwin)
610 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
611 || mouse_col < W_WINCOL(curwin)
612 || mouse_col >= W_ENDCOL(curwin)
613 || dragging_outside)
614 {
615 /* click outside the current window */
616 stuffcharReadbuff(c);
617 mouse_was_outside = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200618 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200619 }
620 }
621 mouse_was_outside = FALSE;
622
623 /* Convert the typed key to a sequence of bytes for the job. */
624 len = term_convert_key(c, buf);
625 if (len > 0)
626 /* TODO: if FAIL is returned, stop? */
627 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaard85f2712017-07-28 21:51:57 +0200628 (char_u *)buf, (int)len, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200629 }
Bram Moolenaard85f2712017-07-28 21:51:57 +0200630 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200631}
632
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200633 static void
634position_cursor(win_T *wp, VTermPos *pos)
635{
636 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
637 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
638}
639
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200640 static void
641may_toggle_cursor(term_T *term)
642{
643 if (curbuf == term->tl_buffer)
644 {
645 if (term->tl_cursor_visible)
646 cursor_on();
647 else
648 cursor_off();
649 }
650}
651
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200652 static int
653handle_damage(VTermRect rect, void *user)
654{
655 term_T *term = (term_T *)user;
656
657 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
658 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
659 redraw_buf_later(term->tl_buffer, NOT_VALID);
660 return 1;
661}
662
663 static int
664handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
665{
666 term_T *term = (term_T *)user;
667
668 /* TODO */
669 redraw_buf_later(term->tl_buffer, NOT_VALID);
670 return 1;
671}
672
673 static int
674handle_movecursor(
675 VTermPos pos,
676 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200677 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200678 void *user)
679{
680 term_T *term = (term_T *)user;
681 win_T *wp;
682 int is_current = FALSE;
683
684 FOR_ALL_WINDOWS(wp)
685 {
686 if (wp->w_buffer == term->tl_buffer)
687 {
688 position_cursor(wp, &pos);
689 if (wp == curwin)
690 is_current = TRUE;
691 }
692 }
693
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200694 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200695 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200696 {
697 may_toggle_cursor(term);
698 update_cursor(term, TRUE);
699 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200700
701 return 1;
702}
703
Bram Moolenaar21554412017-07-24 21:44:43 +0200704 static int
705handle_settermprop(
706 VTermProp prop,
707 VTermValue *value,
708 void *user)
709{
710 term_T *term = (term_T *)user;
711
712 switch (prop)
713 {
714 case VTERM_PROP_TITLE:
715 vim_free(term->tl_title);
716 term->tl_title = vim_strsave((char_u *)value->string);
717 vim_free(term->tl_status_text);
718 term->tl_status_text = NULL;
719 if (term == curbuf->b_term)
720 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200721 break;
722
723 case VTERM_PROP_CURSORVISIBLE:
724 term->tl_cursor_visible = value->boolean;
725 may_toggle_cursor(term);
726 out_flush();
727 break;
728
Bram Moolenaar21554412017-07-24 21:44:43 +0200729 default:
730 break;
731 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200732 /* Always return 1, otherwise vterm doesn't store the value internally. */
733 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +0200734}
735
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200736/*
737 * The job running in the terminal resized the terminal.
738 */
739 static int
740handle_resize(int rows, int cols, void *user)
741{
742 term_T *term = (term_T *)user;
743 win_T *wp;
744
745 term->tl_rows = rows;
746 term->tl_cols = cols;
747 FOR_ALL_WINDOWS(wp)
748 {
749 if (wp->w_buffer == term->tl_buffer)
750 {
751 win_setheight_win(rows, wp);
752 win_setwidth_win(cols, wp);
753 }
754 }
755
756 redraw_buf_later(term->tl_buffer, NOT_VALID);
757 return 1;
758}
759
Bram Moolenaard85f2712017-07-28 21:51:57 +0200760/*
761 * Handle a line that is pushed off the top of the screen.
762 */
763 static int
764handle_pushline(int cols, const VTermScreenCell *cells, void *user)
765{
766 term_T *term = (term_T *)user;
767
768 /* TODO: Limit the number of lines that are stored. */
769 /* TODO: put the text in the buffer. */
770 if (ga_grow(&term->tl_scrollback, 1) == OK)
771 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200772 VTermScreenCell *p = NULL;
773 int len = 0;
774 int i;
775 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200776
777 /* do not store empty cells at the end */
778 for (i = 0; i < cols; ++i)
779 if (cells[i].chars[0] != 0)
780 len = i + 1;
781
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200782 if (len > 0)
783 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200784 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +0200785 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200786
787 line = (sb_line_T *)term->tl_scrollback.ga_data
788 + term->tl_scrollback.ga_len;
789 line->sb_cols = len;
790 line->sb_cells = p;
791 ++term->tl_scrollback.ga_len;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200792 }
793 return 0; /* ignored */
794}
795
796/*
797 * Fill the buffer with the scrollback lines and current lines of the terminal.
798 * Called after the job has ended.
799 */
800 static void
801move_scrollback_to_buffer(term_T *term)
802{
803 linenr_T lnum;
804 garray_T ga;
805 int c;
806 int col;
807 int i;
808 win_T *wp;
809 int len;
810 int lines_skipped = 0;
811 VTermPos pos;
812 VTermScreenCell cell;
813 VTermScreenCell *p;
814 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
815
816 /* Append the the visible lines to the scrollback. */
817 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
818 {
819 len = 0;
820 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
821 if (vterm_screen_get_cell(screen, pos, &cell) != 0
822 && cell.chars[0] != NUL)
823 len = pos.col + 1;
824
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200825 if (len == 0)
826 ++lines_skipped;
827 else
Bram Moolenaard85f2712017-07-28 21:51:57 +0200828 {
829 while (lines_skipped > 0)
830 {
831 /* Line was skipped, add an empty line. */
832 --lines_skipped;
833 if (ga_grow(&term->tl_scrollback, 1) == OK)
834 {
835 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
836 + term->tl_scrollback.ga_len;
837
838 line->sb_cols = 0;
839 line->sb_cells = NULL;
840 ++term->tl_scrollback.ga_len;
841 }
842 }
843
844 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
845 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
846 {
847 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
848 + term->tl_scrollback.ga_len;
849
850 for (pos.col = 0; pos.col < len; ++pos.col)
851 {
852 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
853 vim_memset(p + pos.col, 0, sizeof(cell));
854 else
855 p[pos.col] = cell;
856 }
857 line->sb_cols = len;
858 line->sb_cells = p;
859 ++term->tl_scrollback.ga_len;
860 }
861 else
862 vim_free(p);
863 }
864 }
865
866 /* Add the text to the buffer. */
867 ga_init2(&ga, 1, 100);
868 for (lnum = 0; lnum < term->tl_scrollback.ga_len; ++lnum)
869 {
870 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
871
872 ga.ga_len = 0;
873 for (col = 0; col < line->sb_cols; ++col)
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200874 {
875 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
876 goto failed;
877 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaard85f2712017-07-28 21:51:57 +0200878 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
879 (char_u *)ga.ga_data + ga.ga_len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200880 }
881 if (ga_grow(&ga, 1) == FAIL)
882 goto failed;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200883 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
884 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
885 }
886
887 /* Delete the empty line that was in the empty buffer. */
888 curbuf = term->tl_buffer;
889 ml_delete(lnum + 1, FALSE);
890 curbuf = curwin->w_buffer;
891
892failed:
893 ga_clear(&ga);
894
895 FOR_ALL_WINDOWS(wp)
896 {
897 if (wp->w_buffer == term->tl_buffer)
898 {
899 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
900 wp->w_cursor.col = 0;
901 wp->w_valid = 0;
902 }
903 }
904}
905
Bram Moolenaar21554412017-07-24 21:44:43 +0200906static VTermScreenCallbacks screen_callbacks = {
907 handle_damage, /* damage */
908 handle_moverect, /* moverect */
909 handle_movecursor, /* movecursor */
910 handle_settermprop, /* settermprop */
911 NULL, /* bell */
912 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200913 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +0200914 NULL /* sb_popline */
915};
916
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200917/*
Bram Moolenaard85f2712017-07-28 21:51:57 +0200918 * Called when a channel has been closed.
919 */
920 void
921term_channel_closed(channel_T *ch)
922{
923 term_T *term;
924 int did_one = FALSE;
925
926 for (term = first_term; term != NULL; term = term->tl_next)
927 if (term->tl_job == ch->ch_job)
928 {
929 vim_free(term->tl_title);
930 term->tl_title = NULL;
931 vim_free(term->tl_status_text);
932 term->tl_status_text = NULL;
933
934 /* move the lines into the buffer and free the vterm */
935 move_scrollback_to_buffer(term);
936 term_free_vterm(term);
937
938 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
939 did_one = TRUE;
940 }
941 if (did_one)
942 {
943 redraw_statuslines();
944
945 /* Need to break out of vgetc(). */
946 ins_char_typebuf(K_IGNORE);
947
948 if (curbuf->b_term != NULL)
949 {
950 if (curbuf->b_term->tl_job == ch->ch_job)
951 maketitle();
952 update_cursor(curbuf->b_term, TRUE);
953 }
954 }
955}
956
957/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200958 * Reverse engineer the RGB value into a cterm color index.
959 * First color is 1. Return 0 if no match found.
960 */
961 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200962color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +0200963{
964 int red = color->red;
965 int blue = color->blue;
966 int green = color->green;
967
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200968 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200969 if (red == 0)
970 {
971 if (green == 0)
972 {
973 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200974 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200975 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200976 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200977 }
978 else if (green == 224)
979 {
980 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200981 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200982 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200983 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200984 }
985 }
986 else if (red == 224)
987 {
988 if (green == 0)
989 {
990 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200991 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200992 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200993 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200994 }
995 else if (green == 224)
996 {
997 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200998 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200999 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001000 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001001 }
1002 }
1003 else if (red == 128)
1004 {
1005 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001006 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001007 }
1008 else if (red == 255)
1009 {
1010 if (green == 64)
1011 {
1012 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001013 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001014 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001015 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001016 }
1017 else if (green == 255)
1018 {
1019 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001020 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001021 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001022 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001023 }
1024 }
1025 else if (red == 64)
1026 {
1027 if (green == 64)
1028 {
1029 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001030 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001031 }
1032 else if (green == 255)
1033 {
1034 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001035 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001036 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001037 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001038 }
1039 }
1040 if (t_colors >= 256)
1041 {
1042 if (red == blue && red == green)
1043 {
1044 /* 24-color greyscale */
1045 static int cutoff[23] = {
1046 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1047 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1048 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1049 int i;
1050
1051 for (i = 0; i < 23; ++i)
1052 if (red < cutoff[i])
1053 return i + 233;
1054 return 256;
1055 }
1056
1057 /* 216-color cube */
1058 return 17 + ((red + 25) / 0x33) * 36
1059 + ((green + 25) / 0x33) * 6
1060 + (blue + 25) / 0x33;
1061 }
1062 return 0;
1063}
1064
1065/*
1066 * Convert the attributes of a vterm cell into an attribute index.
1067 */
1068 static int
1069cell2attr(VTermScreenCell *cell)
1070{
1071 int attr = 0;
1072
1073 if (cell->attrs.bold)
1074 attr |= HL_BOLD;
1075 if (cell->attrs.underline)
1076 attr |= HL_UNDERLINE;
1077 if (cell->attrs.italic)
1078 attr |= HL_ITALIC;
1079 if (cell->attrs.strike)
1080 attr |= HL_STANDOUT;
1081 if (cell->attrs.reverse)
1082 attr |= HL_INVERSE;
1083 if (cell->attrs.strike)
1084 attr |= HL_UNDERLINE;
1085
1086#ifdef FEAT_GUI
1087 if (gui.in_use)
1088 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001089 guicolor_T fg, bg;
1090
1091 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1092 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1093 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001094 }
1095 else
1096#endif
1097#ifdef FEAT_TERMGUICOLORS
1098 if (p_tgc)
1099 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001100 guicolor_T fg, bg;
1101
1102 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1103 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1104
1105 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001106 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001107 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001108#endif
1109 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001110 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1111 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001112 }
1113 return 0;
1114}
1115
1116/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001117 * Called to update the window that contains a terminal.
1118 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001119 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001120 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001121term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001122{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001123 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001124 VTerm *vterm;
1125 VTermScreen *screen;
1126 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001127 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001128
Bram Moolenaard85f2712017-07-28 21:51:57 +02001129 if (term == NULL || term->tl_vterm == NULL)
1130 return FAIL;
1131 vterm = term->tl_vterm;
1132 screen = vterm_obtain_screen(vterm);
1133 state = vterm_obtain_state(vterm);
1134
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001135 /*
1136 * If the window was resized a redraw will be triggered and we get here.
1137 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1138 */
1139 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1140 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001141 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001142 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1143 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1144 win_T *twp;
1145
1146 FOR_ALL_WINDOWS(twp)
1147 {
1148 /* When more than one window shows the same terminal, use the
1149 * smallest size. */
1150 if (twp->w_buffer == term->tl_buffer)
1151 {
1152 if (!term->tl_rows_fixed && rows > twp->w_height)
1153 rows = twp->w_height;
1154 if (!term->tl_cols_fixed && cols > twp->w_width)
1155 cols = twp->w_width;
1156 }
1157 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001158
1159 vterm_set_size(vterm, rows, cols);
1160 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1161 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001162 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001163 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001164
1165 /* The cursor may have been moved when resizing. */
1166 vterm_state_get_cursorpos(state, &pos);
1167 position_cursor(wp, &pos);
1168
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001169 /* TODO: Only redraw what changed. */
1170 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001171 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001172 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001173 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001174
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001175 if (pos.row < term->tl_rows)
1176 {
1177 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001178 {
1179 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001180 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001181
Bram Moolenaareeac6772017-07-23 15:48:37 +02001182 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1183 vim_memset(&cell, 0, sizeof(cell));
1184
1185 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001186 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001187 if (c == NUL)
1188 {
1189 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001190#if defined(FEAT_MBYTE)
1191 if (enc_utf8)
1192 ScreenLinesUC[off] = NUL;
1193#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001194 }
1195 else
1196 {
1197#if defined(FEAT_MBYTE)
1198 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001199 {
1200 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001201 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001202 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001203 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001204 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001205 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001206 if (enc_utf8)
1207 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001208 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001209#else
1210 ScreenLines[off] = c;
1211#endif
1212 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001213 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001214
1215 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001216 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001217 if (cell.width == 2)
1218 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001219 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001220#if defined(FEAT_MBYTE)
1221 if (enc_utf8)
1222 ScreenLinesUC[off] = NUL;
1223#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001224 ++pos.col;
1225 ++off;
1226 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001227 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001228 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001229 else
1230 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001231
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001232 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1233 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001234 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001235
1236 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001237}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001238
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001239/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001240 * Return TRUE if "wp" is a terminal window where the job has finished.
1241 */
1242 int
1243term_is_finished(buf_T *buf)
1244{
1245 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1246}
1247
1248/*
1249 * The current buffer is going to be changed. If there is terminal
1250 * highlighting remove it now.
1251 */
1252 void
1253term_change_in_curbuf(void)
1254{
1255 term_T *term = curbuf->b_term;
1256
1257 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1258 {
1259 free_scrollback(term);
1260 redraw_buf_later(term->tl_buffer, NOT_VALID);
1261 }
1262}
1263
1264/*
1265 * Get the screen attribute for a position in the buffer.
1266 */
1267 int
1268term_get_attr(buf_T *buf, linenr_T lnum, int col)
1269{
1270 term_T *term = buf->b_term;
1271 sb_line_T *line;
1272
1273 if (lnum >= term->tl_scrollback.ga_len)
1274 return 0;
1275 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1276 if (col >= line->sb_cols)
1277 return 0;
1278 return cell2attr(line->sb_cells + col);
1279}
1280
1281/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001282 * Set job options common for Unix and MS-Windows.
1283 */
1284 static void
1285setup_job_options(jobopt_T *opt, int rows, int cols)
1286{
1287 clear_job_options(opt);
1288 opt->jo_mode = MODE_RAW;
1289 opt->jo_out_mode = MODE_RAW;
1290 opt->jo_err_mode = MODE_RAW;
1291 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001292
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001293 opt->jo_io[PART_OUT] = JIO_BUFFER;
1294 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001295 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1296
1297 opt->jo_modifiable[PART_OUT] = 0;
1298 opt->jo_modifiable[PART_ERR] = 0;
1299 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1300
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001301 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1302 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001303 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001304 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1305
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001306 opt->jo_term_rows = rows;
1307 opt->jo_term_cols = cols;
1308}
1309
1310/*
1311 * Create a new vterm and initialize it.
1312 */
1313 static void
1314create_vterm(term_T *term, int rows, int cols)
1315{
1316 VTerm *vterm;
1317 VTermScreen *screen;
1318
1319 vterm = vterm_new(rows, cols);
1320 term->tl_vterm = vterm;
1321 screen = vterm_obtain_screen(vterm);
1322 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1323 /* TODO: depends on 'encoding'. */
1324 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001325
1326 /* Vterm uses a default black background. Set it to white when
1327 * 'background' is "light". */
1328 if (*p_bg == 'l')
1329 {
1330 VTermColor fg, bg;
1331
1332 fg.red = fg.green = fg.blue = 0;
1333 bg.red = bg.green = bg.blue = 255;
1334 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1335 }
1336
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001337 /* Required to initialize most things. */
1338 vterm_screen_reset(screen, 1 /* hard */);
1339}
1340
Bram Moolenaar21554412017-07-24 21:44:43 +02001341/*
1342 * Return the text to show for the buffer name and status.
1343 */
1344 char_u *
1345term_get_status_text(term_T *term)
1346{
1347 if (term->tl_status_text == NULL)
1348 {
1349 char_u *txt;
1350 size_t len;
1351
1352 if (term->tl_title != NULL)
1353 txt = term->tl_title;
1354 else if (term_job_running(term))
1355 txt = (char_u *)_("running");
1356 else
1357 txt = (char_u *)_("finished");
1358 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001359 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001360 if (term->tl_status_text != NULL)
1361 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1362 term->tl_buffer->b_fname, txt);
1363 }
1364 return term->tl_status_text;
1365}
1366
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001367/*
1368 * Mark references in jobs of terminals.
1369 */
1370 int
1371set_ref_in_term(int copyID)
1372{
1373 int abort = FALSE;
1374 term_T *term;
1375 typval_T tv;
1376
1377 for (term = first_term; term != NULL; term = term->tl_next)
1378 if (term->tl_job != NULL)
1379 {
1380 tv.v_type = VAR_JOB;
1381 tv.vval.v_job = term->tl_job;
1382 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1383 }
1384 return abort;
1385}
1386
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001387# ifdef WIN3264
1388
1389#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1390#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1391
Bram Moolenaar8a773062017-07-24 22:29:21 +02001392void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001393void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001394void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001395BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1396void (*winpty_config_set_initial_size)(void*, int, int);
1397LPCWSTR (*winpty_conin_name)(void*);
1398LPCWSTR (*winpty_conout_name)(void*);
1399LPCWSTR (*winpty_conerr_name)(void*);
1400void (*winpty_free)(void*);
1401void (*winpty_config_free)(void*);
1402void (*winpty_spawn_config_free)(void*);
1403void (*winpty_error_free)(void*);
1404LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001405BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001406
1407/**************************************
1408 * 2. MS-Windows implementation.
1409 */
1410
1411#define WINPTY_DLL "winpty.dll"
1412
1413static HINSTANCE hWinPtyDLL = NULL;
1414
1415 int
1416dyn_winpty_init(void)
1417{
1418 int i;
1419 static struct
1420 {
1421 char *name;
1422 FARPROC *ptr;
1423 } winpty_entry[] =
1424 {
1425 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1426 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1427 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1428 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1429 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1430 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1431 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1432 {"winpty_free", (FARPROC*)&winpty_free},
1433 {"winpty_open", (FARPROC*)&winpty_open},
1434 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1435 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1436 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1437 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001438 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001439 {NULL, NULL}
1440 };
1441
1442 /* No need to initialize twice. */
1443 if (hWinPtyDLL)
1444 return 1;
1445 /* Load winpty.dll */
1446 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1447 if (!hWinPtyDLL)
1448 {
1449 EMSG2(_(e_loadlib), WINPTY_DLL);
1450 return 0;
1451 }
1452 for (i = 0; winpty_entry[i].name != NULL
1453 && winpty_entry[i].ptr != NULL; ++i)
1454 {
1455 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1456 winpty_entry[i].name)) == NULL)
1457 {
1458 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1459 return 0;
1460 }
1461 }
1462
1463 return 1;
1464}
1465
1466/*
1467 * Create a new terminal of "rows" by "cols" cells.
1468 * Store a reference in "term".
1469 * Return OK or FAIL.
1470 */
1471 static int
1472term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1473{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001474 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001475 channel_T *channel = NULL;
1476 job_T *job = NULL;
1477 jobopt_T opt;
1478 DWORD error;
1479 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1480 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001481 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001482
1483 if (!dyn_winpty_init())
1484 return FAIL;
1485
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001486 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001487 if (p == NULL)
1488 return FAIL;
1489
1490 job = job_alloc();
1491 if (job == NULL)
1492 goto failed;
1493
1494 channel = add_channel();
1495 if (channel == NULL)
1496 goto failed;
1497
1498 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1499 if (term->tl_winpty_config == NULL)
1500 goto failed;
1501
1502 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1503 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1504 if (term->tl_winpty == NULL)
1505 goto failed;
1506
1507 spawn_config = winpty_spawn_config_new(
1508 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1509 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1510 NULL,
1511 p,
1512 NULL,
1513 NULL,
1514 &winpty_err);
1515 if (spawn_config == NULL)
1516 goto failed;
1517
1518 channel = add_channel();
1519 if (channel == NULL)
1520 goto failed;
1521
1522 job = job_alloc();
1523 if (job == NULL)
1524 goto failed;
1525
1526 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1527 &child_thread_handle, &error, &winpty_err))
1528 goto failed;
1529
1530 channel_set_pipes(channel,
1531 (sock_T) CreateFileW(
1532 winpty_conin_name(term->tl_winpty),
1533 GENERIC_WRITE, 0, NULL,
1534 OPEN_EXISTING, 0, NULL),
1535 (sock_T) CreateFileW(
1536 winpty_conout_name(term->tl_winpty),
1537 GENERIC_READ, 0, NULL,
1538 OPEN_EXISTING, 0, NULL),
1539 (sock_T) CreateFileW(
1540 winpty_conerr_name(term->tl_winpty),
1541 GENERIC_READ, 0, NULL,
1542 OPEN_EXISTING, 0, NULL));
1543
1544 jo = CreateJobObject(NULL, NULL);
1545 if (jo == NULL)
1546 goto failed;
1547
1548 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001549 {
1550 /* Failed, switch the way to terminate process with TerminateProcess. */
1551 CloseHandle(jo);
1552 jo = NULL;
1553 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001554
1555 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001556 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001557
1558 create_vterm(term, rows, cols);
1559
1560 setup_job_options(&opt, rows, cols);
1561 channel_set_job(channel, job, &opt);
1562
1563 job->jv_channel = channel;
1564 job->jv_proc_info.hProcess = child_process_handle;
1565 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1566 job->jv_job_object = jo;
1567 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001568 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001569 term->tl_job = job;
1570
1571 return OK;
1572
1573failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001574 if (spawn_config != NULL)
1575 winpty_spawn_config_free(spawn_config);
1576 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001577 if (channel != NULL)
1578 channel_clear(channel);
1579 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001580 {
1581 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001582 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001583 }
1584 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001585 if (jo != NULL)
1586 CloseHandle(jo);
1587 if (term->tl_winpty != NULL)
1588 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001589 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001590 if (term->tl_winpty_config != NULL)
1591 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001592 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001593 if (winpty_err != NULL)
1594 {
1595 char_u *msg = utf16_to_enc(
1596 (short_u *)winpty_error_msg(winpty_err), NULL);
1597
1598 EMSG(msg);
1599 winpty_error_free(winpty_err);
1600 }
1601 return FAIL;
1602}
1603
1604/*
1605 * Free the terminal emulator part of "term".
1606 */
1607 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02001608term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001609{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001610 if (term->tl_winpty != NULL)
1611 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001612 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001613 if (term->tl_winpty_config != NULL)
1614 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001615 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001616 if (term->tl_vterm != NULL)
1617 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02001618 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001619}
1620
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001621/*
1622 * Request size to terminal.
1623 */
1624 static void
1625term_report_winsize(term_T *term, int rows, int cols)
1626{
1627 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1628}
1629
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001630# else
1631
1632/**************************************
1633 * 3. Unix-like implementation.
1634 */
1635
1636/*
1637 * Create a new terminal of "rows" by "cols" cells.
1638 * Start job for "cmd".
1639 * Store the pointers in "term".
1640 * Return OK or FAIL.
1641 */
1642 static int
1643term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1644{
1645 typval_T argvars[2];
1646 jobopt_T opt;
1647
1648 create_vterm(term, rows, cols);
1649
1650 argvars[0].v_type = VAR_STRING;
1651 argvars[0].vval.v_string = cmd;
1652 setup_job_options(&opt, rows, cols);
1653 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001654 if (term->tl_job != NULL)
1655 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001656
Bram Moolenaar61a66052017-07-22 18:39:00 +02001657 return term->tl_job != NULL
1658 && term->tl_job->jv_channel != NULL
1659 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001660}
1661
1662/*
1663 * Free the terminal emulator part of "term".
1664 */
1665 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02001666term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001667{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001668 if (term->tl_vterm != NULL)
1669 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001670 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001671}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001672
1673/*
1674 * Request size to terminal.
1675 */
1676 static void
1677term_report_winsize(term_T *term, int rows, int cols)
1678{
1679 /* Use an ioctl() to report the new window size to the job. */
1680 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1681 {
1682 int fd = -1;
1683 int part;
1684
1685 for (part = PART_OUT; part < PART_COUNT; ++part)
1686 {
1687 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1688 if (isatty(fd))
1689 break;
1690 }
1691 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1692 mch_stop_job(term->tl_job, (char_u *)"winch");
1693 }
1694}
1695
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001696# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001697
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001698#endif /* FEAT_TERMINAL */