blob: 4cc755c0c357efe5d4d2faf9cad30365eb5e25e4 [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
22 * that 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 *
35 * TODO:
Bram Moolenaar1c844932017-07-24 23:36:41 +020036 * - include functions from #1871
37 * - do not store terminal buffer in viminfo. Or prefix term:// ?
Bram Moolenaara1b5b092017-07-26 21:29:34 +020038 * - Make CTRL-W . send CTRL-W to terminal?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020039 * - Add a scrollback buffer (contains lines to scroll off the top).
40 * Can use the buf_T lines, store attributes somewhere else?
41 * - When the job ends:
42 * - Write "-- JOB ENDED --" in the terminal.
43 * - Put the terminal contents in the scrollback buffer.
44 * - Free the terminal emulator.
45 * - Display the scrollback buffer (but with attributes).
46 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - Need an option or argument to drop the window+buffer right away, to be
48 * used for a shell or Vim.
49 * - add a character in :ls output
Bram Moolenaar938783d2017-07-16 20:13:26 +020050 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020051 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020052 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020053 * - command line completion for :terminal
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020054 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020055 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020056 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020057 * - implement "term" for job_start(): more job options when starting a
58 * terminal.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020059 * - implement term_list() list of buffers with a terminal
60 * - implement term_getsize(buf)
61 * - implement term_setsize(buf)
62 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
63 * - implement term_wait(buf) wait for screen to be updated
64 * - implement term_scrape(buf, row) inspect terminal screen
65 * - implement term_open(command, options) open terminal window
66 * - implement term_getjob(buf)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020067 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
68 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020069 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020070 */
71
72#include "vim.h"
73
74#ifdef FEAT_TERMINAL
75
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020076#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020077# define MIN(x,y) (x < y ? x : y)
78# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020079#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020080
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020081#include "libvterm/include/vterm.h"
82
Bram Moolenaare4f25e42017-07-07 11:54:15 +020083/* typedef term_T in structs.h */
84struct terminal_S {
85 term_T *tl_next;
86
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020087#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020088 void *tl_winpty_config;
89 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020090#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020091 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020093 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020094
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020095 /* last known vterm size */
96 int tl_rows;
97 int tl_cols;
98 /* vterm size does not follow window size */
99 int tl_rows_fixed;
100 int tl_cols_fixed;
101
Bram Moolenaar21554412017-07-24 21:44:43 +0200102 char_u *tl_title; /* NULL or allocated */
103 char_u *tl_status_text; /* NULL or allocated */
104
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200105 /* Range of screen rows to update. Zero based. */
106 int tl_dirty_row_start; /* -1 if nothing dirty */
107 int tl_dirty_row_end; /* row below last one to update */
108
109 pos_T tl_cursor;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200110 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200111};
112
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200113/*
114 * List of all active terminals.
115 */
116static term_T *first_term = NULL;
117
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200118
119#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
120#define KEY_BUF_LEN 200
121
122/*
123 * Functions with separate implementation for MS-Windows and Unix-like systems.
124 */
125static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200126static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200127static void term_free(term_T *term);
128
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200129/**************************************
130 * 1. Generic code for all systems.
131 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200132
133/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200134 * Determine the terminal size from 'termsize' and the current window.
135 * Assumes term->tl_rows and term->tl_cols are zero.
136 */
137 static void
138set_term_and_win_size(term_T *term)
139{
140 if (*curwin->w_p_tms != NUL)
141 {
142 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
143
144 term->tl_rows = atoi((char *)curwin->w_p_tms);
145 term->tl_cols = atoi((char *)p);
146 }
147 if (term->tl_rows == 0)
148 term->tl_rows = curwin->w_height;
149 else
150 {
151 win_setheight_win(term->tl_rows, curwin);
152 term->tl_rows_fixed = TRUE;
153 }
154 if (term->tl_cols == 0)
155 term->tl_cols = curwin->w_width;
156 else
157 {
158 win_setwidth_win(term->tl_cols, curwin);
159 term->tl_cols_fixed = TRUE;
160 }
161}
162
163/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200164 * ":terminal": open a terminal window and execute a job in it.
165 */
166 void
167ex_terminal(exarg_T *eap)
168{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200169 exarg_T split_ea;
170 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200171 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200172 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200173
174 if (check_restricted() || check_secure())
175 return;
176
177 term = (term_T *)alloc_clear(sizeof(term_T));
178 if (term == NULL)
179 return;
180 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200181 term->tl_cursor_visible = TRUE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200182
183 /* Open a new window or tab. */
184 vim_memset(&split_ea, 0, sizeof(split_ea));
185 split_ea.cmdidx = CMD_new;
186 split_ea.cmd = (char_u *)"new";
187 split_ea.arg = (char_u *)"";
188 ex_splitview(&split_ea);
189 if (curwin == old_curwin)
190 {
191 /* split failed */
192 vim_free(term);
193 return;
194 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200195 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200196 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200197
198 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200199 term->tl_next = first_term;
200 first_term = term;
201
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200202 if (buflist_findname(cmd) == NULL)
203 curbuf->b_ffname = vim_strsave(cmd);
204 else
205 {
206 int i;
207 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200208 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200209
210 for (i = 1; p != NULL; ++i)
211 {
212 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
213 if (buflist_findname(p) == NULL)
214 {
215 curbuf->b_ffname = p;
216 break;
217 }
218 }
219 }
220 curbuf->b_fname = curbuf->b_ffname;
221
222 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
223 curbuf->b_changed = TRUE;
224 curbuf->b_p_ma = FALSE;
225 set_string_option_direct((char_u *)"buftype", -1,
226 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200227
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200228 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200229
Bram Moolenaare173fd02017-07-22 19:03:32 +0200230 if (cmd == NULL || *cmd == NUL)
231 cmd = p_sh;
232
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200233 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200234 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200235 {
236 /* store the size we ended up with */
237 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200238 }
239 else
240 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200241 free_terminal(term);
242 curbuf->b_term = NULL;
243
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200244 /* Wiping out the buffer will also close the window and call
245 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200246 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200247 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200248
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200249 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250}
251
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200252/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200253 * Free a terminal and everything it refers to.
254 * Kills the job if there is one.
255 * Called when wiping out a buffer.
256 */
257 void
258free_terminal(term_T *term)
259{
260 term_T *tp;
261
262 if (term == NULL)
263 return;
264 if (first_term == term)
265 first_term = term->tl_next;
266 else
267 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
268 if (tp->tl_next == term)
269 {
270 tp->tl_next = term->tl_next;
271 break;
272 }
273
274 if (term->tl_job != NULL)
275 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200276 if (term->tl_job->jv_status != JOB_ENDED
277 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200278 job_stop(term->tl_job, NULL, "kill");
279 job_unref(term->tl_job);
280 }
281
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200282 term_free(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200283 vim_free(term->tl_title);
284 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200285 vim_free(term);
286}
287
288/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200289 * Write job output "msg[len]" to the vterm.
290 */
291 static void
292term_write_job_output(term_T *term, char_u *msg, size_t len)
293{
294 VTerm *vterm = term->tl_vterm;
295 char_u *p;
296 size_t done;
297 size_t len_now;
298
299 for (done = 0; done < len; done += len_now)
300 {
301 for (p = msg + done; p < msg + len; )
302 {
303 if (*p == NL)
304 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200305 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200306 }
307 len_now = p - msg - done;
308 vterm_input_write(vterm, (char *)msg + done, len_now);
309 if (p < msg + len && *p == NL)
310 {
311 /* Convert NL to CR-NL, that appears to work best. */
312 vterm_input_write(vterm, "\r\n", 2);
313 ++len_now;
314 }
315 }
316
317 /* this invokes the damage callbacks */
318 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
319}
320
Bram Moolenaar1c844932017-07-24 23:36:41 +0200321 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200322update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200323{
324 /* TODO: this should not always be needed */
325 setcursor();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200326 if (redraw && term->tl_buffer == curbuf && term->tl_cursor_visible)
327 {
328 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200329#ifdef FEAT_GUI
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200330 if (gui.in_use)
331 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200332#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200333 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200334}
335
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200336/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200337 * Invoked when "msg" output from a job was received. Write it to the terminal
338 * of "buffer".
339 */
340 void
341write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
342{
343 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200344 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200345
346 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200347 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200348
349 /* TODO: only update once in a while. */
350 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200351 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200352}
353
354/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200355 * Convert typed key "c" into bytes to send to the job.
356 * Return the number of bytes in "buf".
357 */
358 static int
359term_convert_key(int c, char *buf)
360{
361 VTerm *vterm = curbuf->b_term->tl_vterm;
362 VTermKey key = VTERM_KEY_NONE;
363 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200364
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200365 switch (c)
366 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200367 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200368 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200369 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
370 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200371 case K_DEL: key = VTERM_KEY_DEL; break;
372 case K_DOWN: key = VTERM_KEY_DOWN; break;
373 case K_END: key = VTERM_KEY_END; break;
374 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
375 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
376 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
377 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
378 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
379 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
380 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
381 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
382 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
383 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
384 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
385 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
386 case K_HOME: key = VTERM_KEY_HOME; break;
387 case K_INS: key = VTERM_KEY_INS; break;
388 case K_K0: key = VTERM_KEY_KP_0; break;
389 case K_K1: key = VTERM_KEY_KP_1; break;
390 case K_K2: key = VTERM_KEY_KP_2; break;
391 case K_K3: key = VTERM_KEY_KP_3; break;
392 case K_K4: key = VTERM_KEY_KP_4; break;
393 case K_K5: key = VTERM_KEY_KP_5; break;
394 case K_K6: key = VTERM_KEY_KP_6; break;
395 case K_K7: key = VTERM_KEY_KP_7; break;
396 case K_K8: key = VTERM_KEY_KP_8; break;
397 case K_K9: key = VTERM_KEY_KP_9; break;
398 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
399 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
400 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
401 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
402 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
403 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
404 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
405 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
406 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
407 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
408 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
409 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
410 case K_LEFT: key = VTERM_KEY_LEFT; break;
411 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
412 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
413 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
414 case K_UP: key = VTERM_KEY_UP; break;
415 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200416
417 case K_MOUSEUP: /* TODO */ break;
418 case K_MOUSEDOWN: /* TODO */ break;
419 case K_MOUSELEFT: /* TODO */ break;
420 case K_MOUSERIGHT: /* TODO */ break;
421
422 case K_LEFTMOUSE: /* TODO */ break;
423 case K_LEFTMOUSE_NM: /* TODO */ break;
424 case K_LEFTDRAG: /* TODO */ break;
425 case K_LEFTRELEASE: /* TODO */ break;
426 case K_LEFTRELEASE_NM: /* TODO */ break;
427 case K_MIDDLEMOUSE: /* TODO */ break;
428 case K_MIDDLEDRAG: /* TODO */ break;
429 case K_MIDDLERELEASE: /* TODO */ break;
430 case K_RIGHTMOUSE: /* TODO */ break;
431 case K_RIGHTDRAG: /* TODO */ break;
432 case K_RIGHTRELEASE: /* TODO */ break;
433 case K_X1MOUSE: /* TODO */ break;
434 case K_X1DRAG: /* TODO */ break;
435 case K_X1RELEASE: /* TODO */ break;
436 case K_X2MOUSE: /* TODO */ break;
437 case K_X2DRAG: /* TODO */ break;
438 case K_X2RELEASE: /* TODO */ break;
439
440 /* TODO: handle all special keys and modifiers that terminal_loop()
441 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200442 }
443
444 /*
445 * Convert special keys to vterm keys:
446 * - Write keys to vterm: vterm_keyboard_key()
447 * - Write output to channel.
448 */
449 if (key != VTERM_KEY_NONE)
450 /* Special key, let vterm convert it. */
451 vterm_keyboard_key(vterm, key, mod);
452 else
453 /* Normal character, let vterm convert it. */
454 vterm_keyboard_unichar(vterm, c, mod);
455
456 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200457 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200458}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200459
Bram Moolenaar938783d2017-07-16 20:13:26 +0200460/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200461 * Wait for input and send it to the job.
462 * Return when the start of a CTRL-W command is typed or anything else that
463 * should be handled as a Normal mode command.
464 */
465 void
466terminal_loop(void)
467{
468 char buf[KEY_BUF_LEN];
469 int c;
470 size_t len;
471 static int mouse_was_outside = FALSE;
472 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200473 int termkey = 0;
474
475 if (*curwin->w_p_tk != NUL)
476 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200477
478 for (;;)
479 {
480 /* TODO: skip screen update when handling a sequence of keys. */
481 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200482 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200483 ++no_mapping;
484 ++allow_keys;
485 got_int = FALSE;
486 c = vgetc();
487 --no_mapping;
488 --allow_keys;
489
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200490 if (c == (termkey == 0 ? Ctrl_W : termkey))
491 {
492 stuffcharReadbuff(Ctrl_W);
493 return;
494 }
495
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200496 /* Catch keys that need to be handled as in Normal mode. */
497 switch (c)
498 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200499 case NUL:
500 case K_ZERO:
501 stuffcharReadbuff(c);
502 return;
503
504 case K_IGNORE: continue;
505
506 case K_LEFTDRAG:
507 case K_MIDDLEDRAG:
508 case K_RIGHTDRAG:
509 case K_X1DRAG:
510 case K_X2DRAG:
511 dragging_outside = mouse_was_outside;
512 /* FALLTHROUGH */
513 case K_LEFTMOUSE:
514 case K_LEFTMOUSE_NM:
515 case K_LEFTRELEASE:
516 case K_LEFTRELEASE_NM:
517 case K_MIDDLEMOUSE:
518 case K_MIDDLERELEASE:
519 case K_RIGHTMOUSE:
520 case K_RIGHTRELEASE:
521 case K_X1MOUSE:
522 case K_X1RELEASE:
523 case K_X2MOUSE:
524 case K_X2RELEASE:
525 if (mouse_row < W_WINROW(curwin)
526 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
527 || mouse_col < W_WINCOL(curwin)
528 || mouse_col >= W_ENDCOL(curwin)
529 || dragging_outside)
530 {
531 /* click outside the current window */
532 stuffcharReadbuff(c);
533 mouse_was_outside = TRUE;
534 return;
535 }
536 }
537 mouse_was_outside = FALSE;
538
539 /* Convert the typed key to a sequence of bytes for the job. */
540 len = term_convert_key(c, buf);
541 if (len > 0)
542 /* TODO: if FAIL is returned, stop? */
543 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200544 (char_u *)buf, (int)len, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200545 }
546}
547
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200548/*
549 * Called when a job has finished.
550 */
551 void
552term_job_ended(job_T *job)
553{
Bram Moolenaar21554412017-07-24 21:44:43 +0200554 term_T *term;
555 int did_one = FALSE;
556
557 for (term = first_term; term != NULL; term = term->tl_next)
558 if (term->tl_job == job)
559 {
560 vim_free(term->tl_title);
561 term->tl_title = NULL;
562 vim_free(term->tl_status_text);
563 term->tl_status_text = NULL;
564 redraw_buf_and_status_later(term->tl_buffer, VALID);
565 did_one = TRUE;
566 }
567 if (did_one)
Bram Moolenaar21554412017-07-24 21:44:43 +0200568 redraw_statuslines();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200569 if (curbuf->b_term != NULL)
570 {
571 if (curbuf->b_term->tl_job == job)
572 maketitle();
573 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar21554412017-07-24 21:44:43 +0200574 }
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200575}
576
577/*
578 * Return TRUE if the job for "buf" is still running.
579 */
Bram Moolenaar21554412017-07-24 21:44:43 +0200580 static int
581term_job_running(term_T *term)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200582{
Bram Moolenaar21554412017-07-24 21:44:43 +0200583 return term->tl_job != NULL && term->tl_job->jv_status == JOB_STARTED;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200584}
585
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200586 static void
587position_cursor(win_T *wp, VTermPos *pos)
588{
589 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
590 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
591}
592
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200593 static void
594may_toggle_cursor(term_T *term)
595{
596 if (curbuf == term->tl_buffer)
597 {
598 if (term->tl_cursor_visible)
599 cursor_on();
600 else
601 cursor_off();
602 }
603}
604
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200605 static int
606handle_damage(VTermRect rect, void *user)
607{
608 term_T *term = (term_T *)user;
609
610 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
611 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
612 redraw_buf_later(term->tl_buffer, NOT_VALID);
613 return 1;
614}
615
616 static int
617handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
618{
619 term_T *term = (term_T *)user;
620
621 /* TODO */
622 redraw_buf_later(term->tl_buffer, NOT_VALID);
623 return 1;
624}
625
626 static int
627handle_movecursor(
628 VTermPos pos,
629 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200630 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200631 void *user)
632{
633 term_T *term = (term_T *)user;
634 win_T *wp;
635 int is_current = FALSE;
636
637 FOR_ALL_WINDOWS(wp)
638 {
639 if (wp->w_buffer == term->tl_buffer)
640 {
641 position_cursor(wp, &pos);
642 if (wp == curwin)
643 is_current = TRUE;
644 }
645 }
646
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200647 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200648 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200649 {
650 may_toggle_cursor(term);
651 update_cursor(term, TRUE);
652 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200653
654 return 1;
655}
656
Bram Moolenaar21554412017-07-24 21:44:43 +0200657 static int
658handle_settermprop(
659 VTermProp prop,
660 VTermValue *value,
661 void *user)
662{
663 term_T *term = (term_T *)user;
664
665 switch (prop)
666 {
667 case VTERM_PROP_TITLE:
668 vim_free(term->tl_title);
669 term->tl_title = vim_strsave((char_u *)value->string);
670 vim_free(term->tl_status_text);
671 term->tl_status_text = NULL;
672 if (term == curbuf->b_term)
673 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200674 break;
675
676 case VTERM_PROP_CURSORVISIBLE:
677 term->tl_cursor_visible = value->boolean;
678 may_toggle_cursor(term);
679 out_flush();
680 break;
681
Bram Moolenaar21554412017-07-24 21:44:43 +0200682 default:
683 break;
684 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200685 /* Always return 1, otherwise vterm doesn't store the value internally. */
686 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +0200687}
688
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200689/*
690 * The job running in the terminal resized the terminal.
691 */
692 static int
693handle_resize(int rows, int cols, void *user)
694{
695 term_T *term = (term_T *)user;
696 win_T *wp;
697
698 term->tl_rows = rows;
699 term->tl_cols = cols;
700 FOR_ALL_WINDOWS(wp)
701 {
702 if (wp->w_buffer == term->tl_buffer)
703 {
704 win_setheight_win(rows, wp);
705 win_setwidth_win(cols, wp);
706 }
707 }
708
709 redraw_buf_later(term->tl_buffer, NOT_VALID);
710 return 1;
711}
712
Bram Moolenaar21554412017-07-24 21:44:43 +0200713static VTermScreenCallbacks screen_callbacks = {
714 handle_damage, /* damage */
715 handle_moverect, /* moverect */
716 handle_movecursor, /* movecursor */
717 handle_settermprop, /* settermprop */
718 NULL, /* bell */
719 handle_resize, /* resize */
720 NULL, /* sb_pushline */
721 NULL /* sb_popline */
722};
723
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200724/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200725 * Reverse engineer the RGB value into a cterm color index.
726 * First color is 1. Return 0 if no match found.
727 */
728 static int
729color2index(VTermColor *color)
730{
731 int red = color->red;
732 int blue = color->blue;
733 int green = color->green;
734
735 if (red == 0)
736 {
737 if (green == 0)
738 {
739 if (blue == 0)
740 return 1; /* black */
741 if (blue == 224)
742 return 5; /* blue */
743 }
744 else if (green == 224)
745 {
746 if (blue == 0)
747 return 3; /* green */
748 if (blue == 224)
749 return 7; /* cyan */
750 }
751 }
752 else if (red == 224)
753 {
754 if (green == 0)
755 {
756 if (blue == 0)
757 return 2; /* red */
758 if (blue == 224)
759 return 6; /* magenta */
760 }
761 else if (green == 224)
762 {
763 if (blue == 0)
764 return 4; /* yellow */
765 if (blue == 224)
766 return 8; /* white */
767 }
768 }
769 else if (red == 128)
770 {
771 if (green == 128 && blue == 128)
Bram Moolenaar8a773062017-07-24 22:29:21 +0200772 return 9; /* high intensity black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200773 }
774 else if (red == 255)
775 {
776 if (green == 64)
777 {
778 if (blue == 64)
779 return 10; /* high intensity red */
780 if (blue == 255)
781 return 14; /* high intensity magenta */
782 }
783 else if (green == 255)
784 {
785 if (blue == 64)
786 return 12; /* high intensity yellow */
787 if (blue == 255)
788 return 16; /* high intensity white */
789 }
790 }
791 else if (red == 64)
792 {
793 if (green == 64)
794 {
795 if (blue == 255)
796 return 13; /* high intensity blue */
797 }
798 else if (green == 255)
799 {
800 if (blue == 64)
801 return 11; /* high intensity green */
802 if (blue == 255)
803 return 15; /* high intensity cyan */
804 }
805 }
806 if (t_colors >= 256)
807 {
808 if (red == blue && red == green)
809 {
810 /* 24-color greyscale */
811 static int cutoff[23] = {
812 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
813 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
814 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
815 int i;
816
817 for (i = 0; i < 23; ++i)
818 if (red < cutoff[i])
819 return i + 233;
820 return 256;
821 }
822
823 /* 216-color cube */
824 return 17 + ((red + 25) / 0x33) * 36
825 + ((green + 25) / 0x33) * 6
826 + (blue + 25) / 0x33;
827 }
828 return 0;
829}
830
831/*
832 * Convert the attributes of a vterm cell into an attribute index.
833 */
834 static int
835cell2attr(VTermScreenCell *cell)
836{
837 int attr = 0;
838
839 if (cell->attrs.bold)
840 attr |= HL_BOLD;
841 if (cell->attrs.underline)
842 attr |= HL_UNDERLINE;
843 if (cell->attrs.italic)
844 attr |= HL_ITALIC;
845 if (cell->attrs.strike)
846 attr |= HL_STANDOUT;
847 if (cell->attrs.reverse)
848 attr |= HL_INVERSE;
849 if (cell->attrs.strike)
850 attr |= HL_UNDERLINE;
851
852#ifdef FEAT_GUI
853 if (gui.in_use)
854 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200855 guicolor_T fg, bg;
856
857 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
858 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
859 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200860 }
861 else
862#endif
863#ifdef FEAT_TERMGUICOLORS
864 if (p_tgc)
865 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200866 guicolor_T fg, bg;
867
868 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
869 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
870
871 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200872 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200873 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200874#endif
875 {
876 return get_cterm_attr_idx(attr, color2index(&cell->fg),
877 color2index(&cell->bg));
878 }
879 return 0;
880}
881
882/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200883 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200884 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200885 void
886term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200887{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200888 term_T *term = wp->w_buffer->b_term;
889 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200890 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200891 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200892 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200893
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200894 /*
895 * If the window was resized a redraw will be triggered and we get here.
896 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
897 */
898 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
899 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200900 {
901 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
902 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
903
904 vterm_set_size(vterm, rows, cols);
905 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
906 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200907 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200908 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200909
910 /* The cursor may have been moved when resizing. */
911 vterm_state_get_cursorpos(state, &pos);
912 position_cursor(wp, &pos);
913
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200914 /* TODO: Only redraw what changed. */
915 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200916 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200917 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200918 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200919
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200920 if (pos.row < term->tl_rows)
921 {
922 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200923 {
924 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200925 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200926
Bram Moolenaareeac6772017-07-23 15:48:37 +0200927 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
928 vim_memset(&cell, 0, sizeof(cell));
929
930 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200931 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200932 if (c == NUL)
933 {
934 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +0200935#if defined(FEAT_MBYTE)
936 if (enc_utf8)
937 ScreenLinesUC[off] = NUL;
938#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200939 }
940 else
941 {
942#if defined(FEAT_MBYTE)
943 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200944 {
945 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200946 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200947 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200948 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200949 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200950 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200951 if (enc_utf8)
952 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200953 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200954#else
955 ScreenLines[off] = c;
956#endif
957 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200958 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200959
960 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200961 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200962 if (cell.width == 2)
963 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200964 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200965#if defined(FEAT_MBYTE)
966 if (enc_utf8)
967 ScreenLinesUC[off] = NUL;
968#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200969 ++pos.col;
970 ++off;
971 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200972 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200973 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200974 else
975 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200976
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200977 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
978 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200979 }
980}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200981
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200982/*
983 * Set job options common for Unix and MS-Windows.
984 */
985 static void
986setup_job_options(jobopt_T *opt, int rows, int cols)
987{
988 clear_job_options(opt);
989 opt->jo_mode = MODE_RAW;
990 opt->jo_out_mode = MODE_RAW;
991 opt->jo_err_mode = MODE_RAW;
992 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200993
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200994 opt->jo_io[PART_OUT] = JIO_BUFFER;
995 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200996 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
997
998 opt->jo_modifiable[PART_OUT] = 0;
999 opt->jo_modifiable[PART_ERR] = 0;
1000 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1001
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001002 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1003 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001004 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001005 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1006
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001007 opt->jo_term_rows = rows;
1008 opt->jo_term_cols = cols;
1009}
1010
1011/*
1012 * Create a new vterm and initialize it.
1013 */
1014 static void
1015create_vterm(term_T *term, int rows, int cols)
1016{
1017 VTerm *vterm;
1018 VTermScreen *screen;
1019
1020 vterm = vterm_new(rows, cols);
1021 term->tl_vterm = vterm;
1022 screen = vterm_obtain_screen(vterm);
1023 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1024 /* TODO: depends on 'encoding'. */
1025 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001026
1027 /* Vterm uses a default black background. Set it to white when
1028 * 'background' is "light". */
1029 if (*p_bg == 'l')
1030 {
1031 VTermColor fg, bg;
1032
1033 fg.red = fg.green = fg.blue = 0;
1034 bg.red = bg.green = bg.blue = 255;
1035 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1036 }
1037
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001038 /* Required to initialize most things. */
1039 vterm_screen_reset(screen, 1 /* hard */);
1040}
1041
Bram Moolenaar21554412017-07-24 21:44:43 +02001042/*
1043 * Return the text to show for the buffer name and status.
1044 */
1045 char_u *
1046term_get_status_text(term_T *term)
1047{
1048 if (term->tl_status_text == NULL)
1049 {
1050 char_u *txt;
1051 size_t len;
1052
1053 if (term->tl_title != NULL)
1054 txt = term->tl_title;
1055 else if (term_job_running(term))
1056 txt = (char_u *)_("running");
1057 else
1058 txt = (char_u *)_("finished");
1059 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001060 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001061 if (term->tl_status_text != NULL)
1062 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1063 term->tl_buffer->b_fname, txt);
1064 }
1065 return term->tl_status_text;
1066}
1067
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001068# ifdef WIN3264
1069
1070#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1071#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1072
Bram Moolenaar8a773062017-07-24 22:29:21 +02001073void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001074void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001075void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001076BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1077void (*winpty_config_set_initial_size)(void*, int, int);
1078LPCWSTR (*winpty_conin_name)(void*);
1079LPCWSTR (*winpty_conout_name)(void*);
1080LPCWSTR (*winpty_conerr_name)(void*);
1081void (*winpty_free)(void*);
1082void (*winpty_config_free)(void*);
1083void (*winpty_spawn_config_free)(void*);
1084void (*winpty_error_free)(void*);
1085LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001086BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001087
1088/**************************************
1089 * 2. MS-Windows implementation.
1090 */
1091
1092#define WINPTY_DLL "winpty.dll"
1093
1094static HINSTANCE hWinPtyDLL = NULL;
1095
1096 int
1097dyn_winpty_init(void)
1098{
1099 int i;
1100 static struct
1101 {
1102 char *name;
1103 FARPROC *ptr;
1104 } winpty_entry[] =
1105 {
1106 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1107 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1108 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1109 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1110 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1111 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1112 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1113 {"winpty_free", (FARPROC*)&winpty_free},
1114 {"winpty_open", (FARPROC*)&winpty_open},
1115 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1116 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1117 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1118 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001119 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001120 {NULL, NULL}
1121 };
1122
1123 /* No need to initialize twice. */
1124 if (hWinPtyDLL)
1125 return 1;
1126 /* Load winpty.dll */
1127 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1128 if (!hWinPtyDLL)
1129 {
1130 EMSG2(_(e_loadlib), WINPTY_DLL);
1131 return 0;
1132 }
1133 for (i = 0; winpty_entry[i].name != NULL
1134 && winpty_entry[i].ptr != NULL; ++i)
1135 {
1136 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1137 winpty_entry[i].name)) == NULL)
1138 {
1139 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1140 return 0;
1141 }
1142 }
1143
1144 return 1;
1145}
1146
1147/*
1148 * Create a new terminal of "rows" by "cols" cells.
1149 * Store a reference in "term".
1150 * Return OK or FAIL.
1151 */
1152 static int
1153term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1154{
1155 WCHAR *p = enc_to_utf16(cmd, NULL);
1156 channel_T *channel = NULL;
1157 job_T *job = NULL;
1158 jobopt_T opt;
1159 DWORD error;
1160 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1161 void *winpty_err;
1162 void *spawn_config;
1163
1164 if (!dyn_winpty_init())
1165 return FAIL;
1166
1167 if (p == NULL)
1168 return FAIL;
1169
1170 job = job_alloc();
1171 if (job == NULL)
1172 goto failed;
1173
1174 channel = add_channel();
1175 if (channel == NULL)
1176 goto failed;
1177
1178 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1179 if (term->tl_winpty_config == NULL)
1180 goto failed;
1181
1182 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1183 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1184 if (term->tl_winpty == NULL)
1185 goto failed;
1186
1187 spawn_config = winpty_spawn_config_new(
1188 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1189 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1190 NULL,
1191 p,
1192 NULL,
1193 NULL,
1194 &winpty_err);
1195 if (spawn_config == NULL)
1196 goto failed;
1197
1198 channel = add_channel();
1199 if (channel == NULL)
1200 goto failed;
1201
1202 job = job_alloc();
1203 if (job == NULL)
1204 goto failed;
1205
1206 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1207 &child_thread_handle, &error, &winpty_err))
1208 goto failed;
1209
1210 channel_set_pipes(channel,
1211 (sock_T) CreateFileW(
1212 winpty_conin_name(term->tl_winpty),
1213 GENERIC_WRITE, 0, NULL,
1214 OPEN_EXISTING, 0, NULL),
1215 (sock_T) CreateFileW(
1216 winpty_conout_name(term->tl_winpty),
1217 GENERIC_READ, 0, NULL,
1218 OPEN_EXISTING, 0, NULL),
1219 (sock_T) CreateFileW(
1220 winpty_conerr_name(term->tl_winpty),
1221 GENERIC_READ, 0, NULL,
1222 OPEN_EXISTING, 0, NULL));
1223
1224 jo = CreateJobObject(NULL, NULL);
1225 if (jo == NULL)
1226 goto failed;
1227
1228 if (!AssignProcessToJobObject(jo, child_process_handle))
1229 goto failed;
1230
1231 winpty_spawn_config_free(spawn_config);
1232
1233 create_vterm(term, rows, cols);
1234
1235 setup_job_options(&opt, rows, cols);
1236 channel_set_job(channel, job, &opt);
1237
1238 job->jv_channel = channel;
1239 job->jv_proc_info.hProcess = child_process_handle;
1240 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1241 job->jv_job_object = jo;
1242 job->jv_status = JOB_STARTED;
1243 term->tl_job = job;
1244
1245 return OK;
1246
1247failed:
1248 if (channel != NULL)
1249 channel_clear(channel);
1250 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001251 {
1252 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001253 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001254 }
1255 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001256 if (jo != NULL)
1257 CloseHandle(jo);
1258 if (term->tl_winpty != NULL)
1259 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001260 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001261 if (term->tl_winpty_config != NULL)
1262 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001263 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001264 if (winpty_err != NULL)
1265 {
1266 char_u *msg = utf16_to_enc(
1267 (short_u *)winpty_error_msg(winpty_err), NULL);
1268
1269 EMSG(msg);
1270 winpty_error_free(winpty_err);
1271 }
1272 return FAIL;
1273}
1274
1275/*
1276 * Free the terminal emulator part of "term".
1277 */
1278 static void
1279term_free(term_T *term)
1280{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001281 if (term->tl_winpty != NULL)
1282 winpty_free(term->tl_winpty);
1283 if (term->tl_winpty_config != NULL)
1284 winpty_config_free(term->tl_winpty_config);
1285 if (term->tl_vterm != NULL)
1286 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001287}
1288
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001289/*
1290 * Request size to terminal.
1291 */
1292 static void
1293term_report_winsize(term_T *term, int rows, int cols)
1294{
1295 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1296}
1297
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001298# else
1299
1300/**************************************
1301 * 3. Unix-like implementation.
1302 */
1303
1304/*
1305 * Create a new terminal of "rows" by "cols" cells.
1306 * Start job for "cmd".
1307 * Store the pointers in "term".
1308 * Return OK or FAIL.
1309 */
1310 static int
1311term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1312{
1313 typval_T argvars[2];
1314 jobopt_T opt;
1315
1316 create_vterm(term, rows, cols);
1317
1318 argvars[0].v_type = VAR_STRING;
1319 argvars[0].vval.v_string = cmd;
1320 setup_job_options(&opt, rows, cols);
1321 term->tl_job = job_start(argvars, &opt);
1322
Bram Moolenaar61a66052017-07-22 18:39:00 +02001323 return term->tl_job != NULL
1324 && term->tl_job->jv_channel != NULL
1325 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001326}
1327
1328/*
1329 * Free the terminal emulator part of "term".
1330 */
1331 static void
1332term_free(term_T *term)
1333{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001334 if (term->tl_vterm != NULL)
1335 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001336}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001337
1338/*
1339 * Request size to terminal.
1340 */
1341 static void
1342term_report_winsize(term_T *term, int rows, int cols)
1343{
1344 /* Use an ioctl() to report the new window size to the job. */
1345 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1346 {
1347 int fd = -1;
1348 int part;
1349
1350 for (part = PART_OUT; part < PART_COUNT; ++part)
1351 {
1352 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1353 if (isatty(fd))
1354 break;
1355 }
1356 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1357 mch_stop_job(term->tl_job, (char_u *)"winch");
1358 }
1359}
1360
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001361# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001362
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001363#endif /* FEAT_TERMINAL */