blob: 4e87b51a1d3f6e71e5967e825fdbe84d283cace5 [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 Moolenaar1f28b4c2017-07-28 13:48:34 +020036 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar1c844932017-07-24 23:36:41 +020037 * - include functions from #1871
38 * - do not store terminal buffer in viminfo. Or prefix term:// ?
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 Moolenaar8f84c3a2017-07-22 16:14:44 +020053 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020054 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020055 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020056 * - implement "term" for job_start(): more job options when starting a
57 * terminal.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020058 * - implement term_list() list of buffers with a terminal
59 * - implement term_getsize(buf)
60 * - implement term_setsize(buf)
61 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
62 * - implement term_wait(buf) wait for screen to be updated
63 * - implement term_scrape(buf, row) inspect terminal screen
64 * - implement term_open(command, options) open terminal window
65 * - implement term_getjob(buf)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
67 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020068 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020069 */
70
71#include "vim.h"
72
73#ifdef FEAT_TERMINAL
74
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020075#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020076# define MIN(x,y) (x < y ? x : y)
77# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020078#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020080#include "libvterm/include/vterm.h"
81
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082/* typedef term_T in structs.h */
83struct terminal_S {
84 term_T *tl_next;
85
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020086#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020087 void *tl_winpty_config;
88 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020089#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020090 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020092 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020093
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020094 /* last known vterm size */
95 int tl_rows;
96 int tl_cols;
97 /* vterm size does not follow window size */
98 int tl_rows_fixed;
99 int tl_cols_fixed;
100
Bram Moolenaar21554412017-07-24 21:44:43 +0200101 char_u *tl_title; /* NULL or allocated */
102 char_u *tl_status_text; /* NULL or allocated */
103
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200104 /* Range of screen rows to update. Zero based. */
105 int tl_dirty_row_start; /* -1 if nothing dirty */
106 int tl_dirty_row_end; /* row below last one to update */
107
108 pos_T tl_cursor;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200109 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200110};
111
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200112/*
113 * List of all active terminals.
114 */
115static term_T *first_term = NULL;
116
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200117
118#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
119#define KEY_BUF_LEN 200
120
121/*
122 * Functions with separate implementation for MS-Windows and Unix-like systems.
123 */
124static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200125static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200126static void term_free(term_T *term);
127
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200128/**************************************
129 * 1. Generic code for all systems.
130 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200131
132/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200133 * Determine the terminal size from 'termsize' and the current window.
134 * Assumes term->tl_rows and term->tl_cols are zero.
135 */
136 static void
137set_term_and_win_size(term_T *term)
138{
139 if (*curwin->w_p_tms != NUL)
140 {
141 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
142
143 term->tl_rows = atoi((char *)curwin->w_p_tms);
144 term->tl_cols = atoi((char *)p);
145 }
146 if (term->tl_rows == 0)
147 term->tl_rows = curwin->w_height;
148 else
149 {
150 win_setheight_win(term->tl_rows, curwin);
151 term->tl_rows_fixed = TRUE;
152 }
153 if (term->tl_cols == 0)
154 term->tl_cols = curwin->w_width;
155 else
156 {
157 win_setwidth_win(term->tl_cols, curwin);
158 term->tl_cols_fixed = TRUE;
159 }
160}
161
162/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200163 * ":terminal": open a terminal window and execute a job in it.
164 */
165 void
166ex_terminal(exarg_T *eap)
167{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200168 exarg_T split_ea;
169 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200170 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200171 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200172
173 if (check_restricted() || check_secure())
174 return;
175
176 term = (term_T *)alloc_clear(sizeof(term_T));
177 if (term == NULL)
178 return;
179 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200180 term->tl_cursor_visible = TRUE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200181
182 /* Open a new window or tab. */
183 vim_memset(&split_ea, 0, sizeof(split_ea));
184 split_ea.cmdidx = CMD_new;
185 split_ea.cmd = (char_u *)"new";
186 split_ea.arg = (char_u *)"";
187 ex_splitview(&split_ea);
188 if (curwin == old_curwin)
189 {
190 /* split failed */
191 vim_free(term);
192 return;
193 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200194 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200195 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200196
197 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200198 term->tl_next = first_term;
199 first_term = term;
200
Bram Moolenaar293424c2017-07-26 23:11:01 +0200201 if (cmd == NULL || *cmd == NUL)
202 cmd = p_sh;
203
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200204 if (buflist_findname(cmd) == NULL)
205 curbuf->b_ffname = vim_strsave(cmd);
206 else
207 {
208 int i;
209 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200210 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200211
212 for (i = 1; p != NULL; ++i)
213 {
214 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
215 if (buflist_findname(p) == NULL)
216 {
217 curbuf->b_ffname = p;
218 break;
219 }
220 }
221 }
222 curbuf->b_fname = curbuf->b_ffname;
223
224 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
225 curbuf->b_changed = TRUE;
226 curbuf->b_p_ma = FALSE;
227 set_string_option_direct((char_u *)"buftype", -1,
228 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200229
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200230 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200231
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200232 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200233 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200234 {
235 /* store the size we ended up with */
236 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200237 }
238 else
239 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200240 free_terminal(term);
241 curbuf->b_term = NULL;
242
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200243 /* Wiping out the buffer will also close the window and call
244 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200245 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200246 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200247
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200248 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249}
250
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200251/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200252 * Free a terminal and everything it refers to.
253 * Kills the job if there is one.
254 * Called when wiping out a buffer.
255 */
256 void
257free_terminal(term_T *term)
258{
259 term_T *tp;
260
261 if (term == NULL)
262 return;
263 if (first_term == term)
264 first_term = term->tl_next;
265 else
266 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
267 if (tp->tl_next == term)
268 {
269 tp->tl_next = term->tl_next;
270 break;
271 }
272
273 if (term->tl_job != NULL)
274 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200275 if (term->tl_job->jv_status != JOB_ENDED
276 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200277 job_stop(term->tl_job, NULL, "kill");
278 job_unref(term->tl_job);
279 }
280
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200281 term_free(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200282 vim_free(term->tl_title);
283 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200284 vim_free(term);
285}
286
287/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200288 * Write job output "msg[len]" to the vterm.
289 */
290 static void
291term_write_job_output(term_T *term, char_u *msg, size_t len)
292{
293 VTerm *vterm = term->tl_vterm;
294 char_u *p;
295 size_t done;
296 size_t len_now;
297
298 for (done = 0; done < len; done += len_now)
299 {
300 for (p = msg + done; p < msg + len; )
301 {
302 if (*p == NL)
303 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200304 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200305 }
306 len_now = p - msg - done;
307 vterm_input_write(vterm, (char *)msg + done, len_now);
308 if (p < msg + len && *p == NL)
309 {
310 /* Convert NL to CR-NL, that appears to work best. */
311 vterm_input_write(vterm, "\r\n", 2);
312 ++len_now;
313 }
314 }
315
316 /* this invokes the damage callbacks */
317 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
318}
319
Bram Moolenaar1c844932017-07-24 23:36:41 +0200320 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200321update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200322{
Bram Moolenaar1c844932017-07-24 23:36:41 +0200323 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200324 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200325 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200326 if (term->tl_cursor_visible)
327 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200328 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200329#ifdef FEAT_GUI
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200330 if (gui.in_use && term->tl_cursor_visible)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200331 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 Moolenaar1f28b4c2017-07-28 13:48:34 +0200461 * Get a key from the user without mapping.
462 * TODO: use terminal mode mappings.
463 */
464 static int
465term_vgetc()
466{
467 int c;
468
469 ++no_mapping;
470 ++allow_keys;
471 got_int = FALSE;
472 c = vgetc();
473 --no_mapping;
474 --allow_keys;
475 return c;
476}
477
478/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200479 * Wait for input and send it to the job.
480 * Return when the start of a CTRL-W command is typed or anything else that
481 * should be handled as a Normal mode command.
482 */
483 void
484terminal_loop(void)
485{
486 char buf[KEY_BUF_LEN];
487 int c;
488 size_t len;
489 static int mouse_was_outside = FALSE;
490 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200491 int termkey = 0;
492
493 if (*curwin->w_p_tk != NUL)
494 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200495
496 for (;;)
497 {
498 /* TODO: skip screen update when handling a sequence of keys. */
499 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200500 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200501 c = term_vgetc();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200502
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200503 if (c == (termkey == 0 ? Ctrl_W : termkey))
504 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200505#ifdef FEAT_CMDL_INFO
506 if (add_to_showcmd(c))
507 out_flush();
508#endif
509 c = term_vgetc();
510#ifdef FEAT_CMDL_INFO
511 clear_showcmd();
512#endif
513
514 if (termkey == 0 && c == '.')
515 /* "CTRL-W .": send CTRL-W to the job */
516 c = Ctrl_W;
517 else if (termkey == 0 || c != termkey)
518 {
519 stuffcharReadbuff(Ctrl_W);
520 stuffcharReadbuff(c);
521 return;
522 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200523 }
524
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200525 /* Catch keys that need to be handled as in Normal mode. */
526 switch (c)
527 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200528 case NUL:
529 case K_ZERO:
530 stuffcharReadbuff(c);
531 return;
532
533 case K_IGNORE: continue;
534
535 case K_LEFTDRAG:
536 case K_MIDDLEDRAG:
537 case K_RIGHTDRAG:
538 case K_X1DRAG:
539 case K_X2DRAG:
540 dragging_outside = mouse_was_outside;
541 /* FALLTHROUGH */
542 case K_LEFTMOUSE:
543 case K_LEFTMOUSE_NM:
544 case K_LEFTRELEASE:
545 case K_LEFTRELEASE_NM:
546 case K_MIDDLEMOUSE:
547 case K_MIDDLERELEASE:
548 case K_RIGHTMOUSE:
549 case K_RIGHTRELEASE:
550 case K_X1MOUSE:
551 case K_X1RELEASE:
552 case K_X2MOUSE:
553 case K_X2RELEASE:
554 if (mouse_row < W_WINROW(curwin)
555 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
556 || mouse_col < W_WINCOL(curwin)
557 || mouse_col >= W_ENDCOL(curwin)
558 || dragging_outside)
559 {
560 /* click outside the current window */
561 stuffcharReadbuff(c);
562 mouse_was_outside = TRUE;
563 return;
564 }
565 }
566 mouse_was_outside = FALSE;
567
568 /* Convert the typed key to a sequence of bytes for the job. */
569 len = term_convert_key(c, buf);
570 if (len > 0)
571 /* TODO: if FAIL is returned, stop? */
572 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200573 (char_u *)buf, (int)len, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200574 }
575}
576
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200577/*
578 * Called when a job has finished.
579 */
580 void
581term_job_ended(job_T *job)
582{
Bram Moolenaar21554412017-07-24 21:44:43 +0200583 term_T *term;
584 int did_one = FALSE;
585
586 for (term = first_term; term != NULL; term = term->tl_next)
587 if (term->tl_job == job)
588 {
589 vim_free(term->tl_title);
590 term->tl_title = NULL;
591 vim_free(term->tl_status_text);
592 term->tl_status_text = NULL;
593 redraw_buf_and_status_later(term->tl_buffer, VALID);
594 did_one = TRUE;
595 }
596 if (did_one)
Bram Moolenaar21554412017-07-24 21:44:43 +0200597 redraw_statuslines();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200598 if (curbuf->b_term != NULL)
599 {
600 if (curbuf->b_term->tl_job == job)
601 maketitle();
602 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar21554412017-07-24 21:44:43 +0200603 }
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200604}
605
606/*
607 * Return TRUE if the job for "buf" is still running.
608 */
Bram Moolenaar21554412017-07-24 21:44:43 +0200609 static int
610term_job_running(term_T *term)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200611{
Bram Moolenaar21554412017-07-24 21:44:43 +0200612 return term->tl_job != NULL && term->tl_job->jv_status == JOB_STARTED;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200613}
614
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200615 static void
616position_cursor(win_T *wp, VTermPos *pos)
617{
618 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
619 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
620}
621
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200622 static void
623may_toggle_cursor(term_T *term)
624{
625 if (curbuf == term->tl_buffer)
626 {
627 if (term->tl_cursor_visible)
628 cursor_on();
629 else
630 cursor_off();
631 }
632}
633
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200634 static int
635handle_damage(VTermRect rect, void *user)
636{
637 term_T *term = (term_T *)user;
638
639 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
640 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
641 redraw_buf_later(term->tl_buffer, NOT_VALID);
642 return 1;
643}
644
645 static int
646handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
647{
648 term_T *term = (term_T *)user;
649
650 /* TODO */
651 redraw_buf_later(term->tl_buffer, NOT_VALID);
652 return 1;
653}
654
655 static int
656handle_movecursor(
657 VTermPos pos,
658 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200659 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200660 void *user)
661{
662 term_T *term = (term_T *)user;
663 win_T *wp;
664 int is_current = FALSE;
665
666 FOR_ALL_WINDOWS(wp)
667 {
668 if (wp->w_buffer == term->tl_buffer)
669 {
670 position_cursor(wp, &pos);
671 if (wp == curwin)
672 is_current = TRUE;
673 }
674 }
675
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200676 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200677 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200678 {
679 may_toggle_cursor(term);
680 update_cursor(term, TRUE);
681 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200682
683 return 1;
684}
685
Bram Moolenaar21554412017-07-24 21:44:43 +0200686 static int
687handle_settermprop(
688 VTermProp prop,
689 VTermValue *value,
690 void *user)
691{
692 term_T *term = (term_T *)user;
693
694 switch (prop)
695 {
696 case VTERM_PROP_TITLE:
697 vim_free(term->tl_title);
698 term->tl_title = vim_strsave((char_u *)value->string);
699 vim_free(term->tl_status_text);
700 term->tl_status_text = NULL;
701 if (term == curbuf->b_term)
702 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200703 break;
704
705 case VTERM_PROP_CURSORVISIBLE:
706 term->tl_cursor_visible = value->boolean;
707 may_toggle_cursor(term);
708 out_flush();
709 break;
710
Bram Moolenaar21554412017-07-24 21:44:43 +0200711 default:
712 break;
713 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200714 /* Always return 1, otherwise vterm doesn't store the value internally. */
715 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +0200716}
717
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200718/*
719 * The job running in the terminal resized the terminal.
720 */
721 static int
722handle_resize(int rows, int cols, void *user)
723{
724 term_T *term = (term_T *)user;
725 win_T *wp;
726
727 term->tl_rows = rows;
728 term->tl_cols = cols;
729 FOR_ALL_WINDOWS(wp)
730 {
731 if (wp->w_buffer == term->tl_buffer)
732 {
733 win_setheight_win(rows, wp);
734 win_setwidth_win(cols, wp);
735 }
736 }
737
738 redraw_buf_later(term->tl_buffer, NOT_VALID);
739 return 1;
740}
741
Bram Moolenaar21554412017-07-24 21:44:43 +0200742static VTermScreenCallbacks screen_callbacks = {
743 handle_damage, /* damage */
744 handle_moverect, /* moverect */
745 handle_movecursor, /* movecursor */
746 handle_settermprop, /* settermprop */
747 NULL, /* bell */
748 handle_resize, /* resize */
749 NULL, /* sb_pushline */
750 NULL /* sb_popline */
751};
752
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200753/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200754 * Reverse engineer the RGB value into a cterm color index.
755 * First color is 1. Return 0 if no match found.
756 */
757 static int
758color2index(VTermColor *color)
759{
760 int red = color->red;
761 int blue = color->blue;
762 int green = color->green;
763
764 if (red == 0)
765 {
766 if (green == 0)
767 {
768 if (blue == 0)
769 return 1; /* black */
770 if (blue == 224)
771 return 5; /* blue */
772 }
773 else if (green == 224)
774 {
775 if (blue == 0)
776 return 3; /* green */
777 if (blue == 224)
778 return 7; /* cyan */
779 }
780 }
781 else if (red == 224)
782 {
783 if (green == 0)
784 {
785 if (blue == 0)
786 return 2; /* red */
787 if (blue == 224)
788 return 6; /* magenta */
789 }
790 else if (green == 224)
791 {
792 if (blue == 0)
793 return 4; /* yellow */
794 if (blue == 224)
795 return 8; /* white */
796 }
797 }
798 else if (red == 128)
799 {
800 if (green == 128 && blue == 128)
Bram Moolenaar8a773062017-07-24 22:29:21 +0200801 return 9; /* high intensity black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200802 }
803 else if (red == 255)
804 {
805 if (green == 64)
806 {
807 if (blue == 64)
808 return 10; /* high intensity red */
809 if (blue == 255)
810 return 14; /* high intensity magenta */
811 }
812 else if (green == 255)
813 {
814 if (blue == 64)
815 return 12; /* high intensity yellow */
816 if (blue == 255)
817 return 16; /* high intensity white */
818 }
819 }
820 else if (red == 64)
821 {
822 if (green == 64)
823 {
824 if (blue == 255)
825 return 13; /* high intensity blue */
826 }
827 else if (green == 255)
828 {
829 if (blue == 64)
830 return 11; /* high intensity green */
831 if (blue == 255)
832 return 15; /* high intensity cyan */
833 }
834 }
835 if (t_colors >= 256)
836 {
837 if (red == blue && red == green)
838 {
839 /* 24-color greyscale */
840 static int cutoff[23] = {
841 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
842 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
843 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
844 int i;
845
846 for (i = 0; i < 23; ++i)
847 if (red < cutoff[i])
848 return i + 233;
849 return 256;
850 }
851
852 /* 216-color cube */
853 return 17 + ((red + 25) / 0x33) * 36
854 + ((green + 25) / 0x33) * 6
855 + (blue + 25) / 0x33;
856 }
857 return 0;
858}
859
860/*
861 * Convert the attributes of a vterm cell into an attribute index.
862 */
863 static int
864cell2attr(VTermScreenCell *cell)
865{
866 int attr = 0;
867
868 if (cell->attrs.bold)
869 attr |= HL_BOLD;
870 if (cell->attrs.underline)
871 attr |= HL_UNDERLINE;
872 if (cell->attrs.italic)
873 attr |= HL_ITALIC;
874 if (cell->attrs.strike)
875 attr |= HL_STANDOUT;
876 if (cell->attrs.reverse)
877 attr |= HL_INVERSE;
878 if (cell->attrs.strike)
879 attr |= HL_UNDERLINE;
880
881#ifdef FEAT_GUI
882 if (gui.in_use)
883 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200884 guicolor_T fg, bg;
885
886 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
887 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
888 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200889 }
890 else
891#endif
892#ifdef FEAT_TERMGUICOLORS
893 if (p_tgc)
894 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200895 guicolor_T fg, bg;
896
897 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
898 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
899
900 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200901 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200902 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200903#endif
904 {
905 return get_cterm_attr_idx(attr, color2index(&cell->fg),
906 color2index(&cell->bg));
907 }
908 return 0;
909}
910
911/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200912 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200913 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200914 void
915term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200916{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200917 term_T *term = wp->w_buffer->b_term;
918 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200919 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200920 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200921 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200922
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200923 /*
924 * If the window was resized a redraw will be triggered and we get here.
925 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
926 */
927 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
928 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200929 {
930 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
931 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
932
933 vterm_set_size(vterm, rows, cols);
934 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
935 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200936 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200937 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200938
939 /* The cursor may have been moved when resizing. */
940 vterm_state_get_cursorpos(state, &pos);
941 position_cursor(wp, &pos);
942
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200943 /* TODO: Only redraw what changed. */
944 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200945 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200946 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200947 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200948
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200949 if (pos.row < term->tl_rows)
950 {
951 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200952 {
953 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200954 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200955
Bram Moolenaareeac6772017-07-23 15:48:37 +0200956 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
957 vim_memset(&cell, 0, sizeof(cell));
958
959 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200960 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200961 if (c == NUL)
962 {
963 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +0200964#if defined(FEAT_MBYTE)
965 if (enc_utf8)
966 ScreenLinesUC[off] = NUL;
967#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200968 }
969 else
970 {
971#if defined(FEAT_MBYTE)
972 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200973 {
974 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200975 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200976 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200977 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200978 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200979 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200980 if (enc_utf8)
981 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200982 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200983#else
984 ScreenLines[off] = c;
985#endif
986 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200987 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200988
989 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200990 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200991 if (cell.width == 2)
992 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200993 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200994#if defined(FEAT_MBYTE)
995 if (enc_utf8)
996 ScreenLinesUC[off] = NUL;
997#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200998 ++pos.col;
999 ++off;
1000 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001001 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001002 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001003 else
1004 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001005
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001006 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1007 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001008 }
1009}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001010
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001011/*
1012 * Set job options common for Unix and MS-Windows.
1013 */
1014 static void
1015setup_job_options(jobopt_T *opt, int rows, int cols)
1016{
1017 clear_job_options(opt);
1018 opt->jo_mode = MODE_RAW;
1019 opt->jo_out_mode = MODE_RAW;
1020 opt->jo_err_mode = MODE_RAW;
1021 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001022
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001023 opt->jo_io[PART_OUT] = JIO_BUFFER;
1024 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001025 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1026
1027 opt->jo_modifiable[PART_OUT] = 0;
1028 opt->jo_modifiable[PART_ERR] = 0;
1029 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1030
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001031 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1032 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001033 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001034 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1035
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001036 opt->jo_term_rows = rows;
1037 opt->jo_term_cols = cols;
1038}
1039
1040/*
1041 * Create a new vterm and initialize it.
1042 */
1043 static void
1044create_vterm(term_T *term, int rows, int cols)
1045{
1046 VTerm *vterm;
1047 VTermScreen *screen;
1048
1049 vterm = vterm_new(rows, cols);
1050 term->tl_vterm = vterm;
1051 screen = vterm_obtain_screen(vterm);
1052 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1053 /* TODO: depends on 'encoding'. */
1054 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001055
1056 /* Vterm uses a default black background. Set it to white when
1057 * 'background' is "light". */
1058 if (*p_bg == 'l')
1059 {
1060 VTermColor fg, bg;
1061
1062 fg.red = fg.green = fg.blue = 0;
1063 bg.red = bg.green = bg.blue = 255;
1064 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1065 }
1066
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001067 /* Required to initialize most things. */
1068 vterm_screen_reset(screen, 1 /* hard */);
1069}
1070
Bram Moolenaar21554412017-07-24 21:44:43 +02001071/*
1072 * Return the text to show for the buffer name and status.
1073 */
1074 char_u *
1075term_get_status_text(term_T *term)
1076{
1077 if (term->tl_status_text == NULL)
1078 {
1079 char_u *txt;
1080 size_t len;
1081
1082 if (term->tl_title != NULL)
1083 txt = term->tl_title;
1084 else if (term_job_running(term))
1085 txt = (char_u *)_("running");
1086 else
1087 txt = (char_u *)_("finished");
1088 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001089 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001090 if (term->tl_status_text != NULL)
1091 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1092 term->tl_buffer->b_fname, txt);
1093 }
1094 return term->tl_status_text;
1095}
1096
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001097# ifdef WIN3264
1098
1099#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1100#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1101
Bram Moolenaar8a773062017-07-24 22:29:21 +02001102void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001103void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001104void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001105BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1106void (*winpty_config_set_initial_size)(void*, int, int);
1107LPCWSTR (*winpty_conin_name)(void*);
1108LPCWSTR (*winpty_conout_name)(void*);
1109LPCWSTR (*winpty_conerr_name)(void*);
1110void (*winpty_free)(void*);
1111void (*winpty_config_free)(void*);
1112void (*winpty_spawn_config_free)(void*);
1113void (*winpty_error_free)(void*);
1114LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001115BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001116
1117/**************************************
1118 * 2. MS-Windows implementation.
1119 */
1120
1121#define WINPTY_DLL "winpty.dll"
1122
1123static HINSTANCE hWinPtyDLL = NULL;
1124
1125 int
1126dyn_winpty_init(void)
1127{
1128 int i;
1129 static struct
1130 {
1131 char *name;
1132 FARPROC *ptr;
1133 } winpty_entry[] =
1134 {
1135 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1136 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1137 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1138 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1139 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1140 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1141 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1142 {"winpty_free", (FARPROC*)&winpty_free},
1143 {"winpty_open", (FARPROC*)&winpty_open},
1144 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1145 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1146 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1147 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001148 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001149 {NULL, NULL}
1150 };
1151
1152 /* No need to initialize twice. */
1153 if (hWinPtyDLL)
1154 return 1;
1155 /* Load winpty.dll */
1156 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1157 if (!hWinPtyDLL)
1158 {
1159 EMSG2(_(e_loadlib), WINPTY_DLL);
1160 return 0;
1161 }
1162 for (i = 0; winpty_entry[i].name != NULL
1163 && winpty_entry[i].ptr != NULL; ++i)
1164 {
1165 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1166 winpty_entry[i].name)) == NULL)
1167 {
1168 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1169 return 0;
1170 }
1171 }
1172
1173 return 1;
1174}
1175
1176/*
1177 * Create a new terminal of "rows" by "cols" cells.
1178 * Store a reference in "term".
1179 * Return OK or FAIL.
1180 */
1181 static int
1182term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1183{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001184 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001185 channel_T *channel = NULL;
1186 job_T *job = NULL;
1187 jobopt_T opt;
1188 DWORD error;
1189 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1190 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001191 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001192
1193 if (!dyn_winpty_init())
1194 return FAIL;
1195
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001196 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001197 if (p == NULL)
1198 return FAIL;
1199
1200 job = job_alloc();
1201 if (job == NULL)
1202 goto failed;
1203
1204 channel = add_channel();
1205 if (channel == NULL)
1206 goto failed;
1207
1208 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1209 if (term->tl_winpty_config == NULL)
1210 goto failed;
1211
1212 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1213 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1214 if (term->tl_winpty == NULL)
1215 goto failed;
1216
1217 spawn_config = winpty_spawn_config_new(
1218 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1219 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1220 NULL,
1221 p,
1222 NULL,
1223 NULL,
1224 &winpty_err);
1225 if (spawn_config == NULL)
1226 goto failed;
1227
1228 channel = add_channel();
1229 if (channel == NULL)
1230 goto failed;
1231
1232 job = job_alloc();
1233 if (job == NULL)
1234 goto failed;
1235
1236 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1237 &child_thread_handle, &error, &winpty_err))
1238 goto failed;
1239
1240 channel_set_pipes(channel,
1241 (sock_T) CreateFileW(
1242 winpty_conin_name(term->tl_winpty),
1243 GENERIC_WRITE, 0, NULL,
1244 OPEN_EXISTING, 0, NULL),
1245 (sock_T) CreateFileW(
1246 winpty_conout_name(term->tl_winpty),
1247 GENERIC_READ, 0, NULL,
1248 OPEN_EXISTING, 0, NULL),
1249 (sock_T) CreateFileW(
1250 winpty_conerr_name(term->tl_winpty),
1251 GENERIC_READ, 0, NULL,
1252 OPEN_EXISTING, 0, NULL));
1253
1254 jo = CreateJobObject(NULL, NULL);
1255 if (jo == NULL)
1256 goto failed;
1257
1258 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001259 {
1260 /* Failed, switch the way to terminate process with TerminateProcess. */
1261 CloseHandle(jo);
1262 jo = NULL;
1263 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001264
1265 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001266 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001267
1268 create_vterm(term, rows, cols);
1269
1270 setup_job_options(&opt, rows, cols);
1271 channel_set_job(channel, job, &opt);
1272
1273 job->jv_channel = channel;
1274 job->jv_proc_info.hProcess = child_process_handle;
1275 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1276 job->jv_job_object = jo;
1277 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001278 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001279 term->tl_job = job;
1280
1281 return OK;
1282
1283failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001284 if (spawn_config != NULL)
1285 winpty_spawn_config_free(spawn_config);
1286 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001287 if (channel != NULL)
1288 channel_clear(channel);
1289 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001290 {
1291 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001292 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001293 }
1294 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001295 if (jo != NULL)
1296 CloseHandle(jo);
1297 if (term->tl_winpty != NULL)
1298 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001299 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001300 if (term->tl_winpty_config != NULL)
1301 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001302 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001303 if (winpty_err != NULL)
1304 {
1305 char_u *msg = utf16_to_enc(
1306 (short_u *)winpty_error_msg(winpty_err), NULL);
1307
1308 EMSG(msg);
1309 winpty_error_free(winpty_err);
1310 }
1311 return FAIL;
1312}
1313
1314/*
1315 * Free the terminal emulator part of "term".
1316 */
1317 static void
1318term_free(term_T *term)
1319{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001320 if (term->tl_winpty != NULL)
1321 winpty_free(term->tl_winpty);
1322 if (term->tl_winpty_config != NULL)
1323 winpty_config_free(term->tl_winpty_config);
1324 if (term->tl_vterm != NULL)
1325 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001326}
1327
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001328/*
1329 * Request size to terminal.
1330 */
1331 static void
1332term_report_winsize(term_T *term, int rows, int cols)
1333{
1334 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1335}
1336
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001337# else
1338
1339/**************************************
1340 * 3. Unix-like implementation.
1341 */
1342
1343/*
1344 * Create a new terminal of "rows" by "cols" cells.
1345 * Start job for "cmd".
1346 * Store the pointers in "term".
1347 * Return OK or FAIL.
1348 */
1349 static int
1350term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1351{
1352 typval_T argvars[2];
1353 jobopt_T opt;
1354
1355 create_vterm(term, rows, cols);
1356
1357 argvars[0].v_type = VAR_STRING;
1358 argvars[0].vval.v_string = cmd;
1359 setup_job_options(&opt, rows, cols);
1360 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001361 if (term->tl_job != NULL)
1362 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001363
Bram Moolenaar61a66052017-07-22 18:39:00 +02001364 return term->tl_job != NULL
1365 && term->tl_job->jv_channel != NULL
1366 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001367}
1368
1369/*
1370 * Free the terminal emulator part of "term".
1371 */
1372 static void
1373term_free(term_T *term)
1374{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001375 if (term->tl_vterm != NULL)
1376 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001377}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001378
1379/*
1380 * Request size to terminal.
1381 */
1382 static void
1383term_report_winsize(term_T *term, int rows, int cols)
1384{
1385 /* Use an ioctl() to report the new window size to the job. */
1386 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1387 {
1388 int fd = -1;
1389 int part;
1390
1391 for (part = PART_OUT; part < PART_COUNT; ++part)
1392 {
1393 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1394 if (isatty(fd))
1395 break;
1396 }
1397 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1398 mch_stop_job(term->tl_job, (char_u *)"winch");
1399 }
1400}
1401
Bram Moolenaara2c45a12017-07-27 22:14:59 +02001402/*
1403 * Mark references in jobs of terminals.
1404 */
1405 int
1406set_ref_in_term(int copyID)
1407{
1408 int abort = FALSE;
1409 term_T *term;
1410 typval_T tv;
1411
1412 for (term = first_term; term != NULL; term = term->tl_next)
1413 if (term->tl_job != NULL)
1414 {
1415 tv.v_type = VAR_JOB;
1416 tv.vval.v_job = term->tl_job;
1417 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1418 }
1419 return abort;
1420}
1421
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001422# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001423
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001424#endif /* FEAT_TERMINAL */