blob: d12df362fd437088da56c1b6a9f37a539dc2d7e3 [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
22 * that library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
35 * TODO:
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020036 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar1c844932017-07-24 23:36:41 +020037 * - include functions from #1871
38 * - do not store terminal buffer in viminfo. Or prefix term:// ?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020039 * - Add a scrollback buffer (contains lines to scroll off the top).
40 * Can use the buf_T lines, store attributes somewhere else?
41 * - When the job ends:
42 * - Write "-- JOB ENDED --" in the terminal.
43 * - Put the terminal contents in the scrollback buffer.
44 * - Free the terminal emulator.
45 * - Display the scrollback buffer (but with attributes).
46 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - Need an option or argument to drop the window+buffer right away, to be
48 * used for a shell or Vim.
49 * - add a character in :ls output
Bram Moolenaar938783d2017-07-16 20:13:26 +020050 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020051 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020052 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020053 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020054 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020055 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020056 * - implement "term" for job_start(): more job options when starting a
57 * terminal.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020058 * - implement term_list() list of buffers with a terminal
59 * - implement term_getsize(buf)
60 * - implement term_setsize(buf)
61 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
62 * - implement term_wait(buf) wait for screen to be updated
63 * - implement term_scrape(buf, row) inspect terminal screen
64 * - implement term_open(command, options) open terminal window
65 * - implement term_getjob(buf)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
67 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020068 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020069 */
70
71#include "vim.h"
72
73#ifdef FEAT_TERMINAL
74
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020075#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020076# define MIN(x,y) (x < y ? x : y)
77# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020078#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020080#include "libvterm/include/vterm.h"
81
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082/* typedef term_T in structs.h */
83struct terminal_S {
84 term_T *tl_next;
85
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020086#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020087 void *tl_winpty_config;
88 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020089#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020090 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020092 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020093
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020094 /* last known vterm size */
95 int tl_rows;
96 int tl_cols;
97 /* vterm size does not follow window size */
98 int tl_rows_fixed;
99 int tl_cols_fixed;
100
Bram Moolenaar21554412017-07-24 21:44:43 +0200101 char_u *tl_title; /* NULL or allocated */
102 char_u *tl_status_text; /* NULL or allocated */
103
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200104 /* Range of screen rows to update. Zero based. */
105 int tl_dirty_row_start; /* -1 if nothing dirty */
106 int tl_dirty_row_end; /* row below last one to update */
107
108 pos_T tl_cursor;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200109 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200110};
111
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200112/*
113 * List of all active terminals.
114 */
115static term_T *first_term = NULL;
116
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200117
118#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
119#define KEY_BUF_LEN 200
120
121/*
122 * Functions with separate implementation for MS-Windows and Unix-like systems.
123 */
124static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200125static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200126static void term_free(term_T *term);
127
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200128/**************************************
129 * 1. Generic code for all systems.
130 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200131
132/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200133 * Determine the terminal size from 'termsize' and the current window.
134 * Assumes term->tl_rows and term->tl_cols are zero.
135 */
136 static void
137set_term_and_win_size(term_T *term)
138{
139 if (*curwin->w_p_tms != NUL)
140 {
141 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
142
143 term->tl_rows = atoi((char *)curwin->w_p_tms);
144 term->tl_cols = atoi((char *)p);
145 }
146 if (term->tl_rows == 0)
147 term->tl_rows = curwin->w_height;
148 else
149 {
150 win_setheight_win(term->tl_rows, curwin);
151 term->tl_rows_fixed = TRUE;
152 }
153 if (term->tl_cols == 0)
154 term->tl_cols = curwin->w_width;
155 else
156 {
157 win_setwidth_win(term->tl_cols, curwin);
158 term->tl_cols_fixed = TRUE;
159 }
160}
161
162/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200163 * ":terminal": open a terminal window and execute a job in it.
164 */
165 void
166ex_terminal(exarg_T *eap)
167{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200168 exarg_T split_ea;
169 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200170 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200171 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200172
173 if (check_restricted() || check_secure())
174 return;
175
176 term = (term_T *)alloc_clear(sizeof(term_T));
177 if (term == NULL)
178 return;
179 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200180 term->tl_cursor_visible = TRUE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200181
182 /* Open a new window or tab. */
183 vim_memset(&split_ea, 0, sizeof(split_ea));
184 split_ea.cmdidx = CMD_new;
185 split_ea.cmd = (char_u *)"new";
186 split_ea.arg = (char_u *)"";
187 ex_splitview(&split_ea);
188 if (curwin == old_curwin)
189 {
190 /* split failed */
191 vim_free(term);
192 return;
193 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200194 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200195 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200196
197 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200198 term->tl_next = first_term;
199 first_term = term;
200
Bram Moolenaar293424c2017-07-26 23:11:01 +0200201 if (cmd == NULL || *cmd == NUL)
202 cmd = p_sh;
203
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200204 if (buflist_findname(cmd) == NULL)
205 curbuf->b_ffname = vim_strsave(cmd);
206 else
207 {
208 int i;
209 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200210 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200211
212 for (i = 1; p != NULL; ++i)
213 {
214 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
215 if (buflist_findname(p) == NULL)
216 {
217 curbuf->b_ffname = p;
218 break;
219 }
220 }
221 }
222 curbuf->b_fname = curbuf->b_ffname;
223
224 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
225 curbuf->b_changed = TRUE;
226 curbuf->b_p_ma = FALSE;
227 set_string_option_direct((char_u *)"buftype", -1,
228 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200229
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200230 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200231
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200232 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200233 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200234 {
235 /* store the size we ended up with */
236 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200237 }
238 else
239 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200240 free_terminal(term);
241 curbuf->b_term = NULL;
242
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200243 /* Wiping out the buffer will also close the window and call
244 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200245 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200246 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200247
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200248 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249}
250
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200251/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200252 * Free a terminal and everything it refers to.
253 * Kills the job if there is one.
254 * Called when wiping out a buffer.
255 */
256 void
257free_terminal(term_T *term)
258{
259 term_T *tp;
260
261 if (term == NULL)
262 return;
263 if (first_term == term)
264 first_term = term->tl_next;
265 else
266 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
267 if (tp->tl_next == term)
268 {
269 tp->tl_next = term->tl_next;
270 break;
271 }
272
273 if (term->tl_job != NULL)
274 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200275 if (term->tl_job->jv_status != JOB_ENDED
276 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200277 job_stop(term->tl_job, NULL, "kill");
278 job_unref(term->tl_job);
279 }
280
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200281 term_free(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200282 vim_free(term->tl_title);
283 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200284 vim_free(term);
285}
286
287/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200288 * Write job output "msg[len]" to the vterm.
289 */
290 static void
291term_write_job_output(term_T *term, char_u *msg, size_t len)
292{
293 VTerm *vterm = term->tl_vterm;
294 char_u *p;
295 size_t done;
296 size_t len_now;
297
298 for (done = 0; done < len; done += len_now)
299 {
300 for (p = msg + done; p < msg + len; )
301 {
302 if (*p == NL)
303 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200304 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200305 }
306 len_now = p - msg - done;
307 vterm_input_write(vterm, (char *)msg + done, len_now);
308 if (p < msg + len && *p == NL)
309 {
310 /* Convert NL to CR-NL, that appears to work best. */
311 vterm_input_write(vterm, "\r\n", 2);
312 ++len_now;
313 }
314 }
315
316 /* this invokes the damage callbacks */
317 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
318}
319
Bram Moolenaar1c844932017-07-24 23:36:41 +0200320 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200321update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200322{
Bram Moolenaar1c844932017-07-24 23:36:41 +0200323 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200324 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200325 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200326 if (term->tl_cursor_visible)
327 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200328 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200329#ifdef FEAT_GUI
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200330 if (gui.in_use && term->tl_cursor_visible)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200331 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200332#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200333 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200334}
335
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200336/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200337 * Invoked when "msg" output from a job was received. Write it to the terminal
338 * of "buffer".
339 */
340 void
341write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
342{
343 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200344 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200345
346 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200347 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200348
349 /* TODO: only update once in a while. */
350 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200351 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200352}
353
354/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200355 * Convert typed key "c" into bytes to send to the job.
356 * Return the number of bytes in "buf".
357 */
358 static int
359term_convert_key(int c, char *buf)
360{
361 VTerm *vterm = curbuf->b_term->tl_vterm;
362 VTermKey key = VTERM_KEY_NONE;
363 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200364
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200365 switch (c)
366 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200367 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200368 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200369 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
370 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200371 case K_DEL: key = VTERM_KEY_DEL; break;
372 case K_DOWN: key = VTERM_KEY_DOWN; break;
373 case K_END: key = VTERM_KEY_END; break;
374 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
375 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
376 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
377 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
378 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
379 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
380 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
381 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
382 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
383 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
384 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
385 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
386 case K_HOME: key = VTERM_KEY_HOME; break;
387 case K_INS: key = VTERM_KEY_INS; break;
388 case K_K0: key = VTERM_KEY_KP_0; break;
389 case K_K1: key = VTERM_KEY_KP_1; break;
390 case K_K2: key = VTERM_KEY_KP_2; break;
391 case K_K3: key = VTERM_KEY_KP_3; break;
392 case K_K4: key = VTERM_KEY_KP_4; break;
393 case K_K5: key = VTERM_KEY_KP_5; break;
394 case K_K6: key = VTERM_KEY_KP_6; break;
395 case K_K7: key = VTERM_KEY_KP_7; break;
396 case K_K8: key = VTERM_KEY_KP_8; break;
397 case K_K9: key = VTERM_KEY_KP_9; break;
398 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
399 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
400 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
401 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
402 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
403 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
404 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
405 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
406 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
407 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
408 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
409 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
410 case K_LEFT: key = VTERM_KEY_LEFT; break;
411 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
412 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
413 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
414 case K_UP: key = VTERM_KEY_UP; break;
415 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200416
417 case K_MOUSEUP: /* TODO */ break;
418 case K_MOUSEDOWN: /* TODO */ break;
419 case K_MOUSELEFT: /* TODO */ break;
420 case K_MOUSERIGHT: /* TODO */ break;
421
422 case K_LEFTMOUSE: /* TODO */ break;
423 case K_LEFTMOUSE_NM: /* TODO */ break;
424 case K_LEFTDRAG: /* TODO */ break;
425 case K_LEFTRELEASE: /* TODO */ break;
426 case K_LEFTRELEASE_NM: /* TODO */ break;
427 case K_MIDDLEMOUSE: /* TODO */ break;
428 case K_MIDDLEDRAG: /* TODO */ break;
429 case K_MIDDLERELEASE: /* TODO */ break;
430 case K_RIGHTMOUSE: /* TODO */ break;
431 case K_RIGHTDRAG: /* TODO */ break;
432 case K_RIGHTRELEASE: /* TODO */ break;
433 case K_X1MOUSE: /* TODO */ break;
434 case K_X1DRAG: /* TODO */ break;
435 case K_X1RELEASE: /* TODO */ break;
436 case K_X2MOUSE: /* TODO */ break;
437 case K_X2DRAG: /* TODO */ break;
438 case K_X2RELEASE: /* TODO */ break;
439
440 /* TODO: handle all special keys and modifiers that terminal_loop()
441 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200442 }
443
444 /*
445 * Convert special keys to vterm keys:
446 * - Write keys to vterm: vterm_keyboard_key()
447 * - Write output to channel.
448 */
449 if (key != VTERM_KEY_NONE)
450 /* Special key, let vterm convert it. */
451 vterm_keyboard_key(vterm, key, mod);
452 else
453 /* Normal character, let vterm convert it. */
454 vterm_keyboard_unichar(vterm, c, mod);
455
456 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200457 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200458}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200459
Bram Moolenaar938783d2017-07-16 20:13:26 +0200460/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200461 * Get a key from the user without mapping.
462 * TODO: use terminal mode mappings.
463 */
464 static int
465term_vgetc()
466{
467 int c;
468
469 ++no_mapping;
470 ++allow_keys;
471 got_int = FALSE;
472 c = vgetc();
473 --no_mapping;
474 --allow_keys;
475 return c;
476}
477
478/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200479 * Wait for input and send it to the job.
480 * Return when the start of a CTRL-W command is typed or anything else that
481 * should be handled as a Normal mode command.
482 */
483 void
484terminal_loop(void)
485{
486 char buf[KEY_BUF_LEN];
487 int c;
488 size_t len;
489 static int mouse_was_outside = FALSE;
490 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200491 int termkey = 0;
492
493 if (*curwin->w_p_tk != NUL)
494 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200495
496 for (;;)
497 {
498 /* TODO: skip screen update when handling a sequence of keys. */
499 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200500 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200501 c = term_vgetc();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200502
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200503 if (c == (termkey == 0 ? Ctrl_W : termkey))
504 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200505#ifdef FEAT_CMDL_INFO
506 if (add_to_showcmd(c))
507 out_flush();
508#endif
509 c = term_vgetc();
510#ifdef FEAT_CMDL_INFO
511 clear_showcmd();
512#endif
513
514 if (termkey == 0 && c == '.')
515 /* "CTRL-W .": send CTRL-W to the job */
516 c = Ctrl_W;
517 else if (termkey == 0 || c != termkey)
518 {
519 stuffcharReadbuff(Ctrl_W);
520 stuffcharReadbuff(c);
521 return;
522 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200523 }
524
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200525 /* Catch keys that need to be handled as in Normal mode. */
526 switch (c)
527 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200528 case NUL:
529 case K_ZERO:
530 stuffcharReadbuff(c);
531 return;
532
533 case K_IGNORE: continue;
534
535 case K_LEFTDRAG:
536 case K_MIDDLEDRAG:
537 case K_RIGHTDRAG:
538 case K_X1DRAG:
539 case K_X2DRAG:
540 dragging_outside = mouse_was_outside;
541 /* FALLTHROUGH */
542 case K_LEFTMOUSE:
543 case K_LEFTMOUSE_NM:
544 case K_LEFTRELEASE:
545 case K_LEFTRELEASE_NM:
546 case K_MIDDLEMOUSE:
547 case K_MIDDLERELEASE:
548 case K_RIGHTMOUSE:
549 case K_RIGHTRELEASE:
550 case K_X1MOUSE:
551 case K_X1RELEASE:
552 case K_X2MOUSE:
553 case K_X2RELEASE:
554 if (mouse_row < W_WINROW(curwin)
555 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
556 || mouse_col < W_WINCOL(curwin)
557 || mouse_col >= W_ENDCOL(curwin)
558 || dragging_outside)
559 {
560 /* click outside the current window */
561 stuffcharReadbuff(c);
562 mouse_was_outside = TRUE;
563 return;
564 }
565 }
566 mouse_was_outside = FALSE;
567
568 /* Convert the typed key to a sequence of bytes for the job. */
569 len = term_convert_key(c, buf);
570 if (len > 0)
571 /* TODO: if FAIL is returned, stop? */
572 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200573 (char_u *)buf, (int)len, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200574 }
575}
576
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200577/*
578 * Called when a job has finished.
579 */
580 void
581term_job_ended(job_T *job)
582{
Bram Moolenaar21554412017-07-24 21:44:43 +0200583 term_T *term;
584 int did_one = FALSE;
585
586 for (term = first_term; term != NULL; term = term->tl_next)
587 if (term->tl_job == job)
588 {
589 vim_free(term->tl_title);
590 term->tl_title = NULL;
591 vim_free(term->tl_status_text);
592 term->tl_status_text = NULL;
593 redraw_buf_and_status_later(term->tl_buffer, VALID);
594 did_one = TRUE;
595 }
596 if (did_one)
Bram Moolenaar21554412017-07-24 21:44:43 +0200597 redraw_statuslines();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200598 if (curbuf->b_term != NULL)
599 {
600 if (curbuf->b_term->tl_job == job)
601 maketitle();
602 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar21554412017-07-24 21:44:43 +0200603 }
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200604}
605
606/*
607 * Return TRUE if the job for "buf" is still running.
608 */
Bram Moolenaar21554412017-07-24 21:44:43 +0200609 static int
610term_job_running(term_T *term)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200611{
Bram Moolenaar21554412017-07-24 21:44:43 +0200612 return term->tl_job != NULL && term->tl_job->jv_status == JOB_STARTED;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200613}
614
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200615 static void
616position_cursor(win_T *wp, VTermPos *pos)
617{
618 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
619 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
620}
621
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200622 static void
623may_toggle_cursor(term_T *term)
624{
625 if (curbuf == term->tl_buffer)
626 {
627 if (term->tl_cursor_visible)
628 cursor_on();
629 else
630 cursor_off();
631 }
632}
633
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200634 static int
635handle_damage(VTermRect rect, void *user)
636{
637 term_T *term = (term_T *)user;
638
639 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
640 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
641 redraw_buf_later(term->tl_buffer, NOT_VALID);
642 return 1;
643}
644
645 static int
646handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
647{
648 term_T *term = (term_T *)user;
649
650 /* TODO */
651 redraw_buf_later(term->tl_buffer, NOT_VALID);
652 return 1;
653}
654
655 static int
656handle_movecursor(
657 VTermPos pos,
658 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200659 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200660 void *user)
661{
662 term_T *term = (term_T *)user;
663 win_T *wp;
664 int is_current = FALSE;
665
666 FOR_ALL_WINDOWS(wp)
667 {
668 if (wp->w_buffer == term->tl_buffer)
669 {
670 position_cursor(wp, &pos);
671 if (wp == curwin)
672 is_current = TRUE;
673 }
674 }
675
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200676 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200677 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200678 {
679 may_toggle_cursor(term);
680 update_cursor(term, TRUE);
681 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200682
683 return 1;
684}
685
Bram Moolenaar21554412017-07-24 21:44:43 +0200686 static int
687handle_settermprop(
688 VTermProp prop,
689 VTermValue *value,
690 void *user)
691{
692 term_T *term = (term_T *)user;
693
694 switch (prop)
695 {
696 case VTERM_PROP_TITLE:
697 vim_free(term->tl_title);
698 term->tl_title = vim_strsave((char_u *)value->string);
699 vim_free(term->tl_status_text);
700 term->tl_status_text = NULL;
701 if (term == curbuf->b_term)
702 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200703 break;
704
705 case VTERM_PROP_CURSORVISIBLE:
706 term->tl_cursor_visible = value->boolean;
707 may_toggle_cursor(term);
708 out_flush();
709 break;
710
Bram Moolenaar21554412017-07-24 21:44:43 +0200711 default:
712 break;
713 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200714 /* Always return 1, otherwise vterm doesn't store the value internally. */
715 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +0200716}
717
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200718/*
719 * The job running in the terminal resized the terminal.
720 */
721 static int
722handle_resize(int rows, int cols, void *user)
723{
724 term_T *term = (term_T *)user;
725 win_T *wp;
726
727 term->tl_rows = rows;
728 term->tl_cols = cols;
729 FOR_ALL_WINDOWS(wp)
730 {
731 if (wp->w_buffer == term->tl_buffer)
732 {
733 win_setheight_win(rows, wp);
734 win_setwidth_win(cols, wp);
735 }
736 }
737
738 redraw_buf_later(term->tl_buffer, NOT_VALID);
739 return 1;
740}
741
Bram Moolenaar21554412017-07-24 21:44:43 +0200742static VTermScreenCallbacks screen_callbacks = {
743 handle_damage, /* damage */
744 handle_moverect, /* moverect */
745 handle_movecursor, /* movecursor */
746 handle_settermprop, /* settermprop */
747 NULL, /* bell */
748 handle_resize, /* resize */
749 NULL, /* sb_pushline */
750 NULL /* sb_popline */
751};
752
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200753/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200754 * Reverse engineer the RGB value into a cterm color index.
755 * First color is 1. Return 0 if no match found.
756 */
757 static int
758color2index(VTermColor *color)
759{
760 int red = color->red;
761 int blue = color->blue;
762 int green = color->green;
763
764 if (red == 0)
765 {
766 if (green == 0)
767 {
768 if (blue == 0)
769 return 1; /* black */
770 if (blue == 224)
771 return 5; /* blue */
772 }
773 else if (green == 224)
774 {
775 if (blue == 0)
776 return 3; /* green */
777 if (blue == 224)
778 return 7; /* cyan */
779 }
780 }
781 else if (red == 224)
782 {
783 if (green == 0)
784 {
785 if (blue == 0)
786 return 2; /* red */
787 if (blue == 224)
788 return 6; /* magenta */
789 }
790 else if (green == 224)
791 {
792 if (blue == 0)
793 return 4; /* yellow */
794 if (blue == 224)
795 return 8; /* white */
796 }
797 }
798 else if (red == 128)
799 {
800 if (green == 128 && blue == 128)
Bram Moolenaar8a773062017-07-24 22:29:21 +0200801 return 9; /* high intensity black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200802 }
803 else if (red == 255)
804 {
805 if (green == 64)
806 {
807 if (blue == 64)
808 return 10; /* high intensity red */
809 if (blue == 255)
810 return 14; /* high intensity magenta */
811 }
812 else if (green == 255)
813 {
814 if (blue == 64)
815 return 12; /* high intensity yellow */
816 if (blue == 255)
817 return 16; /* high intensity white */
818 }
819 }
820 else if (red == 64)
821 {
822 if (green == 64)
823 {
824 if (blue == 255)
825 return 13; /* high intensity blue */
826 }
827 else if (green == 255)
828 {
829 if (blue == 64)
830 return 11; /* high intensity green */
831 if (blue == 255)
832 return 15; /* high intensity cyan */
833 }
834 }
835 if (t_colors >= 256)
836 {
837 if (red == blue && red == green)
838 {
839 /* 24-color greyscale */
840 static int cutoff[23] = {
841 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
842 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
843 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
844 int i;
845
846 for (i = 0; i < 23; ++i)
847 if (red < cutoff[i])
848 return i + 233;
849 return 256;
850 }
851
852 /* 216-color cube */
853 return 17 + ((red + 25) / 0x33) * 36
854 + ((green + 25) / 0x33) * 6
855 + (blue + 25) / 0x33;
856 }
857 return 0;
858}
859
860/*
861 * Convert the attributes of a vterm cell into an attribute index.
862 */
863 static int
864cell2attr(VTermScreenCell *cell)
865{
866 int attr = 0;
867
868 if (cell->attrs.bold)
869 attr |= HL_BOLD;
870 if (cell->attrs.underline)
871 attr |= HL_UNDERLINE;
872 if (cell->attrs.italic)
873 attr |= HL_ITALIC;
874 if (cell->attrs.strike)
875 attr |= HL_STANDOUT;
876 if (cell->attrs.reverse)
877 attr |= HL_INVERSE;
878 if (cell->attrs.strike)
879 attr |= HL_UNDERLINE;
880
881#ifdef FEAT_GUI
882 if (gui.in_use)
883 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200884 guicolor_T fg, bg;
885
886 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
887 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
888 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200889 }
890 else
891#endif
892#ifdef FEAT_TERMGUICOLORS
893 if (p_tgc)
894 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200895 guicolor_T fg, bg;
896
897 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
898 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
899
900 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200901 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200902 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200903#endif
904 {
905 return get_cterm_attr_idx(attr, color2index(&cell->fg),
906 color2index(&cell->bg));
907 }
908 return 0;
909}
910
911/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200912 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200913 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200914 void
915term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200916{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200917 term_T *term = wp->w_buffer->b_term;
918 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200919 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200920 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200921 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200922
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200923 /*
924 * If the window was resized a redraw will be triggered and we get here.
925 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
926 */
927 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
928 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200929 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +0200930 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
931 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
932 win_T *twp;
933
934 FOR_ALL_WINDOWS(twp)
935 {
936 /* When more than one window shows the same terminal, use the
937 * smallest size. */
938 if (twp->w_buffer == term->tl_buffer)
939 {
940 if (!term->tl_rows_fixed && rows > twp->w_height)
941 rows = twp->w_height;
942 if (!term->tl_cols_fixed && cols > twp->w_width)
943 cols = twp->w_width;
944 }
945 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200946
947 vterm_set_size(vterm, rows, cols);
948 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
949 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200950 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200951 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200952
953 /* The cursor may have been moved when resizing. */
954 vterm_state_get_cursorpos(state, &pos);
955 position_cursor(wp, &pos);
956
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200957 /* TODO: Only redraw what changed. */
958 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200959 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200960 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200961 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200962
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200963 if (pos.row < term->tl_rows)
964 {
965 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200966 {
967 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200968 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200969
Bram Moolenaareeac6772017-07-23 15:48:37 +0200970 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
971 vim_memset(&cell, 0, sizeof(cell));
972
973 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200974 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200975 if (c == NUL)
976 {
977 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +0200978#if defined(FEAT_MBYTE)
979 if (enc_utf8)
980 ScreenLinesUC[off] = NUL;
981#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200982 }
983 else
984 {
985#if defined(FEAT_MBYTE)
986 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200987 {
988 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200989 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200990 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200991 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200992 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200993 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200994 if (enc_utf8)
995 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200996 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200997#else
998 ScreenLines[off] = c;
999#endif
1000 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001001 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001002
1003 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001004 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001005 if (cell.width == 2)
1006 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001007 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001008#if defined(FEAT_MBYTE)
1009 if (enc_utf8)
1010 ScreenLinesUC[off] = NUL;
1011#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001012 ++pos.col;
1013 ++off;
1014 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001015 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001016 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001017 else
1018 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001019
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001020 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1021 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001022 }
1023}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001024
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001025/*
1026 * Set job options common for Unix and MS-Windows.
1027 */
1028 static void
1029setup_job_options(jobopt_T *opt, int rows, int cols)
1030{
1031 clear_job_options(opt);
1032 opt->jo_mode = MODE_RAW;
1033 opt->jo_out_mode = MODE_RAW;
1034 opt->jo_err_mode = MODE_RAW;
1035 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001036
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001037 opt->jo_io[PART_OUT] = JIO_BUFFER;
1038 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001039 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1040
1041 opt->jo_modifiable[PART_OUT] = 0;
1042 opt->jo_modifiable[PART_ERR] = 0;
1043 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1044
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001045 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1046 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001047 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001048 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1049
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001050 opt->jo_term_rows = rows;
1051 opt->jo_term_cols = cols;
1052}
1053
1054/*
1055 * Create a new vterm and initialize it.
1056 */
1057 static void
1058create_vterm(term_T *term, int rows, int cols)
1059{
1060 VTerm *vterm;
1061 VTermScreen *screen;
1062
1063 vterm = vterm_new(rows, cols);
1064 term->tl_vterm = vterm;
1065 screen = vterm_obtain_screen(vterm);
1066 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1067 /* TODO: depends on 'encoding'. */
1068 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001069
1070 /* Vterm uses a default black background. Set it to white when
1071 * 'background' is "light". */
1072 if (*p_bg == 'l')
1073 {
1074 VTermColor fg, bg;
1075
1076 fg.red = fg.green = fg.blue = 0;
1077 bg.red = bg.green = bg.blue = 255;
1078 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1079 }
1080
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001081 /* Required to initialize most things. */
1082 vterm_screen_reset(screen, 1 /* hard */);
1083}
1084
Bram Moolenaar21554412017-07-24 21:44:43 +02001085/*
1086 * Return the text to show for the buffer name and status.
1087 */
1088 char_u *
1089term_get_status_text(term_T *term)
1090{
1091 if (term->tl_status_text == NULL)
1092 {
1093 char_u *txt;
1094 size_t len;
1095
1096 if (term->tl_title != NULL)
1097 txt = term->tl_title;
1098 else if (term_job_running(term))
1099 txt = (char_u *)_("running");
1100 else
1101 txt = (char_u *)_("finished");
1102 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001103 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001104 if (term->tl_status_text != NULL)
1105 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1106 term->tl_buffer->b_fname, txt);
1107 }
1108 return term->tl_status_text;
1109}
1110
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001111/*
1112 * Mark references in jobs of terminals.
1113 */
1114 int
1115set_ref_in_term(int copyID)
1116{
1117 int abort = FALSE;
1118 term_T *term;
1119 typval_T tv;
1120
1121 for (term = first_term; term != NULL; term = term->tl_next)
1122 if (term->tl_job != NULL)
1123 {
1124 tv.v_type = VAR_JOB;
1125 tv.vval.v_job = term->tl_job;
1126 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1127 }
1128 return abort;
1129}
1130
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001131# ifdef WIN3264
1132
1133#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1134#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1135
Bram Moolenaar8a773062017-07-24 22:29:21 +02001136void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001137void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001138void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001139BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1140void (*winpty_config_set_initial_size)(void*, int, int);
1141LPCWSTR (*winpty_conin_name)(void*);
1142LPCWSTR (*winpty_conout_name)(void*);
1143LPCWSTR (*winpty_conerr_name)(void*);
1144void (*winpty_free)(void*);
1145void (*winpty_config_free)(void*);
1146void (*winpty_spawn_config_free)(void*);
1147void (*winpty_error_free)(void*);
1148LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001149BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001150
1151/**************************************
1152 * 2. MS-Windows implementation.
1153 */
1154
1155#define WINPTY_DLL "winpty.dll"
1156
1157static HINSTANCE hWinPtyDLL = NULL;
1158
1159 int
1160dyn_winpty_init(void)
1161{
1162 int i;
1163 static struct
1164 {
1165 char *name;
1166 FARPROC *ptr;
1167 } winpty_entry[] =
1168 {
1169 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1170 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1171 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1172 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1173 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1174 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1175 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1176 {"winpty_free", (FARPROC*)&winpty_free},
1177 {"winpty_open", (FARPROC*)&winpty_open},
1178 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1179 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1180 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1181 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001182 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001183 {NULL, NULL}
1184 };
1185
1186 /* No need to initialize twice. */
1187 if (hWinPtyDLL)
1188 return 1;
1189 /* Load winpty.dll */
1190 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1191 if (!hWinPtyDLL)
1192 {
1193 EMSG2(_(e_loadlib), WINPTY_DLL);
1194 return 0;
1195 }
1196 for (i = 0; winpty_entry[i].name != NULL
1197 && winpty_entry[i].ptr != NULL; ++i)
1198 {
1199 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1200 winpty_entry[i].name)) == NULL)
1201 {
1202 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1203 return 0;
1204 }
1205 }
1206
1207 return 1;
1208}
1209
1210/*
1211 * Create a new terminal of "rows" by "cols" cells.
1212 * Store a reference in "term".
1213 * Return OK or FAIL.
1214 */
1215 static int
1216term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1217{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001218 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001219 channel_T *channel = NULL;
1220 job_T *job = NULL;
1221 jobopt_T opt;
1222 DWORD error;
1223 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1224 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001225 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001226
1227 if (!dyn_winpty_init())
1228 return FAIL;
1229
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001230 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001231 if (p == NULL)
1232 return FAIL;
1233
1234 job = job_alloc();
1235 if (job == NULL)
1236 goto failed;
1237
1238 channel = add_channel();
1239 if (channel == NULL)
1240 goto failed;
1241
1242 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1243 if (term->tl_winpty_config == NULL)
1244 goto failed;
1245
1246 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1247 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1248 if (term->tl_winpty == NULL)
1249 goto failed;
1250
1251 spawn_config = winpty_spawn_config_new(
1252 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1253 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1254 NULL,
1255 p,
1256 NULL,
1257 NULL,
1258 &winpty_err);
1259 if (spawn_config == NULL)
1260 goto failed;
1261
1262 channel = add_channel();
1263 if (channel == NULL)
1264 goto failed;
1265
1266 job = job_alloc();
1267 if (job == NULL)
1268 goto failed;
1269
1270 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1271 &child_thread_handle, &error, &winpty_err))
1272 goto failed;
1273
1274 channel_set_pipes(channel,
1275 (sock_T) CreateFileW(
1276 winpty_conin_name(term->tl_winpty),
1277 GENERIC_WRITE, 0, NULL,
1278 OPEN_EXISTING, 0, NULL),
1279 (sock_T) CreateFileW(
1280 winpty_conout_name(term->tl_winpty),
1281 GENERIC_READ, 0, NULL,
1282 OPEN_EXISTING, 0, NULL),
1283 (sock_T) CreateFileW(
1284 winpty_conerr_name(term->tl_winpty),
1285 GENERIC_READ, 0, NULL,
1286 OPEN_EXISTING, 0, NULL));
1287
1288 jo = CreateJobObject(NULL, NULL);
1289 if (jo == NULL)
1290 goto failed;
1291
1292 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001293 {
1294 /* Failed, switch the way to terminate process with TerminateProcess. */
1295 CloseHandle(jo);
1296 jo = NULL;
1297 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001298
1299 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001300 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001301
1302 create_vterm(term, rows, cols);
1303
1304 setup_job_options(&opt, rows, cols);
1305 channel_set_job(channel, job, &opt);
1306
1307 job->jv_channel = channel;
1308 job->jv_proc_info.hProcess = child_process_handle;
1309 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1310 job->jv_job_object = jo;
1311 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001312 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001313 term->tl_job = job;
1314
1315 return OK;
1316
1317failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001318 if (spawn_config != NULL)
1319 winpty_spawn_config_free(spawn_config);
1320 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001321 if (channel != NULL)
1322 channel_clear(channel);
1323 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001324 {
1325 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001326 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001327 }
1328 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001329 if (jo != NULL)
1330 CloseHandle(jo);
1331 if (term->tl_winpty != NULL)
1332 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001333 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001334 if (term->tl_winpty_config != NULL)
1335 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001336 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001337 if (winpty_err != NULL)
1338 {
1339 char_u *msg = utf16_to_enc(
1340 (short_u *)winpty_error_msg(winpty_err), NULL);
1341
1342 EMSG(msg);
1343 winpty_error_free(winpty_err);
1344 }
1345 return FAIL;
1346}
1347
1348/*
1349 * Free the terminal emulator part of "term".
1350 */
1351 static void
1352term_free(term_T *term)
1353{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001354 if (term->tl_winpty != NULL)
1355 winpty_free(term->tl_winpty);
1356 if (term->tl_winpty_config != NULL)
1357 winpty_config_free(term->tl_winpty_config);
1358 if (term->tl_vterm != NULL)
1359 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001360}
1361
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001362/*
1363 * Request size to terminal.
1364 */
1365 static void
1366term_report_winsize(term_T *term, int rows, int cols)
1367{
1368 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1369}
1370
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001371# else
1372
1373/**************************************
1374 * 3. Unix-like implementation.
1375 */
1376
1377/*
1378 * Create a new terminal of "rows" by "cols" cells.
1379 * Start job for "cmd".
1380 * Store the pointers in "term".
1381 * Return OK or FAIL.
1382 */
1383 static int
1384term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1385{
1386 typval_T argvars[2];
1387 jobopt_T opt;
1388
1389 create_vterm(term, rows, cols);
1390
1391 argvars[0].v_type = VAR_STRING;
1392 argvars[0].vval.v_string = cmd;
1393 setup_job_options(&opt, rows, cols);
1394 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001395 if (term->tl_job != NULL)
1396 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001397
Bram Moolenaar61a66052017-07-22 18:39:00 +02001398 return term->tl_job != NULL
1399 && term->tl_job->jv_channel != NULL
1400 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001401}
1402
1403/*
1404 * Free the terminal emulator part of "term".
1405 */
1406 static void
1407term_free(term_T *term)
1408{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001409 if (term->tl_vterm != NULL)
1410 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001411}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001412
1413/*
1414 * Request size to terminal.
1415 */
1416 static void
1417term_report_winsize(term_T *term, int rows, int cols)
1418{
1419 /* Use an ioctl() to report the new window size to the job. */
1420 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1421 {
1422 int fd = -1;
1423 int part;
1424
1425 for (part = PART_OUT; part < PART_COUNT; ++part)
1426 {
1427 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1428 if (isatty(fd))
1429 break;
1430 }
1431 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1432 mch_stop_job(term->tl_job, (char_u *)"winch");
1433 }
1434}
1435
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001436# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001437
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001438#endif /* FEAT_TERMINAL */