blob: d04b99ec6986a0407704350bc10229be4e253959 [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
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020022 * this 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 *
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020035 * When the job ends the text is put in a buffer. Redrawing then happens from
36 * that buffer, attributes come from the scrollback buffer tl_scrollback.
37 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020038 * TODO:
Bram Moolenaar423802d2017-07-30 16:52:24 +020039 * - Make CTRL-W "" paste register content to the job?
40 * - in bash mouse clicks are inserting characters.
41 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaard85f2712017-07-28 21:51:57 +020042 * - For the scrollback buffer store lines in the buffer, only attributes in
43 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020044 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020045 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020046 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
47 * job finishes).
48 * - add option values to the command:
49 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020050 * - support different cursor shapes, colors and attributes
51 * - make term_getcursor() return type (none/block/bar/underline) and
52 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020053 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020054 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020055 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020056 * - add 't' to mode()
Bram Moolenaar938783d2017-07-16 20:13:26 +020057 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaard85f2712017-07-28 21:51:57 +020058 * - when closing window and job has ended, make buffer hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020059 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020060 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020061 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020062 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020063 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020064 * - implement "term" for job_start(): more job options when starting a
65 * terminal.
Bram Moolenaar423802d2017-07-30 16:52:24 +020066 * - if the job in the terminal does not support the mouse, we can use the
67 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020068 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
69 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020070 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020071 */
72
73#include "vim.h"
74
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020075#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020076
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020077#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020078# define MIN(x,y) (x < y ? x : y)
79# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020080#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020081
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020082#include "libvterm/include/vterm.h"
83
Bram Moolenaard85f2712017-07-28 21:51:57 +020084typedef struct sb_line_S {
85 int sb_cols; /* can differ per line */
86 VTermScreenCell *sb_cells; /* allocated */
87} sb_line_T;
88
Bram Moolenaare4f25e42017-07-07 11:54:15 +020089/* typedef term_T in structs.h */
90struct terminal_S {
91 term_T *tl_next;
92
Bram Moolenaar423802d2017-07-30 16:52:24 +020093 VTerm *tl_vterm;
94 job_T *tl_job;
95 buf_T *tl_buffer;
96
97 int tl_terminal_mode;
98 int tl_channel_closed;
99
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200100#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200101 void *tl_winpty_config;
102 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200103#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200104
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200105 /* last known vterm size */
106 int tl_rows;
107 int tl_cols;
108 /* vterm size does not follow window size */
109 int tl_rows_fixed;
110 int tl_cols_fixed;
111
Bram Moolenaar21554412017-07-24 21:44:43 +0200112 char_u *tl_title; /* NULL or allocated */
113 char_u *tl_status_text; /* NULL or allocated */
114
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200115 /* Range of screen rows to update. Zero based. */
116 int tl_dirty_row_start; /* -1 if nothing dirty */
117 int tl_dirty_row_end; /* row below last one to update */
118
Bram Moolenaard85f2712017-07-28 21:51:57 +0200119 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200120 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200121
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200122 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200123 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200124};
125
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126/*
127 * List of all active terminals.
128 */
129static term_T *first_term = NULL;
130
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200131
132#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
133#define KEY_BUF_LEN 200
134
135/*
136 * Functions with separate implementation for MS-Windows and Unix-like systems.
137 */
138static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200139static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200140static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200141
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200142/**************************************
143 * 1. Generic code for all systems.
144 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200145
146/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200147 * Determine the terminal size from 'termsize' and the current window.
148 * Assumes term->tl_rows and term->tl_cols are zero.
149 */
150 static void
151set_term_and_win_size(term_T *term)
152{
153 if (*curwin->w_p_tms != NUL)
154 {
155 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
156
157 term->tl_rows = atoi((char *)curwin->w_p_tms);
158 term->tl_cols = atoi((char *)p);
159 }
160 if (term->tl_rows == 0)
161 term->tl_rows = curwin->w_height;
162 else
163 {
164 win_setheight_win(term->tl_rows, curwin);
165 term->tl_rows_fixed = TRUE;
166 }
167 if (term->tl_cols == 0)
168 term->tl_cols = curwin->w_width;
169 else
170 {
171 win_setwidth_win(term->tl_cols, curwin);
172 term->tl_cols_fixed = TRUE;
173 }
174}
175
176/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200177 * ":terminal": open a terminal window and execute a job in it.
178 */
179 void
180ex_terminal(exarg_T *eap)
181{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200182 exarg_T split_ea;
183 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200184 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200185 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200186
187 if (check_restricted() || check_secure())
188 return;
189
190 term = (term_T *)alloc_clear(sizeof(term_T));
191 if (term == NULL)
192 return;
193 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200194 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200195 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200196
197 /* Open a new window or tab. */
198 vim_memset(&split_ea, 0, sizeof(split_ea));
199 split_ea.cmdidx = CMD_new;
200 split_ea.cmd = (char_u *)"new";
201 split_ea.arg = (char_u *)"";
202 ex_splitview(&split_ea);
203 if (curwin == old_curwin)
204 {
205 /* split failed */
206 vim_free(term);
207 return;
208 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200209 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200210 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200211
212 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200213 term->tl_next = first_term;
214 first_term = term;
215
Bram Moolenaar293424c2017-07-26 23:11:01 +0200216 if (cmd == NULL || *cmd == NUL)
217 cmd = p_sh;
218
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200219 if (buflist_findname(cmd) == NULL)
220 curbuf->b_ffname = vim_strsave(cmd);
221 else
222 {
223 int i;
224 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200225 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200226
227 for (i = 1; p != NULL; ++i)
228 {
229 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
230 if (buflist_findname(p) == NULL)
231 {
232 curbuf->b_ffname = p;
233 break;
234 }
235 }
236 }
237 curbuf->b_fname = curbuf->b_ffname;
238
239 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
240 curbuf->b_changed = TRUE;
241 curbuf->b_p_ma = FALSE;
242 set_string_option_direct((char_u *)"buftype", -1,
243 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200244
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200245 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200247 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200248 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200249 {
250 /* store the size we ended up with */
251 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252 }
253 else
254 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200255 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200256
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200257 /* Wiping out the buffer will also close the window and call
258 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200259 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200260 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200261
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200262 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263}
264
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200265/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200266 * Free the scrollback buffer for "term".
267 */
268 static void
269free_scrollback(term_T *term)
270{
271 int i;
272
273 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
274 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
275 ga_clear(&term->tl_scrollback);
276}
277
278/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200279 * Free a terminal and everything it refers to.
280 * Kills the job if there is one.
281 * Called when wiping out a buffer.
282 */
283 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200284free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200285{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200286 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200287 term_T *tp;
288
289 if (term == NULL)
290 return;
291 if (first_term == term)
292 first_term = term->tl_next;
293 else
294 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
295 if (tp->tl_next == term)
296 {
297 tp->tl_next = term->tl_next;
298 break;
299 }
300
301 if (term->tl_job != NULL)
302 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200303 if (term->tl_job->jv_status != JOB_ENDED
304 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200305 job_stop(term->tl_job, NULL, "kill");
306 job_unref(term->tl_job);
307 }
308
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200309 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200310
311 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200312 vim_free(term->tl_title);
313 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200314 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200315 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200316}
317
318/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200319 * Write job output "msg[len]" to the vterm.
320 */
321 static void
322term_write_job_output(term_T *term, char_u *msg, size_t len)
323{
324 VTerm *vterm = term->tl_vterm;
325 char_u *p;
326 size_t done;
327 size_t len_now;
328
329 for (done = 0; done < len; done += len_now)
330 {
331 for (p = msg + done; p < msg + len; )
332 {
333 if (*p == NL)
334 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200335 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200336 }
337 len_now = p - msg - done;
338 vterm_input_write(vterm, (char *)msg + done, len_now);
339 if (p < msg + len && *p == NL)
340 {
341 /* Convert NL to CR-NL, that appears to work best. */
342 vterm_input_write(vterm, "\r\n", 2);
343 ++len_now;
344 }
345 }
346
347 /* this invokes the damage callbacks */
348 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
349}
350
Bram Moolenaar1c844932017-07-24 23:36:41 +0200351 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200352update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200353{
Bram Moolenaar1c844932017-07-24 23:36:41 +0200354 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200355 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200356 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200357 if (term->tl_cursor_visible)
358 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200359 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200360#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200361 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200362 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200363#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200364 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200365}
366
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200367/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200368 * Invoked when "msg" output from a job was received. Write it to the terminal
369 * of "buffer".
370 */
371 void
372write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
373{
374 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200375 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200376
Bram Moolenaard85f2712017-07-28 21:51:57 +0200377 if (term->tl_vterm == NULL)
378 {
379 ch_logn(channel, "NOT writing %d bytes to terminal", (int)len);
380 return;
381 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200382 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200383 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200384
385 /* TODO: only update once in a while. */
386 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200387 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200388}
389
390/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200391 * Send a mouse position and click to the vterm
392 */
393 static int
394term_send_mouse(VTerm *vterm, int button, int pressed)
395{
396 VTermModifier mod = VTERM_MOD_NONE;
397
398 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
399 mouse_col - W_WINCOL(curwin), mod);
400 vterm_mouse_button(vterm, button, pressed, mod);
401 return TRUE;
402}
403
404/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200405 * Convert typed key "c" into bytes to send to the job.
406 * Return the number of bytes in "buf".
407 */
408 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200409term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200410{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200411 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200412 VTermKey key = VTERM_KEY_NONE;
413 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200414 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200415
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200416 switch (c)
417 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200418 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200419 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200420 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
421 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200422 case K_DEL: key = VTERM_KEY_DEL; break;
423 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200424 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
425 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200426 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200427 case K_S_END: mod = VTERM_MOD_SHIFT;
428 key = VTERM_KEY_END; break;
429 case K_C_END: mod = VTERM_MOD_CTRL;
430 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200431 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
432 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
433 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
434 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
435 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
436 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
437 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
438 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
439 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
440 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
441 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
442 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
443 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200444 case K_S_HOME: mod = VTERM_MOD_SHIFT;
445 key = VTERM_KEY_HOME; break;
446 case K_C_HOME: mod = VTERM_MOD_CTRL;
447 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200448 case K_INS: key = VTERM_KEY_INS; break;
449 case K_K0: key = VTERM_KEY_KP_0; break;
450 case K_K1: key = VTERM_KEY_KP_1; break;
451 case K_K2: key = VTERM_KEY_KP_2; break;
452 case K_K3: key = VTERM_KEY_KP_3; break;
453 case K_K4: key = VTERM_KEY_KP_4; break;
454 case K_K5: key = VTERM_KEY_KP_5; break;
455 case K_K6: key = VTERM_KEY_KP_6; break;
456 case K_K7: key = VTERM_KEY_KP_7; break;
457 case K_K8: key = VTERM_KEY_KP_8; break;
458 case K_K9: key = VTERM_KEY_KP_9; break;
459 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
460 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
461 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
462 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
463 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
464 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
465 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
466 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
467 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
468 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
469 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
470 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
471 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200472 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
473 key = VTERM_KEY_LEFT; break;
474 case K_C_LEFT: mod = VTERM_MOD_CTRL;
475 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200476 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
477 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
478 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200479 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
480 key = VTERM_KEY_RIGHT; break;
481 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
482 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200483 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200484 case K_S_UP: mod = VTERM_MOD_SHIFT;
485 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200486 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200487
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200488 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
489 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
490 case K_MOUSELEFT: /* TODO */ return 0;
491 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200492
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200493 case K_LEFTMOUSE:
494 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
495 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
496 case K_LEFTRELEASE:
497 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
498 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
499 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
500 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
501 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
502 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
503 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
504 case K_X1MOUSE: /* TODO */ return 0;
505 case K_X1DRAG: /* TODO */ return 0;
506 case K_X1RELEASE: /* TODO */ return 0;
507 case K_X2MOUSE: /* TODO */ return 0;
508 case K_X2DRAG: /* TODO */ return 0;
509 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200510
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200511 case K_IGNORE: return 0;
512 case K_NOP: return 0;
513 case K_UNDO: return 0;
514 case K_HELP: return 0;
515 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
516 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
517 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
518 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
519 case K_SELECT: return 0;
520#ifdef FEAT_GUI
521 case K_VER_SCROLLBAR: return 0;
522 case K_HOR_SCROLLBAR: return 0;
523#endif
524#ifdef FEAT_GUI_TABLINE
525 case K_TABLINE: return 0;
526 case K_TABMENU: return 0;
527#endif
528#ifdef FEAT_NETBEANS_INTG
529 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
530#endif
531#ifdef FEAT_DND
532 case K_DROP: return 0;
533#endif
534#ifdef FEAT_AUTOCMD
535 case K_CURSORHOLD: return 0;
536#endif
537 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
538 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200539 }
540
541 /*
542 * Convert special keys to vterm keys:
543 * - Write keys to vterm: vterm_keyboard_key()
544 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200545 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200546 */
547 if (key != VTERM_KEY_NONE)
548 /* Special key, let vterm convert it. */
549 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200550 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200551 /* Normal character, let vterm convert it. */
552 vterm_keyboard_unichar(vterm, c, mod);
553
554 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200555 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200556}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200557
Bram Moolenaar938783d2017-07-16 20:13:26 +0200558/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200559 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200560 */
561 static int
562term_job_running(term_T *term)
563{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200564 /* Also consider the job finished when the channel is closed, to avoid a
565 * race condition when updating the title. */
566 return term->tl_job != NULL
567 && term->tl_job->jv_status == JOB_STARTED
568 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200569}
570
571/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200572 * Add the last line of the scrollback buffer to the buffer in the window.
573 */
574 static void
575add_scrollback_line_to_buffer(term_T *term)
576{
577 linenr_T lnum = term->tl_scrollback.ga_len - 1;
578 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
579 garray_T ga;
580 int c;
581 int col;
582 int i;
583
584 ga_init2(&ga, 1, 100);
585 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
586 {
587 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
588 goto failed;
589 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
590 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
591 (char_u *)ga.ga_data + ga.ga_len);
592 }
593 if (ga_grow(&ga, 1) == FAIL)
594 goto failed;
595 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
596 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
597
598 if (lnum == 0)
599 {
600 /* Delete the empty line that was in the empty buffer. */
601 curbuf = term->tl_buffer;
602 ml_delete(2, FALSE);
603 curbuf = curwin->w_buffer;
604 }
605
606failed:
607 ga_clear(&ga);
608}
609
610/*
611 * Add the current lines of the terminal to scrollback and to the buffer.
612 * Called after the job has ended and when switching to Terminal mode.
613 */
614 static void
615move_terminal_to_buffer(term_T *term)
616{
617 win_T *wp;
618 int len;
619 int lines_skipped = 0;
620 VTermPos pos;
621 VTermScreenCell cell;
622 VTermScreenCell *p;
623 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
624
625 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
626 {
627 len = 0;
628 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
629 if (vterm_screen_get_cell(screen, pos, &cell) != 0
630 && cell.chars[0] != NUL)
631 len = pos.col + 1;
632
633 if (len == 0)
634 ++lines_skipped;
635 else
636 {
637 while (lines_skipped > 0)
638 {
639 /* Line was skipped, add an empty line. */
640 --lines_skipped;
641 if (ga_grow(&term->tl_scrollback, 1) == OK)
642 {
643 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
644 + term->tl_scrollback.ga_len;
645
646 line->sb_cols = 0;
647 line->sb_cells = NULL;
648 ++term->tl_scrollback.ga_len;
649
650 add_scrollback_line_to_buffer(term);
651 }
652 }
653
654 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
655 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
656 {
657 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
658 + term->tl_scrollback.ga_len;
659
660 for (pos.col = 0; pos.col < len; ++pos.col)
661 {
662 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
663 vim_memset(p + pos.col, 0, sizeof(cell));
664 else
665 p[pos.col] = cell;
666 }
667 line->sb_cols = len;
668 line->sb_cells = p;
669 ++term->tl_scrollback.ga_len;
670
671 add_scrollback_line_to_buffer(term);
672 }
673 else
674 vim_free(p);
675 }
676 }
677
678 FOR_ALL_WINDOWS(wp)
679 {
680 if (wp->w_buffer == term->tl_buffer)
681 {
682 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
683 wp->w_cursor.col = 0;
684 wp->w_valid = 0;
685 redraw_win_later(wp, NOT_VALID);
686 }
687 }
688}
689
690 static void
691set_terminal_mode(term_T *term, int on)
692{
693 term->tl_terminal_mode = on;
694 vim_free(term->tl_status_text);
695 term->tl_status_text = NULL;
696 if (term->tl_buffer == curbuf)
697 maketitle();
698}
699
700/*
701 * Called after the job if finished and Terminal mode is not active:
702 * Move the vterm contents into the scrollback buffer and free the vterm.
703 */
704 static void
705cleanup_vterm(term_T *term)
706{
707 move_terminal_to_buffer(term);
708 term_free_vterm(term);
709 set_terminal_mode(term, FALSE);
710}
711
712/*
713 * Switch from sending keys to the job to Terminal-Normal mode.
714 * Suspends updating the terminal window.
715 */
716 static void
717term_enter_terminal_mode()
718{
719 term_T *term = curbuf->b_term;
720
721 /* Append the current terminal contents to the buffer. */
722 move_terminal_to_buffer(term);
723
724 set_terminal_mode(term, TRUE);
725}
726
727/*
728 * Returns TRUE if the current window contains a terminal and we are in
729 * Terminal-Normal mode.
730 */
731 int
732term_in_terminal_mode()
733{
734 term_T *term = curbuf->b_term;
735
736 return term != NULL && term->tl_terminal_mode;
737}
738
739/*
740 * Switch from Terminal-Normal mode to sending keys to the job.
741 * Restores updating the terminal window.
742 */
743 void
744term_leave_terminal_mode()
745{
746 term_T *term = curbuf->b_term;
747 sb_line_T *line;
748 garray_T *gap;
749
750 /* Remove the terminal contents from the scrollback and the buffer. */
751 gap = &term->tl_scrollback;
752 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
753 {
754 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
755 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
756 vim_free(line->sb_cells);
757 --gap->ga_len;
758 if (gap->ga_len == 0)
759 break;
760 }
761 check_cursor();
762
763 set_terminal_mode(term, FALSE);
764
765 if (term->tl_channel_closed)
766 cleanup_vterm(term);
767 redraw_buf_and_status_later(curbuf, NOT_VALID);
768}
769
770/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200771 * Get a key from the user without mapping.
772 * TODO: use terminal mode mappings.
773 */
774 static int
775term_vgetc()
776{
777 int c;
778
779 ++no_mapping;
780 ++allow_keys;
781 got_int = FALSE;
782 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200783 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200784 --no_mapping;
785 --allow_keys;
786 return c;
787}
788
789/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200790 * Send keys to terminal.
791 */
792 static int
793send_keys_to_term(term_T *term, int c, int typed)
794{
795 char msg[KEY_BUF_LEN];
796 size_t len;
797 static int mouse_was_outside = FALSE;
798 int dragging_outside = FALSE;
799
800 /* Catch keys that need to be handled as in Normal mode. */
801 switch (c)
802 {
803 case NUL:
804 case K_ZERO:
805 if (typed)
806 stuffcharReadbuff(c);
807 return FAIL;
808
809 case K_IGNORE:
810 return FAIL;
811
812 case K_LEFTDRAG:
813 case K_MIDDLEDRAG:
814 case K_RIGHTDRAG:
815 case K_X1DRAG:
816 case K_X2DRAG:
817 dragging_outside = mouse_was_outside;
818 /* FALLTHROUGH */
819 case K_LEFTMOUSE:
820 case K_LEFTMOUSE_NM:
821 case K_LEFTRELEASE:
822 case K_LEFTRELEASE_NM:
823 case K_MIDDLEMOUSE:
824 case K_MIDDLERELEASE:
825 case K_RIGHTMOUSE:
826 case K_RIGHTRELEASE:
827 case K_X1MOUSE:
828 case K_X1RELEASE:
829 case K_X2MOUSE:
830 case K_X2RELEASE:
831 if (mouse_row < W_WINROW(curwin)
832 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
833 || mouse_col < W_WINCOL(curwin)
834 || mouse_col >= W_ENDCOL(curwin)
835 || dragging_outside)
836 {
837 /* click outside the current window */
838 if (typed)
839 {
840 stuffcharReadbuff(c);
841 mouse_was_outside = TRUE;
842 }
843 return FAIL;
844 }
845 }
846 if (typed)
847 mouse_was_outside = FALSE;
848
849 /* Convert the typed key to a sequence of bytes for the job. */
850 len = term_convert_key(term, c, msg);
851 if (len > 0)
852 /* TODO: if FAIL is returned, stop? */
853 channel_send(term->tl_job->jv_channel, PART_IN,
854 (char_u *)msg, (int)len, NULL);
855
856 return OK;
857}
858
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200859 static void
860position_cursor(win_T *wp, VTermPos *pos)
861{
862 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
863 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
864 wp->w_valid |= (VALID_WCOL|VALID_WROW);
865}
866
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200867/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200868 * Returns TRUE if the current window contains a terminal and we are sending
869 * keys to the job.
870 */
871 int
872term_use_loop()
873{
874 term_T *term = curbuf->b_term;
875
876 return term != NULL
877 && !term->tl_terminal_mode
878 && term->tl_vterm != NULL
879 && term_job_running(term);
880}
881
882/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200883 * Wait for input and send it to the job.
884 * Return when the start of a CTRL-W command is typed or anything else that
885 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200886 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
887 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200888 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200889 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200890terminal_loop(void)
891{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200892 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200893 int termkey = 0;
894
895 if (*curwin->w_p_tk != NUL)
896 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200897 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200898
899 for (;;)
900 {
901 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200902 /* Repeat redrawing in case a message is received while redrawing. */
903 while (curwin->w_redr_type != 0)
904 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200905 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200906
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200907 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200908 if (curbuf->b_term->tl_vterm == NULL
909 || !term_job_running(curbuf->b_term))
910 /* job finished while waiting for a character */
911 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200912
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200913 if (c == (termkey == 0 ? Ctrl_W : termkey))
914 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200915#ifdef FEAT_CMDL_INFO
916 if (add_to_showcmd(c))
917 out_flush();
918#endif
919 c = term_vgetc();
920#ifdef FEAT_CMDL_INFO
921 clear_showcmd();
922#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200923 if (curbuf->b_term->tl_vterm == NULL
924 || !term_job_running(curbuf->b_term))
925 /* job finished while waiting for a character */
926 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200927
928 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200929 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200930 /* "CTRL-W .": send CTRL-W to the job */
931 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200932 }
933 else if (termkey == 0 && c == 'N')
934 {
935 term_enter_terminal_mode();
936 return FAIL;
937 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200938 else if (termkey == 0 || c != termkey)
939 {
940 stuffcharReadbuff(Ctrl_W);
941 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200942 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200943 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200944 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200945 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
946 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200947 }
Bram Moolenaard85f2712017-07-28 21:51:57 +0200948 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200949}
950
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200951/*
952 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200953 * This updates the title and status, but does not close the vter, because
954 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200955 */
956 void
957term_job_ended(job_T *job)
958{
959 term_T *term;
960 int did_one = FALSE;
961
962 for (term = first_term; term != NULL; term = term->tl_next)
963 if (term->tl_job == job)
964 {
965 vim_free(term->tl_title);
966 term->tl_title = NULL;
967 vim_free(term->tl_status_text);
968 term->tl_status_text = NULL;
969 redraw_buf_and_status_later(term->tl_buffer, VALID);
970 did_one = TRUE;
971 }
972 if (did_one)
973 redraw_statuslines();
974 if (curbuf->b_term != NULL)
975 {
976 if (curbuf->b_term->tl_job == job)
977 maketitle();
978 update_cursor(curbuf->b_term, TRUE);
979 }
980}
981
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200982 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200983may_toggle_cursor(term_T *term)
984{
985 if (curbuf == term->tl_buffer)
986 {
987 if (term->tl_cursor_visible)
988 cursor_on();
989 else
990 cursor_off();
991 }
992}
993
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200994 static int
995handle_damage(VTermRect rect, void *user)
996{
997 term_T *term = (term_T *)user;
998
999 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1000 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1001 redraw_buf_later(term->tl_buffer, NOT_VALID);
1002 return 1;
1003}
1004
1005 static int
1006handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1007{
1008 term_T *term = (term_T *)user;
1009
1010 /* TODO */
1011 redraw_buf_later(term->tl_buffer, NOT_VALID);
1012 return 1;
1013}
1014
1015 static int
1016handle_movecursor(
1017 VTermPos pos,
1018 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001019 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001020 void *user)
1021{
1022 term_T *term = (term_T *)user;
1023 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001024
1025 term->tl_cursor_pos = pos;
1026 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001027
1028 FOR_ALL_WINDOWS(wp)
1029 {
1030 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001031 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001032 }
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001033 if (term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001034 {
1035 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001036 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001037 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001038
1039 return 1;
1040}
1041
Bram Moolenaar21554412017-07-24 21:44:43 +02001042 static int
1043handle_settermprop(
1044 VTermProp prop,
1045 VTermValue *value,
1046 void *user)
1047{
1048 term_T *term = (term_T *)user;
1049
1050 switch (prop)
1051 {
1052 case VTERM_PROP_TITLE:
1053 vim_free(term->tl_title);
1054 term->tl_title = vim_strsave((char_u *)value->string);
1055 vim_free(term->tl_status_text);
1056 term->tl_status_text = NULL;
1057 if (term == curbuf->b_term)
1058 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001059 break;
1060
1061 case VTERM_PROP_CURSORVISIBLE:
1062 term->tl_cursor_visible = value->boolean;
1063 may_toggle_cursor(term);
1064 out_flush();
1065 break;
1066
Bram Moolenaar21554412017-07-24 21:44:43 +02001067 default:
1068 break;
1069 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001070 /* Always return 1, otherwise vterm doesn't store the value internally. */
1071 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001072}
1073
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001074/*
1075 * The job running in the terminal resized the terminal.
1076 */
1077 static int
1078handle_resize(int rows, int cols, void *user)
1079{
1080 term_T *term = (term_T *)user;
1081 win_T *wp;
1082
1083 term->tl_rows = rows;
1084 term->tl_cols = cols;
1085 FOR_ALL_WINDOWS(wp)
1086 {
1087 if (wp->w_buffer == term->tl_buffer)
1088 {
1089 win_setheight_win(rows, wp);
1090 win_setwidth_win(cols, wp);
1091 }
1092 }
1093
1094 redraw_buf_later(term->tl_buffer, NOT_VALID);
1095 return 1;
1096}
1097
Bram Moolenaard85f2712017-07-28 21:51:57 +02001098/*
1099 * Handle a line that is pushed off the top of the screen.
1100 */
1101 static int
1102handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1103{
1104 term_T *term = (term_T *)user;
1105
1106 /* TODO: Limit the number of lines that are stored. */
1107 /* TODO: put the text in the buffer. */
1108 if (ga_grow(&term->tl_scrollback, 1) == OK)
1109 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001110 VTermScreenCell *p = NULL;
1111 int len = 0;
1112 int i;
1113 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001114
1115 /* do not store empty cells at the end */
1116 for (i = 0; i < cols; ++i)
1117 if (cells[i].chars[0] != 0)
1118 len = i + 1;
1119
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001120 if (len > 0)
1121 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001122 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001123 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001124
1125 line = (sb_line_T *)term->tl_scrollback.ga_data
1126 + term->tl_scrollback.ga_len;
1127 line->sb_cols = len;
1128 line->sb_cells = p;
1129 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001130 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001131
1132 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001133 }
1134 return 0; /* ignored */
1135}
1136
Bram Moolenaar21554412017-07-24 21:44:43 +02001137static VTermScreenCallbacks screen_callbacks = {
1138 handle_damage, /* damage */
1139 handle_moverect, /* moverect */
1140 handle_movecursor, /* movecursor */
1141 handle_settermprop, /* settermprop */
1142 NULL, /* bell */
1143 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001144 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001145 NULL /* sb_popline */
1146};
1147
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001148/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001149 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001150 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001151 */
1152 void
1153term_channel_closed(channel_T *ch)
1154{
1155 term_T *term;
1156 int did_one = FALSE;
1157
1158 for (term = first_term; term != NULL; term = term->tl_next)
1159 if (term->tl_job == ch->ch_job)
1160 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001161 term->tl_channel_closed = TRUE;
1162
Bram Moolenaard85f2712017-07-28 21:51:57 +02001163 vim_free(term->tl_title);
1164 term->tl_title = NULL;
1165 vim_free(term->tl_status_text);
1166 term->tl_status_text = NULL;
1167
Bram Moolenaar423802d2017-07-30 16:52:24 +02001168 /* Unless in Terminal-Normal mode: clear the vterm. */
1169 if (!term->tl_terminal_mode)
1170 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001171
1172 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1173 did_one = TRUE;
1174 }
1175 if (did_one)
1176 {
1177 redraw_statuslines();
1178
1179 /* Need to break out of vgetc(). */
1180 ins_char_typebuf(K_IGNORE);
1181
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001182 term = curbuf->b_term;
1183 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001184 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001185 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001186 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001187 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001188 }
1189 }
1190}
1191
1192/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001193 * Reverse engineer the RGB value into a cterm color index.
1194 * First color is 1. Return 0 if no match found.
1195 */
1196 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001197color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001198{
1199 int red = color->red;
1200 int blue = color->blue;
1201 int green = color->green;
1202
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001203 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001204 if (red == 0)
1205 {
1206 if (green == 0)
1207 {
1208 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001209 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001210 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001211 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001212 }
1213 else if (green == 224)
1214 {
1215 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001216 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001217 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001218 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001219 }
1220 }
1221 else if (red == 224)
1222 {
1223 if (green == 0)
1224 {
1225 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001226 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001227 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001228 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001229 }
1230 else if (green == 224)
1231 {
1232 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001233 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001234 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001235 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001236 }
1237 }
1238 else if (red == 128)
1239 {
1240 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001241 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001242 }
1243 else if (red == 255)
1244 {
1245 if (green == 64)
1246 {
1247 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001248 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001249 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001250 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001251 }
1252 else if (green == 255)
1253 {
1254 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001255 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001256 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001257 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001258 }
1259 }
1260 else if (red == 64)
1261 {
1262 if (green == 64)
1263 {
1264 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001265 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001266 }
1267 else if (green == 255)
1268 {
1269 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001270 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001271 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001272 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001273 }
1274 }
1275 if (t_colors >= 256)
1276 {
1277 if (red == blue && red == green)
1278 {
1279 /* 24-color greyscale */
1280 static int cutoff[23] = {
1281 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1282 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1283 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1284 int i;
1285
1286 for (i = 0; i < 23; ++i)
1287 if (red < cutoff[i])
1288 return i + 233;
1289 return 256;
1290 }
1291
1292 /* 216-color cube */
1293 return 17 + ((red + 25) / 0x33) * 36
1294 + ((green + 25) / 0x33) * 6
1295 + (blue + 25) / 0x33;
1296 }
1297 return 0;
1298}
1299
1300/*
1301 * Convert the attributes of a vterm cell into an attribute index.
1302 */
1303 static int
1304cell2attr(VTermScreenCell *cell)
1305{
1306 int attr = 0;
1307
1308 if (cell->attrs.bold)
1309 attr |= HL_BOLD;
1310 if (cell->attrs.underline)
1311 attr |= HL_UNDERLINE;
1312 if (cell->attrs.italic)
1313 attr |= HL_ITALIC;
1314 if (cell->attrs.strike)
1315 attr |= HL_STANDOUT;
1316 if (cell->attrs.reverse)
1317 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001318
1319#ifdef FEAT_GUI
1320 if (gui.in_use)
1321 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001322 guicolor_T fg, bg;
1323
1324 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1325 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1326 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001327 }
1328 else
1329#endif
1330#ifdef FEAT_TERMGUICOLORS
1331 if (p_tgc)
1332 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001333 guicolor_T fg, bg;
1334
1335 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1336 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1337
1338 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001339 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001340 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001341#endif
1342 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001343 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1344 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001345 }
1346 return 0;
1347}
1348
1349/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001350 * Called to update the window that contains a terminal.
1351 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001352 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001353 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001354term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001355{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001356 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001357 VTerm *vterm;
1358 VTermScreen *screen;
1359 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001360 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001361
Bram Moolenaar423802d2017-07-30 16:52:24 +02001362 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001363 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001364
Bram Moolenaard85f2712017-07-28 21:51:57 +02001365 vterm = term->tl_vterm;
1366 screen = vterm_obtain_screen(vterm);
1367 state = vterm_obtain_state(vterm);
1368
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001369 /*
1370 * If the window was resized a redraw will be triggered and we get here.
1371 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1372 */
1373 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1374 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001375 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001376 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1377 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1378 win_T *twp;
1379
1380 FOR_ALL_WINDOWS(twp)
1381 {
1382 /* When more than one window shows the same terminal, use the
1383 * smallest size. */
1384 if (twp->w_buffer == term->tl_buffer)
1385 {
1386 if (!term->tl_rows_fixed && rows > twp->w_height)
1387 rows = twp->w_height;
1388 if (!term->tl_cols_fixed && cols > twp->w_width)
1389 cols = twp->w_width;
1390 }
1391 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001392
1393 vterm_set_size(vterm, rows, cols);
1394 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1395 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001396 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001397 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001398
1399 /* The cursor may have been moved when resizing. */
1400 vterm_state_get_cursorpos(state, &pos);
1401 position_cursor(wp, &pos);
1402
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001403 /* TODO: Only redraw what changed. */
1404 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001405 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001406 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001407 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001408
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001409 if (pos.row < term->tl_rows)
1410 {
1411 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001412 {
1413 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001414 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001415
Bram Moolenaareeac6772017-07-23 15:48:37 +02001416 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1417 vim_memset(&cell, 0, sizeof(cell));
1418
1419 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001420 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001421 if (c == NUL)
1422 {
1423 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001424#if defined(FEAT_MBYTE)
1425 if (enc_utf8)
1426 ScreenLinesUC[off] = NUL;
1427#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001428 }
1429 else
1430 {
1431#if defined(FEAT_MBYTE)
1432 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001433 {
1434 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001435 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001436 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001437 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001438 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001439 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001440 if (enc_utf8)
1441 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001442 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001443#else
1444 ScreenLines[off] = c;
1445#endif
1446 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001447 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001448
1449 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001450 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001451 if (cell.width == 2)
1452 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001453 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001454#if defined(FEAT_MBYTE)
1455 if (enc_utf8)
1456 ScreenLinesUC[off] = NUL;
1457#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001458 ++pos.col;
1459 ++off;
1460 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001461 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001462 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001463 else
1464 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001465
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001466 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1467 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001468 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001469
1470 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001471}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001472
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001473/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001474 * Return TRUE if "wp" is a terminal window where the job has finished.
1475 */
1476 int
1477term_is_finished(buf_T *buf)
1478{
1479 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1480}
1481
1482/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001483 * Return TRUE if "wp" is a terminal window where the job has finished or we
1484 * are in Terminal-Normal mode.
1485 */
1486 int
1487term_show_buffer(buf_T *buf)
1488{
1489 term_T *term = buf->b_term;
1490
1491 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1492}
1493
1494/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001495 * The current buffer is going to be changed. If there is terminal
1496 * highlighting remove it now.
1497 */
1498 void
1499term_change_in_curbuf(void)
1500{
1501 term_T *term = curbuf->b_term;
1502
1503 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1504 {
1505 free_scrollback(term);
1506 redraw_buf_later(term->tl_buffer, NOT_VALID);
1507 }
1508}
1509
1510/*
1511 * Get the screen attribute for a position in the buffer.
1512 */
1513 int
1514term_get_attr(buf_T *buf, linenr_T lnum, int col)
1515{
1516 term_T *term = buf->b_term;
1517 sb_line_T *line;
1518
Bram Moolenaar70229f92017-07-29 16:01:53 +02001519 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001520 return 0;
1521 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1522 if (col >= line->sb_cols)
1523 return 0;
1524 return cell2attr(line->sb_cells + col);
1525}
1526
1527/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001528 * Set job options common for Unix and MS-Windows.
1529 */
1530 static void
1531setup_job_options(jobopt_T *opt, int rows, int cols)
1532{
1533 clear_job_options(opt);
1534 opt->jo_mode = MODE_RAW;
1535 opt->jo_out_mode = MODE_RAW;
1536 opt->jo_err_mode = MODE_RAW;
1537 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001538
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001539 opt->jo_io[PART_OUT] = JIO_BUFFER;
1540 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001541 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1542
1543 opt->jo_modifiable[PART_OUT] = 0;
1544 opt->jo_modifiable[PART_ERR] = 0;
1545 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1546
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001547 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1548 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001549 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001550 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1551
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001552 opt->jo_term_rows = rows;
1553 opt->jo_term_cols = cols;
1554}
1555
1556/*
1557 * Create a new vterm and initialize it.
1558 */
1559 static void
1560create_vterm(term_T *term, int rows, int cols)
1561{
1562 VTerm *vterm;
1563 VTermScreen *screen;
1564
1565 vterm = vterm_new(rows, cols);
1566 term->tl_vterm = vterm;
1567 screen = vterm_obtain_screen(vterm);
1568 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1569 /* TODO: depends on 'encoding'. */
1570 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001571
1572 /* Vterm uses a default black background. Set it to white when
1573 * 'background' is "light". */
1574 if (*p_bg == 'l')
1575 {
1576 VTermColor fg, bg;
1577
1578 fg.red = fg.green = fg.blue = 0;
1579 bg.red = bg.green = bg.blue = 255;
1580 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1581 }
1582
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001583 /* Required to initialize most things. */
1584 vterm_screen_reset(screen, 1 /* hard */);
1585}
1586
Bram Moolenaar21554412017-07-24 21:44:43 +02001587/*
1588 * Return the text to show for the buffer name and status.
1589 */
1590 char_u *
1591term_get_status_text(term_T *term)
1592{
1593 if (term->tl_status_text == NULL)
1594 {
1595 char_u *txt;
1596 size_t len;
1597
Bram Moolenaar423802d2017-07-30 16:52:24 +02001598 if (term->tl_terminal_mode)
1599 {
1600 if (term_job_running(term))
1601 txt = (char_u *)_("Terminal");
1602 else
1603 txt = (char_u *)_("Terminal-finished");
1604 }
1605 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001606 txt = term->tl_title;
1607 else if (term_job_running(term))
1608 txt = (char_u *)_("running");
1609 else
1610 txt = (char_u *)_("finished");
1611 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001612 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001613 if (term->tl_status_text != NULL)
1614 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1615 term->tl_buffer->b_fname, txt);
1616 }
1617 return term->tl_status_text;
1618}
1619
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001620/*
1621 * Mark references in jobs of terminals.
1622 */
1623 int
1624set_ref_in_term(int copyID)
1625{
1626 int abort = FALSE;
1627 term_T *term;
1628 typval_T tv;
1629
1630 for (term = first_term; term != NULL; term = term->tl_next)
1631 if (term->tl_job != NULL)
1632 {
1633 tv.v_type = VAR_JOB;
1634 tv.vval.v_job = term->tl_job;
1635 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1636 }
1637 return abort;
1638}
1639
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001640/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001641 * Get the buffer from the first argument in "argvars".
1642 * Returns NULL when the buffer is not for a terminal window.
1643 */
1644 static buf_T *
1645term_get_buf(typval_T *argvars)
1646{
1647 buf_T *buf;
1648
1649 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1650 ++emsg_off;
1651 buf = get_buf_tv(&argvars[0], FALSE);
1652 --emsg_off;
1653 if (buf == NULL || buf->b_term == NULL)
1654 return NULL;
1655 return buf;
1656}
1657
1658/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001659 * "term_getattr(attr, name)" function
1660 */
1661 void
1662f_term_getattr(typval_T *argvars, typval_T *rettv)
1663{
1664 int attr;
1665 size_t i;
1666 char_u *name;
1667
1668 static struct {
1669 char *name;
1670 int attr;
1671 } attrs[] = {
1672 {"bold", HL_BOLD},
1673 {"italic", HL_ITALIC},
1674 {"underline", HL_UNDERLINE},
1675 {"strike", HL_STANDOUT},
1676 {"reverse", HL_INVERSE},
1677 };
1678
1679 attr = get_tv_number(&argvars[0]);
1680 name = get_tv_string_chk(&argvars[1]);
1681 if (name == NULL)
1682 return;
1683
1684 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1685 if (STRCMP(name, attrs[i].name) == 0)
1686 {
1687 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1688 break;
1689 }
1690}
1691
1692/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001693 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001694 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001695 void
1696f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001697{
Bram Moolenaar97870002017-07-30 18:28:38 +02001698 buf_T *buf = term_get_buf(argvars);
1699 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001700
Bram Moolenaar97870002017-07-30 18:28:38 +02001701 if (rettv_list_alloc(rettv) == FAIL)
1702 return;
1703 if (buf == NULL)
1704 return;
1705
1706 l = rettv->vval.v_list;
1707 list_append_number(l, buf->b_term->tl_cursor_pos.row);
1708 list_append_number(l, buf->b_term->tl_cursor_pos.col);
1709 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001710}
1711
1712/*
1713 * "term_getjob(buf)" function
1714 */
1715 void
1716f_term_getjob(typval_T *argvars, typval_T *rettv)
1717{
1718 buf_T *buf = term_get_buf(argvars);
1719
1720 rettv->v_type = VAR_JOB;
1721 rettv->vval.v_job = NULL;
1722 if (buf == NULL)
1723 return;
1724
1725 rettv->vval.v_job = buf->b_term->tl_job;
1726 if (rettv->vval.v_job != NULL)
1727 ++rettv->vval.v_job->jv_refcount;
1728}
1729
1730/*
1731 * "term_getline(buf, row)" function
1732 */
1733 void
1734f_term_getline(typval_T *argvars, typval_T *rettv)
1735{
1736 buf_T *buf = term_get_buf(argvars);
1737 term_T *term;
1738 int row;
1739
1740 rettv->v_type = VAR_STRING;
1741 if (buf == NULL)
1742 return;
1743 term = buf->b_term;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001744 if (argvars[1].v_type == VAR_UNKNOWN)
1745 row = term->tl_cursor_pos.row;
1746 else
1747 row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001748
1749 if (term->tl_vterm == NULL)
1750 {
1751 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1752
1753 /* vterm is finished, get the text from the buffer */
1754 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1755 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1756 }
1757 else
1758 {
1759 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1760 VTermRect rect;
1761 int len;
1762 char_u *p;
1763
1764 len = term->tl_cols * MB_MAXBYTES + 1;
1765 p = alloc(len);
1766 if (p == NULL)
1767 return;
1768 rettv->vval.v_string = p;
1769
1770 rect.start_col = 0;
1771 rect.end_col = term->tl_cols;
1772 rect.start_row = row;
1773 rect.end_row = row + 1;
1774 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1775 }
1776}
1777
1778/*
1779 * "term_getsize(buf)" function
1780 */
1781 void
1782f_term_getsize(typval_T *argvars, typval_T *rettv)
1783{
1784 buf_T *buf = term_get_buf(argvars);
1785 list_T *l;
1786
1787 if (rettv_list_alloc(rettv) == FAIL)
1788 return;
1789 if (buf == NULL)
1790 return;
1791
1792 l = rettv->vval.v_list;
1793 list_append_number(l, buf->b_term->tl_rows);
1794 list_append_number(l, buf->b_term->tl_cols);
1795}
1796
1797/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001798 * "term_getstatus(buf)" function
1799 */
1800 void
1801f_term_getstatus(typval_T *argvars, typval_T *rettv)
1802{
1803 buf_T *buf = term_get_buf(argvars);
1804 term_T *term;
1805 char_u val[100];
1806
1807 rettv->v_type = VAR_STRING;
1808 if (buf == NULL)
1809 return;
1810 term = buf->b_term;
1811
1812 if (term_job_running(term))
1813 STRCPY(val, "running");
1814 else
1815 STRCPY(val, "finished");
1816 if (term->tl_terminal_mode)
1817 STRCAT(val, ",terminal");
1818 rettv->vval.v_string = vim_strsave(val);
1819}
1820
1821/*
1822 * "term_gettitle(buf)" function
1823 */
1824 void
1825f_term_gettitle(typval_T *argvars, typval_T *rettv)
1826{
1827 buf_T *buf = term_get_buf(argvars);
1828
1829 rettv->v_type = VAR_STRING;
1830 if (buf == NULL)
1831 return;
1832
1833 if (buf->b_term->tl_title != NULL)
1834 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1835}
1836
1837/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001838 * "term_list()" function
1839 */
1840 void
1841f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1842{
1843 term_T *tp;
1844 list_T *l;
1845
1846 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1847 return;
1848
1849 l = rettv->vval.v_list;
1850 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1851 if (tp != NULL && tp->tl_buffer != NULL)
1852 if (list_append_number(l,
1853 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1854 return;
1855}
1856
1857/*
1858 * "term_scrape(buf, row)" function
1859 */
1860 void
1861f_term_scrape(typval_T *argvars, typval_T *rettv)
1862{
1863 buf_T *buf = term_get_buf(argvars);
1864 VTermScreen *screen = NULL;
1865 VTermPos pos;
1866 list_T *l;
1867 term_T *term;
1868
1869 if (rettv_list_alloc(rettv) == FAIL)
1870 return;
1871 if (buf == NULL)
1872 return;
1873 term = buf->b_term;
1874 if (term->tl_vterm != NULL)
1875 screen = vterm_obtain_screen(term->tl_vterm);
1876
1877 l = rettv->vval.v_list;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001878 if (argvars[1].v_type == VAR_UNKNOWN)
1879 pos.row = term->tl_cursor_pos.row;
1880 else
1881 pos.row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001882 for (pos.col = 0; pos.col < term->tl_cols; )
1883 {
1884 dict_T *dcell;
1885 VTermScreenCell cell;
1886 char_u rgb[8];
1887 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1888 int off = 0;
1889 int i;
1890
1891 if (screen == NULL)
1892 {
1893 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1894 sb_line_T *line;
1895
1896 /* vterm has finished, get the cell from scrollback */
1897 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1898 break;
1899 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1900 if (pos.col >= line->sb_cols)
1901 break;
1902 cell = line->sb_cells[pos.col];
1903 }
1904 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1905 break;
1906 dcell = dict_alloc();
1907 list_append_dict(l, dcell);
1908
1909 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1910 {
1911 if (cell.chars[i] == 0)
1912 break;
1913 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1914 }
1915 mbs[off] = NUL;
1916 dict_add_nr_str(dcell, "chars", 0, mbs);
1917
1918 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1919 cell.fg.red, cell.fg.green, cell.fg.blue);
1920 dict_add_nr_str(dcell, "fg", 0, rgb);
1921 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1922 cell.bg.red, cell.bg.green, cell.bg.blue);
1923 dict_add_nr_str(dcell, "bg", 0, rgb);
1924
1925 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
1926 dict_add_nr_str(dcell, "width", cell.width, NULL);
1927
1928 ++pos.col;
1929 if (cell.width == 2)
1930 ++pos.col;
1931 }
1932}
1933
1934/*
1935 * "term_sendkeys(buf, keys)" function
1936 */
1937 void
1938f_term_sendkeys(typval_T *argvars, typval_T *rettv)
1939{
1940 buf_T *buf = term_get_buf(argvars);
1941 char_u *msg;
1942 term_T *term;
1943
1944 rettv->v_type = VAR_UNKNOWN;
1945 if (buf == NULL)
1946 return;
1947
1948 msg = get_tv_string_chk(&argvars[1]);
1949 if (msg == NULL)
1950 return;
1951 term = buf->b_term;
1952 if (term->tl_vterm == NULL)
1953 return;
1954
1955 while (*msg != NUL)
1956 {
1957 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
1958 msg += MB_PTR2LEN(msg);
1959 }
1960
1961 /* TODO: only update once in a while. */
1962 update_screen(0);
1963 if (buf == curbuf)
1964 update_cursor(term, TRUE);
1965}
1966
1967/*
1968 * "term_start(command, options)" function
1969 */
1970 void
1971f_term_start(typval_T *argvars, typval_T *rettv)
1972{
1973 char_u *cmd = get_tv_string_chk(&argvars[0]);
1974 exarg_T ea;
1975
1976 if (cmd == NULL)
1977 return;
1978 ea.arg = cmd;
1979 ex_terminal(&ea);
1980
1981 if (curbuf->b_term != NULL)
1982 rettv->vval.v_number = curbuf->b_fnum;
1983}
1984
1985/*
1986 * "term_wait" function
1987 */
1988 void
1989f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
1990{
1991 buf_T *buf = term_get_buf(argvars);
1992
1993 if (buf == NULL)
1994 return;
1995
1996 /* Get the job status, this will detect a job that finished. */
1997 if (buf->b_term->tl_job != NULL)
1998 (void)job_status(buf->b_term->tl_job);
1999
2000 /* Check for any pending channel I/O. */
2001 vpeekc_any();
2002 ui_delay(10L, FALSE);
2003
2004 /* Flushing messages on channels is hopefully sufficient.
2005 * TODO: is there a better way? */
2006 parse_queued_messages();
2007}
2008
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002009# ifdef WIN3264
2010
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002011/**************************************
2012 * 2. MS-Windows implementation.
2013 */
2014
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002015#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2016#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2017
Bram Moolenaar8a773062017-07-24 22:29:21 +02002018void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002019void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002020void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002021BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2022void (*winpty_config_set_initial_size)(void*, int, int);
2023LPCWSTR (*winpty_conin_name)(void*);
2024LPCWSTR (*winpty_conout_name)(void*);
2025LPCWSTR (*winpty_conerr_name)(void*);
2026void (*winpty_free)(void*);
2027void (*winpty_config_free)(void*);
2028void (*winpty_spawn_config_free)(void*);
2029void (*winpty_error_free)(void*);
2030LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002031BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002032
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002033#define WINPTY_DLL "winpty.dll"
2034
2035static HINSTANCE hWinPtyDLL = NULL;
2036
2037 int
2038dyn_winpty_init(void)
2039{
2040 int i;
2041 static struct
2042 {
2043 char *name;
2044 FARPROC *ptr;
2045 } winpty_entry[] =
2046 {
2047 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2048 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2049 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2050 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2051 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2052 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2053 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2054 {"winpty_free", (FARPROC*)&winpty_free},
2055 {"winpty_open", (FARPROC*)&winpty_open},
2056 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2057 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2058 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2059 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002060 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002061 {NULL, NULL}
2062 };
2063
2064 /* No need to initialize twice. */
2065 if (hWinPtyDLL)
2066 return 1;
2067 /* Load winpty.dll */
2068 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2069 if (!hWinPtyDLL)
2070 {
2071 EMSG2(_(e_loadlib), WINPTY_DLL);
2072 return 0;
2073 }
2074 for (i = 0; winpty_entry[i].name != NULL
2075 && winpty_entry[i].ptr != NULL; ++i)
2076 {
2077 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2078 winpty_entry[i].name)) == NULL)
2079 {
2080 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2081 return 0;
2082 }
2083 }
2084
2085 return 1;
2086}
2087
2088/*
2089 * Create a new terminal of "rows" by "cols" cells.
2090 * Store a reference in "term".
2091 * Return OK or FAIL.
2092 */
2093 static int
2094term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2095{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002096 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002097 channel_T *channel = NULL;
2098 job_T *job = NULL;
2099 jobopt_T opt;
2100 DWORD error;
2101 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2102 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002103 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002104
2105 if (!dyn_winpty_init())
2106 return FAIL;
2107
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002108 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002109 if (p == NULL)
2110 return FAIL;
2111
2112 job = job_alloc();
2113 if (job == NULL)
2114 goto failed;
2115
2116 channel = add_channel();
2117 if (channel == NULL)
2118 goto failed;
2119
2120 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2121 if (term->tl_winpty_config == NULL)
2122 goto failed;
2123
2124 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2125 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2126 if (term->tl_winpty == NULL)
2127 goto failed;
2128
2129 spawn_config = winpty_spawn_config_new(
2130 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2131 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2132 NULL,
2133 p,
2134 NULL,
2135 NULL,
2136 &winpty_err);
2137 if (spawn_config == NULL)
2138 goto failed;
2139
2140 channel = add_channel();
2141 if (channel == NULL)
2142 goto failed;
2143
2144 job = job_alloc();
2145 if (job == NULL)
2146 goto failed;
2147
2148 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2149 &child_thread_handle, &error, &winpty_err))
2150 goto failed;
2151
2152 channel_set_pipes(channel,
2153 (sock_T) CreateFileW(
2154 winpty_conin_name(term->tl_winpty),
2155 GENERIC_WRITE, 0, NULL,
2156 OPEN_EXISTING, 0, NULL),
2157 (sock_T) CreateFileW(
2158 winpty_conout_name(term->tl_winpty),
2159 GENERIC_READ, 0, NULL,
2160 OPEN_EXISTING, 0, NULL),
2161 (sock_T) CreateFileW(
2162 winpty_conerr_name(term->tl_winpty),
2163 GENERIC_READ, 0, NULL,
2164 OPEN_EXISTING, 0, NULL));
2165
2166 jo = CreateJobObject(NULL, NULL);
2167 if (jo == NULL)
2168 goto failed;
2169
2170 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002171 {
2172 /* Failed, switch the way to terminate process with TerminateProcess. */
2173 CloseHandle(jo);
2174 jo = NULL;
2175 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002176
2177 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002178 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002179
2180 create_vterm(term, rows, cols);
2181
2182 setup_job_options(&opt, rows, cols);
2183 channel_set_job(channel, job, &opt);
2184
2185 job->jv_channel = channel;
2186 job->jv_proc_info.hProcess = child_process_handle;
2187 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2188 job->jv_job_object = jo;
2189 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002190 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002191 term->tl_job = job;
2192
2193 return OK;
2194
2195failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002196 if (spawn_config != NULL)
2197 winpty_spawn_config_free(spawn_config);
2198 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002199 if (channel != NULL)
2200 channel_clear(channel);
2201 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002202 {
2203 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002204 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002205 }
2206 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002207 if (jo != NULL)
2208 CloseHandle(jo);
2209 if (term->tl_winpty != NULL)
2210 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002211 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002212 if (term->tl_winpty_config != NULL)
2213 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002214 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002215 if (winpty_err != NULL)
2216 {
2217 char_u *msg = utf16_to_enc(
2218 (short_u *)winpty_error_msg(winpty_err), NULL);
2219
2220 EMSG(msg);
2221 winpty_error_free(winpty_err);
2222 }
2223 return FAIL;
2224}
2225
2226/*
2227 * Free the terminal emulator part of "term".
2228 */
2229 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002230term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002231{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002232 if (term->tl_winpty != NULL)
2233 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002234 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002235 if (term->tl_winpty_config != NULL)
2236 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002237 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002238 if (term->tl_vterm != NULL)
2239 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002240 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002241}
2242
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002243/*
2244 * Request size to terminal.
2245 */
2246 static void
2247term_report_winsize(term_T *term, int rows, int cols)
2248{
2249 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2250}
2251
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002252# else
2253
2254/**************************************
2255 * 3. Unix-like implementation.
2256 */
2257
2258/*
2259 * Create a new terminal of "rows" by "cols" cells.
2260 * Start job for "cmd".
2261 * Store the pointers in "term".
2262 * Return OK or FAIL.
2263 */
2264 static int
2265term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2266{
2267 typval_T argvars[2];
2268 jobopt_T opt;
2269
2270 create_vterm(term, rows, cols);
2271
2272 argvars[0].v_type = VAR_STRING;
2273 argvars[0].vval.v_string = cmd;
2274 setup_job_options(&opt, rows, cols);
2275 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002276 if (term->tl_job != NULL)
2277 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002278
Bram Moolenaar61a66052017-07-22 18:39:00 +02002279 return term->tl_job != NULL
2280 && term->tl_job->jv_channel != NULL
2281 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002282}
2283
2284/*
2285 * Free the terminal emulator part of "term".
2286 */
2287 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002288term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002289{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002290 if (term->tl_vterm != NULL)
2291 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002292 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002293}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002294
2295/*
2296 * Request size to terminal.
2297 */
2298 static void
2299term_report_winsize(term_T *term, int rows, int cols)
2300{
2301 /* Use an ioctl() to report the new window size to the job. */
2302 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2303 {
2304 int fd = -1;
2305 int part;
2306
2307 for (part = PART_OUT; part < PART_COUNT; ++part)
2308 {
2309 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2310 if (isatty(fd))
2311 break;
2312 }
2313 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2314 mch_stop_job(term->tl_job, (char_u *)"winch");
2315 }
2316}
2317
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002318# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002319
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002320#endif /* FEAT_TERMINAL */