blob: 9833a43b77611695e14133f38b0fa8554067a428 [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 Moolenaar8c0095c2017-07-18 22:53:21 +020038 * - Add a scrollback buffer (contains lines to scroll off the top).
39 * Can use the buf_T lines, store attributes somewhere else?
40 * - When the job ends:
41 * - Write "-- JOB ENDED --" in the terminal.
42 * - Put the terminal contents in the scrollback buffer.
43 * - Free the terminal emulator.
44 * - Display the scrollback buffer (but with attributes).
45 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar21554412017-07-24 21:44:43 +020046 * - Need an option or argument to drop the window+buffer right away, to be
47 * used for a shell or Vim.
48 * - add a character in :ls output
Bram Moolenaar938783d2017-07-16 20:13:26 +020049 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020050 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020051 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020052 * - command line completion for :terminal
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 Moolenaar1f2903c2017-07-23 19:51:01 +0200201 if (buflist_findname(cmd) == NULL)
202 curbuf->b_ffname = vim_strsave(cmd);
203 else
204 {
205 int i;
206 size_t len = STRLEN(cmd) + 10;
207 char_u *p = alloc(len);
208
209 for (i = 1; p != NULL; ++i)
210 {
211 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
212 if (buflist_findname(p) == NULL)
213 {
214 curbuf->b_ffname = p;
215 break;
216 }
217 }
218 }
219 curbuf->b_fname = curbuf->b_ffname;
220
221 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
222 curbuf->b_changed = TRUE;
223 curbuf->b_p_ma = FALSE;
224 set_string_option_direct((char_u *)"buftype", -1,
225 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200226
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200227 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200228
Bram Moolenaare173fd02017-07-22 19:03:32 +0200229 if (cmd == NULL || *cmd == NUL)
230 cmd = p_sh;
231
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;
304 p += utf_ptr2len_len(p, len - (p - msg));
305 }
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{
323 /* TODO: this should not always be needed */
324 setcursor();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200325 if (redraw && term->tl_buffer == curbuf && term->tl_cursor_visible)
326 {
327 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200328#ifdef FEAT_GUI
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200329 if (gui.in_use)
330 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200331#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200332 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200333}
334
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200335/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200336 * Invoked when "msg" output from a job was received. Write it to the terminal
337 * of "buffer".
338 */
339 void
340write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
341{
342 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200343 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200344
345 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200346 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200347
348 /* TODO: only update once in a while. */
349 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200350 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200351}
352
353/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200354 * Convert typed key "c" into bytes to send to the job.
355 * Return the number of bytes in "buf".
356 */
357 static int
358term_convert_key(int c, char *buf)
359{
360 VTerm *vterm = curbuf->b_term->tl_vterm;
361 VTermKey key = VTERM_KEY_NONE;
362 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200363
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200364 switch (c)
365 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200366 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200367 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200368 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
369 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200370 case K_DEL: key = VTERM_KEY_DEL; break;
371 case K_DOWN: key = VTERM_KEY_DOWN; break;
372 case K_END: key = VTERM_KEY_END; break;
373 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
374 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
375 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
376 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
377 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
378 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
379 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
380 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
381 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
382 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
383 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
384 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
385 case K_HOME: key = VTERM_KEY_HOME; break;
386 case K_INS: key = VTERM_KEY_INS; break;
387 case K_K0: key = VTERM_KEY_KP_0; break;
388 case K_K1: key = VTERM_KEY_KP_1; break;
389 case K_K2: key = VTERM_KEY_KP_2; break;
390 case K_K3: key = VTERM_KEY_KP_3; break;
391 case K_K4: key = VTERM_KEY_KP_4; break;
392 case K_K5: key = VTERM_KEY_KP_5; break;
393 case K_K6: key = VTERM_KEY_KP_6; break;
394 case K_K7: key = VTERM_KEY_KP_7; break;
395 case K_K8: key = VTERM_KEY_KP_8; break;
396 case K_K9: key = VTERM_KEY_KP_9; break;
397 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
398 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
399 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
400 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
401 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
402 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
403 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
404 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
405 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
406 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
407 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
408 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
409 case K_LEFT: key = VTERM_KEY_LEFT; break;
410 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
411 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
412 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
413 case K_UP: key = VTERM_KEY_UP; break;
414 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200415
416 case K_MOUSEUP: /* TODO */ break;
417 case K_MOUSEDOWN: /* TODO */ break;
418 case K_MOUSELEFT: /* TODO */ break;
419 case K_MOUSERIGHT: /* TODO */ break;
420
421 case K_LEFTMOUSE: /* TODO */ break;
422 case K_LEFTMOUSE_NM: /* TODO */ break;
423 case K_LEFTDRAG: /* TODO */ break;
424 case K_LEFTRELEASE: /* TODO */ break;
425 case K_LEFTRELEASE_NM: /* TODO */ break;
426 case K_MIDDLEMOUSE: /* TODO */ break;
427 case K_MIDDLEDRAG: /* TODO */ break;
428 case K_MIDDLERELEASE: /* TODO */ break;
429 case K_RIGHTMOUSE: /* TODO */ break;
430 case K_RIGHTDRAG: /* TODO */ break;
431 case K_RIGHTRELEASE: /* TODO */ break;
432 case K_X1MOUSE: /* TODO */ break;
433 case K_X1DRAG: /* TODO */ break;
434 case K_X1RELEASE: /* TODO */ break;
435 case K_X2MOUSE: /* TODO */ break;
436 case K_X2DRAG: /* TODO */ break;
437 case K_X2RELEASE: /* TODO */ break;
438
439 /* TODO: handle all special keys and modifiers that terminal_loop()
440 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200441 }
442
443 /*
444 * Convert special keys to vterm keys:
445 * - Write keys to vterm: vterm_keyboard_key()
446 * - Write output to channel.
447 */
448 if (key != VTERM_KEY_NONE)
449 /* Special key, let vterm convert it. */
450 vterm_keyboard_key(vterm, key, mod);
451 else
452 /* Normal character, let vterm convert it. */
453 vterm_keyboard_unichar(vterm, c, mod);
454
455 /* Read back the converted escape sequence. */
456 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
457}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200458
Bram Moolenaar938783d2017-07-16 20:13:26 +0200459/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200460 * Wait for input and send it to the job.
461 * Return when the start of a CTRL-W command is typed or anything else that
462 * should be handled as a Normal mode command.
463 */
464 void
465terminal_loop(void)
466{
467 char buf[KEY_BUF_LEN];
468 int c;
469 size_t len;
470 static int mouse_was_outside = FALSE;
471 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200472 int termkey = 0;
473
474 if (*curwin->w_p_tk != NUL)
475 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200476
477 for (;;)
478 {
479 /* TODO: skip screen update when handling a sequence of keys. */
480 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200481 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200482 ++no_mapping;
483 ++allow_keys;
484 got_int = FALSE;
485 c = vgetc();
486 --no_mapping;
487 --allow_keys;
488
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200489 if (c == (termkey == 0 ? Ctrl_W : termkey))
490 {
491 stuffcharReadbuff(Ctrl_W);
492 return;
493 }
494
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200495 /* Catch keys that need to be handled as in Normal mode. */
496 switch (c)
497 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200498 case NUL:
499 case K_ZERO:
500 stuffcharReadbuff(c);
501 return;
502
503 case K_IGNORE: continue;
504
505 case K_LEFTDRAG:
506 case K_MIDDLEDRAG:
507 case K_RIGHTDRAG:
508 case K_X1DRAG:
509 case K_X2DRAG:
510 dragging_outside = mouse_was_outside;
511 /* FALLTHROUGH */
512 case K_LEFTMOUSE:
513 case K_LEFTMOUSE_NM:
514 case K_LEFTRELEASE:
515 case K_LEFTRELEASE_NM:
516 case K_MIDDLEMOUSE:
517 case K_MIDDLERELEASE:
518 case K_RIGHTMOUSE:
519 case K_RIGHTRELEASE:
520 case K_X1MOUSE:
521 case K_X1RELEASE:
522 case K_X2MOUSE:
523 case K_X2RELEASE:
524 if (mouse_row < W_WINROW(curwin)
525 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
526 || mouse_col < W_WINCOL(curwin)
527 || mouse_col >= W_ENDCOL(curwin)
528 || dragging_outside)
529 {
530 /* click outside the current window */
531 stuffcharReadbuff(c);
532 mouse_was_outside = TRUE;
533 return;
534 }
535 }
536 mouse_was_outside = FALSE;
537
538 /* Convert the typed key to a sequence of bytes for the job. */
539 len = term_convert_key(c, buf);
540 if (len > 0)
541 /* TODO: if FAIL is returned, stop? */
542 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
543 (char_u *)buf, len, NULL);
544 }
545}
546
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200547/*
548 * Called when a job has finished.
549 */
550 void
551term_job_ended(job_T *job)
552{
Bram Moolenaar21554412017-07-24 21:44:43 +0200553 term_T *term;
554 int did_one = FALSE;
555
556 for (term = first_term; term != NULL; term = term->tl_next)
557 if (term->tl_job == job)
558 {
559 vim_free(term->tl_title);
560 term->tl_title = NULL;
561 vim_free(term->tl_status_text);
562 term->tl_status_text = NULL;
563 redraw_buf_and_status_later(term->tl_buffer, VALID);
564 did_one = TRUE;
565 }
566 if (did_one)
Bram Moolenaar21554412017-07-24 21:44:43 +0200567 redraw_statuslines();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200568 if (curbuf->b_term != NULL)
569 {
570 if (curbuf->b_term->tl_job == job)
571 maketitle();
572 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar21554412017-07-24 21:44:43 +0200573 }
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200574}
575
576/*
577 * Return TRUE if the job for "buf" is still running.
578 */
Bram Moolenaar21554412017-07-24 21:44:43 +0200579 static int
580term_job_running(term_T *term)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200581{
Bram Moolenaar21554412017-07-24 21:44:43 +0200582 return term->tl_job != NULL && term->tl_job->jv_status == JOB_STARTED;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200583}
584
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200585 static void
586position_cursor(win_T *wp, VTermPos *pos)
587{
588 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
589 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
590}
591
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200592 static void
593may_toggle_cursor(term_T *term)
594{
595 if (curbuf == term->tl_buffer)
596 {
597 if (term->tl_cursor_visible)
598 cursor_on();
599 else
600 cursor_off();
601 }
602}
603
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200604 static int
605handle_damage(VTermRect rect, void *user)
606{
607 term_T *term = (term_T *)user;
608
609 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
610 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
611 redraw_buf_later(term->tl_buffer, NOT_VALID);
612 return 1;
613}
614
615 static int
616handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
617{
618 term_T *term = (term_T *)user;
619
620 /* TODO */
621 redraw_buf_later(term->tl_buffer, NOT_VALID);
622 return 1;
623}
624
625 static int
626handle_movecursor(
627 VTermPos pos,
628 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200629 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200630 void *user)
631{
632 term_T *term = (term_T *)user;
633 win_T *wp;
634 int is_current = FALSE;
635
636 FOR_ALL_WINDOWS(wp)
637 {
638 if (wp->w_buffer == term->tl_buffer)
639 {
640 position_cursor(wp, &pos);
641 if (wp == curwin)
642 is_current = TRUE;
643 }
644 }
645
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200646 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200647 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200648 {
649 may_toggle_cursor(term);
650 update_cursor(term, TRUE);
651 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200652
653 return 1;
654}
655
Bram Moolenaar21554412017-07-24 21:44:43 +0200656 static int
657handle_settermprop(
658 VTermProp prop,
659 VTermValue *value,
660 void *user)
661{
662 term_T *term = (term_T *)user;
663
664 switch (prop)
665 {
666 case VTERM_PROP_TITLE:
667 vim_free(term->tl_title);
668 term->tl_title = vim_strsave((char_u *)value->string);
669 vim_free(term->tl_status_text);
670 term->tl_status_text = NULL;
671 if (term == curbuf->b_term)
672 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200673 break;
674
675 case VTERM_PROP_CURSORVISIBLE:
676 term->tl_cursor_visible = value->boolean;
677 may_toggle_cursor(term);
678 out_flush();
679 break;
680
Bram Moolenaar21554412017-07-24 21:44:43 +0200681 default:
682 break;
683 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200684 /* Always return 1, otherwise vterm doesn't store the value internally. */
685 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +0200686}
687
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200688/*
689 * The job running in the terminal resized the terminal.
690 */
691 static int
692handle_resize(int rows, int cols, void *user)
693{
694 term_T *term = (term_T *)user;
695 win_T *wp;
696
697 term->tl_rows = rows;
698 term->tl_cols = cols;
699 FOR_ALL_WINDOWS(wp)
700 {
701 if (wp->w_buffer == term->tl_buffer)
702 {
703 win_setheight_win(rows, wp);
704 win_setwidth_win(cols, wp);
705 }
706 }
707
708 redraw_buf_later(term->tl_buffer, NOT_VALID);
709 return 1;
710}
711
Bram Moolenaar21554412017-07-24 21:44:43 +0200712static VTermScreenCallbacks screen_callbacks = {
713 handle_damage, /* damage */
714 handle_moverect, /* moverect */
715 handle_movecursor, /* movecursor */
716 handle_settermprop, /* settermprop */
717 NULL, /* bell */
718 handle_resize, /* resize */
719 NULL, /* sb_pushline */
720 NULL /* sb_popline */
721};
722
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200723/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200724 * Reverse engineer the RGB value into a cterm color index.
725 * First color is 1. Return 0 if no match found.
726 */
727 static int
728color2index(VTermColor *color)
729{
730 int red = color->red;
731 int blue = color->blue;
732 int green = color->green;
733
734 if (red == 0)
735 {
736 if (green == 0)
737 {
738 if (blue == 0)
739 return 1; /* black */
740 if (blue == 224)
741 return 5; /* blue */
742 }
743 else if (green == 224)
744 {
745 if (blue == 0)
746 return 3; /* green */
747 if (blue == 224)
748 return 7; /* cyan */
749 }
750 }
751 else if (red == 224)
752 {
753 if (green == 0)
754 {
755 if (blue == 0)
756 return 2; /* red */
757 if (blue == 224)
758 return 6; /* magenta */
759 }
760 else if (green == 224)
761 {
762 if (blue == 0)
763 return 4; /* yellow */
764 if (blue == 224)
765 return 8; /* white */
766 }
767 }
768 else if (red == 128)
769 {
770 if (green == 128 && blue == 128)
Bram Moolenaar8a773062017-07-24 22:29:21 +0200771 return 9; /* high intensity black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200772 }
773 else if (red == 255)
774 {
775 if (green == 64)
776 {
777 if (blue == 64)
778 return 10; /* high intensity red */
779 if (blue == 255)
780 return 14; /* high intensity magenta */
781 }
782 else if (green == 255)
783 {
784 if (blue == 64)
785 return 12; /* high intensity yellow */
786 if (blue == 255)
787 return 16; /* high intensity white */
788 }
789 }
790 else if (red == 64)
791 {
792 if (green == 64)
793 {
794 if (blue == 255)
795 return 13; /* high intensity blue */
796 }
797 else if (green == 255)
798 {
799 if (blue == 64)
800 return 11; /* high intensity green */
801 if (blue == 255)
802 return 15; /* high intensity cyan */
803 }
804 }
805 if (t_colors >= 256)
806 {
807 if (red == blue && red == green)
808 {
809 /* 24-color greyscale */
810 static int cutoff[23] = {
811 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
812 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
813 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
814 int i;
815
816 for (i = 0; i < 23; ++i)
817 if (red < cutoff[i])
818 return i + 233;
819 return 256;
820 }
821
822 /* 216-color cube */
823 return 17 + ((red + 25) / 0x33) * 36
824 + ((green + 25) / 0x33) * 6
825 + (blue + 25) / 0x33;
826 }
827 return 0;
828}
829
830/*
831 * Convert the attributes of a vterm cell into an attribute index.
832 */
833 static int
834cell2attr(VTermScreenCell *cell)
835{
836 int attr = 0;
837
838 if (cell->attrs.bold)
839 attr |= HL_BOLD;
840 if (cell->attrs.underline)
841 attr |= HL_UNDERLINE;
842 if (cell->attrs.italic)
843 attr |= HL_ITALIC;
844 if (cell->attrs.strike)
845 attr |= HL_STANDOUT;
846 if (cell->attrs.reverse)
847 attr |= HL_INVERSE;
848 if (cell->attrs.strike)
849 attr |= HL_UNDERLINE;
850
851#ifdef FEAT_GUI
852 if (gui.in_use)
853 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200854 guicolor_T fg, bg;
855
856 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
857 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
858 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200859 }
860 else
861#endif
862#ifdef FEAT_TERMGUICOLORS
863 if (p_tgc)
864 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200865 guicolor_T fg, bg;
866
867 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
868 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
869
870 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200871 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200872 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200873#endif
874 {
875 return get_cterm_attr_idx(attr, color2index(&cell->fg),
876 color2index(&cell->bg));
877 }
878 return 0;
879}
880
881/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200882 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200883 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200884 void
885term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200886{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200887 term_T *term = wp->w_buffer->b_term;
888 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200889 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200890 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200891 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200892
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200893 /*
894 * If the window was resized a redraw will be triggered and we get here.
895 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
896 */
897 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
898 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200899 {
900 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
901 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
902
903 vterm_set_size(vterm, rows, cols);
904 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
905 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200906 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200907 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200908
909 /* The cursor may have been moved when resizing. */
910 vterm_state_get_cursorpos(state, &pos);
911 position_cursor(wp, &pos);
912
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200913 /* TODO: Only redraw what changed. */
914 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200915 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200916 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200917 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200918
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200919 if (pos.row < term->tl_rows)
920 {
921 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200922 {
923 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200924 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200925
Bram Moolenaareeac6772017-07-23 15:48:37 +0200926 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
927 vim_memset(&cell, 0, sizeof(cell));
928
929 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200930 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200931 if (c == NUL)
932 {
933 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +0200934#if defined(FEAT_MBYTE)
935 if (enc_utf8)
936 ScreenLinesUC[off] = NUL;
937#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200938 }
939 else
940 {
941#if defined(FEAT_MBYTE)
942 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200943 {
944 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200945 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200946 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200947 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200948 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200949 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200950 if (enc_utf8)
951 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200952 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200953#else
954 ScreenLines[off] = c;
955#endif
956 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200957 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200958
959 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200960 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200961 if (cell.width == 2)
962 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200963 ScreenLines[off] = NUL;
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 ++pos.col;
969 ++off;
970 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200971 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200972 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200973 else
974 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200975
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200976 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
977 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200978 }
979}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200980
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200981/*
982 * Set job options common for Unix and MS-Windows.
983 */
984 static void
985setup_job_options(jobopt_T *opt, int rows, int cols)
986{
987 clear_job_options(opt);
988 opt->jo_mode = MODE_RAW;
989 opt->jo_out_mode = MODE_RAW;
990 opt->jo_err_mode = MODE_RAW;
991 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200992
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200993 opt->jo_io[PART_OUT] = JIO_BUFFER;
994 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200995 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
996
997 opt->jo_modifiable[PART_OUT] = 0;
998 opt->jo_modifiable[PART_ERR] = 0;
999 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1000
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001001 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1002 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001003 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001004 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1005
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001006 opt->jo_term_rows = rows;
1007 opt->jo_term_cols = cols;
1008}
1009
1010/*
1011 * Create a new vterm and initialize it.
1012 */
1013 static void
1014create_vterm(term_T *term, int rows, int cols)
1015{
1016 VTerm *vterm;
1017 VTermScreen *screen;
1018
1019 vterm = vterm_new(rows, cols);
1020 term->tl_vterm = vterm;
1021 screen = vterm_obtain_screen(vterm);
1022 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1023 /* TODO: depends on 'encoding'. */
1024 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001025
1026 /* Vterm uses a default black background. Set it to white when
1027 * 'background' is "light". */
1028 if (*p_bg == 'l')
1029 {
1030 VTermColor fg, bg;
1031
1032 fg.red = fg.green = fg.blue = 0;
1033 bg.red = bg.green = bg.blue = 255;
1034 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1035 }
1036
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001037 /* Required to initialize most things. */
1038 vterm_screen_reset(screen, 1 /* hard */);
1039}
1040
Bram Moolenaar21554412017-07-24 21:44:43 +02001041/*
1042 * Return the text to show for the buffer name and status.
1043 */
1044 char_u *
1045term_get_status_text(term_T *term)
1046{
1047 if (term->tl_status_text == NULL)
1048 {
1049 char_u *txt;
1050 size_t len;
1051
1052 if (term->tl_title != NULL)
1053 txt = term->tl_title;
1054 else if (term_job_running(term))
1055 txt = (char_u *)_("running");
1056 else
1057 txt = (char_u *)_("finished");
1058 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
1059 term->tl_status_text = alloc(len);
1060 if (term->tl_status_text != NULL)
1061 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1062 term->tl_buffer->b_fname, txt);
1063 }
1064 return term->tl_status_text;
1065}
1066
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001067# ifdef WIN3264
1068
1069#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1070#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1071
Bram Moolenaar8a773062017-07-24 22:29:21 +02001072void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001073void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001074void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001075BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1076void (*winpty_config_set_initial_size)(void*, int, int);
1077LPCWSTR (*winpty_conin_name)(void*);
1078LPCWSTR (*winpty_conout_name)(void*);
1079LPCWSTR (*winpty_conerr_name)(void*);
1080void (*winpty_free)(void*);
1081void (*winpty_config_free)(void*);
1082void (*winpty_spawn_config_free)(void*);
1083void (*winpty_error_free)(void*);
1084LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001085BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001086
1087/**************************************
1088 * 2. MS-Windows implementation.
1089 */
1090
1091#define WINPTY_DLL "winpty.dll"
1092
1093static HINSTANCE hWinPtyDLL = NULL;
1094
1095 int
1096dyn_winpty_init(void)
1097{
1098 int i;
1099 static struct
1100 {
1101 char *name;
1102 FARPROC *ptr;
1103 } winpty_entry[] =
1104 {
1105 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1106 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1107 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1108 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1109 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1110 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1111 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1112 {"winpty_free", (FARPROC*)&winpty_free},
1113 {"winpty_open", (FARPROC*)&winpty_open},
1114 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1115 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1116 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1117 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001118 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001119 {NULL, NULL}
1120 };
1121
1122 /* No need to initialize twice. */
1123 if (hWinPtyDLL)
1124 return 1;
1125 /* Load winpty.dll */
1126 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1127 if (!hWinPtyDLL)
1128 {
1129 EMSG2(_(e_loadlib), WINPTY_DLL);
1130 return 0;
1131 }
1132 for (i = 0; winpty_entry[i].name != NULL
1133 && winpty_entry[i].ptr != NULL; ++i)
1134 {
1135 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1136 winpty_entry[i].name)) == NULL)
1137 {
1138 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1139 return 0;
1140 }
1141 }
1142
1143 return 1;
1144}
1145
1146/*
1147 * Create a new terminal of "rows" by "cols" cells.
1148 * Store a reference in "term".
1149 * Return OK or FAIL.
1150 */
1151 static int
1152term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1153{
1154 WCHAR *p = enc_to_utf16(cmd, NULL);
1155 channel_T *channel = NULL;
1156 job_T *job = NULL;
1157 jobopt_T opt;
1158 DWORD error;
1159 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1160 void *winpty_err;
1161 void *spawn_config;
1162
1163 if (!dyn_winpty_init())
1164 return FAIL;
1165
1166 if (p == NULL)
1167 return FAIL;
1168
1169 job = job_alloc();
1170 if (job == NULL)
1171 goto failed;
1172
1173 channel = add_channel();
1174 if (channel == NULL)
1175 goto failed;
1176
1177 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1178 if (term->tl_winpty_config == NULL)
1179 goto failed;
1180
1181 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1182 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1183 if (term->tl_winpty == NULL)
1184 goto failed;
1185
1186 spawn_config = winpty_spawn_config_new(
1187 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1188 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1189 NULL,
1190 p,
1191 NULL,
1192 NULL,
1193 &winpty_err);
1194 if (spawn_config == NULL)
1195 goto failed;
1196
1197 channel = add_channel();
1198 if (channel == NULL)
1199 goto failed;
1200
1201 job = job_alloc();
1202 if (job == NULL)
1203 goto failed;
1204
1205 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1206 &child_thread_handle, &error, &winpty_err))
1207 goto failed;
1208
1209 channel_set_pipes(channel,
1210 (sock_T) CreateFileW(
1211 winpty_conin_name(term->tl_winpty),
1212 GENERIC_WRITE, 0, NULL,
1213 OPEN_EXISTING, 0, NULL),
1214 (sock_T) CreateFileW(
1215 winpty_conout_name(term->tl_winpty),
1216 GENERIC_READ, 0, NULL,
1217 OPEN_EXISTING, 0, NULL),
1218 (sock_T) CreateFileW(
1219 winpty_conerr_name(term->tl_winpty),
1220 GENERIC_READ, 0, NULL,
1221 OPEN_EXISTING, 0, NULL));
1222
1223 jo = CreateJobObject(NULL, NULL);
1224 if (jo == NULL)
1225 goto failed;
1226
1227 if (!AssignProcessToJobObject(jo, child_process_handle))
1228 goto failed;
1229
1230 winpty_spawn_config_free(spawn_config);
1231
1232 create_vterm(term, rows, cols);
1233
1234 setup_job_options(&opt, rows, cols);
1235 channel_set_job(channel, job, &opt);
1236
1237 job->jv_channel = channel;
1238 job->jv_proc_info.hProcess = child_process_handle;
1239 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1240 job->jv_job_object = jo;
1241 job->jv_status = JOB_STARTED;
1242 term->tl_job = job;
1243
1244 return OK;
1245
1246failed:
1247 if (channel != NULL)
1248 channel_clear(channel);
1249 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001250 {
1251 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001252 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001253 }
1254 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001255 if (jo != NULL)
1256 CloseHandle(jo);
1257 if (term->tl_winpty != NULL)
1258 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001259 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001260 if (term->tl_winpty_config != NULL)
1261 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001262 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001263 if (winpty_err != NULL)
1264 {
1265 char_u *msg = utf16_to_enc(
1266 (short_u *)winpty_error_msg(winpty_err), NULL);
1267
1268 EMSG(msg);
1269 winpty_error_free(winpty_err);
1270 }
1271 return FAIL;
1272}
1273
1274/*
1275 * Free the terminal emulator part of "term".
1276 */
1277 static void
1278term_free(term_T *term)
1279{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001280 if (term->tl_winpty != NULL)
1281 winpty_free(term->tl_winpty);
1282 if (term->tl_winpty_config != NULL)
1283 winpty_config_free(term->tl_winpty_config);
1284 if (term->tl_vterm != NULL)
1285 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001286}
1287
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001288/*
1289 * Request size to terminal.
1290 */
1291 static void
1292term_report_winsize(term_T *term, int rows, int cols)
1293{
1294 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1295}
1296
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001297# else
1298
1299/**************************************
1300 * 3. Unix-like implementation.
1301 */
1302
1303/*
1304 * Create a new terminal of "rows" by "cols" cells.
1305 * Start job for "cmd".
1306 * Store the pointers in "term".
1307 * Return OK or FAIL.
1308 */
1309 static int
1310term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1311{
1312 typval_T argvars[2];
1313 jobopt_T opt;
1314
1315 create_vterm(term, rows, cols);
1316
1317 argvars[0].v_type = VAR_STRING;
1318 argvars[0].vval.v_string = cmd;
1319 setup_job_options(&opt, rows, cols);
1320 term->tl_job = job_start(argvars, &opt);
1321
Bram Moolenaar61a66052017-07-22 18:39:00 +02001322 return term->tl_job != NULL
1323 && term->tl_job->jv_channel != NULL
1324 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001325}
1326
1327/*
1328 * Free the terminal emulator part of "term".
1329 */
1330 static void
1331term_free(term_T *term)
1332{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001333 if (term->tl_vterm != NULL)
1334 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001335}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001336
1337/*
1338 * Request size to terminal.
1339 */
1340 static void
1341term_report_winsize(term_T *term, int rows, int cols)
1342{
1343 /* Use an ioctl() to report the new window size to the job. */
1344 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1345 {
1346 int fd = -1;
1347 int part;
1348
1349 for (part = PART_OUT; part < PART_COUNT; ++part)
1350 {
1351 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1352 if (isatty(fd))
1353 break;
1354 }
1355 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1356 mch_stop_job(term->tl_job, (char_u *)"winch");
1357 }
1358}
1359
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001360# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001361
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001362#endif /* FEAT_TERMINAL */