blob: 6f14b3f82ac490230fd155e7d6eeaa28810cb081 [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
22 * that library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
35 * TODO:
Bram Moolenaar1c844932017-07-24 23:36:41 +020036 * - include functions from #1871
37 * - do not store terminal buffer in viminfo. Or prefix term:// ?
Bram Moolenaara1b5b092017-07-26 21:29:34 +020038 * - Make CTRL-W . send CTRL-W to terminal?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020039 * - Add a scrollback buffer (contains lines to scroll off the top).
40 * Can use the buf_T lines, store attributes somewhere else?
41 * - When the job ends:
42 * - Write "-- JOB ENDED --" in the terminal.
43 * - Put the terminal contents in the scrollback buffer.
44 * - Free the terminal emulator.
45 * - Display the scrollback buffer (but with attributes).
46 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - Need an option or argument to drop the window+buffer right away, to be
48 * used for a shell or Vim.
49 * - add a character in :ls output
Bram Moolenaar938783d2017-07-16 20:13:26 +020050 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020051 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020052 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020053 * - command line completion for :terminal
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020054 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020055 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020056 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020057 * - implement "term" for job_start(): more job options when starting a
58 * terminal.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020059 * - implement term_list() list of buffers with a terminal
60 * - implement term_getsize(buf)
61 * - implement term_setsize(buf)
62 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
63 * - implement term_wait(buf) wait for screen to be updated
64 * - implement term_scrape(buf, row) inspect terminal screen
65 * - implement term_open(command, options) open terminal window
66 * - implement term_getjob(buf)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020067 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
68 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020069 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020070 */
71
72#include "vim.h"
73
74#ifdef FEAT_TERMINAL
75
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020076#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020077# define MIN(x,y) (x < y ? x : y)
78# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020079#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020080
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020081#include "libvterm/include/vterm.h"
82
Bram Moolenaare4f25e42017-07-07 11:54:15 +020083/* typedef term_T in structs.h */
84struct terminal_S {
85 term_T *tl_next;
86
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020087#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020088 void *tl_winpty_config;
89 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020090#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020091 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020093 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020094
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020095 /* last known vterm size */
96 int tl_rows;
97 int tl_cols;
98 /* vterm size does not follow window size */
99 int tl_rows_fixed;
100 int tl_cols_fixed;
101
Bram Moolenaar21554412017-07-24 21:44:43 +0200102 char_u *tl_title; /* NULL or allocated */
103 char_u *tl_status_text; /* NULL or allocated */
104
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200105 /* Range of screen rows to update. Zero based. */
106 int tl_dirty_row_start; /* -1 if nothing dirty */
107 int tl_dirty_row_end; /* row below last one to update */
108
109 pos_T tl_cursor;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200110 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200111};
112
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200113/*
114 * List of all active terminals.
115 */
116static term_T *first_term = NULL;
117
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200118
119#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
120#define KEY_BUF_LEN 200
121
122/*
123 * Functions with separate implementation for MS-Windows and Unix-like systems.
124 */
125static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200126static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200127static void term_free(term_T *term);
128
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200129/**************************************
130 * 1. Generic code for all systems.
131 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200132
133/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200134 * Determine the terminal size from 'termsize' and the current window.
135 * Assumes term->tl_rows and term->tl_cols are zero.
136 */
137 static void
138set_term_and_win_size(term_T *term)
139{
140 if (*curwin->w_p_tms != NUL)
141 {
142 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
143
144 term->tl_rows = atoi((char *)curwin->w_p_tms);
145 term->tl_cols = atoi((char *)p);
146 }
147 if (term->tl_rows == 0)
148 term->tl_rows = curwin->w_height;
149 else
150 {
151 win_setheight_win(term->tl_rows, curwin);
152 term->tl_rows_fixed = TRUE;
153 }
154 if (term->tl_cols == 0)
155 term->tl_cols = curwin->w_width;
156 else
157 {
158 win_setwidth_win(term->tl_cols, curwin);
159 term->tl_cols_fixed = TRUE;
160 }
161}
162
163/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200164 * ":terminal": open a terminal window and execute a job in it.
165 */
166 void
167ex_terminal(exarg_T *eap)
168{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200169 exarg_T split_ea;
170 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200171 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200172 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200173
174 if (check_restricted() || check_secure())
175 return;
176
177 term = (term_T *)alloc_clear(sizeof(term_T));
178 if (term == NULL)
179 return;
180 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200181 term->tl_cursor_visible = TRUE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200182
183 /* Open a new window or tab. */
184 vim_memset(&split_ea, 0, sizeof(split_ea));
185 split_ea.cmdidx = CMD_new;
186 split_ea.cmd = (char_u *)"new";
187 split_ea.arg = (char_u *)"";
188 ex_splitview(&split_ea);
189 if (curwin == old_curwin)
190 {
191 /* split failed */
192 vim_free(term);
193 return;
194 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200195 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200196 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200197
198 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200199 term->tl_next = first_term;
200 first_term = term;
201
Bram Moolenaar293424c2017-07-26 23:11:01 +0200202 if (cmd == NULL || *cmd == NUL)
203 cmd = p_sh;
204
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200205 if (buflist_findname(cmd) == NULL)
206 curbuf->b_ffname = vim_strsave(cmd);
207 else
208 {
209 int i;
210 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200211 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200212
213 for (i = 1; p != NULL; ++i)
214 {
215 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
216 if (buflist_findname(p) == NULL)
217 {
218 curbuf->b_ffname = p;
219 break;
220 }
221 }
222 }
223 curbuf->b_fname = curbuf->b_ffname;
224
225 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
226 curbuf->b_changed = TRUE;
227 curbuf->b_p_ma = FALSE;
228 set_string_option_direct((char_u *)"buftype", -1,
229 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200230
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200231 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200232
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200233 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200234 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200235 {
236 /* store the size we ended up with */
237 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200238 }
239 else
240 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200241 free_terminal(term);
242 curbuf->b_term = NULL;
243
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200244 /* Wiping out the buffer will also close the window and call
245 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200246 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200247 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200248
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200249 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250}
251
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200252/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200253 * Free a terminal and everything it refers to.
254 * Kills the job if there is one.
255 * Called when wiping out a buffer.
256 */
257 void
258free_terminal(term_T *term)
259{
260 term_T *tp;
261
262 if (term == NULL)
263 return;
264 if (first_term == term)
265 first_term = term->tl_next;
266 else
267 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
268 if (tp->tl_next == term)
269 {
270 tp->tl_next = term->tl_next;
271 break;
272 }
273
274 if (term->tl_job != NULL)
275 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200276 if (term->tl_job->jv_status != JOB_ENDED
277 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200278 job_stop(term->tl_job, NULL, "kill");
279 job_unref(term->tl_job);
280 }
281
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200282 term_free(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200283 vim_free(term->tl_title);
284 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200285 vim_free(term);
286}
287
288/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200289 * Write job output "msg[len]" to the vterm.
290 */
291 static void
292term_write_job_output(term_T *term, char_u *msg, size_t len)
293{
294 VTerm *vterm = term->tl_vterm;
295 char_u *p;
296 size_t done;
297 size_t len_now;
298
299 for (done = 0; done < len; done += len_now)
300 {
301 for (p = msg + done; p < msg + len; )
302 {
303 if (*p == NL)
304 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200305 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200306 }
307 len_now = p - msg - done;
308 vterm_input_write(vterm, (char *)msg + done, len_now);
309 if (p < msg + len && *p == NL)
310 {
311 /* Convert NL to CR-NL, that appears to work best. */
312 vterm_input_write(vterm, "\r\n", 2);
313 ++len_now;
314 }
315 }
316
317 /* this invokes the damage callbacks */
318 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
319}
320
Bram Moolenaar1c844932017-07-24 23:36:41 +0200321 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200322update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200323{
Bram Moolenaar1c844932017-07-24 23:36:41 +0200324 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200325 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200326 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200327 if (term->tl_cursor_visible)
328 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200329 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200330#ifdef FEAT_GUI
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200331 if (gui.in_use && term->tl_cursor_visible)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200332 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200333#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200334 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200335}
336
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200337/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200338 * Invoked when "msg" output from a job was received. Write it to the terminal
339 * of "buffer".
340 */
341 void
342write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
343{
344 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200345 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200346
347 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200348 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200349
350 /* TODO: only update once in a while. */
351 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200352 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200353}
354
355/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200356 * Convert typed key "c" into bytes to send to the job.
357 * Return the number of bytes in "buf".
358 */
359 static int
360term_convert_key(int c, char *buf)
361{
362 VTerm *vterm = curbuf->b_term->tl_vterm;
363 VTermKey key = VTERM_KEY_NONE;
364 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200365
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200366 switch (c)
367 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200368 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200369 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200370 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
371 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200372 case K_DEL: key = VTERM_KEY_DEL; break;
373 case K_DOWN: key = VTERM_KEY_DOWN; break;
374 case K_END: key = VTERM_KEY_END; break;
375 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
376 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
377 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
378 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
379 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
380 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
381 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
382 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
383 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
384 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
385 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
386 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
387 case K_HOME: key = VTERM_KEY_HOME; break;
388 case K_INS: key = VTERM_KEY_INS; break;
389 case K_K0: key = VTERM_KEY_KP_0; break;
390 case K_K1: key = VTERM_KEY_KP_1; break;
391 case K_K2: key = VTERM_KEY_KP_2; break;
392 case K_K3: key = VTERM_KEY_KP_3; break;
393 case K_K4: key = VTERM_KEY_KP_4; break;
394 case K_K5: key = VTERM_KEY_KP_5; break;
395 case K_K6: key = VTERM_KEY_KP_6; break;
396 case K_K7: key = VTERM_KEY_KP_7; break;
397 case K_K8: key = VTERM_KEY_KP_8; break;
398 case K_K9: key = VTERM_KEY_KP_9; break;
399 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
400 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
401 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
402 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
403 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
404 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
405 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
406 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
407 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
408 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
409 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
410 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
411 case K_LEFT: key = VTERM_KEY_LEFT; break;
412 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
413 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
414 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
415 case K_UP: key = VTERM_KEY_UP; break;
416 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200417
418 case K_MOUSEUP: /* TODO */ break;
419 case K_MOUSEDOWN: /* TODO */ break;
420 case K_MOUSELEFT: /* TODO */ break;
421 case K_MOUSERIGHT: /* TODO */ break;
422
423 case K_LEFTMOUSE: /* TODO */ break;
424 case K_LEFTMOUSE_NM: /* TODO */ break;
425 case K_LEFTDRAG: /* TODO */ break;
426 case K_LEFTRELEASE: /* TODO */ break;
427 case K_LEFTRELEASE_NM: /* TODO */ break;
428 case K_MIDDLEMOUSE: /* TODO */ break;
429 case K_MIDDLEDRAG: /* TODO */ break;
430 case K_MIDDLERELEASE: /* TODO */ break;
431 case K_RIGHTMOUSE: /* TODO */ break;
432 case K_RIGHTDRAG: /* TODO */ break;
433 case K_RIGHTRELEASE: /* TODO */ break;
434 case K_X1MOUSE: /* TODO */ break;
435 case K_X1DRAG: /* TODO */ break;
436 case K_X1RELEASE: /* TODO */ break;
437 case K_X2MOUSE: /* TODO */ break;
438 case K_X2DRAG: /* TODO */ break;
439 case K_X2RELEASE: /* TODO */ break;
440
441 /* TODO: handle all special keys and modifiers that terminal_loop()
442 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200443 }
444
445 /*
446 * Convert special keys to vterm keys:
447 * - Write keys to vterm: vterm_keyboard_key()
448 * - Write output to channel.
449 */
450 if (key != VTERM_KEY_NONE)
451 /* Special key, let vterm convert it. */
452 vterm_keyboard_key(vterm, key, mod);
453 else
454 /* Normal character, let vterm convert it. */
455 vterm_keyboard_unichar(vterm, c, mod);
456
457 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200458 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200459}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200460
Bram Moolenaar938783d2017-07-16 20:13:26 +0200461/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200462 * Wait for input and send it to the job.
463 * Return when the start of a CTRL-W command is typed or anything else that
464 * should be handled as a Normal mode command.
465 */
466 void
467terminal_loop(void)
468{
469 char buf[KEY_BUF_LEN];
470 int c;
471 size_t len;
472 static int mouse_was_outside = FALSE;
473 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200474 int termkey = 0;
475
476 if (*curwin->w_p_tk != NUL)
477 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200478
479 for (;;)
480 {
481 /* TODO: skip screen update when handling a sequence of keys. */
482 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200483 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200484 ++no_mapping;
485 ++allow_keys;
486 got_int = FALSE;
487 c = vgetc();
488 --no_mapping;
489 --allow_keys;
490
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200491 if (c == (termkey == 0 ? Ctrl_W : termkey))
492 {
493 stuffcharReadbuff(Ctrl_W);
494 return;
495 }
496
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200497 /* Catch keys that need to be handled as in Normal mode. */
498 switch (c)
499 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200500 case NUL:
501 case K_ZERO:
502 stuffcharReadbuff(c);
503 return;
504
505 case K_IGNORE: continue;
506
507 case K_LEFTDRAG:
508 case K_MIDDLEDRAG:
509 case K_RIGHTDRAG:
510 case K_X1DRAG:
511 case K_X2DRAG:
512 dragging_outside = mouse_was_outside;
513 /* FALLTHROUGH */
514 case K_LEFTMOUSE:
515 case K_LEFTMOUSE_NM:
516 case K_LEFTRELEASE:
517 case K_LEFTRELEASE_NM:
518 case K_MIDDLEMOUSE:
519 case K_MIDDLERELEASE:
520 case K_RIGHTMOUSE:
521 case K_RIGHTRELEASE:
522 case K_X1MOUSE:
523 case K_X1RELEASE:
524 case K_X2MOUSE:
525 case K_X2RELEASE:
526 if (mouse_row < W_WINROW(curwin)
527 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
528 || mouse_col < W_WINCOL(curwin)
529 || mouse_col >= W_ENDCOL(curwin)
530 || dragging_outside)
531 {
532 /* click outside the current window */
533 stuffcharReadbuff(c);
534 mouse_was_outside = TRUE;
535 return;
536 }
537 }
538 mouse_was_outside = FALSE;
539
540 /* Convert the typed key to a sequence of bytes for the job. */
541 len = term_convert_key(c, buf);
542 if (len > 0)
543 /* TODO: if FAIL is returned, stop? */
544 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200545 (char_u *)buf, (int)len, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200546 }
547}
548
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200549/*
550 * Called when a job has finished.
551 */
552 void
553term_job_ended(job_T *job)
554{
Bram Moolenaar21554412017-07-24 21:44:43 +0200555 term_T *term;
556 int did_one = FALSE;
557
558 for (term = first_term; term != NULL; term = term->tl_next)
559 if (term->tl_job == job)
560 {
561 vim_free(term->tl_title);
562 term->tl_title = NULL;
563 vim_free(term->tl_status_text);
564 term->tl_status_text = NULL;
565 redraw_buf_and_status_later(term->tl_buffer, VALID);
566 did_one = TRUE;
567 }
568 if (did_one)
Bram Moolenaar21554412017-07-24 21:44:43 +0200569 redraw_statuslines();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200570 if (curbuf->b_term != NULL)
571 {
572 if (curbuf->b_term->tl_job == job)
573 maketitle();
574 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar21554412017-07-24 21:44:43 +0200575 }
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200576}
577
578/*
579 * Return TRUE if the job for "buf" is still running.
580 */
Bram Moolenaar21554412017-07-24 21:44:43 +0200581 static int
582term_job_running(term_T *term)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200583{
Bram Moolenaar21554412017-07-24 21:44:43 +0200584 return term->tl_job != NULL && term->tl_job->jv_status == JOB_STARTED;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200585}
586
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200587 static void
588position_cursor(win_T *wp, VTermPos *pos)
589{
590 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
591 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
592}
593
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200594 static void
595may_toggle_cursor(term_T *term)
596{
597 if (curbuf == term->tl_buffer)
598 {
599 if (term->tl_cursor_visible)
600 cursor_on();
601 else
602 cursor_off();
603 }
604}
605
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200606 static int
607handle_damage(VTermRect rect, void *user)
608{
609 term_T *term = (term_T *)user;
610
611 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
612 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
613 redraw_buf_later(term->tl_buffer, NOT_VALID);
614 return 1;
615}
616
617 static int
618handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
619{
620 term_T *term = (term_T *)user;
621
622 /* TODO */
623 redraw_buf_later(term->tl_buffer, NOT_VALID);
624 return 1;
625}
626
627 static int
628handle_movecursor(
629 VTermPos pos,
630 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200631 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200632 void *user)
633{
634 term_T *term = (term_T *)user;
635 win_T *wp;
636 int is_current = FALSE;
637
638 FOR_ALL_WINDOWS(wp)
639 {
640 if (wp->w_buffer == term->tl_buffer)
641 {
642 position_cursor(wp, &pos);
643 if (wp == curwin)
644 is_current = TRUE;
645 }
646 }
647
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200648 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200649 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200650 {
651 may_toggle_cursor(term);
652 update_cursor(term, TRUE);
653 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200654
655 return 1;
656}
657
Bram Moolenaar21554412017-07-24 21:44:43 +0200658 static int
659handle_settermprop(
660 VTermProp prop,
661 VTermValue *value,
662 void *user)
663{
664 term_T *term = (term_T *)user;
665
666 switch (prop)
667 {
668 case VTERM_PROP_TITLE:
669 vim_free(term->tl_title);
670 term->tl_title = vim_strsave((char_u *)value->string);
671 vim_free(term->tl_status_text);
672 term->tl_status_text = NULL;
673 if (term == curbuf->b_term)
674 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200675 break;
676
677 case VTERM_PROP_CURSORVISIBLE:
678 term->tl_cursor_visible = value->boolean;
679 may_toggle_cursor(term);
680 out_flush();
681 break;
682
Bram Moolenaar21554412017-07-24 21:44:43 +0200683 default:
684 break;
685 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200686 /* Always return 1, otherwise vterm doesn't store the value internally. */
687 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +0200688}
689
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200690/*
691 * The job running in the terminal resized the terminal.
692 */
693 static int
694handle_resize(int rows, int cols, void *user)
695{
696 term_T *term = (term_T *)user;
697 win_T *wp;
698
699 term->tl_rows = rows;
700 term->tl_cols = cols;
701 FOR_ALL_WINDOWS(wp)
702 {
703 if (wp->w_buffer == term->tl_buffer)
704 {
705 win_setheight_win(rows, wp);
706 win_setwidth_win(cols, wp);
707 }
708 }
709
710 redraw_buf_later(term->tl_buffer, NOT_VALID);
711 return 1;
712}
713
Bram Moolenaar21554412017-07-24 21:44:43 +0200714static VTermScreenCallbacks screen_callbacks = {
715 handle_damage, /* damage */
716 handle_moverect, /* moverect */
717 handle_movecursor, /* movecursor */
718 handle_settermprop, /* settermprop */
719 NULL, /* bell */
720 handle_resize, /* resize */
721 NULL, /* sb_pushline */
722 NULL /* sb_popline */
723};
724
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200725/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200726 * Reverse engineer the RGB value into a cterm color index.
727 * First color is 1. Return 0 if no match found.
728 */
729 static int
730color2index(VTermColor *color)
731{
732 int red = color->red;
733 int blue = color->blue;
734 int green = color->green;
735
736 if (red == 0)
737 {
738 if (green == 0)
739 {
740 if (blue == 0)
741 return 1; /* black */
742 if (blue == 224)
743 return 5; /* blue */
744 }
745 else if (green == 224)
746 {
747 if (blue == 0)
748 return 3; /* green */
749 if (blue == 224)
750 return 7; /* cyan */
751 }
752 }
753 else if (red == 224)
754 {
755 if (green == 0)
756 {
757 if (blue == 0)
758 return 2; /* red */
759 if (blue == 224)
760 return 6; /* magenta */
761 }
762 else if (green == 224)
763 {
764 if (blue == 0)
765 return 4; /* yellow */
766 if (blue == 224)
767 return 8; /* white */
768 }
769 }
770 else if (red == 128)
771 {
772 if (green == 128 && blue == 128)
Bram Moolenaar8a773062017-07-24 22:29:21 +0200773 return 9; /* high intensity black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200774 }
775 else if (red == 255)
776 {
777 if (green == 64)
778 {
779 if (blue == 64)
780 return 10; /* high intensity red */
781 if (blue == 255)
782 return 14; /* high intensity magenta */
783 }
784 else if (green == 255)
785 {
786 if (blue == 64)
787 return 12; /* high intensity yellow */
788 if (blue == 255)
789 return 16; /* high intensity white */
790 }
791 }
792 else if (red == 64)
793 {
794 if (green == 64)
795 {
796 if (blue == 255)
797 return 13; /* high intensity blue */
798 }
799 else if (green == 255)
800 {
801 if (blue == 64)
802 return 11; /* high intensity green */
803 if (blue == 255)
804 return 15; /* high intensity cyan */
805 }
806 }
807 if (t_colors >= 256)
808 {
809 if (red == blue && red == green)
810 {
811 /* 24-color greyscale */
812 static int cutoff[23] = {
813 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
814 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
815 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
816 int i;
817
818 for (i = 0; i < 23; ++i)
819 if (red < cutoff[i])
820 return i + 233;
821 return 256;
822 }
823
824 /* 216-color cube */
825 return 17 + ((red + 25) / 0x33) * 36
826 + ((green + 25) / 0x33) * 6
827 + (blue + 25) / 0x33;
828 }
829 return 0;
830}
831
832/*
833 * Convert the attributes of a vterm cell into an attribute index.
834 */
835 static int
836cell2attr(VTermScreenCell *cell)
837{
838 int attr = 0;
839
840 if (cell->attrs.bold)
841 attr |= HL_BOLD;
842 if (cell->attrs.underline)
843 attr |= HL_UNDERLINE;
844 if (cell->attrs.italic)
845 attr |= HL_ITALIC;
846 if (cell->attrs.strike)
847 attr |= HL_STANDOUT;
848 if (cell->attrs.reverse)
849 attr |= HL_INVERSE;
850 if (cell->attrs.strike)
851 attr |= HL_UNDERLINE;
852
853#ifdef FEAT_GUI
854 if (gui.in_use)
855 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200856 guicolor_T fg, bg;
857
858 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
859 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
860 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200861 }
862 else
863#endif
864#ifdef FEAT_TERMGUICOLORS
865 if (p_tgc)
866 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200867 guicolor_T fg, bg;
868
869 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
870 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
871
872 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200873 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200874 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200875#endif
876 {
877 return get_cterm_attr_idx(attr, color2index(&cell->fg),
878 color2index(&cell->bg));
879 }
880 return 0;
881}
882
883/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200884 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200885 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200886 void
887term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200888{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200889 term_T *term = wp->w_buffer->b_term;
890 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200891 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200892 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200893 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200894
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200895 /*
896 * If the window was resized a redraw will be triggered and we get here.
897 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
898 */
899 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
900 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200901 {
902 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
903 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
904
905 vterm_set_size(vterm, rows, cols);
906 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
907 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200908 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200909 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200910
911 /* The cursor may have been moved when resizing. */
912 vterm_state_get_cursorpos(state, &pos);
913 position_cursor(wp, &pos);
914
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200915 /* TODO: Only redraw what changed. */
916 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200917 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200918 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200919 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200920
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200921 if (pos.row < term->tl_rows)
922 {
923 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200924 {
925 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200926 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200927
Bram Moolenaareeac6772017-07-23 15:48:37 +0200928 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
929 vim_memset(&cell, 0, sizeof(cell));
930
931 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200932 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200933 if (c == NUL)
934 {
935 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +0200936#if defined(FEAT_MBYTE)
937 if (enc_utf8)
938 ScreenLinesUC[off] = NUL;
939#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200940 }
941 else
942 {
943#if defined(FEAT_MBYTE)
944 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200945 {
946 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200947 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200948 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200949 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200950 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200951 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200952 if (enc_utf8)
953 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200954 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200955#else
956 ScreenLines[off] = c;
957#endif
958 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200959 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200960
961 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200962 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200963 if (cell.width == 2)
964 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200965 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200966#if defined(FEAT_MBYTE)
967 if (enc_utf8)
968 ScreenLinesUC[off] = NUL;
969#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200970 ++pos.col;
971 ++off;
972 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200973 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200974 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200975 else
976 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200977
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200978 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
979 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200980 }
981}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200982
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200983/*
984 * Set job options common for Unix and MS-Windows.
985 */
986 static void
987setup_job_options(jobopt_T *opt, int rows, int cols)
988{
989 clear_job_options(opt);
990 opt->jo_mode = MODE_RAW;
991 opt->jo_out_mode = MODE_RAW;
992 opt->jo_err_mode = MODE_RAW;
993 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200994
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200995 opt->jo_io[PART_OUT] = JIO_BUFFER;
996 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200997 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
998
999 opt->jo_modifiable[PART_OUT] = 0;
1000 opt->jo_modifiable[PART_ERR] = 0;
1001 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1002
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001003 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1004 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001005 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001006 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1007
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001008 opt->jo_term_rows = rows;
1009 opt->jo_term_cols = cols;
1010}
1011
1012/*
1013 * Create a new vterm and initialize it.
1014 */
1015 static void
1016create_vterm(term_T *term, int rows, int cols)
1017{
1018 VTerm *vterm;
1019 VTermScreen *screen;
1020
1021 vterm = vterm_new(rows, cols);
1022 term->tl_vterm = vterm;
1023 screen = vterm_obtain_screen(vterm);
1024 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1025 /* TODO: depends on 'encoding'. */
1026 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001027
1028 /* Vterm uses a default black background. Set it to white when
1029 * 'background' is "light". */
1030 if (*p_bg == 'l')
1031 {
1032 VTermColor fg, bg;
1033
1034 fg.red = fg.green = fg.blue = 0;
1035 bg.red = bg.green = bg.blue = 255;
1036 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1037 }
1038
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001039 /* Required to initialize most things. */
1040 vterm_screen_reset(screen, 1 /* hard */);
1041}
1042
Bram Moolenaar21554412017-07-24 21:44:43 +02001043/*
1044 * Return the text to show for the buffer name and status.
1045 */
1046 char_u *
1047term_get_status_text(term_T *term)
1048{
1049 if (term->tl_status_text == NULL)
1050 {
1051 char_u *txt;
1052 size_t len;
1053
1054 if (term->tl_title != NULL)
1055 txt = term->tl_title;
1056 else if (term_job_running(term))
1057 txt = (char_u *)_("running");
1058 else
1059 txt = (char_u *)_("finished");
1060 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001061 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001062 if (term->tl_status_text != NULL)
1063 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1064 term->tl_buffer->b_fname, txt);
1065 }
1066 return term->tl_status_text;
1067}
1068
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001069# ifdef WIN3264
1070
1071#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1072#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1073
Bram Moolenaar8a773062017-07-24 22:29:21 +02001074void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001075void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001076void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001077BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1078void (*winpty_config_set_initial_size)(void*, int, int);
1079LPCWSTR (*winpty_conin_name)(void*);
1080LPCWSTR (*winpty_conout_name)(void*);
1081LPCWSTR (*winpty_conerr_name)(void*);
1082void (*winpty_free)(void*);
1083void (*winpty_config_free)(void*);
1084void (*winpty_spawn_config_free)(void*);
1085void (*winpty_error_free)(void*);
1086LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001087BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001088
1089/**************************************
1090 * 2. MS-Windows implementation.
1091 */
1092
1093#define WINPTY_DLL "winpty.dll"
1094
1095static HINSTANCE hWinPtyDLL = NULL;
1096
1097 int
1098dyn_winpty_init(void)
1099{
1100 int i;
1101 static struct
1102 {
1103 char *name;
1104 FARPROC *ptr;
1105 } winpty_entry[] =
1106 {
1107 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1108 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1109 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1110 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1111 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1112 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1113 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1114 {"winpty_free", (FARPROC*)&winpty_free},
1115 {"winpty_open", (FARPROC*)&winpty_open},
1116 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1117 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1118 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1119 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001120 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001121 {NULL, NULL}
1122 };
1123
1124 /* No need to initialize twice. */
1125 if (hWinPtyDLL)
1126 return 1;
1127 /* Load winpty.dll */
1128 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1129 if (!hWinPtyDLL)
1130 {
1131 EMSG2(_(e_loadlib), WINPTY_DLL);
1132 return 0;
1133 }
1134 for (i = 0; winpty_entry[i].name != NULL
1135 && winpty_entry[i].ptr != NULL; ++i)
1136 {
1137 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1138 winpty_entry[i].name)) == NULL)
1139 {
1140 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1141 return 0;
1142 }
1143 }
1144
1145 return 1;
1146}
1147
1148/*
1149 * Create a new terminal of "rows" by "cols" cells.
1150 * Store a reference in "term".
1151 * Return OK or FAIL.
1152 */
1153 static int
1154term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1155{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001156 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001157 channel_T *channel = NULL;
1158 job_T *job = NULL;
1159 jobopt_T opt;
1160 DWORD error;
1161 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1162 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001163 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001164
1165 if (!dyn_winpty_init())
1166 return FAIL;
1167
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001168 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001169 if (p == NULL)
1170 return FAIL;
1171
1172 job = job_alloc();
1173 if (job == NULL)
1174 goto failed;
1175
1176 channel = add_channel();
1177 if (channel == NULL)
1178 goto failed;
1179
1180 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1181 if (term->tl_winpty_config == NULL)
1182 goto failed;
1183
1184 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1185 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1186 if (term->tl_winpty == NULL)
1187 goto failed;
1188
1189 spawn_config = winpty_spawn_config_new(
1190 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1191 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1192 NULL,
1193 p,
1194 NULL,
1195 NULL,
1196 &winpty_err);
1197 if (spawn_config == NULL)
1198 goto failed;
1199
1200 channel = add_channel();
1201 if (channel == NULL)
1202 goto failed;
1203
1204 job = job_alloc();
1205 if (job == NULL)
1206 goto failed;
1207
1208 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1209 &child_thread_handle, &error, &winpty_err))
1210 goto failed;
1211
1212 channel_set_pipes(channel,
1213 (sock_T) CreateFileW(
1214 winpty_conin_name(term->tl_winpty),
1215 GENERIC_WRITE, 0, NULL,
1216 OPEN_EXISTING, 0, NULL),
1217 (sock_T) CreateFileW(
1218 winpty_conout_name(term->tl_winpty),
1219 GENERIC_READ, 0, NULL,
1220 OPEN_EXISTING, 0, NULL),
1221 (sock_T) CreateFileW(
1222 winpty_conerr_name(term->tl_winpty),
1223 GENERIC_READ, 0, NULL,
1224 OPEN_EXISTING, 0, NULL));
1225
1226 jo = CreateJobObject(NULL, NULL);
1227 if (jo == NULL)
1228 goto failed;
1229
1230 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001231 {
1232 /* Failed, switch the way to terminate process with TerminateProcess. */
1233 CloseHandle(jo);
1234 jo = NULL;
1235 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001236
1237 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001238 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001239
1240 create_vterm(term, rows, cols);
1241
1242 setup_job_options(&opt, rows, cols);
1243 channel_set_job(channel, job, &opt);
1244
1245 job->jv_channel = channel;
1246 job->jv_proc_info.hProcess = child_process_handle;
1247 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1248 job->jv_job_object = jo;
1249 job->jv_status = JOB_STARTED;
1250 term->tl_job = job;
1251
1252 return OK;
1253
1254failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001255 if (spawn_config != NULL)
1256 winpty_spawn_config_free(spawn_config);
1257 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001258 if (channel != NULL)
1259 channel_clear(channel);
1260 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001261 {
1262 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001263 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001264 }
1265 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001266 if (jo != NULL)
1267 CloseHandle(jo);
1268 if (term->tl_winpty != NULL)
1269 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001270 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001271 if (term->tl_winpty_config != NULL)
1272 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001273 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001274 if (winpty_err != NULL)
1275 {
1276 char_u *msg = utf16_to_enc(
1277 (short_u *)winpty_error_msg(winpty_err), NULL);
1278
1279 EMSG(msg);
1280 winpty_error_free(winpty_err);
1281 }
1282 return FAIL;
1283}
1284
1285/*
1286 * Free the terminal emulator part of "term".
1287 */
1288 static void
1289term_free(term_T *term)
1290{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001291 if (term->tl_winpty != NULL)
1292 winpty_free(term->tl_winpty);
1293 if (term->tl_winpty_config != NULL)
1294 winpty_config_free(term->tl_winpty_config);
1295 if (term->tl_vterm != NULL)
1296 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001297}
1298
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001299/*
1300 * Request size to terminal.
1301 */
1302 static void
1303term_report_winsize(term_T *term, int rows, int cols)
1304{
1305 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1306}
1307
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001308# else
1309
1310/**************************************
1311 * 3. Unix-like implementation.
1312 */
1313
1314/*
1315 * Create a new terminal of "rows" by "cols" cells.
1316 * Start job for "cmd".
1317 * Store the pointers in "term".
1318 * Return OK or FAIL.
1319 */
1320 static int
1321term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1322{
1323 typval_T argvars[2];
1324 jobopt_T opt;
1325
1326 create_vterm(term, rows, cols);
1327
1328 argvars[0].v_type = VAR_STRING;
1329 argvars[0].vval.v_string = cmd;
1330 setup_job_options(&opt, rows, cols);
1331 term->tl_job = job_start(argvars, &opt);
1332
Bram Moolenaar61a66052017-07-22 18:39:00 +02001333 return term->tl_job != NULL
1334 && term->tl_job->jv_channel != NULL
1335 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001336}
1337
1338/*
1339 * Free the terminal emulator part of "term".
1340 */
1341 static void
1342term_free(term_T *term)
1343{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001344 if (term->tl_vterm != NULL)
1345 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001346}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001347
1348/*
1349 * Request size to terminal.
1350 */
1351 static void
1352term_report_winsize(term_T *term, int rows, int cols)
1353{
1354 /* Use an ioctl() to report the new window size to the job. */
1355 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1356 {
1357 int fd = -1;
1358 int part;
1359
1360 for (part = PART_OUT; part < PART_COUNT; ++part)
1361 {
1362 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1363 if (isatty(fd))
1364 break;
1365 }
1366 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1367 mch_stop_job(term->tl_job, (char_u *)"winch");
1368 }
1369}
1370
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001371# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001372
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001373#endif /* FEAT_TERMINAL */