blob: b68081f314aadbaf87c25198141463d94c02134d [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 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200768 VTermScreenCell *p = NULL;
769 int len = 0;
770 int i;
771 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200772
773 /* do not store empty cells at the end */
774 for (i = 0; i < cols; ++i)
775 if (cells[i].chars[0] != 0)
776 len = i + 1;
777
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200778 if (len > 0)
779 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200780 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +0200781 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200782
783 line = (sb_line_T *)term->tl_scrollback.ga_data
784 + term->tl_scrollback.ga_len;
785 line->sb_cols = len;
786 line->sb_cells = p;
787 ++term->tl_scrollback.ga_len;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200788 }
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
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200821 if (len == 0)
822 ++lines_skipped;
823 else
Bram Moolenaard85f2712017-07-28 21:51:57 +0200824 {
825 while (lines_skipped > 0)
826 {
827 /* Line was skipped, add an empty line. */
828 --lines_skipped;
829 if (ga_grow(&term->tl_scrollback, 1) == OK)
830 {
831 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
832 + term->tl_scrollback.ga_len;
833
834 line->sb_cols = 0;
835 line->sb_cells = NULL;
836 ++term->tl_scrollback.ga_len;
837 }
838 }
839
840 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
841 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
842 {
843 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
844 + term->tl_scrollback.ga_len;
845
846 for (pos.col = 0; pos.col < len; ++pos.col)
847 {
848 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
849 vim_memset(p + pos.col, 0, sizeof(cell));
850 else
851 p[pos.col] = cell;
852 }
853 line->sb_cols = len;
854 line->sb_cells = p;
855 ++term->tl_scrollback.ga_len;
856 }
857 else
858 vim_free(p);
859 }
860 }
861
862 /* Add the text to the buffer. */
863 ga_init2(&ga, 1, 100);
864 for (lnum = 0; lnum < term->tl_scrollback.ga_len; ++lnum)
865 {
866 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
867
868 ga.ga_len = 0;
869 for (col = 0; col < line->sb_cols; ++col)
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200870 {
871 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
872 goto failed;
873 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaard85f2712017-07-28 21:51:57 +0200874 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
875 (char_u *)ga.ga_data + ga.ga_len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200876 }
877 if (ga_grow(&ga, 1) == FAIL)
878 goto failed;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200879 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
880 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
881 }
882
883 /* Delete the empty line that was in the empty buffer. */
884 curbuf = term->tl_buffer;
885 ml_delete(lnum + 1, FALSE);
886 curbuf = curwin->w_buffer;
887
888failed:
889 ga_clear(&ga);
890
891 FOR_ALL_WINDOWS(wp)
892 {
893 if (wp->w_buffer == term->tl_buffer)
894 {
895 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
896 wp->w_cursor.col = 0;
897 wp->w_valid = 0;
898 }
899 }
900}
901
Bram Moolenaar21554412017-07-24 21:44:43 +0200902static VTermScreenCallbacks screen_callbacks = {
903 handle_damage, /* damage */
904 handle_moverect, /* moverect */
905 handle_movecursor, /* movecursor */
906 handle_settermprop, /* settermprop */
907 NULL, /* bell */
908 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200909 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +0200910 NULL /* sb_popline */
911};
912
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200913/*
Bram Moolenaard85f2712017-07-28 21:51:57 +0200914 * Called when a channel has been closed.
915 */
916 void
917term_channel_closed(channel_T *ch)
918{
919 term_T *term;
920 int did_one = FALSE;
921
922 for (term = first_term; term != NULL; term = term->tl_next)
923 if (term->tl_job == ch->ch_job)
924 {
925 vim_free(term->tl_title);
926 term->tl_title = NULL;
927 vim_free(term->tl_status_text);
928 term->tl_status_text = NULL;
929
930 /* move the lines into the buffer and free the vterm */
931 move_scrollback_to_buffer(term);
932 term_free_vterm(term);
933
934 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
935 did_one = TRUE;
936 }
937 if (did_one)
938 {
939 redraw_statuslines();
940
941 /* Need to break out of vgetc(). */
942 ins_char_typebuf(K_IGNORE);
943
944 if (curbuf->b_term != NULL)
945 {
946 if (curbuf->b_term->tl_job == ch->ch_job)
947 maketitle();
948 update_cursor(curbuf->b_term, TRUE);
949 }
950 }
951}
952
953/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200954 * Reverse engineer the RGB value into a cterm color index.
955 * First color is 1. Return 0 if no match found.
956 */
957 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200958color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +0200959{
960 int red = color->red;
961 int blue = color->blue;
962 int green = color->green;
963
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200964 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200965 if (red == 0)
966 {
967 if (green == 0)
968 {
969 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200970 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200971 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200972 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200973 }
974 else if (green == 224)
975 {
976 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200977 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200978 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200979 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200980 }
981 }
982 else if (red == 224)
983 {
984 if (green == 0)
985 {
986 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200987 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200988 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200989 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200990 }
991 else if (green == 224)
992 {
993 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200994 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200995 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200996 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200997 }
998 }
999 else if (red == 128)
1000 {
1001 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001002 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001003 }
1004 else if (red == 255)
1005 {
1006 if (green == 64)
1007 {
1008 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001009 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001010 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001011 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001012 }
1013 else if (green == 255)
1014 {
1015 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001016 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001017 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001018 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001019 }
1020 }
1021 else if (red == 64)
1022 {
1023 if (green == 64)
1024 {
1025 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001026 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001027 }
1028 else if (green == 255)
1029 {
1030 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001031 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001032 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001033 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001034 }
1035 }
1036 if (t_colors >= 256)
1037 {
1038 if (red == blue && red == green)
1039 {
1040 /* 24-color greyscale */
1041 static int cutoff[23] = {
1042 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1043 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1044 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1045 int i;
1046
1047 for (i = 0; i < 23; ++i)
1048 if (red < cutoff[i])
1049 return i + 233;
1050 return 256;
1051 }
1052
1053 /* 216-color cube */
1054 return 17 + ((red + 25) / 0x33) * 36
1055 + ((green + 25) / 0x33) * 6
1056 + (blue + 25) / 0x33;
1057 }
1058 return 0;
1059}
1060
1061/*
1062 * Convert the attributes of a vterm cell into an attribute index.
1063 */
1064 static int
1065cell2attr(VTermScreenCell *cell)
1066{
1067 int attr = 0;
1068
1069 if (cell->attrs.bold)
1070 attr |= HL_BOLD;
1071 if (cell->attrs.underline)
1072 attr |= HL_UNDERLINE;
1073 if (cell->attrs.italic)
1074 attr |= HL_ITALIC;
1075 if (cell->attrs.strike)
1076 attr |= HL_STANDOUT;
1077 if (cell->attrs.reverse)
1078 attr |= HL_INVERSE;
1079 if (cell->attrs.strike)
1080 attr |= HL_UNDERLINE;
1081
1082#ifdef FEAT_GUI
1083 if (gui.in_use)
1084 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001085 guicolor_T fg, bg;
1086
1087 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1088 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1089 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001090 }
1091 else
1092#endif
1093#ifdef FEAT_TERMGUICOLORS
1094 if (p_tgc)
1095 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001096 guicolor_T fg, bg;
1097
1098 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1099 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1100
1101 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001102 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001103 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001104#endif
1105 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001106 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1107 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001108 }
1109 return 0;
1110}
1111
1112/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001113 * Called to update the window that contains a terminal.
1114 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001115 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001116 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001117term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001118{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001119 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001120 VTerm *vterm;
1121 VTermScreen *screen;
1122 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001123 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001124
Bram Moolenaard85f2712017-07-28 21:51:57 +02001125 if (term == NULL || term->tl_vterm == NULL)
1126 return FAIL;
1127 vterm = term->tl_vterm;
1128 screen = vterm_obtain_screen(vterm);
1129 state = vterm_obtain_state(vterm);
1130
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001131 /*
1132 * If the window was resized a redraw will be triggered and we get here.
1133 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1134 */
1135 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1136 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001137 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001138 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1139 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1140 win_T *twp;
1141
1142 FOR_ALL_WINDOWS(twp)
1143 {
1144 /* When more than one window shows the same terminal, use the
1145 * smallest size. */
1146 if (twp->w_buffer == term->tl_buffer)
1147 {
1148 if (!term->tl_rows_fixed && rows > twp->w_height)
1149 rows = twp->w_height;
1150 if (!term->tl_cols_fixed && cols > twp->w_width)
1151 cols = twp->w_width;
1152 }
1153 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001154
1155 vterm_set_size(vterm, rows, cols);
1156 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1157 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001158 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001159 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001160
1161 /* The cursor may have been moved when resizing. */
1162 vterm_state_get_cursorpos(state, &pos);
1163 position_cursor(wp, &pos);
1164
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001165 /* TODO: Only redraw what changed. */
1166 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001167 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001168 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001169 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001170
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001171 if (pos.row < term->tl_rows)
1172 {
1173 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001174 {
1175 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001176 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001177
Bram Moolenaareeac6772017-07-23 15:48:37 +02001178 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1179 vim_memset(&cell, 0, sizeof(cell));
1180
1181 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001182 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001183 if (c == NUL)
1184 {
1185 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001186#if defined(FEAT_MBYTE)
1187 if (enc_utf8)
1188 ScreenLinesUC[off] = NUL;
1189#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001190 }
1191 else
1192 {
1193#if defined(FEAT_MBYTE)
1194 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001195 {
1196 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001197 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001198 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001199 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001200 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001201 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001202 if (enc_utf8)
1203 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001204 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001205#else
1206 ScreenLines[off] = c;
1207#endif
1208 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001209 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001210
1211 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001212 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001213 if (cell.width == 2)
1214 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001215 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001216#if defined(FEAT_MBYTE)
1217 if (enc_utf8)
1218 ScreenLinesUC[off] = NUL;
1219#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001220 ++pos.col;
1221 ++off;
1222 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001223 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001224 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001225 else
1226 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001227
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001228 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1229 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001230 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001231
1232 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001233}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001234
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001235/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001236 * Return TRUE if "wp" is a terminal window where the job has finished.
1237 */
1238 int
1239term_is_finished(buf_T *buf)
1240{
1241 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1242}
1243
1244/*
1245 * The current buffer is going to be changed. If there is terminal
1246 * highlighting remove it now.
1247 */
1248 void
1249term_change_in_curbuf(void)
1250{
1251 term_T *term = curbuf->b_term;
1252
1253 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1254 {
1255 free_scrollback(term);
1256 redraw_buf_later(term->tl_buffer, NOT_VALID);
1257 }
1258}
1259
1260/*
1261 * Get the screen attribute for a position in the buffer.
1262 */
1263 int
1264term_get_attr(buf_T *buf, linenr_T lnum, int col)
1265{
1266 term_T *term = buf->b_term;
1267 sb_line_T *line;
1268
1269 if (lnum >= term->tl_scrollback.ga_len)
1270 return 0;
1271 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1272 if (col >= line->sb_cols)
1273 return 0;
1274 return cell2attr(line->sb_cells + col);
1275}
1276
1277/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001278 * Set job options common for Unix and MS-Windows.
1279 */
1280 static void
1281setup_job_options(jobopt_T *opt, int rows, int cols)
1282{
1283 clear_job_options(opt);
1284 opt->jo_mode = MODE_RAW;
1285 opt->jo_out_mode = MODE_RAW;
1286 opt->jo_err_mode = MODE_RAW;
1287 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001288
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001289 opt->jo_io[PART_OUT] = JIO_BUFFER;
1290 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001291 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1292
1293 opt->jo_modifiable[PART_OUT] = 0;
1294 opt->jo_modifiable[PART_ERR] = 0;
1295 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1296
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001297 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1298 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001299 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001300 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1301
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001302 opt->jo_term_rows = rows;
1303 opt->jo_term_cols = cols;
1304}
1305
1306/*
1307 * Create a new vterm and initialize it.
1308 */
1309 static void
1310create_vterm(term_T *term, int rows, int cols)
1311{
1312 VTerm *vterm;
1313 VTermScreen *screen;
1314
1315 vterm = vterm_new(rows, cols);
1316 term->tl_vterm = vterm;
1317 screen = vterm_obtain_screen(vterm);
1318 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1319 /* TODO: depends on 'encoding'. */
1320 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001321
1322 /* Vterm uses a default black background. Set it to white when
1323 * 'background' is "light". */
1324 if (*p_bg == 'l')
1325 {
1326 VTermColor fg, bg;
1327
1328 fg.red = fg.green = fg.blue = 0;
1329 bg.red = bg.green = bg.blue = 255;
1330 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1331 }
1332
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001333 /* Required to initialize most things. */
1334 vterm_screen_reset(screen, 1 /* hard */);
1335}
1336
Bram Moolenaar21554412017-07-24 21:44:43 +02001337/*
1338 * Return the text to show for the buffer name and status.
1339 */
1340 char_u *
1341term_get_status_text(term_T *term)
1342{
1343 if (term->tl_status_text == NULL)
1344 {
1345 char_u *txt;
1346 size_t len;
1347
1348 if (term->tl_title != NULL)
1349 txt = term->tl_title;
1350 else if (term_job_running(term))
1351 txt = (char_u *)_("running");
1352 else
1353 txt = (char_u *)_("finished");
1354 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001355 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001356 if (term->tl_status_text != NULL)
1357 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1358 term->tl_buffer->b_fname, txt);
1359 }
1360 return term->tl_status_text;
1361}
1362
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001363/*
1364 * Mark references in jobs of terminals.
1365 */
1366 int
1367set_ref_in_term(int copyID)
1368{
1369 int abort = FALSE;
1370 term_T *term;
1371 typval_T tv;
1372
1373 for (term = first_term; term != NULL; term = term->tl_next)
1374 if (term->tl_job != NULL)
1375 {
1376 tv.v_type = VAR_JOB;
1377 tv.vval.v_job = term->tl_job;
1378 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1379 }
1380 return abort;
1381}
1382
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001383# ifdef WIN3264
1384
1385#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1386#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1387
Bram Moolenaar8a773062017-07-24 22:29:21 +02001388void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001389void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001390void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001391BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1392void (*winpty_config_set_initial_size)(void*, int, int);
1393LPCWSTR (*winpty_conin_name)(void*);
1394LPCWSTR (*winpty_conout_name)(void*);
1395LPCWSTR (*winpty_conerr_name)(void*);
1396void (*winpty_free)(void*);
1397void (*winpty_config_free)(void*);
1398void (*winpty_spawn_config_free)(void*);
1399void (*winpty_error_free)(void*);
1400LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001401BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001402
1403/**************************************
1404 * 2. MS-Windows implementation.
1405 */
1406
1407#define WINPTY_DLL "winpty.dll"
1408
1409static HINSTANCE hWinPtyDLL = NULL;
1410
1411 int
1412dyn_winpty_init(void)
1413{
1414 int i;
1415 static struct
1416 {
1417 char *name;
1418 FARPROC *ptr;
1419 } winpty_entry[] =
1420 {
1421 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1422 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1423 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1424 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1425 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1426 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1427 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1428 {"winpty_free", (FARPROC*)&winpty_free},
1429 {"winpty_open", (FARPROC*)&winpty_open},
1430 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1431 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1432 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1433 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001434 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001435 {NULL, NULL}
1436 };
1437
1438 /* No need to initialize twice. */
1439 if (hWinPtyDLL)
1440 return 1;
1441 /* Load winpty.dll */
1442 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1443 if (!hWinPtyDLL)
1444 {
1445 EMSG2(_(e_loadlib), WINPTY_DLL);
1446 return 0;
1447 }
1448 for (i = 0; winpty_entry[i].name != NULL
1449 && winpty_entry[i].ptr != NULL; ++i)
1450 {
1451 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1452 winpty_entry[i].name)) == NULL)
1453 {
1454 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1455 return 0;
1456 }
1457 }
1458
1459 return 1;
1460}
1461
1462/*
1463 * Create a new terminal of "rows" by "cols" cells.
1464 * Store a reference in "term".
1465 * Return OK or FAIL.
1466 */
1467 static int
1468term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1469{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001470 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001471 channel_T *channel = NULL;
1472 job_T *job = NULL;
1473 jobopt_T opt;
1474 DWORD error;
1475 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1476 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001477 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001478
1479 if (!dyn_winpty_init())
1480 return FAIL;
1481
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001482 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001483 if (p == NULL)
1484 return FAIL;
1485
1486 job = job_alloc();
1487 if (job == NULL)
1488 goto failed;
1489
1490 channel = add_channel();
1491 if (channel == NULL)
1492 goto failed;
1493
1494 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1495 if (term->tl_winpty_config == NULL)
1496 goto failed;
1497
1498 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1499 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1500 if (term->tl_winpty == NULL)
1501 goto failed;
1502
1503 spawn_config = winpty_spawn_config_new(
1504 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1505 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1506 NULL,
1507 p,
1508 NULL,
1509 NULL,
1510 &winpty_err);
1511 if (spawn_config == NULL)
1512 goto failed;
1513
1514 channel = add_channel();
1515 if (channel == NULL)
1516 goto failed;
1517
1518 job = job_alloc();
1519 if (job == NULL)
1520 goto failed;
1521
1522 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1523 &child_thread_handle, &error, &winpty_err))
1524 goto failed;
1525
1526 channel_set_pipes(channel,
1527 (sock_T) CreateFileW(
1528 winpty_conin_name(term->tl_winpty),
1529 GENERIC_WRITE, 0, NULL,
1530 OPEN_EXISTING, 0, NULL),
1531 (sock_T) CreateFileW(
1532 winpty_conout_name(term->tl_winpty),
1533 GENERIC_READ, 0, NULL,
1534 OPEN_EXISTING, 0, NULL),
1535 (sock_T) CreateFileW(
1536 winpty_conerr_name(term->tl_winpty),
1537 GENERIC_READ, 0, NULL,
1538 OPEN_EXISTING, 0, NULL));
1539
1540 jo = CreateJobObject(NULL, NULL);
1541 if (jo == NULL)
1542 goto failed;
1543
1544 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001545 {
1546 /* Failed, switch the way to terminate process with TerminateProcess. */
1547 CloseHandle(jo);
1548 jo = NULL;
1549 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001550
1551 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001552 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001553
1554 create_vterm(term, rows, cols);
1555
1556 setup_job_options(&opt, rows, cols);
1557 channel_set_job(channel, job, &opt);
1558
1559 job->jv_channel = channel;
1560 job->jv_proc_info.hProcess = child_process_handle;
1561 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1562 job->jv_job_object = jo;
1563 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001564 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001565 term->tl_job = job;
1566
1567 return OK;
1568
1569failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001570 if (spawn_config != NULL)
1571 winpty_spawn_config_free(spawn_config);
1572 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001573 if (channel != NULL)
1574 channel_clear(channel);
1575 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001576 {
1577 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001578 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001579 }
1580 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001581 if (jo != NULL)
1582 CloseHandle(jo);
1583 if (term->tl_winpty != NULL)
1584 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001585 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001586 if (term->tl_winpty_config != NULL)
1587 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001588 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001589 if (winpty_err != NULL)
1590 {
1591 char_u *msg = utf16_to_enc(
1592 (short_u *)winpty_error_msg(winpty_err), NULL);
1593
1594 EMSG(msg);
1595 winpty_error_free(winpty_err);
1596 }
1597 return FAIL;
1598}
1599
1600/*
1601 * Free the terminal emulator part of "term".
1602 */
1603 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02001604term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001605{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001606 if (term->tl_winpty != NULL)
1607 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001608 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001609 if (term->tl_winpty_config != NULL)
1610 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001611 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001612 if (term->tl_vterm != NULL)
1613 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02001614 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001615}
1616
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001617/*
1618 * Request size to terminal.
1619 */
1620 static void
1621term_report_winsize(term_T *term, int rows, int cols)
1622{
1623 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1624}
1625
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001626# else
1627
1628/**************************************
1629 * 3. Unix-like implementation.
1630 */
1631
1632/*
1633 * Create a new terminal of "rows" by "cols" cells.
1634 * Start job for "cmd".
1635 * Store the pointers in "term".
1636 * Return OK or FAIL.
1637 */
1638 static int
1639term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1640{
1641 typval_T argvars[2];
1642 jobopt_T opt;
1643
1644 create_vterm(term, rows, cols);
1645
1646 argvars[0].v_type = VAR_STRING;
1647 argvars[0].vval.v_string = cmd;
1648 setup_job_options(&opt, rows, cols);
1649 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001650 if (term->tl_job != NULL)
1651 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001652
Bram Moolenaar61a66052017-07-22 18:39:00 +02001653 return term->tl_job != NULL
1654 && term->tl_job->jv_channel != NULL
1655 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001656}
1657
1658/*
1659 * Free the terminal emulator part of "term".
1660 */
1661 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02001662term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001663{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001664 if (term->tl_vterm != NULL)
1665 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001666 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001667}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001668
1669/*
1670 * Request size to terminal.
1671 */
1672 static void
1673term_report_winsize(term_T *term, int rows, int cols)
1674{
1675 /* Use an ioctl() to report the new window size to the job. */
1676 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1677 {
1678 int fd = -1;
1679 int part;
1680
1681 for (part = PART_OUT; part < PART_COUNT; ++part)
1682 {
1683 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1684 if (isatty(fd))
1685 break;
1686 }
1687 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1688 mch_stop_job(term->tl_job, (char_u *)"winch");
1689 }
1690}
1691
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001692# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001693
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001694#endif /* FEAT_TERMINAL */