blob: aba3d45690d03cde78d5dbfb1d1bf2caa9083848 [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{
494 return term->tl_job != NULL && term->tl_job->jv_status == JOB_STARTED;
495}
496
497/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200498 * Get a key from the user without mapping.
499 * TODO: use terminal mode mappings.
500 */
501 static int
502term_vgetc()
503{
504 int c;
505
506 ++no_mapping;
507 ++allow_keys;
508 got_int = FALSE;
509 c = vgetc();
510 --no_mapping;
511 --allow_keys;
512 return c;
513}
514
515/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200516 * Wait for input and send it to the job.
517 * Return when the start of a CTRL-W command is typed or anything else that
518 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200519 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
520 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200521 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200522 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200523terminal_loop(void)
524{
525 char buf[KEY_BUF_LEN];
526 int c;
527 size_t len;
528 static int mouse_was_outside = FALSE;
529 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200530 int termkey = 0;
531
Bram Moolenaard85f2712017-07-28 21:51:57 +0200532 if (curbuf->b_term->tl_vterm == NULL || !term_job_running(curbuf->b_term))
533 /* job finished */
534 return OK;
535
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200536 if (*curwin->w_p_tk != NUL)
537 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200538
539 for (;;)
540 {
541 /* TODO: skip screen update when handling a sequence of keys. */
542 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200543 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200544 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200545 if (curbuf->b_term->tl_vterm == NULL
546 || !term_job_running(curbuf->b_term))
547 /* job finished while waiting for a character */
548 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200549
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200550 if (c == (termkey == 0 ? Ctrl_W : termkey))
551 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200552#ifdef FEAT_CMDL_INFO
553 if (add_to_showcmd(c))
554 out_flush();
555#endif
556 c = term_vgetc();
557#ifdef FEAT_CMDL_INFO
558 clear_showcmd();
559#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200560 if (curbuf->b_term->tl_vterm == NULL
561 || !term_job_running(curbuf->b_term))
562 /* job finished while waiting for a character */
563 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200564
565 if (termkey == 0 && c == '.')
566 /* "CTRL-W .": send CTRL-W to the job */
567 c = Ctrl_W;
568 else if (termkey == 0 || c != termkey)
569 {
570 stuffcharReadbuff(Ctrl_W);
571 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200572 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200573 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200574 }
575
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200576 /* Catch keys that need to be handled as in Normal mode. */
577 switch (c)
578 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200579 case NUL:
580 case K_ZERO:
581 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200582 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200583
584 case K_IGNORE: continue;
585
586 case K_LEFTDRAG:
587 case K_MIDDLEDRAG:
588 case K_RIGHTDRAG:
589 case K_X1DRAG:
590 case K_X2DRAG:
591 dragging_outside = mouse_was_outside;
592 /* FALLTHROUGH */
593 case K_LEFTMOUSE:
594 case K_LEFTMOUSE_NM:
595 case K_LEFTRELEASE:
596 case K_LEFTRELEASE_NM:
597 case K_MIDDLEMOUSE:
598 case K_MIDDLERELEASE:
599 case K_RIGHTMOUSE:
600 case K_RIGHTRELEASE:
601 case K_X1MOUSE:
602 case K_X1RELEASE:
603 case K_X2MOUSE:
604 case K_X2RELEASE:
605 if (mouse_row < W_WINROW(curwin)
606 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
607 || mouse_col < W_WINCOL(curwin)
608 || mouse_col >= W_ENDCOL(curwin)
609 || dragging_outside)
610 {
611 /* click outside the current window */
612 stuffcharReadbuff(c);
613 mouse_was_outside = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200614 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200615 }
616 }
617 mouse_was_outside = FALSE;
618
619 /* Convert the typed key to a sequence of bytes for the job. */
620 len = term_convert_key(c, buf);
621 if (len > 0)
622 /* TODO: if FAIL is returned, stop? */
623 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaard85f2712017-07-28 21:51:57 +0200624 (char_u *)buf, (int)len, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200625 }
Bram Moolenaard85f2712017-07-28 21:51:57 +0200626 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200627}
628
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200629 static void
630position_cursor(win_T *wp, VTermPos *pos)
631{
632 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
633 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
634}
635
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200636 static void
637may_toggle_cursor(term_T *term)
638{
639 if (curbuf == term->tl_buffer)
640 {
641 if (term->tl_cursor_visible)
642 cursor_on();
643 else
644 cursor_off();
645 }
646}
647
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200648 static int
649handle_damage(VTermRect rect, void *user)
650{
651 term_T *term = (term_T *)user;
652
653 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
654 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
655 redraw_buf_later(term->tl_buffer, NOT_VALID);
656 return 1;
657}
658
659 static int
660handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
661{
662 term_T *term = (term_T *)user;
663
664 /* TODO */
665 redraw_buf_later(term->tl_buffer, NOT_VALID);
666 return 1;
667}
668
669 static int
670handle_movecursor(
671 VTermPos pos,
672 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200673 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200674 void *user)
675{
676 term_T *term = (term_T *)user;
677 win_T *wp;
678 int is_current = FALSE;
679
680 FOR_ALL_WINDOWS(wp)
681 {
682 if (wp->w_buffer == term->tl_buffer)
683 {
684 position_cursor(wp, &pos);
685 if (wp == curwin)
686 is_current = TRUE;
687 }
688 }
689
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200690 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200691 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200692 {
693 may_toggle_cursor(term);
694 update_cursor(term, TRUE);
695 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200696
697 return 1;
698}
699
Bram Moolenaar21554412017-07-24 21:44:43 +0200700 static int
701handle_settermprop(
702 VTermProp prop,
703 VTermValue *value,
704 void *user)
705{
706 term_T *term = (term_T *)user;
707
708 switch (prop)
709 {
710 case VTERM_PROP_TITLE:
711 vim_free(term->tl_title);
712 term->tl_title = vim_strsave((char_u *)value->string);
713 vim_free(term->tl_status_text);
714 term->tl_status_text = NULL;
715 if (term == curbuf->b_term)
716 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200717 break;
718
719 case VTERM_PROP_CURSORVISIBLE:
720 term->tl_cursor_visible = value->boolean;
721 may_toggle_cursor(term);
722 out_flush();
723 break;
724
Bram Moolenaar21554412017-07-24 21:44:43 +0200725 default:
726 break;
727 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200728 /* Always return 1, otherwise vterm doesn't store the value internally. */
729 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +0200730}
731
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200732/*
733 * The job running in the terminal resized the terminal.
734 */
735 static int
736handle_resize(int rows, int cols, void *user)
737{
738 term_T *term = (term_T *)user;
739 win_T *wp;
740
741 term->tl_rows = rows;
742 term->tl_cols = cols;
743 FOR_ALL_WINDOWS(wp)
744 {
745 if (wp->w_buffer == term->tl_buffer)
746 {
747 win_setheight_win(rows, wp);
748 win_setwidth_win(cols, wp);
749 }
750 }
751
752 redraw_buf_later(term->tl_buffer, NOT_VALID);
753 return 1;
754}
755
Bram Moolenaard85f2712017-07-28 21:51:57 +0200756/*
757 * Handle a line that is pushed off the top of the screen.
758 */
759 static int
760handle_pushline(int cols, const VTermScreenCell *cells, void *user)
761{
762 term_T *term = (term_T *)user;
763
764 /* TODO: Limit the number of lines that are stored. */
765 /* TODO: put the text in the buffer. */
766 if (ga_grow(&term->tl_scrollback, 1) == OK)
767 {
768 VTermScreenCell *p;
769 int len;
770 int i;
771
772 /* do not store empty cells at the end */
773 for (i = 0; i < cols; ++i)
774 if (cells[i].chars[0] != 0)
775 len = i + 1;
776
777 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
778 if (p != NULL)
779 {
780 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
781 + term->tl_scrollback.ga_len;
782
783 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
784 line->sb_cols = len;
785 line->sb_cells = p;
786 ++term->tl_scrollback.ga_len;
787 }
788 }
789 return 0; /* ignored */
790}
791
792/*
793 * Fill the buffer with the scrollback lines and current lines of the terminal.
794 * Called after the job has ended.
795 */
796 static void
797move_scrollback_to_buffer(term_T *term)
798{
799 linenr_T lnum;
800 garray_T ga;
801 int c;
802 int col;
803 int i;
804 win_T *wp;
805 int len;
806 int lines_skipped = 0;
807 VTermPos pos;
808 VTermScreenCell cell;
809 VTermScreenCell *p;
810 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
811
812 /* Append the the visible lines to the scrollback. */
813 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
814 {
815 len = 0;
816 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
817 if (vterm_screen_get_cell(screen, pos, &cell) != 0
818 && cell.chars[0] != NUL)
819 len = pos.col + 1;
820
821 if (len > 0)
822 {
823 while (lines_skipped > 0)
824 {
825 /* Line was skipped, add an empty line. */
826 --lines_skipped;
827 if (ga_grow(&term->tl_scrollback, 1) == OK)
828 {
829 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
830 + term->tl_scrollback.ga_len;
831
832 line->sb_cols = 0;
833 line->sb_cells = NULL;
834 ++term->tl_scrollback.ga_len;
835 }
836 }
837
838 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
839 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
840 {
841 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
842 + term->tl_scrollback.ga_len;
843
844 for (pos.col = 0; pos.col < len; ++pos.col)
845 {
846 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
847 vim_memset(p + pos.col, 0, sizeof(cell));
848 else
849 p[pos.col] = cell;
850 }
851 line->sb_cols = len;
852 line->sb_cells = p;
853 ++term->tl_scrollback.ga_len;
854 }
855 else
856 vim_free(p);
857 }
858 }
859
860 /* Add the text to the buffer. */
861 ga_init2(&ga, 1, 100);
862 for (lnum = 0; lnum < term->tl_scrollback.ga_len; ++lnum)
863 {
864 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
865
866 ga.ga_len = 0;
867 for (col = 0; col < line->sb_cols; ++col)
868 for (i = 0; (c = line->sb_cells[col].chars[i]) != 0 || i == 0; ++i)
869 {
870 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
871 goto failed;
872 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
873 (char_u *)ga.ga_data + ga.ga_len);
874 }
875 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
876 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
877 }
878
879 /* Delete the empty line that was in the empty buffer. */
880 curbuf = term->tl_buffer;
881 ml_delete(lnum + 1, FALSE);
882 curbuf = curwin->w_buffer;
883
884failed:
885 ga_clear(&ga);
886
887 FOR_ALL_WINDOWS(wp)
888 {
889 if (wp->w_buffer == term->tl_buffer)
890 {
891 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
892 wp->w_cursor.col = 0;
893 wp->w_valid = 0;
894 }
895 }
896}
897
Bram Moolenaar21554412017-07-24 21:44:43 +0200898static VTermScreenCallbacks screen_callbacks = {
899 handle_damage, /* damage */
900 handle_moverect, /* moverect */
901 handle_movecursor, /* movecursor */
902 handle_settermprop, /* settermprop */
903 NULL, /* bell */
904 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200905 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +0200906 NULL /* sb_popline */
907};
908
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200909/*
Bram Moolenaard85f2712017-07-28 21:51:57 +0200910 * Called when a channel has been closed.
911 */
912 void
913term_channel_closed(channel_T *ch)
914{
915 term_T *term;
916 int did_one = FALSE;
917
918 for (term = first_term; term != NULL; term = term->tl_next)
919 if (term->tl_job == ch->ch_job)
920 {
921 vim_free(term->tl_title);
922 term->tl_title = NULL;
923 vim_free(term->tl_status_text);
924 term->tl_status_text = NULL;
925
926 /* move the lines into the buffer and free the vterm */
927 move_scrollback_to_buffer(term);
928 term_free_vterm(term);
929
930 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
931 did_one = TRUE;
932 }
933 if (did_one)
934 {
935 redraw_statuslines();
936
937 /* Need to break out of vgetc(). */
938 ins_char_typebuf(K_IGNORE);
939
940 if (curbuf->b_term != NULL)
941 {
942 if (curbuf->b_term->tl_job == ch->ch_job)
943 maketitle();
944 update_cursor(curbuf->b_term, TRUE);
945 }
946 }
947}
948
949/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200950 * Reverse engineer the RGB value into a cterm color index.
951 * First color is 1. Return 0 if no match found.
952 */
953 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200954color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +0200955{
956 int red = color->red;
957 int blue = color->blue;
958 int green = color->green;
959
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200960 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200961 if (red == 0)
962 {
963 if (green == 0)
964 {
965 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200966 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200967 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200968 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200969 }
970 else if (green == 224)
971 {
972 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200973 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200974 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200975 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200976 }
977 }
978 else if (red == 224)
979 {
980 if (green == 0)
981 {
982 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200983 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200984 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200985 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200986 }
987 else if (green == 224)
988 {
989 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200990 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200991 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200992 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200993 }
994 }
995 else if (red == 128)
996 {
997 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200998 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200999 }
1000 else if (red == 255)
1001 {
1002 if (green == 64)
1003 {
1004 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001005 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001006 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001007 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001008 }
1009 else if (green == 255)
1010 {
1011 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001012 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001013 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001014 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001015 }
1016 }
1017 else if (red == 64)
1018 {
1019 if (green == 64)
1020 {
1021 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001022 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001023 }
1024 else if (green == 255)
1025 {
1026 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001027 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001028 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001029 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001030 }
1031 }
1032 if (t_colors >= 256)
1033 {
1034 if (red == blue && red == green)
1035 {
1036 /* 24-color greyscale */
1037 static int cutoff[23] = {
1038 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1039 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1040 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1041 int i;
1042
1043 for (i = 0; i < 23; ++i)
1044 if (red < cutoff[i])
1045 return i + 233;
1046 return 256;
1047 }
1048
1049 /* 216-color cube */
1050 return 17 + ((red + 25) / 0x33) * 36
1051 + ((green + 25) / 0x33) * 6
1052 + (blue + 25) / 0x33;
1053 }
1054 return 0;
1055}
1056
1057/*
1058 * Convert the attributes of a vterm cell into an attribute index.
1059 */
1060 static int
1061cell2attr(VTermScreenCell *cell)
1062{
1063 int attr = 0;
1064
1065 if (cell->attrs.bold)
1066 attr |= HL_BOLD;
1067 if (cell->attrs.underline)
1068 attr |= HL_UNDERLINE;
1069 if (cell->attrs.italic)
1070 attr |= HL_ITALIC;
1071 if (cell->attrs.strike)
1072 attr |= HL_STANDOUT;
1073 if (cell->attrs.reverse)
1074 attr |= HL_INVERSE;
1075 if (cell->attrs.strike)
1076 attr |= HL_UNDERLINE;
1077
1078#ifdef FEAT_GUI
1079 if (gui.in_use)
1080 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001081 guicolor_T fg, bg;
1082
1083 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1084 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1085 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001086 }
1087 else
1088#endif
1089#ifdef FEAT_TERMGUICOLORS
1090 if (p_tgc)
1091 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001092 guicolor_T fg, bg;
1093
1094 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1095 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1096
1097 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001098 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001099 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001100#endif
1101 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001102 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1103 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001104 }
1105 return 0;
1106}
1107
1108/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001109 * Called to update the window that contains a terminal.
1110 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001111 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001112 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001113term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001114{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001115 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001116 VTerm *vterm;
1117 VTermScreen *screen;
1118 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001119 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001120
Bram Moolenaard85f2712017-07-28 21:51:57 +02001121 if (term == NULL || term->tl_vterm == NULL)
1122 return FAIL;
1123 vterm = term->tl_vterm;
1124 screen = vterm_obtain_screen(vterm);
1125 state = vterm_obtain_state(vterm);
1126
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001127 /*
1128 * If the window was resized a redraw will be triggered and we get here.
1129 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1130 */
1131 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1132 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001133 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001134 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1135 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1136 win_T *twp;
1137
1138 FOR_ALL_WINDOWS(twp)
1139 {
1140 /* When more than one window shows the same terminal, use the
1141 * smallest size. */
1142 if (twp->w_buffer == term->tl_buffer)
1143 {
1144 if (!term->tl_rows_fixed && rows > twp->w_height)
1145 rows = twp->w_height;
1146 if (!term->tl_cols_fixed && cols > twp->w_width)
1147 cols = twp->w_width;
1148 }
1149 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001150
1151 vterm_set_size(vterm, rows, cols);
1152 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1153 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001154 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001155 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001156
1157 /* The cursor may have been moved when resizing. */
1158 vterm_state_get_cursorpos(state, &pos);
1159 position_cursor(wp, &pos);
1160
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001161 /* TODO: Only redraw what changed. */
1162 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001163 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001164 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001165 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001166
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001167 if (pos.row < term->tl_rows)
1168 {
1169 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001170 {
1171 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001172 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001173
Bram Moolenaareeac6772017-07-23 15:48:37 +02001174 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1175 vim_memset(&cell, 0, sizeof(cell));
1176
1177 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001178 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001179 if (c == NUL)
1180 {
1181 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001182#if defined(FEAT_MBYTE)
1183 if (enc_utf8)
1184 ScreenLinesUC[off] = NUL;
1185#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001186 }
1187 else
1188 {
1189#if defined(FEAT_MBYTE)
1190 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001191 {
1192 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001193 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001194 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001195 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001196 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001197 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001198 if (enc_utf8)
1199 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001200 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001201#else
1202 ScreenLines[off] = c;
1203#endif
1204 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001205 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001206
1207 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001208 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001209 if (cell.width == 2)
1210 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001211 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001212#if defined(FEAT_MBYTE)
1213 if (enc_utf8)
1214 ScreenLinesUC[off] = NUL;
1215#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001216 ++pos.col;
1217 ++off;
1218 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001219 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001220 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001221 else
1222 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001223
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001224 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1225 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001226 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001227
1228 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001229}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001230
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001231/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001232 * Return TRUE if "wp" is a terminal window where the job has finished.
1233 */
1234 int
1235term_is_finished(buf_T *buf)
1236{
1237 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1238}
1239
1240/*
1241 * The current buffer is going to be changed. If there is terminal
1242 * highlighting remove it now.
1243 */
1244 void
1245term_change_in_curbuf(void)
1246{
1247 term_T *term = curbuf->b_term;
1248
1249 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1250 {
1251 free_scrollback(term);
1252 redraw_buf_later(term->tl_buffer, NOT_VALID);
1253 }
1254}
1255
1256/*
1257 * Get the screen attribute for a position in the buffer.
1258 */
1259 int
1260term_get_attr(buf_T *buf, linenr_T lnum, int col)
1261{
1262 term_T *term = buf->b_term;
1263 sb_line_T *line;
1264
1265 if (lnum >= term->tl_scrollback.ga_len)
1266 return 0;
1267 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1268 if (col >= line->sb_cols)
1269 return 0;
1270 return cell2attr(line->sb_cells + col);
1271}
1272
1273/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001274 * Set job options common for Unix and MS-Windows.
1275 */
1276 static void
1277setup_job_options(jobopt_T *opt, int rows, int cols)
1278{
1279 clear_job_options(opt);
1280 opt->jo_mode = MODE_RAW;
1281 opt->jo_out_mode = MODE_RAW;
1282 opt->jo_err_mode = MODE_RAW;
1283 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001284
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001285 opt->jo_io[PART_OUT] = JIO_BUFFER;
1286 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001287 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1288
1289 opt->jo_modifiable[PART_OUT] = 0;
1290 opt->jo_modifiable[PART_ERR] = 0;
1291 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1292
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001293 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1294 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001295 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001296 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1297
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001298 opt->jo_term_rows = rows;
1299 opt->jo_term_cols = cols;
1300}
1301
1302/*
1303 * Create a new vterm and initialize it.
1304 */
1305 static void
1306create_vterm(term_T *term, int rows, int cols)
1307{
1308 VTerm *vterm;
1309 VTermScreen *screen;
1310
1311 vterm = vterm_new(rows, cols);
1312 term->tl_vterm = vterm;
1313 screen = vterm_obtain_screen(vterm);
1314 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1315 /* TODO: depends on 'encoding'. */
1316 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001317
1318 /* Vterm uses a default black background. Set it to white when
1319 * 'background' is "light". */
1320 if (*p_bg == 'l')
1321 {
1322 VTermColor fg, bg;
1323
1324 fg.red = fg.green = fg.blue = 0;
1325 bg.red = bg.green = bg.blue = 255;
1326 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1327 }
1328
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001329 /* Required to initialize most things. */
1330 vterm_screen_reset(screen, 1 /* hard */);
1331}
1332
Bram Moolenaar21554412017-07-24 21:44:43 +02001333/*
1334 * Return the text to show for the buffer name and status.
1335 */
1336 char_u *
1337term_get_status_text(term_T *term)
1338{
1339 if (term->tl_status_text == NULL)
1340 {
1341 char_u *txt;
1342 size_t len;
1343
1344 if (term->tl_title != NULL)
1345 txt = term->tl_title;
1346 else if (term_job_running(term))
1347 txt = (char_u *)_("running");
1348 else
1349 txt = (char_u *)_("finished");
1350 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001351 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001352 if (term->tl_status_text != NULL)
1353 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1354 term->tl_buffer->b_fname, txt);
1355 }
1356 return term->tl_status_text;
1357}
1358
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001359/*
1360 * Mark references in jobs of terminals.
1361 */
1362 int
1363set_ref_in_term(int copyID)
1364{
1365 int abort = FALSE;
1366 term_T *term;
1367 typval_T tv;
1368
1369 for (term = first_term; term != NULL; term = term->tl_next)
1370 if (term->tl_job != NULL)
1371 {
1372 tv.v_type = VAR_JOB;
1373 tv.vval.v_job = term->tl_job;
1374 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1375 }
1376 return abort;
1377}
1378
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001379# ifdef WIN3264
1380
1381#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1382#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1383
Bram Moolenaar8a773062017-07-24 22:29:21 +02001384void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001385void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001386void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001387BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1388void (*winpty_config_set_initial_size)(void*, int, int);
1389LPCWSTR (*winpty_conin_name)(void*);
1390LPCWSTR (*winpty_conout_name)(void*);
1391LPCWSTR (*winpty_conerr_name)(void*);
1392void (*winpty_free)(void*);
1393void (*winpty_config_free)(void*);
1394void (*winpty_spawn_config_free)(void*);
1395void (*winpty_error_free)(void*);
1396LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001397BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001398
1399/**************************************
1400 * 2. MS-Windows implementation.
1401 */
1402
1403#define WINPTY_DLL "winpty.dll"
1404
1405static HINSTANCE hWinPtyDLL = NULL;
1406
1407 int
1408dyn_winpty_init(void)
1409{
1410 int i;
1411 static struct
1412 {
1413 char *name;
1414 FARPROC *ptr;
1415 } winpty_entry[] =
1416 {
1417 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1418 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1419 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1420 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1421 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1422 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1423 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1424 {"winpty_free", (FARPROC*)&winpty_free},
1425 {"winpty_open", (FARPROC*)&winpty_open},
1426 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1427 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1428 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1429 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001430 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001431 {NULL, NULL}
1432 };
1433
1434 /* No need to initialize twice. */
1435 if (hWinPtyDLL)
1436 return 1;
1437 /* Load winpty.dll */
1438 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1439 if (!hWinPtyDLL)
1440 {
1441 EMSG2(_(e_loadlib), WINPTY_DLL);
1442 return 0;
1443 }
1444 for (i = 0; winpty_entry[i].name != NULL
1445 && winpty_entry[i].ptr != NULL; ++i)
1446 {
1447 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1448 winpty_entry[i].name)) == NULL)
1449 {
1450 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1451 return 0;
1452 }
1453 }
1454
1455 return 1;
1456}
1457
1458/*
1459 * Create a new terminal of "rows" by "cols" cells.
1460 * Store a reference in "term".
1461 * Return OK or FAIL.
1462 */
1463 static int
1464term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1465{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001466 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001467 channel_T *channel = NULL;
1468 job_T *job = NULL;
1469 jobopt_T opt;
1470 DWORD error;
1471 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1472 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001473 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001474
1475 if (!dyn_winpty_init())
1476 return FAIL;
1477
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001478 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001479 if (p == NULL)
1480 return FAIL;
1481
1482 job = job_alloc();
1483 if (job == NULL)
1484 goto failed;
1485
1486 channel = add_channel();
1487 if (channel == NULL)
1488 goto failed;
1489
1490 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1491 if (term->tl_winpty_config == NULL)
1492 goto failed;
1493
1494 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1495 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1496 if (term->tl_winpty == NULL)
1497 goto failed;
1498
1499 spawn_config = winpty_spawn_config_new(
1500 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1501 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1502 NULL,
1503 p,
1504 NULL,
1505 NULL,
1506 &winpty_err);
1507 if (spawn_config == NULL)
1508 goto failed;
1509
1510 channel = add_channel();
1511 if (channel == NULL)
1512 goto failed;
1513
1514 job = job_alloc();
1515 if (job == NULL)
1516 goto failed;
1517
1518 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1519 &child_thread_handle, &error, &winpty_err))
1520 goto failed;
1521
1522 channel_set_pipes(channel,
1523 (sock_T) CreateFileW(
1524 winpty_conin_name(term->tl_winpty),
1525 GENERIC_WRITE, 0, NULL,
1526 OPEN_EXISTING, 0, NULL),
1527 (sock_T) CreateFileW(
1528 winpty_conout_name(term->tl_winpty),
1529 GENERIC_READ, 0, NULL,
1530 OPEN_EXISTING, 0, NULL),
1531 (sock_T) CreateFileW(
1532 winpty_conerr_name(term->tl_winpty),
1533 GENERIC_READ, 0, NULL,
1534 OPEN_EXISTING, 0, NULL));
1535
1536 jo = CreateJobObject(NULL, NULL);
1537 if (jo == NULL)
1538 goto failed;
1539
1540 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001541 {
1542 /* Failed, switch the way to terminate process with TerminateProcess. */
1543 CloseHandle(jo);
1544 jo = NULL;
1545 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001546
1547 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001548 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001549
1550 create_vterm(term, rows, cols);
1551
1552 setup_job_options(&opt, rows, cols);
1553 channel_set_job(channel, job, &opt);
1554
1555 job->jv_channel = channel;
1556 job->jv_proc_info.hProcess = child_process_handle;
1557 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1558 job->jv_job_object = jo;
1559 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001560 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001561 term->tl_job = job;
1562
1563 return OK;
1564
1565failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001566 if (spawn_config != NULL)
1567 winpty_spawn_config_free(spawn_config);
1568 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001569 if (channel != NULL)
1570 channel_clear(channel);
1571 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001572 {
1573 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001574 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001575 }
1576 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001577 if (jo != NULL)
1578 CloseHandle(jo);
1579 if (term->tl_winpty != NULL)
1580 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001581 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001582 if (term->tl_winpty_config != NULL)
1583 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001584 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001585 if (winpty_err != NULL)
1586 {
1587 char_u *msg = utf16_to_enc(
1588 (short_u *)winpty_error_msg(winpty_err), NULL);
1589
1590 EMSG(msg);
1591 winpty_error_free(winpty_err);
1592 }
1593 return FAIL;
1594}
1595
1596/*
1597 * Free the terminal emulator part of "term".
1598 */
1599 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02001600term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001601{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001602 if (term->tl_winpty != NULL)
1603 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001604 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001605 if (term->tl_winpty_config != NULL)
1606 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001607 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001608 if (term->tl_vterm != NULL)
1609 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001610 term->tl_vterm = NULL
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001611}
1612
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001613/*
1614 * Request size to terminal.
1615 */
1616 static void
1617term_report_winsize(term_T *term, int rows, int cols)
1618{
1619 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1620}
1621
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001622# else
1623
1624/**************************************
1625 * 3. Unix-like implementation.
1626 */
1627
1628/*
1629 * Create a new terminal of "rows" by "cols" cells.
1630 * Start job for "cmd".
1631 * Store the pointers in "term".
1632 * Return OK or FAIL.
1633 */
1634 static int
1635term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1636{
1637 typval_T argvars[2];
1638 jobopt_T opt;
1639
1640 create_vterm(term, rows, cols);
1641
1642 argvars[0].v_type = VAR_STRING;
1643 argvars[0].vval.v_string = cmd;
1644 setup_job_options(&opt, rows, cols);
1645 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001646 if (term->tl_job != NULL)
1647 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001648
Bram Moolenaar61a66052017-07-22 18:39:00 +02001649 return term->tl_job != NULL
1650 && term->tl_job->jv_channel != NULL
1651 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001652}
1653
1654/*
1655 * Free the terminal emulator part of "term".
1656 */
1657 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02001658term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001659{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001660 if (term->tl_vterm != NULL)
1661 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001662 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001663}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001664
1665/*
1666 * Request size to terminal.
1667 */
1668 static void
1669term_report_winsize(term_T *term, int rows, int cols)
1670{
1671 /* Use an ioctl() to report the new window size to the job. */
1672 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1673 {
1674 int fd = -1;
1675 int part;
1676
1677 for (part = PART_OUT; part < PART_COUNT; ++part)
1678 {
1679 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1680 if (isatty(fd))
1681 break;
1682 }
1683 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1684 mch_stop_job(term->tl_job, (char_u *)"winch");
1685 }
1686}
1687
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001688# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001689
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001690#endif /* FEAT_TERMINAL */