blob: 2ddbb264b5809a70d4aa3fea96254716879dc07a [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 Moolenaarb41bf8e2017-07-28 15:11:38 +020036 * - if 'term' starts witth "xterm" use it for $TERM.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020037 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar1c844932017-07-24 23:36:41 +020038 * - include functions from #1871
39 * - do not store terminal buffer in viminfo. Or prefix term:// ?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020040 * - Add a scrollback buffer (contains lines to scroll off the top).
41 * Can use the buf_T lines, store attributes somewhere else?
42 * - When the job ends:
43 * - Write "-- JOB ENDED --" in the terminal.
44 * - Put the terminal contents in the scrollback buffer.
45 * - Free the terminal emulator.
46 * - Display the scrollback buffer (but with attributes).
47 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar21554412017-07-24 21:44:43 +020048 * - Need an option or argument to drop the window+buffer right away, to be
49 * used for a shell or Vim.
50 * - add a character in :ls output
Bram Moolenaar938783d2017-07-16 20:13:26 +020051 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020052 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020053 * - use win_del_lines() to make scroll-up efficient.
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 Moolenaar1f28b4c2017-07-28 13:48:34 +0200462 * Get a key from the user without mapping.
463 * TODO: use terminal mode mappings.
464 */
465 static int
466term_vgetc()
467{
468 int c;
469
470 ++no_mapping;
471 ++allow_keys;
472 got_int = FALSE;
473 c = vgetc();
474 --no_mapping;
475 --allow_keys;
476 return c;
477}
478
479/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200480 * Wait for input and send it to the job.
481 * Return when the start of a CTRL-W command is typed or anything else that
482 * should be handled as a Normal mode command.
483 */
484 void
485terminal_loop(void)
486{
487 char buf[KEY_BUF_LEN];
488 int c;
489 size_t len;
490 static int mouse_was_outside = FALSE;
491 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200492 int termkey = 0;
493
494 if (*curwin->w_p_tk != NUL)
495 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200496
497 for (;;)
498 {
499 /* TODO: skip screen update when handling a sequence of keys. */
500 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200501 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200502 c = term_vgetc();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200503
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200504 if (c == (termkey == 0 ? Ctrl_W : termkey))
505 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200506#ifdef FEAT_CMDL_INFO
507 if (add_to_showcmd(c))
508 out_flush();
509#endif
510 c = term_vgetc();
511#ifdef FEAT_CMDL_INFO
512 clear_showcmd();
513#endif
514
515 if (termkey == 0 && c == '.')
516 /* "CTRL-W .": send CTRL-W to the job */
517 c = Ctrl_W;
518 else if (termkey == 0 || c != termkey)
519 {
520 stuffcharReadbuff(Ctrl_W);
521 stuffcharReadbuff(c);
522 return;
523 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200524 }
525
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200526 /* Catch keys that need to be handled as in Normal mode. */
527 switch (c)
528 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200529 case NUL:
530 case K_ZERO:
531 stuffcharReadbuff(c);
532 return;
533
534 case K_IGNORE: continue;
535
536 case K_LEFTDRAG:
537 case K_MIDDLEDRAG:
538 case K_RIGHTDRAG:
539 case K_X1DRAG:
540 case K_X2DRAG:
541 dragging_outside = mouse_was_outside;
542 /* FALLTHROUGH */
543 case K_LEFTMOUSE:
544 case K_LEFTMOUSE_NM:
545 case K_LEFTRELEASE:
546 case K_LEFTRELEASE_NM:
547 case K_MIDDLEMOUSE:
548 case K_MIDDLERELEASE:
549 case K_RIGHTMOUSE:
550 case K_RIGHTRELEASE:
551 case K_X1MOUSE:
552 case K_X1RELEASE:
553 case K_X2MOUSE:
554 case K_X2RELEASE:
555 if (mouse_row < W_WINROW(curwin)
556 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
557 || mouse_col < W_WINCOL(curwin)
558 || mouse_col >= W_ENDCOL(curwin)
559 || dragging_outside)
560 {
561 /* click outside the current window */
562 stuffcharReadbuff(c);
563 mouse_was_outside = TRUE;
564 return;
565 }
566 }
567 mouse_was_outside = FALSE;
568
569 /* Convert the typed key to a sequence of bytes for the job. */
570 len = term_convert_key(c, buf);
571 if (len > 0)
572 /* TODO: if FAIL is returned, stop? */
573 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200574 (char_u *)buf, (int)len, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200575 }
576}
577
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200578/*
579 * Called when a job has finished.
580 */
581 void
582term_job_ended(job_T *job)
583{
Bram Moolenaar21554412017-07-24 21:44:43 +0200584 term_T *term;
585 int did_one = FALSE;
586
587 for (term = first_term; term != NULL; term = term->tl_next)
588 if (term->tl_job == job)
589 {
590 vim_free(term->tl_title);
591 term->tl_title = NULL;
592 vim_free(term->tl_status_text);
593 term->tl_status_text = NULL;
594 redraw_buf_and_status_later(term->tl_buffer, VALID);
595 did_one = TRUE;
596 }
597 if (did_one)
Bram Moolenaar21554412017-07-24 21:44:43 +0200598 redraw_statuslines();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200599 if (curbuf->b_term != NULL)
600 {
601 if (curbuf->b_term->tl_job == job)
602 maketitle();
603 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar21554412017-07-24 21:44:43 +0200604 }
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200605}
606
607/*
608 * Return TRUE if the job for "buf" is still running.
609 */
Bram Moolenaar21554412017-07-24 21:44:43 +0200610 static int
611term_job_running(term_T *term)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200612{
Bram Moolenaar21554412017-07-24 21:44:43 +0200613 return term->tl_job != NULL && term->tl_job->jv_status == JOB_STARTED;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200614}
615
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200616 static void
617position_cursor(win_T *wp, VTermPos *pos)
618{
619 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
620 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
621}
622
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200623 static void
624may_toggle_cursor(term_T *term)
625{
626 if (curbuf == term->tl_buffer)
627 {
628 if (term->tl_cursor_visible)
629 cursor_on();
630 else
631 cursor_off();
632 }
633}
634
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200635 static int
636handle_damage(VTermRect rect, void *user)
637{
638 term_T *term = (term_T *)user;
639
640 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
641 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
642 redraw_buf_later(term->tl_buffer, NOT_VALID);
643 return 1;
644}
645
646 static int
647handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
648{
649 term_T *term = (term_T *)user;
650
651 /* TODO */
652 redraw_buf_later(term->tl_buffer, NOT_VALID);
653 return 1;
654}
655
656 static int
657handle_movecursor(
658 VTermPos pos,
659 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200660 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200661 void *user)
662{
663 term_T *term = (term_T *)user;
664 win_T *wp;
665 int is_current = FALSE;
666
667 FOR_ALL_WINDOWS(wp)
668 {
669 if (wp->w_buffer == term->tl_buffer)
670 {
671 position_cursor(wp, &pos);
672 if (wp == curwin)
673 is_current = TRUE;
674 }
675 }
676
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200677 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200678 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200679 {
680 may_toggle_cursor(term);
681 update_cursor(term, TRUE);
682 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200683
684 return 1;
685}
686
Bram Moolenaar21554412017-07-24 21:44:43 +0200687 static int
688handle_settermprop(
689 VTermProp prop,
690 VTermValue *value,
691 void *user)
692{
693 term_T *term = (term_T *)user;
694
695 switch (prop)
696 {
697 case VTERM_PROP_TITLE:
698 vim_free(term->tl_title);
699 term->tl_title = vim_strsave((char_u *)value->string);
700 vim_free(term->tl_status_text);
701 term->tl_status_text = NULL;
702 if (term == curbuf->b_term)
703 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200704 break;
705
706 case VTERM_PROP_CURSORVISIBLE:
707 term->tl_cursor_visible = value->boolean;
708 may_toggle_cursor(term);
709 out_flush();
710 break;
711
Bram Moolenaar21554412017-07-24 21:44:43 +0200712 default:
713 break;
714 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200715 /* Always return 1, otherwise vterm doesn't store the value internally. */
716 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +0200717}
718
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200719/*
720 * The job running in the terminal resized the terminal.
721 */
722 static int
723handle_resize(int rows, int cols, void *user)
724{
725 term_T *term = (term_T *)user;
726 win_T *wp;
727
728 term->tl_rows = rows;
729 term->tl_cols = cols;
730 FOR_ALL_WINDOWS(wp)
731 {
732 if (wp->w_buffer == term->tl_buffer)
733 {
734 win_setheight_win(rows, wp);
735 win_setwidth_win(cols, wp);
736 }
737 }
738
739 redraw_buf_later(term->tl_buffer, NOT_VALID);
740 return 1;
741}
742
Bram Moolenaar21554412017-07-24 21:44:43 +0200743static VTermScreenCallbacks screen_callbacks = {
744 handle_damage, /* damage */
745 handle_moverect, /* moverect */
746 handle_movecursor, /* movecursor */
747 handle_settermprop, /* settermprop */
748 NULL, /* bell */
749 handle_resize, /* resize */
750 NULL, /* sb_pushline */
751 NULL /* sb_popline */
752};
753
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200754/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200755 * Reverse engineer the RGB value into a cterm color index.
756 * First color is 1. Return 0 if no match found.
757 */
758 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200759color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +0200760{
761 int red = color->red;
762 int blue = color->blue;
763 int green = color->green;
764
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200765 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200766 if (red == 0)
767 {
768 if (green == 0)
769 {
770 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200771 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200772 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200773 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200774 }
775 else if (green == 224)
776 {
777 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200778 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200779 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200780 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200781 }
782 }
783 else if (red == 224)
784 {
785 if (green == 0)
786 {
787 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200788 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200789 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200790 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200791 }
792 else if (green == 224)
793 {
794 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200795 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200796 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200797 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200798 }
799 }
800 else if (red == 128)
801 {
802 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200803 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200804 }
805 else if (red == 255)
806 {
807 if (green == 64)
808 {
809 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200810 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200811 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200812 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200813 }
814 else if (green == 255)
815 {
816 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200817 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200818 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200819 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200820 }
821 }
822 else if (red == 64)
823 {
824 if (green == 64)
825 {
826 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200827 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200828 }
829 else if (green == 255)
830 {
831 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200832 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200833 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200834 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200835 }
836 }
837 if (t_colors >= 256)
838 {
839 if (red == blue && red == green)
840 {
841 /* 24-color greyscale */
842 static int cutoff[23] = {
843 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
844 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
845 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
846 int i;
847
848 for (i = 0; i < 23; ++i)
849 if (red < cutoff[i])
850 return i + 233;
851 return 256;
852 }
853
854 /* 216-color cube */
855 return 17 + ((red + 25) / 0x33) * 36
856 + ((green + 25) / 0x33) * 6
857 + (blue + 25) / 0x33;
858 }
859 return 0;
860}
861
862/*
863 * Convert the attributes of a vterm cell into an attribute index.
864 */
865 static int
866cell2attr(VTermScreenCell *cell)
867{
868 int attr = 0;
869
870 if (cell->attrs.bold)
871 attr |= HL_BOLD;
872 if (cell->attrs.underline)
873 attr |= HL_UNDERLINE;
874 if (cell->attrs.italic)
875 attr |= HL_ITALIC;
876 if (cell->attrs.strike)
877 attr |= HL_STANDOUT;
878 if (cell->attrs.reverse)
879 attr |= HL_INVERSE;
880 if (cell->attrs.strike)
881 attr |= HL_UNDERLINE;
882
883#ifdef FEAT_GUI
884 if (gui.in_use)
885 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200886 guicolor_T fg, bg;
887
888 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
889 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
890 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200891 }
892 else
893#endif
894#ifdef FEAT_TERMGUICOLORS
895 if (p_tgc)
896 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200897 guicolor_T fg, bg;
898
899 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
900 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
901
902 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200903 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200904 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200905#endif
906 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +0200907 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
908 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +0200909 }
910 return 0;
911}
912
913/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200914 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200915 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200916 void
917term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200918{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200919 term_T *term = wp->w_buffer->b_term;
920 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200921 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200922 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200923 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200924
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200925 /*
926 * If the window was resized a redraw will be triggered and we get here.
927 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
928 */
929 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
930 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200931 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +0200932 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
933 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
934 win_T *twp;
935
936 FOR_ALL_WINDOWS(twp)
937 {
938 /* When more than one window shows the same terminal, use the
939 * smallest size. */
940 if (twp->w_buffer == term->tl_buffer)
941 {
942 if (!term->tl_rows_fixed && rows > twp->w_height)
943 rows = twp->w_height;
944 if (!term->tl_cols_fixed && cols > twp->w_width)
945 cols = twp->w_width;
946 }
947 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200948
949 vterm_set_size(vterm, rows, cols);
950 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
951 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200952 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200953 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200954
955 /* The cursor may have been moved when resizing. */
956 vterm_state_get_cursorpos(state, &pos);
957 position_cursor(wp, &pos);
958
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200959 /* TODO: Only redraw what changed. */
960 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200961 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200962 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200963 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200964
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200965 if (pos.row < term->tl_rows)
966 {
967 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200968 {
969 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200970 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200971
Bram Moolenaareeac6772017-07-23 15:48:37 +0200972 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
973 vim_memset(&cell, 0, sizeof(cell));
974
975 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200976 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200977 if (c == NUL)
978 {
979 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +0200980#if defined(FEAT_MBYTE)
981 if (enc_utf8)
982 ScreenLinesUC[off] = NUL;
983#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200984 }
985 else
986 {
987#if defined(FEAT_MBYTE)
988 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200989 {
990 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200991 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200992 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200993 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200994 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200995 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200996 if (enc_utf8)
997 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200998 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200999#else
1000 ScreenLines[off] = c;
1001#endif
1002 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001003 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001004
1005 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001006 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001007 if (cell.width == 2)
1008 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001009 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001010#if defined(FEAT_MBYTE)
1011 if (enc_utf8)
1012 ScreenLinesUC[off] = NUL;
1013#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001014 ++pos.col;
1015 ++off;
1016 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001017 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001018 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001019 else
1020 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001021
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001022 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1023 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001024 }
1025}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001026
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001027/*
1028 * Set job options common for Unix and MS-Windows.
1029 */
1030 static void
1031setup_job_options(jobopt_T *opt, int rows, int cols)
1032{
1033 clear_job_options(opt);
1034 opt->jo_mode = MODE_RAW;
1035 opt->jo_out_mode = MODE_RAW;
1036 opt->jo_err_mode = MODE_RAW;
1037 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001038
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001039 opt->jo_io[PART_OUT] = JIO_BUFFER;
1040 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001041 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1042
1043 opt->jo_modifiable[PART_OUT] = 0;
1044 opt->jo_modifiable[PART_ERR] = 0;
1045 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1046
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001047 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1048 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001049 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001050 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1051
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001052 opt->jo_term_rows = rows;
1053 opt->jo_term_cols = cols;
1054}
1055
1056/*
1057 * Create a new vterm and initialize it.
1058 */
1059 static void
1060create_vterm(term_T *term, int rows, int cols)
1061{
1062 VTerm *vterm;
1063 VTermScreen *screen;
1064
1065 vterm = vterm_new(rows, cols);
1066 term->tl_vterm = vterm;
1067 screen = vterm_obtain_screen(vterm);
1068 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1069 /* TODO: depends on 'encoding'. */
1070 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001071
1072 /* Vterm uses a default black background. Set it to white when
1073 * 'background' is "light". */
1074 if (*p_bg == 'l')
1075 {
1076 VTermColor fg, bg;
1077
1078 fg.red = fg.green = fg.blue = 0;
1079 bg.red = bg.green = bg.blue = 255;
1080 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1081 }
1082
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001083 /* Required to initialize most things. */
1084 vterm_screen_reset(screen, 1 /* hard */);
1085}
1086
Bram Moolenaar21554412017-07-24 21:44:43 +02001087/*
1088 * Return the text to show for the buffer name and status.
1089 */
1090 char_u *
1091term_get_status_text(term_T *term)
1092{
1093 if (term->tl_status_text == NULL)
1094 {
1095 char_u *txt;
1096 size_t len;
1097
1098 if (term->tl_title != NULL)
1099 txt = term->tl_title;
1100 else if (term_job_running(term))
1101 txt = (char_u *)_("running");
1102 else
1103 txt = (char_u *)_("finished");
1104 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001105 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001106 if (term->tl_status_text != NULL)
1107 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1108 term->tl_buffer->b_fname, txt);
1109 }
1110 return term->tl_status_text;
1111}
1112
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001113/*
1114 * Mark references in jobs of terminals.
1115 */
1116 int
1117set_ref_in_term(int copyID)
1118{
1119 int abort = FALSE;
1120 term_T *term;
1121 typval_T tv;
1122
1123 for (term = first_term; term != NULL; term = term->tl_next)
1124 if (term->tl_job != NULL)
1125 {
1126 tv.v_type = VAR_JOB;
1127 tv.vval.v_job = term->tl_job;
1128 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1129 }
1130 return abort;
1131}
1132
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001133# ifdef WIN3264
1134
1135#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1136#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1137
Bram Moolenaar8a773062017-07-24 22:29:21 +02001138void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001139void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001140void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001141BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1142void (*winpty_config_set_initial_size)(void*, int, int);
1143LPCWSTR (*winpty_conin_name)(void*);
1144LPCWSTR (*winpty_conout_name)(void*);
1145LPCWSTR (*winpty_conerr_name)(void*);
1146void (*winpty_free)(void*);
1147void (*winpty_config_free)(void*);
1148void (*winpty_spawn_config_free)(void*);
1149void (*winpty_error_free)(void*);
1150LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001151BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001152
1153/**************************************
1154 * 2. MS-Windows implementation.
1155 */
1156
1157#define WINPTY_DLL "winpty.dll"
1158
1159static HINSTANCE hWinPtyDLL = NULL;
1160
1161 int
1162dyn_winpty_init(void)
1163{
1164 int i;
1165 static struct
1166 {
1167 char *name;
1168 FARPROC *ptr;
1169 } winpty_entry[] =
1170 {
1171 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1172 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1173 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1174 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1175 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1176 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1177 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1178 {"winpty_free", (FARPROC*)&winpty_free},
1179 {"winpty_open", (FARPROC*)&winpty_open},
1180 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1181 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1182 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1183 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001184 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001185 {NULL, NULL}
1186 };
1187
1188 /* No need to initialize twice. */
1189 if (hWinPtyDLL)
1190 return 1;
1191 /* Load winpty.dll */
1192 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1193 if (!hWinPtyDLL)
1194 {
1195 EMSG2(_(e_loadlib), WINPTY_DLL);
1196 return 0;
1197 }
1198 for (i = 0; winpty_entry[i].name != NULL
1199 && winpty_entry[i].ptr != NULL; ++i)
1200 {
1201 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1202 winpty_entry[i].name)) == NULL)
1203 {
1204 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1205 return 0;
1206 }
1207 }
1208
1209 return 1;
1210}
1211
1212/*
1213 * Create a new terminal of "rows" by "cols" cells.
1214 * Store a reference in "term".
1215 * Return OK or FAIL.
1216 */
1217 static int
1218term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1219{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001220 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001221 channel_T *channel = NULL;
1222 job_T *job = NULL;
1223 jobopt_T opt;
1224 DWORD error;
1225 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1226 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001227 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001228
1229 if (!dyn_winpty_init())
1230 return FAIL;
1231
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001232 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001233 if (p == NULL)
1234 return FAIL;
1235
1236 job = job_alloc();
1237 if (job == NULL)
1238 goto failed;
1239
1240 channel = add_channel();
1241 if (channel == NULL)
1242 goto failed;
1243
1244 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1245 if (term->tl_winpty_config == NULL)
1246 goto failed;
1247
1248 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1249 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1250 if (term->tl_winpty == NULL)
1251 goto failed;
1252
1253 spawn_config = winpty_spawn_config_new(
1254 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1255 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1256 NULL,
1257 p,
1258 NULL,
1259 NULL,
1260 &winpty_err);
1261 if (spawn_config == NULL)
1262 goto failed;
1263
1264 channel = add_channel();
1265 if (channel == NULL)
1266 goto failed;
1267
1268 job = job_alloc();
1269 if (job == NULL)
1270 goto failed;
1271
1272 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1273 &child_thread_handle, &error, &winpty_err))
1274 goto failed;
1275
1276 channel_set_pipes(channel,
1277 (sock_T) CreateFileW(
1278 winpty_conin_name(term->tl_winpty),
1279 GENERIC_WRITE, 0, NULL,
1280 OPEN_EXISTING, 0, NULL),
1281 (sock_T) CreateFileW(
1282 winpty_conout_name(term->tl_winpty),
1283 GENERIC_READ, 0, NULL,
1284 OPEN_EXISTING, 0, NULL),
1285 (sock_T) CreateFileW(
1286 winpty_conerr_name(term->tl_winpty),
1287 GENERIC_READ, 0, NULL,
1288 OPEN_EXISTING, 0, NULL));
1289
1290 jo = CreateJobObject(NULL, NULL);
1291 if (jo == NULL)
1292 goto failed;
1293
1294 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001295 {
1296 /* Failed, switch the way to terminate process with TerminateProcess. */
1297 CloseHandle(jo);
1298 jo = NULL;
1299 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001300
1301 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001302 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001303
1304 create_vterm(term, rows, cols);
1305
1306 setup_job_options(&opt, rows, cols);
1307 channel_set_job(channel, job, &opt);
1308
1309 job->jv_channel = channel;
1310 job->jv_proc_info.hProcess = child_process_handle;
1311 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1312 job->jv_job_object = jo;
1313 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001314 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001315 term->tl_job = job;
1316
1317 return OK;
1318
1319failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001320 if (spawn_config != NULL)
1321 winpty_spawn_config_free(spawn_config);
1322 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001323 if (channel != NULL)
1324 channel_clear(channel);
1325 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001326 {
1327 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001328 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001329 }
1330 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001331 if (jo != NULL)
1332 CloseHandle(jo);
1333 if (term->tl_winpty != NULL)
1334 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001335 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001336 if (term->tl_winpty_config != NULL)
1337 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001338 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001339 if (winpty_err != NULL)
1340 {
1341 char_u *msg = utf16_to_enc(
1342 (short_u *)winpty_error_msg(winpty_err), NULL);
1343
1344 EMSG(msg);
1345 winpty_error_free(winpty_err);
1346 }
1347 return FAIL;
1348}
1349
1350/*
1351 * Free the terminal emulator part of "term".
1352 */
1353 static void
1354term_free(term_T *term)
1355{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001356 if (term->tl_winpty != NULL)
1357 winpty_free(term->tl_winpty);
1358 if (term->tl_winpty_config != NULL)
1359 winpty_config_free(term->tl_winpty_config);
1360 if (term->tl_vterm != NULL)
1361 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001362}
1363
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001364/*
1365 * Request size to terminal.
1366 */
1367 static void
1368term_report_winsize(term_T *term, int rows, int cols)
1369{
1370 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1371}
1372
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001373# else
1374
1375/**************************************
1376 * 3. Unix-like implementation.
1377 */
1378
1379/*
1380 * Create a new terminal of "rows" by "cols" cells.
1381 * Start job for "cmd".
1382 * Store the pointers in "term".
1383 * Return OK or FAIL.
1384 */
1385 static int
1386term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1387{
1388 typval_T argvars[2];
1389 jobopt_T opt;
1390
1391 create_vterm(term, rows, cols);
1392
1393 argvars[0].v_type = VAR_STRING;
1394 argvars[0].vval.v_string = cmd;
1395 setup_job_options(&opt, rows, cols);
1396 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001397 if (term->tl_job != NULL)
1398 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001399
Bram Moolenaar61a66052017-07-22 18:39:00 +02001400 return term->tl_job != NULL
1401 && term->tl_job->jv_channel != NULL
1402 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001403}
1404
1405/*
1406 * Free the terminal emulator part of "term".
1407 */
1408 static void
1409term_free(term_T *term)
1410{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001411 if (term->tl_vterm != NULL)
1412 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001413}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001414
1415/*
1416 * Request size to terminal.
1417 */
1418 static void
1419term_report_winsize(term_T *term, int rows, int cols)
1420{
1421 /* Use an ioctl() to report the new window size to the job. */
1422 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1423 {
1424 int fd = -1;
1425 int part;
1426
1427 for (part = PART_OUT; part < PART_COUNT; ++part)
1428 {
1429 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1430 if (isatty(fd))
1431 break;
1432 }
1433 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1434 mch_stop_job(term->tl_job, (char_u *)"winch");
1435 }
1436}
1437
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001438# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001439
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001440#endif /* FEAT_TERMINAL */