blob: b0ba8c1902c44cfa40317886db71076b17dd0763 [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 * - Problem with statusline (Zyx, Christian)
40 * - Make CTRL-W "" paste register content to the job?
41 * - in bash mouse clicks are inserting characters.
42 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaard85f2712017-07-28 21:51:57 +020043 * - For the scrollback buffer store lines in the buffer, only attributes in
44 * tl_scrollback.
Bram Moolenaar423802d2017-07-30 16:52:24 +020045 * - Add term_status(): "" if not a terminal, "running" if job running,
46 * "finished" if finished, "running,vim" when job is running and in
47 * Terminal mode, "running,vim,pending" when job output is pending.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020048 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020049 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020050 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
51 * job finishes).
52 * - add option values to the command:
53 * :term <24x80> <close> vim notes.txt
Bram Moolenaard85f2712017-07-28 21:51:57 +020054 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020055 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020056 * - add a character in :ls output
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 Moolenaare4f25e42017-07-07 11:54:15 +0200122 pos_T tl_cursor;
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 Moolenaar4cc93dc2017-07-26 21:49:37 +0200361 if (gui.in_use && term->tl_cursor_visible)
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 Moolenaard85f2712017-07-28 21:51:57 +0200559 * Return TRUE if the job for "buf" is still running.
560 */
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();
783 --no_mapping;
784 --allow_keys;
785 return c;
786}
787
788/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200789 * Send keys to terminal.
790 */
791 static int
792send_keys_to_term(term_T *term, int c, int typed)
793{
794 char msg[KEY_BUF_LEN];
795 size_t len;
796 static int mouse_was_outside = FALSE;
797 int dragging_outside = FALSE;
798
799 /* Catch keys that need to be handled as in Normal mode. */
800 switch (c)
801 {
802 case NUL:
803 case K_ZERO:
804 if (typed)
805 stuffcharReadbuff(c);
806 return FAIL;
807
808 case K_IGNORE:
809 return FAIL;
810
811 case K_LEFTDRAG:
812 case K_MIDDLEDRAG:
813 case K_RIGHTDRAG:
814 case K_X1DRAG:
815 case K_X2DRAG:
816 dragging_outside = mouse_was_outside;
817 /* FALLTHROUGH */
818 case K_LEFTMOUSE:
819 case K_LEFTMOUSE_NM:
820 case K_LEFTRELEASE:
821 case K_LEFTRELEASE_NM:
822 case K_MIDDLEMOUSE:
823 case K_MIDDLERELEASE:
824 case K_RIGHTMOUSE:
825 case K_RIGHTRELEASE:
826 case K_X1MOUSE:
827 case K_X1RELEASE:
828 case K_X2MOUSE:
829 case K_X2RELEASE:
830 if (mouse_row < W_WINROW(curwin)
831 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
832 || mouse_col < W_WINCOL(curwin)
833 || mouse_col >= W_ENDCOL(curwin)
834 || dragging_outside)
835 {
836 /* click outside the current window */
837 if (typed)
838 {
839 stuffcharReadbuff(c);
840 mouse_was_outside = TRUE;
841 }
842 return FAIL;
843 }
844 }
845 if (typed)
846 mouse_was_outside = FALSE;
847
848 /* Convert the typed key to a sequence of bytes for the job. */
849 len = term_convert_key(term, c, msg);
850 if (len > 0)
851 /* TODO: if FAIL is returned, stop? */
852 channel_send(term->tl_job->jv_channel, PART_IN,
853 (char_u *)msg, (int)len, NULL);
854
855 return OK;
856}
857
858/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200859 * Returns TRUE if the current window contains a terminal and we are sending
860 * keys to the job.
861 */
862 int
863term_use_loop()
864{
865 term_T *term = curbuf->b_term;
866
867 return term != NULL
868 && !term->tl_terminal_mode
869 && term->tl_vterm != NULL
870 && term_job_running(term);
871}
872
873/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200874 * Wait for input and send it to the job.
875 * Return when the start of a CTRL-W command is typed or anything else that
876 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200877 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
878 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200879 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200880 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200881terminal_loop(void)
882{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200883 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200884 int termkey = 0;
885
886 if (*curwin->w_p_tk != NUL)
887 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200888
889 for (;;)
890 {
891 /* TODO: skip screen update when handling a sequence of keys. */
892 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200893 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200894
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200895 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200896 if (curbuf->b_term->tl_vterm == NULL
897 || !term_job_running(curbuf->b_term))
898 /* job finished while waiting for a character */
899 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200900
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200901 if (c == (termkey == 0 ? Ctrl_W : termkey))
902 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200903#ifdef FEAT_CMDL_INFO
904 if (add_to_showcmd(c))
905 out_flush();
906#endif
907 c = term_vgetc();
908#ifdef FEAT_CMDL_INFO
909 clear_showcmd();
910#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200911 if (curbuf->b_term->tl_vterm == NULL
912 || !term_job_running(curbuf->b_term))
913 /* job finished while waiting for a character */
914 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200915
916 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200917 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200918 /* "CTRL-W .": send CTRL-W to the job */
919 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200920 }
921 else if (termkey == 0 && c == 'N')
922 {
923 term_enter_terminal_mode();
924 return FAIL;
925 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200926 else if (termkey == 0 || c != termkey)
927 {
928 stuffcharReadbuff(Ctrl_W);
929 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200930 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200931 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200932 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200933 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
934 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200935 }
Bram Moolenaard85f2712017-07-28 21:51:57 +0200936 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200937}
938
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200939/*
940 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200941 * This updates the title and status, but does not close the vter, because
942 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200943 */
944 void
945term_job_ended(job_T *job)
946{
947 term_T *term;
948 int did_one = FALSE;
949
950 for (term = first_term; term != NULL; term = term->tl_next)
951 if (term->tl_job == job)
952 {
953 vim_free(term->tl_title);
954 term->tl_title = NULL;
955 vim_free(term->tl_status_text);
956 term->tl_status_text = NULL;
957 redraw_buf_and_status_later(term->tl_buffer, VALID);
958 did_one = TRUE;
959 }
960 if (did_one)
961 redraw_statuslines();
962 if (curbuf->b_term != NULL)
963 {
964 if (curbuf->b_term->tl_job == job)
965 maketitle();
966 update_cursor(curbuf->b_term, TRUE);
967 }
968}
969
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200970 static void
971position_cursor(win_T *wp, VTermPos *pos)
972{
973 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
974 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
975}
976
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200977 static void
978may_toggle_cursor(term_T *term)
979{
980 if (curbuf == term->tl_buffer)
981 {
982 if (term->tl_cursor_visible)
983 cursor_on();
984 else
985 cursor_off();
986 }
987}
988
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200989 static int
990handle_damage(VTermRect rect, void *user)
991{
992 term_T *term = (term_T *)user;
993
994 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
995 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
996 redraw_buf_later(term->tl_buffer, NOT_VALID);
997 return 1;
998}
999
1000 static int
1001handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1002{
1003 term_T *term = (term_T *)user;
1004
1005 /* TODO */
1006 redraw_buf_later(term->tl_buffer, NOT_VALID);
1007 return 1;
1008}
1009
1010 static int
1011handle_movecursor(
1012 VTermPos pos,
1013 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001014 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001015 void *user)
1016{
1017 term_T *term = (term_T *)user;
1018 win_T *wp;
1019 int is_current = FALSE;
1020
1021 FOR_ALL_WINDOWS(wp)
1022 {
1023 if (wp->w_buffer == term->tl_buffer)
1024 {
1025 position_cursor(wp, &pos);
1026 if (wp == curwin)
1027 is_current = TRUE;
1028 }
1029 }
1030
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001031 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001032 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001033 {
1034 may_toggle_cursor(term);
1035 update_cursor(term, TRUE);
1036 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001037
1038 return 1;
1039}
1040
Bram Moolenaar21554412017-07-24 21:44:43 +02001041 static int
1042handle_settermprop(
1043 VTermProp prop,
1044 VTermValue *value,
1045 void *user)
1046{
1047 term_T *term = (term_T *)user;
1048
1049 switch (prop)
1050 {
1051 case VTERM_PROP_TITLE:
1052 vim_free(term->tl_title);
1053 term->tl_title = vim_strsave((char_u *)value->string);
1054 vim_free(term->tl_status_text);
1055 term->tl_status_text = NULL;
1056 if (term == curbuf->b_term)
1057 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001058 break;
1059
1060 case VTERM_PROP_CURSORVISIBLE:
1061 term->tl_cursor_visible = value->boolean;
1062 may_toggle_cursor(term);
1063 out_flush();
1064 break;
1065
Bram Moolenaar21554412017-07-24 21:44:43 +02001066 default:
1067 break;
1068 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001069 /* Always return 1, otherwise vterm doesn't store the value internally. */
1070 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001071}
1072
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001073/*
1074 * The job running in the terminal resized the terminal.
1075 */
1076 static int
1077handle_resize(int rows, int cols, void *user)
1078{
1079 term_T *term = (term_T *)user;
1080 win_T *wp;
1081
1082 term->tl_rows = rows;
1083 term->tl_cols = cols;
1084 FOR_ALL_WINDOWS(wp)
1085 {
1086 if (wp->w_buffer == term->tl_buffer)
1087 {
1088 win_setheight_win(rows, wp);
1089 win_setwidth_win(cols, wp);
1090 }
1091 }
1092
1093 redraw_buf_later(term->tl_buffer, NOT_VALID);
1094 return 1;
1095}
1096
Bram Moolenaard85f2712017-07-28 21:51:57 +02001097/*
1098 * Handle a line that is pushed off the top of the screen.
1099 */
1100 static int
1101handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1102{
1103 term_T *term = (term_T *)user;
1104
1105 /* TODO: Limit the number of lines that are stored. */
1106 /* TODO: put the text in the buffer. */
1107 if (ga_grow(&term->tl_scrollback, 1) == OK)
1108 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001109 VTermScreenCell *p = NULL;
1110 int len = 0;
1111 int i;
1112 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001113
1114 /* do not store empty cells at the end */
1115 for (i = 0; i < cols; ++i)
1116 if (cells[i].chars[0] != 0)
1117 len = i + 1;
1118
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001119 if (len > 0)
1120 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001121 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001122 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001123
1124 line = (sb_line_T *)term->tl_scrollback.ga_data
1125 + term->tl_scrollback.ga_len;
1126 line->sb_cols = len;
1127 line->sb_cells = p;
1128 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001129 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001130
1131 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001132 }
1133 return 0; /* ignored */
1134}
1135
Bram Moolenaar21554412017-07-24 21:44:43 +02001136static VTermScreenCallbacks screen_callbacks = {
1137 handle_damage, /* damage */
1138 handle_moverect, /* moverect */
1139 handle_movecursor, /* movecursor */
1140 handle_settermprop, /* settermprop */
1141 NULL, /* bell */
1142 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001143 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001144 NULL /* sb_popline */
1145};
1146
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001147/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001148 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001149 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001150 */
1151 void
1152term_channel_closed(channel_T *ch)
1153{
1154 term_T *term;
1155 int did_one = FALSE;
1156
1157 for (term = first_term; term != NULL; term = term->tl_next)
1158 if (term->tl_job == ch->ch_job)
1159 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001160 term->tl_channel_closed = TRUE;
1161
Bram Moolenaard85f2712017-07-28 21:51:57 +02001162 vim_free(term->tl_title);
1163 term->tl_title = NULL;
1164 vim_free(term->tl_status_text);
1165 term->tl_status_text = NULL;
1166
Bram Moolenaar423802d2017-07-30 16:52:24 +02001167 /* Unless in Terminal-Normal mode: clear the vterm. */
1168 if (!term->tl_terminal_mode)
1169 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001170
1171 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1172 did_one = TRUE;
1173 }
1174 if (did_one)
1175 {
1176 redraw_statuslines();
1177
1178 /* Need to break out of vgetc(). */
1179 ins_char_typebuf(K_IGNORE);
1180
1181 if (curbuf->b_term != NULL)
1182 {
1183 if (curbuf->b_term->tl_job == ch->ch_job)
1184 maketitle();
1185 update_cursor(curbuf->b_term, TRUE);
1186 }
1187 }
1188}
1189
1190/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001191 * Reverse engineer the RGB value into a cterm color index.
1192 * First color is 1. Return 0 if no match found.
1193 */
1194 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001195color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001196{
1197 int red = color->red;
1198 int blue = color->blue;
1199 int green = color->green;
1200
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001201 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001202 if (red == 0)
1203 {
1204 if (green == 0)
1205 {
1206 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001207 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001208 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001209 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001210 }
1211 else if (green == 224)
1212 {
1213 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001214 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001215 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001216 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001217 }
1218 }
1219 else if (red == 224)
1220 {
1221 if (green == 0)
1222 {
1223 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001224 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001225 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001226 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001227 }
1228 else if (green == 224)
1229 {
1230 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001231 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001232 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001233 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001234 }
1235 }
1236 else if (red == 128)
1237 {
1238 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001239 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001240 }
1241 else if (red == 255)
1242 {
1243 if (green == 64)
1244 {
1245 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001246 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001247 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001248 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001249 }
1250 else if (green == 255)
1251 {
1252 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001253 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001254 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001255 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001256 }
1257 }
1258 else if (red == 64)
1259 {
1260 if (green == 64)
1261 {
1262 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001263 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001264 }
1265 else if (green == 255)
1266 {
1267 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001268 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001269 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001270 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001271 }
1272 }
1273 if (t_colors >= 256)
1274 {
1275 if (red == blue && red == green)
1276 {
1277 /* 24-color greyscale */
1278 static int cutoff[23] = {
1279 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1280 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1281 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1282 int i;
1283
1284 for (i = 0; i < 23; ++i)
1285 if (red < cutoff[i])
1286 return i + 233;
1287 return 256;
1288 }
1289
1290 /* 216-color cube */
1291 return 17 + ((red + 25) / 0x33) * 36
1292 + ((green + 25) / 0x33) * 6
1293 + (blue + 25) / 0x33;
1294 }
1295 return 0;
1296}
1297
1298/*
1299 * Convert the attributes of a vterm cell into an attribute index.
1300 */
1301 static int
1302cell2attr(VTermScreenCell *cell)
1303{
1304 int attr = 0;
1305
1306 if (cell->attrs.bold)
1307 attr |= HL_BOLD;
1308 if (cell->attrs.underline)
1309 attr |= HL_UNDERLINE;
1310 if (cell->attrs.italic)
1311 attr |= HL_ITALIC;
1312 if (cell->attrs.strike)
1313 attr |= HL_STANDOUT;
1314 if (cell->attrs.reverse)
1315 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001316
1317#ifdef FEAT_GUI
1318 if (gui.in_use)
1319 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001320 guicolor_T fg, bg;
1321
1322 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1323 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1324 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001325 }
1326 else
1327#endif
1328#ifdef FEAT_TERMGUICOLORS
1329 if (p_tgc)
1330 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001331 guicolor_T fg, bg;
1332
1333 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1334 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1335
1336 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001337 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001338 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001339#endif
1340 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001341 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1342 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001343 }
1344 return 0;
1345}
1346
1347/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001348 * Called to update the window that contains a terminal.
1349 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001350 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001351 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001352term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001353{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001354 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001355 VTerm *vterm;
1356 VTermScreen *screen;
1357 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001358 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001359
Bram Moolenaar423802d2017-07-30 16:52:24 +02001360 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001361 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001362
Bram Moolenaard85f2712017-07-28 21:51:57 +02001363 vterm = term->tl_vterm;
1364 screen = vterm_obtain_screen(vterm);
1365 state = vterm_obtain_state(vterm);
1366
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001367 /*
1368 * If the window was resized a redraw will be triggered and we get here.
1369 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1370 */
1371 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1372 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001373 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001374 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1375 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1376 win_T *twp;
1377
1378 FOR_ALL_WINDOWS(twp)
1379 {
1380 /* When more than one window shows the same terminal, use the
1381 * smallest size. */
1382 if (twp->w_buffer == term->tl_buffer)
1383 {
1384 if (!term->tl_rows_fixed && rows > twp->w_height)
1385 rows = twp->w_height;
1386 if (!term->tl_cols_fixed && cols > twp->w_width)
1387 cols = twp->w_width;
1388 }
1389 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001390
1391 vterm_set_size(vterm, rows, cols);
1392 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1393 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001394 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001395 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001396
1397 /* The cursor may have been moved when resizing. */
1398 vterm_state_get_cursorpos(state, &pos);
1399 position_cursor(wp, &pos);
1400
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001401 /* TODO: Only redraw what changed. */
1402 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001403 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001404 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001405 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001406
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001407 if (pos.row < term->tl_rows)
1408 {
1409 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001410 {
1411 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001412 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001413
Bram Moolenaareeac6772017-07-23 15:48:37 +02001414 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1415 vim_memset(&cell, 0, sizeof(cell));
1416
1417 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001418 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001419 if (c == NUL)
1420 {
1421 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001422#if defined(FEAT_MBYTE)
1423 if (enc_utf8)
1424 ScreenLinesUC[off] = NUL;
1425#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001426 }
1427 else
1428 {
1429#if defined(FEAT_MBYTE)
1430 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001431 {
1432 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001433 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001434 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001435 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001436 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001437 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001438 if (enc_utf8)
1439 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001440 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001441#else
1442 ScreenLines[off] = c;
1443#endif
1444 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001445 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001446
1447 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001448 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001449 if (cell.width == 2)
1450 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001451 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001452#if defined(FEAT_MBYTE)
1453 if (enc_utf8)
1454 ScreenLinesUC[off] = NUL;
1455#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001456 ++pos.col;
1457 ++off;
1458 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001459 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001460 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001461 else
1462 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001463
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001464 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1465 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001466 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001467
1468 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001469}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001470
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001471/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001472 * Return TRUE if "wp" is a terminal window where the job has finished.
1473 */
1474 int
1475term_is_finished(buf_T *buf)
1476{
1477 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1478}
1479
1480/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001481 * Return TRUE if "wp" is a terminal window where the job has finished or we
1482 * are in Terminal-Normal mode.
1483 */
1484 int
1485term_show_buffer(buf_T *buf)
1486{
1487 term_T *term = buf->b_term;
1488
1489 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1490}
1491
1492/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001493 * The current buffer is going to be changed. If there is terminal
1494 * highlighting remove it now.
1495 */
1496 void
1497term_change_in_curbuf(void)
1498{
1499 term_T *term = curbuf->b_term;
1500
1501 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1502 {
1503 free_scrollback(term);
1504 redraw_buf_later(term->tl_buffer, NOT_VALID);
1505 }
1506}
1507
1508/*
1509 * Get the screen attribute for a position in the buffer.
1510 */
1511 int
1512term_get_attr(buf_T *buf, linenr_T lnum, int col)
1513{
1514 term_T *term = buf->b_term;
1515 sb_line_T *line;
1516
Bram Moolenaar70229f92017-07-29 16:01:53 +02001517 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001518 return 0;
1519 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1520 if (col >= line->sb_cols)
1521 return 0;
1522 return cell2attr(line->sb_cells + col);
1523}
1524
1525/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001526 * Set job options common for Unix and MS-Windows.
1527 */
1528 static void
1529setup_job_options(jobopt_T *opt, int rows, int cols)
1530{
1531 clear_job_options(opt);
1532 opt->jo_mode = MODE_RAW;
1533 opt->jo_out_mode = MODE_RAW;
1534 opt->jo_err_mode = MODE_RAW;
1535 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001536
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001537 opt->jo_io[PART_OUT] = JIO_BUFFER;
1538 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001539 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1540
1541 opt->jo_modifiable[PART_OUT] = 0;
1542 opt->jo_modifiable[PART_ERR] = 0;
1543 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1544
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001545 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1546 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001547 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001548 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1549
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001550 opt->jo_term_rows = rows;
1551 opt->jo_term_cols = cols;
1552}
1553
1554/*
1555 * Create a new vterm and initialize it.
1556 */
1557 static void
1558create_vterm(term_T *term, int rows, int cols)
1559{
1560 VTerm *vterm;
1561 VTermScreen *screen;
1562
1563 vterm = vterm_new(rows, cols);
1564 term->tl_vterm = vterm;
1565 screen = vterm_obtain_screen(vterm);
1566 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1567 /* TODO: depends on 'encoding'. */
1568 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001569
1570 /* Vterm uses a default black background. Set it to white when
1571 * 'background' is "light". */
1572 if (*p_bg == 'l')
1573 {
1574 VTermColor fg, bg;
1575
1576 fg.red = fg.green = fg.blue = 0;
1577 bg.red = bg.green = bg.blue = 255;
1578 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1579 }
1580
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001581 /* Required to initialize most things. */
1582 vterm_screen_reset(screen, 1 /* hard */);
1583}
1584
Bram Moolenaar21554412017-07-24 21:44:43 +02001585/*
1586 * Return the text to show for the buffer name and status.
1587 */
1588 char_u *
1589term_get_status_text(term_T *term)
1590{
1591 if (term->tl_status_text == NULL)
1592 {
1593 char_u *txt;
1594 size_t len;
1595
Bram Moolenaar423802d2017-07-30 16:52:24 +02001596 if (term->tl_terminal_mode)
1597 {
1598 if (term_job_running(term))
1599 txt = (char_u *)_("Terminal");
1600 else
1601 txt = (char_u *)_("Terminal-finished");
1602 }
1603 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001604 txt = term->tl_title;
1605 else if (term_job_running(term))
1606 txt = (char_u *)_("running");
1607 else
1608 txt = (char_u *)_("finished");
1609 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001610 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001611 if (term->tl_status_text != NULL)
1612 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1613 term->tl_buffer->b_fname, txt);
1614 }
1615 return term->tl_status_text;
1616}
1617
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001618/*
1619 * Mark references in jobs of terminals.
1620 */
1621 int
1622set_ref_in_term(int copyID)
1623{
1624 int abort = FALSE;
1625 term_T *term;
1626 typval_T tv;
1627
1628 for (term = first_term; term != NULL; term = term->tl_next)
1629 if (term->tl_job != NULL)
1630 {
1631 tv.v_type = VAR_JOB;
1632 tv.vval.v_job = term->tl_job;
1633 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1634 }
1635 return abort;
1636}
1637
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001638/*
1639 * "term_getattr(attr, name)" function
1640 */
1641 void
1642f_term_getattr(typval_T *argvars, typval_T *rettv)
1643{
1644 int attr;
1645 size_t i;
1646 char_u *name;
1647
1648 static struct {
1649 char *name;
1650 int attr;
1651 } attrs[] = {
1652 {"bold", HL_BOLD},
1653 {"italic", HL_ITALIC},
1654 {"underline", HL_UNDERLINE},
1655 {"strike", HL_STANDOUT},
1656 {"reverse", HL_INVERSE},
1657 };
1658
1659 attr = get_tv_number(&argvars[0]);
1660 name = get_tv_string_chk(&argvars[1]);
1661 if (name == NULL)
1662 return;
1663
1664 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1665 if (STRCMP(name, attrs[i].name) == 0)
1666 {
1667 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1668 break;
1669 }
1670}
1671
1672/*
1673 * Get the buffer from the first argument in "argvars".
1674 * Returns NULL when the buffer is not for a terminal window.
1675 */
1676 static buf_T *
1677term_get_buf(typval_T *argvars)
1678{
1679 buf_T *buf;
1680
1681 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1682 ++emsg_off;
1683 buf = get_buf_tv(&argvars[0], FALSE);
1684 --emsg_off;
1685 if (buf->b_term == NULL)
1686 return NULL;
1687 return buf;
1688}
1689
1690/*
1691 * "term_getjob(buf)" function
1692 */
1693 void
1694f_term_getjob(typval_T *argvars, typval_T *rettv)
1695{
1696 buf_T *buf = term_get_buf(argvars);
1697
1698 rettv->v_type = VAR_JOB;
1699 rettv->vval.v_job = NULL;
1700 if (buf == NULL)
1701 return;
1702
1703 rettv->vval.v_job = buf->b_term->tl_job;
1704 if (rettv->vval.v_job != NULL)
1705 ++rettv->vval.v_job->jv_refcount;
1706}
1707
1708/*
1709 * "term_getline(buf, row)" function
1710 */
1711 void
1712f_term_getline(typval_T *argvars, typval_T *rettv)
1713{
1714 buf_T *buf = term_get_buf(argvars);
1715 term_T *term;
1716 int row;
1717
1718 rettv->v_type = VAR_STRING;
1719 if (buf == NULL)
1720 return;
1721 term = buf->b_term;
1722 row = (int)get_tv_number(&argvars[1]);
1723
1724 if (term->tl_vterm == NULL)
1725 {
1726 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1727
1728 /* vterm is finished, get the text from the buffer */
1729 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1730 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1731 }
1732 else
1733 {
1734 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1735 VTermRect rect;
1736 int len;
1737 char_u *p;
1738
1739 len = term->tl_cols * MB_MAXBYTES + 1;
1740 p = alloc(len);
1741 if (p == NULL)
1742 return;
1743 rettv->vval.v_string = p;
1744
1745 rect.start_col = 0;
1746 rect.end_col = term->tl_cols;
1747 rect.start_row = row;
1748 rect.end_row = row + 1;
1749 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1750 }
1751}
1752
1753/*
1754 * "term_getsize(buf)" function
1755 */
1756 void
1757f_term_getsize(typval_T *argvars, typval_T *rettv)
1758{
1759 buf_T *buf = term_get_buf(argvars);
1760 list_T *l;
1761
1762 if (rettv_list_alloc(rettv) == FAIL)
1763 return;
1764 if (buf == NULL)
1765 return;
1766
1767 l = rettv->vval.v_list;
1768 list_append_number(l, buf->b_term->tl_rows);
1769 list_append_number(l, buf->b_term->tl_cols);
1770}
1771
1772/*
1773 * "term_list()" function
1774 */
1775 void
1776f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1777{
1778 term_T *tp;
1779 list_T *l;
1780
1781 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1782 return;
1783
1784 l = rettv->vval.v_list;
1785 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1786 if (tp != NULL && tp->tl_buffer != NULL)
1787 if (list_append_number(l,
1788 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1789 return;
1790}
1791
1792/*
1793 * "term_scrape(buf, row)" function
1794 */
1795 void
1796f_term_scrape(typval_T *argvars, typval_T *rettv)
1797{
1798 buf_T *buf = term_get_buf(argvars);
1799 VTermScreen *screen = NULL;
1800 VTermPos pos;
1801 list_T *l;
1802 term_T *term;
1803
1804 if (rettv_list_alloc(rettv) == FAIL)
1805 return;
1806 if (buf == NULL)
1807 return;
1808 term = buf->b_term;
1809 if (term->tl_vterm != NULL)
1810 screen = vterm_obtain_screen(term->tl_vterm);
1811
1812 l = rettv->vval.v_list;
1813 pos.row = (int)get_tv_number(&argvars[1]);
1814 for (pos.col = 0; pos.col < term->tl_cols; )
1815 {
1816 dict_T *dcell;
1817 VTermScreenCell cell;
1818 char_u rgb[8];
1819 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1820 int off = 0;
1821 int i;
1822
1823 if (screen == NULL)
1824 {
1825 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1826 sb_line_T *line;
1827
1828 /* vterm has finished, get the cell from scrollback */
1829 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1830 break;
1831 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1832 if (pos.col >= line->sb_cols)
1833 break;
1834 cell = line->sb_cells[pos.col];
1835 }
1836 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1837 break;
1838 dcell = dict_alloc();
1839 list_append_dict(l, dcell);
1840
1841 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1842 {
1843 if (cell.chars[i] == 0)
1844 break;
1845 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1846 }
1847 mbs[off] = NUL;
1848 dict_add_nr_str(dcell, "chars", 0, mbs);
1849
1850 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1851 cell.fg.red, cell.fg.green, cell.fg.blue);
1852 dict_add_nr_str(dcell, "fg", 0, rgb);
1853 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1854 cell.bg.red, cell.bg.green, cell.bg.blue);
1855 dict_add_nr_str(dcell, "bg", 0, rgb);
1856
1857 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
1858 dict_add_nr_str(dcell, "width", cell.width, NULL);
1859
1860 ++pos.col;
1861 if (cell.width == 2)
1862 ++pos.col;
1863 }
1864}
1865
1866/*
1867 * "term_sendkeys(buf, keys)" function
1868 */
1869 void
1870f_term_sendkeys(typval_T *argvars, typval_T *rettv)
1871{
1872 buf_T *buf = term_get_buf(argvars);
1873 char_u *msg;
1874 term_T *term;
1875
1876 rettv->v_type = VAR_UNKNOWN;
1877 if (buf == NULL)
1878 return;
1879
1880 msg = get_tv_string_chk(&argvars[1]);
1881 if (msg == NULL)
1882 return;
1883 term = buf->b_term;
1884 if (term->tl_vterm == NULL)
1885 return;
1886
1887 while (*msg != NUL)
1888 {
1889 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
1890 msg += MB_PTR2LEN(msg);
1891 }
1892
1893 /* TODO: only update once in a while. */
1894 update_screen(0);
1895 if (buf == curbuf)
1896 update_cursor(term, TRUE);
1897}
1898
1899/*
1900 * "term_start(command, options)" function
1901 */
1902 void
1903f_term_start(typval_T *argvars, typval_T *rettv)
1904{
1905 char_u *cmd = get_tv_string_chk(&argvars[0]);
1906 exarg_T ea;
1907
1908 if (cmd == NULL)
1909 return;
1910 ea.arg = cmd;
1911 ex_terminal(&ea);
1912
1913 if (curbuf->b_term != NULL)
1914 rettv->vval.v_number = curbuf->b_fnum;
1915}
1916
1917/*
1918 * "term_wait" function
1919 */
1920 void
1921f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
1922{
1923 buf_T *buf = term_get_buf(argvars);
1924
1925 if (buf == NULL)
1926 return;
1927
1928 /* Get the job status, this will detect a job that finished. */
1929 if (buf->b_term->tl_job != NULL)
1930 (void)job_status(buf->b_term->tl_job);
1931
1932 /* Check for any pending channel I/O. */
1933 vpeekc_any();
1934 ui_delay(10L, FALSE);
1935
1936 /* Flushing messages on channels is hopefully sufficient.
1937 * TODO: is there a better way? */
1938 parse_queued_messages();
1939}
1940
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001941# ifdef WIN3264
1942
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001943/**************************************
1944 * 2. MS-Windows implementation.
1945 */
1946
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001947#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1948#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1949
Bram Moolenaar8a773062017-07-24 22:29:21 +02001950void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001951void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001952void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001953BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1954void (*winpty_config_set_initial_size)(void*, int, int);
1955LPCWSTR (*winpty_conin_name)(void*);
1956LPCWSTR (*winpty_conout_name)(void*);
1957LPCWSTR (*winpty_conerr_name)(void*);
1958void (*winpty_free)(void*);
1959void (*winpty_config_free)(void*);
1960void (*winpty_spawn_config_free)(void*);
1961void (*winpty_error_free)(void*);
1962LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001963BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001964
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001965#define WINPTY_DLL "winpty.dll"
1966
1967static HINSTANCE hWinPtyDLL = NULL;
1968
1969 int
1970dyn_winpty_init(void)
1971{
1972 int i;
1973 static struct
1974 {
1975 char *name;
1976 FARPROC *ptr;
1977 } winpty_entry[] =
1978 {
1979 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1980 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1981 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1982 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1983 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1984 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1985 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1986 {"winpty_free", (FARPROC*)&winpty_free},
1987 {"winpty_open", (FARPROC*)&winpty_open},
1988 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1989 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1990 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1991 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001992 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001993 {NULL, NULL}
1994 };
1995
1996 /* No need to initialize twice. */
1997 if (hWinPtyDLL)
1998 return 1;
1999 /* Load winpty.dll */
2000 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2001 if (!hWinPtyDLL)
2002 {
2003 EMSG2(_(e_loadlib), WINPTY_DLL);
2004 return 0;
2005 }
2006 for (i = 0; winpty_entry[i].name != NULL
2007 && winpty_entry[i].ptr != NULL; ++i)
2008 {
2009 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2010 winpty_entry[i].name)) == NULL)
2011 {
2012 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2013 return 0;
2014 }
2015 }
2016
2017 return 1;
2018}
2019
2020/*
2021 * Create a new terminal of "rows" by "cols" cells.
2022 * Store a reference in "term".
2023 * Return OK or FAIL.
2024 */
2025 static int
2026term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2027{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002028 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002029 channel_T *channel = NULL;
2030 job_T *job = NULL;
2031 jobopt_T opt;
2032 DWORD error;
2033 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2034 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002035 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002036
2037 if (!dyn_winpty_init())
2038 return FAIL;
2039
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002040 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002041 if (p == NULL)
2042 return FAIL;
2043
2044 job = job_alloc();
2045 if (job == NULL)
2046 goto failed;
2047
2048 channel = add_channel();
2049 if (channel == NULL)
2050 goto failed;
2051
2052 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2053 if (term->tl_winpty_config == NULL)
2054 goto failed;
2055
2056 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2057 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2058 if (term->tl_winpty == NULL)
2059 goto failed;
2060
2061 spawn_config = winpty_spawn_config_new(
2062 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2063 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2064 NULL,
2065 p,
2066 NULL,
2067 NULL,
2068 &winpty_err);
2069 if (spawn_config == NULL)
2070 goto failed;
2071
2072 channel = add_channel();
2073 if (channel == NULL)
2074 goto failed;
2075
2076 job = job_alloc();
2077 if (job == NULL)
2078 goto failed;
2079
2080 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2081 &child_thread_handle, &error, &winpty_err))
2082 goto failed;
2083
2084 channel_set_pipes(channel,
2085 (sock_T) CreateFileW(
2086 winpty_conin_name(term->tl_winpty),
2087 GENERIC_WRITE, 0, NULL,
2088 OPEN_EXISTING, 0, NULL),
2089 (sock_T) CreateFileW(
2090 winpty_conout_name(term->tl_winpty),
2091 GENERIC_READ, 0, NULL,
2092 OPEN_EXISTING, 0, NULL),
2093 (sock_T) CreateFileW(
2094 winpty_conerr_name(term->tl_winpty),
2095 GENERIC_READ, 0, NULL,
2096 OPEN_EXISTING, 0, NULL));
2097
2098 jo = CreateJobObject(NULL, NULL);
2099 if (jo == NULL)
2100 goto failed;
2101
2102 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002103 {
2104 /* Failed, switch the way to terminate process with TerminateProcess. */
2105 CloseHandle(jo);
2106 jo = NULL;
2107 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002108
2109 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002110 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002111
2112 create_vterm(term, rows, cols);
2113
2114 setup_job_options(&opt, rows, cols);
2115 channel_set_job(channel, job, &opt);
2116
2117 job->jv_channel = channel;
2118 job->jv_proc_info.hProcess = child_process_handle;
2119 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2120 job->jv_job_object = jo;
2121 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002122 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002123 term->tl_job = job;
2124
2125 return OK;
2126
2127failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002128 if (spawn_config != NULL)
2129 winpty_spawn_config_free(spawn_config);
2130 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002131 if (channel != NULL)
2132 channel_clear(channel);
2133 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002134 {
2135 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002136 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002137 }
2138 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002139 if (jo != NULL)
2140 CloseHandle(jo);
2141 if (term->tl_winpty != NULL)
2142 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002143 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002144 if (term->tl_winpty_config != NULL)
2145 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002146 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002147 if (winpty_err != NULL)
2148 {
2149 char_u *msg = utf16_to_enc(
2150 (short_u *)winpty_error_msg(winpty_err), NULL);
2151
2152 EMSG(msg);
2153 winpty_error_free(winpty_err);
2154 }
2155 return FAIL;
2156}
2157
2158/*
2159 * Free the terminal emulator part of "term".
2160 */
2161 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002162term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002163{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002164 if (term->tl_winpty != NULL)
2165 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002166 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002167 if (term->tl_winpty_config != NULL)
2168 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002169 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002170 if (term->tl_vterm != NULL)
2171 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002172 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002173}
2174
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002175/*
2176 * Request size to terminal.
2177 */
2178 static void
2179term_report_winsize(term_T *term, int rows, int cols)
2180{
2181 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2182}
2183
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002184# else
2185
2186/**************************************
2187 * 3. Unix-like implementation.
2188 */
2189
2190/*
2191 * Create a new terminal of "rows" by "cols" cells.
2192 * Start job for "cmd".
2193 * Store the pointers in "term".
2194 * Return OK or FAIL.
2195 */
2196 static int
2197term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2198{
2199 typval_T argvars[2];
2200 jobopt_T opt;
2201
2202 create_vterm(term, rows, cols);
2203
2204 argvars[0].v_type = VAR_STRING;
2205 argvars[0].vval.v_string = cmd;
2206 setup_job_options(&opt, rows, cols);
2207 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002208 if (term->tl_job != NULL)
2209 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002210
Bram Moolenaar61a66052017-07-22 18:39:00 +02002211 return term->tl_job != NULL
2212 && term->tl_job->jv_channel != NULL
2213 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002214}
2215
2216/*
2217 * Free the terminal emulator part of "term".
2218 */
2219 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002220term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002221{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002222 if (term->tl_vterm != NULL)
2223 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002224 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002225}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002226
2227/*
2228 * Request size to terminal.
2229 */
2230 static void
2231term_report_winsize(term_T *term, int rows, int cols)
2232{
2233 /* Use an ioctl() to report the new window size to the job. */
2234 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2235 {
2236 int fd = -1;
2237 int part;
2238
2239 for (part = PART_OUT; part < PART_COUNT; ++part)
2240 {
2241 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2242 if (isatty(fd))
2243 break;
2244 }
2245 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2246 mch_stop_job(term->tl_job, (char_u *)"winch");
2247 }
2248}
2249
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002250# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002251
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002252#endif /* FEAT_TERMINAL */