blob: 8febb8d2777b03e90f280dbb55dd715a273d250b [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 Moolenaar22aad2f2017-07-30 18:19:46 +020056 * - add term_getcursor() - return cursor position: [row, col, visible]
Bram Moolenaar21554412017-07-24 21:44:43 +020057 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020058 * - add 't' to mode()
Bram Moolenaar938783d2017-07-16 20:13:26 +020059 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaard85f2712017-07-28 21:51:57 +020060 * - when closing window and job has ended, make buffer hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020061 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020062 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020063 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020064 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020066 * - implement "term" for job_start(): more job options when starting a
67 * terminal.
Bram Moolenaar423802d2017-07-30 16:52:24 +020068 * - if the job in the terminal does not support the mouse, we can use the
69 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020070 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
71 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020072 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020073 */
74
75#include "vim.h"
76
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020077#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020079#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020080# define MIN(x,y) (x < y ? x : y)
81# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020082#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020083
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020084#include "libvterm/include/vterm.h"
85
Bram Moolenaard85f2712017-07-28 21:51:57 +020086typedef struct sb_line_S {
87 int sb_cols; /* can differ per line */
88 VTermScreenCell *sb_cells; /* allocated */
89} sb_line_T;
90
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091/* typedef term_T in structs.h */
92struct terminal_S {
93 term_T *tl_next;
94
Bram Moolenaar423802d2017-07-30 16:52:24 +020095 VTerm *tl_vterm;
96 job_T *tl_job;
97 buf_T *tl_buffer;
98
99 int tl_terminal_mode;
100 int tl_channel_closed;
101
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200102#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200103 void *tl_winpty_config;
104 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200105#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200106
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200107 /* last known vterm size */
108 int tl_rows;
109 int tl_cols;
110 /* vterm size does not follow window size */
111 int tl_rows_fixed;
112 int tl_cols_fixed;
113
Bram Moolenaar21554412017-07-24 21:44:43 +0200114 char_u *tl_title; /* NULL or allocated */
115 char_u *tl_status_text; /* NULL or allocated */
116
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200117 /* Range of screen rows to update. Zero based. */
118 int tl_dirty_row_start; /* -1 if nothing dirty */
119 int tl_dirty_row_end; /* row below last one to update */
120
Bram Moolenaard85f2712017-07-28 21:51:57 +0200121 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200122 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200123
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200124 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200125 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126};
127
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200128/*
129 * List of all active terminals.
130 */
131static term_T *first_term = NULL;
132
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200133
134#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
135#define KEY_BUF_LEN 200
136
137/*
138 * Functions with separate implementation for MS-Windows and Unix-like systems.
139 */
140static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200141static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200142static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200143
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200144/**************************************
145 * 1. Generic code for all systems.
146 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200147
148/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200149 * Determine the terminal size from 'termsize' and the current window.
150 * Assumes term->tl_rows and term->tl_cols are zero.
151 */
152 static void
153set_term_and_win_size(term_T *term)
154{
155 if (*curwin->w_p_tms != NUL)
156 {
157 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
158
159 term->tl_rows = atoi((char *)curwin->w_p_tms);
160 term->tl_cols = atoi((char *)p);
161 }
162 if (term->tl_rows == 0)
163 term->tl_rows = curwin->w_height;
164 else
165 {
166 win_setheight_win(term->tl_rows, curwin);
167 term->tl_rows_fixed = TRUE;
168 }
169 if (term->tl_cols == 0)
170 term->tl_cols = curwin->w_width;
171 else
172 {
173 win_setwidth_win(term->tl_cols, curwin);
174 term->tl_cols_fixed = TRUE;
175 }
176}
177
178/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200179 * ":terminal": open a terminal window and execute a job in it.
180 */
181 void
182ex_terminal(exarg_T *eap)
183{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200184 exarg_T split_ea;
185 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200186 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200187 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200188
189 if (check_restricted() || check_secure())
190 return;
191
192 term = (term_T *)alloc_clear(sizeof(term_T));
193 if (term == NULL)
194 return;
195 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200196 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200197 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200198
199 /* Open a new window or tab. */
200 vim_memset(&split_ea, 0, sizeof(split_ea));
201 split_ea.cmdidx = CMD_new;
202 split_ea.cmd = (char_u *)"new";
203 split_ea.arg = (char_u *)"";
204 ex_splitview(&split_ea);
205 if (curwin == old_curwin)
206 {
207 /* split failed */
208 vim_free(term);
209 return;
210 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200211 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200212 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200213
214 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200215 term->tl_next = first_term;
216 first_term = term;
217
Bram Moolenaar293424c2017-07-26 23:11:01 +0200218 if (cmd == NULL || *cmd == NUL)
219 cmd = p_sh;
220
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200221 if (buflist_findname(cmd) == NULL)
222 curbuf->b_ffname = vim_strsave(cmd);
223 else
224 {
225 int i;
226 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200227 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200228
229 for (i = 1; p != NULL; ++i)
230 {
231 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
232 if (buflist_findname(p) == NULL)
233 {
234 curbuf->b_ffname = p;
235 break;
236 }
237 }
238 }
239 curbuf->b_fname = curbuf->b_ffname;
240
241 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
242 curbuf->b_changed = TRUE;
243 curbuf->b_p_ma = FALSE;
244 set_string_option_direct((char_u *)"buftype", -1,
245 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200247 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200249 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200250 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200251 {
252 /* store the size we ended up with */
253 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254 }
255 else
256 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200257 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200258
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200259 /* Wiping out the buffer will also close the window and call
260 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200261 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200262 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200263
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200264 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200265}
266
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200267/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200268 * Free the scrollback buffer for "term".
269 */
270 static void
271free_scrollback(term_T *term)
272{
273 int i;
274
275 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
276 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
277 ga_clear(&term->tl_scrollback);
278}
279
280/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200281 * Free a terminal and everything it refers to.
282 * Kills the job if there is one.
283 * Called when wiping out a buffer.
284 */
285 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200286free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200287{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200288 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200289 term_T *tp;
290
291 if (term == NULL)
292 return;
293 if (first_term == term)
294 first_term = term->tl_next;
295 else
296 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
297 if (tp->tl_next == term)
298 {
299 tp->tl_next = term->tl_next;
300 break;
301 }
302
303 if (term->tl_job != NULL)
304 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200305 if (term->tl_job->jv_status != JOB_ENDED
306 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200307 job_stop(term->tl_job, NULL, "kill");
308 job_unref(term->tl_job);
309 }
310
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200311 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200312
313 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200314 vim_free(term->tl_title);
315 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200316 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200317 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200318}
319
320/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200321 * Write job output "msg[len]" to the vterm.
322 */
323 static void
324term_write_job_output(term_T *term, char_u *msg, size_t len)
325{
326 VTerm *vterm = term->tl_vterm;
327 char_u *p;
328 size_t done;
329 size_t len_now;
330
331 for (done = 0; done < len; done += len_now)
332 {
333 for (p = msg + done; p < msg + len; )
334 {
335 if (*p == NL)
336 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200337 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200338 }
339 len_now = p - msg - done;
340 vterm_input_write(vterm, (char *)msg + done, len_now);
341 if (p < msg + len && *p == NL)
342 {
343 /* Convert NL to CR-NL, that appears to work best. */
344 vterm_input_write(vterm, "\r\n", 2);
345 ++len_now;
346 }
347 }
348
349 /* this invokes the damage callbacks */
350 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
351}
352
Bram Moolenaar1c844932017-07-24 23:36:41 +0200353 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200354update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200355{
Bram Moolenaar1c844932017-07-24 23:36:41 +0200356 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200357 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200358 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200359 if (term->tl_cursor_visible)
360 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200361 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200362#ifdef FEAT_GUI
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200363 if (gui.in_use && term->tl_cursor_visible)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200364 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200365#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200366 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200367}
368
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200369/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200370 * Invoked when "msg" output from a job was received. Write it to the terminal
371 * of "buffer".
372 */
373 void
374write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
375{
376 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200377 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200378
Bram Moolenaard85f2712017-07-28 21:51:57 +0200379 if (term->tl_vterm == NULL)
380 {
381 ch_logn(channel, "NOT writing %d bytes to terminal", (int)len);
382 return;
383 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200384 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200385 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200386
387 /* TODO: only update once in a while. */
388 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200389 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200390}
391
392/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200393 * Send a mouse position and click to the vterm
394 */
395 static int
396term_send_mouse(VTerm *vterm, int button, int pressed)
397{
398 VTermModifier mod = VTERM_MOD_NONE;
399
400 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
401 mouse_col - W_WINCOL(curwin), mod);
402 vterm_mouse_button(vterm, button, pressed, mod);
403 return TRUE;
404}
405
406/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200407 * Convert typed key "c" into bytes to send to the job.
408 * Return the number of bytes in "buf".
409 */
410 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200411term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200412{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200413 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200414 VTermKey key = VTERM_KEY_NONE;
415 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200416 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200417
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200418 switch (c)
419 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200420 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200421 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200422 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
423 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200424 case K_DEL: key = VTERM_KEY_DEL; break;
425 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200426 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
427 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200428 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200429 case K_S_END: mod = VTERM_MOD_SHIFT;
430 key = VTERM_KEY_END; break;
431 case K_C_END: mod = VTERM_MOD_CTRL;
432 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200433 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
434 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
435 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
436 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
437 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
438 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
439 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
440 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
441 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
442 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
443 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
444 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
445 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200446 case K_S_HOME: mod = VTERM_MOD_SHIFT;
447 key = VTERM_KEY_HOME; break;
448 case K_C_HOME: mod = VTERM_MOD_CTRL;
449 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200450 case K_INS: key = VTERM_KEY_INS; break;
451 case K_K0: key = VTERM_KEY_KP_0; break;
452 case K_K1: key = VTERM_KEY_KP_1; break;
453 case K_K2: key = VTERM_KEY_KP_2; break;
454 case K_K3: key = VTERM_KEY_KP_3; break;
455 case K_K4: key = VTERM_KEY_KP_4; break;
456 case K_K5: key = VTERM_KEY_KP_5; break;
457 case K_K6: key = VTERM_KEY_KP_6; break;
458 case K_K7: key = VTERM_KEY_KP_7; break;
459 case K_K8: key = VTERM_KEY_KP_8; break;
460 case K_K9: key = VTERM_KEY_KP_9; break;
461 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
462 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
463 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
464 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
465 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
466 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
467 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
468 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
469 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
470 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
471 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
472 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
473 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200474 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
475 key = VTERM_KEY_LEFT; break;
476 case K_C_LEFT: mod = VTERM_MOD_CTRL;
477 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200478 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
479 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
480 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200481 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
482 key = VTERM_KEY_RIGHT; break;
483 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
484 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200485 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200486 case K_S_UP: mod = VTERM_MOD_SHIFT;
487 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200488 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200489
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200490 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
491 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
492 case K_MOUSELEFT: /* TODO */ return 0;
493 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200494
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200495 case K_LEFTMOUSE:
496 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
497 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
498 case K_LEFTRELEASE:
499 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
500 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
501 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
502 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
503 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
504 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
505 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
506 case K_X1MOUSE: /* TODO */ return 0;
507 case K_X1DRAG: /* TODO */ return 0;
508 case K_X1RELEASE: /* TODO */ return 0;
509 case K_X2MOUSE: /* TODO */ return 0;
510 case K_X2DRAG: /* TODO */ return 0;
511 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200512
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200513 case K_IGNORE: return 0;
514 case K_NOP: return 0;
515 case K_UNDO: return 0;
516 case K_HELP: return 0;
517 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
518 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
519 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
520 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
521 case K_SELECT: return 0;
522#ifdef FEAT_GUI
523 case K_VER_SCROLLBAR: return 0;
524 case K_HOR_SCROLLBAR: return 0;
525#endif
526#ifdef FEAT_GUI_TABLINE
527 case K_TABLINE: return 0;
528 case K_TABMENU: return 0;
529#endif
530#ifdef FEAT_NETBEANS_INTG
531 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
532#endif
533#ifdef FEAT_DND
534 case K_DROP: return 0;
535#endif
536#ifdef FEAT_AUTOCMD
537 case K_CURSORHOLD: return 0;
538#endif
539 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
540 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200541 }
542
543 /*
544 * Convert special keys to vterm keys:
545 * - Write keys to vterm: vterm_keyboard_key()
546 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200547 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200548 */
549 if (key != VTERM_KEY_NONE)
550 /* Special key, let vterm convert it. */
551 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200552 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200553 /* Normal character, let vterm convert it. */
554 vterm_keyboard_unichar(vterm, c, mod);
555
556 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200557 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200558}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200559
Bram Moolenaar938783d2017-07-16 20:13:26 +0200560/*
Bram Moolenaard85f2712017-07-28 21:51:57 +0200561 * Return TRUE if the job for "buf" is still running.
562 */
563 static int
564term_job_running(term_T *term)
565{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200566 /* Also consider the job finished when the channel is closed, to avoid a
567 * race condition when updating the title. */
568 return term->tl_job != NULL
569 && term->tl_job->jv_status == JOB_STARTED
570 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200571}
572
573/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200574 * Add the last line of the scrollback buffer to the buffer in the window.
575 */
576 static void
577add_scrollback_line_to_buffer(term_T *term)
578{
579 linenr_T lnum = term->tl_scrollback.ga_len - 1;
580 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
581 garray_T ga;
582 int c;
583 int col;
584 int i;
585
586 ga_init2(&ga, 1, 100);
587 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
588 {
589 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
590 goto failed;
591 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
592 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
593 (char_u *)ga.ga_data + ga.ga_len);
594 }
595 if (ga_grow(&ga, 1) == FAIL)
596 goto failed;
597 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
598 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
599
600 if (lnum == 0)
601 {
602 /* Delete the empty line that was in the empty buffer. */
603 curbuf = term->tl_buffer;
604 ml_delete(2, FALSE);
605 curbuf = curwin->w_buffer;
606 }
607
608failed:
609 ga_clear(&ga);
610}
611
612/*
613 * Add the current lines of the terminal to scrollback and to the buffer.
614 * Called after the job has ended and when switching to Terminal mode.
615 */
616 static void
617move_terminal_to_buffer(term_T *term)
618{
619 win_T *wp;
620 int len;
621 int lines_skipped = 0;
622 VTermPos pos;
623 VTermScreenCell cell;
624 VTermScreenCell *p;
625 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
626
627 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
628 {
629 len = 0;
630 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
631 if (vterm_screen_get_cell(screen, pos, &cell) != 0
632 && cell.chars[0] != NUL)
633 len = pos.col + 1;
634
635 if (len == 0)
636 ++lines_skipped;
637 else
638 {
639 while (lines_skipped > 0)
640 {
641 /* Line was skipped, add an empty line. */
642 --lines_skipped;
643 if (ga_grow(&term->tl_scrollback, 1) == OK)
644 {
645 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
646 + term->tl_scrollback.ga_len;
647
648 line->sb_cols = 0;
649 line->sb_cells = NULL;
650 ++term->tl_scrollback.ga_len;
651
652 add_scrollback_line_to_buffer(term);
653 }
654 }
655
656 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
657 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
658 {
659 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
660 + term->tl_scrollback.ga_len;
661
662 for (pos.col = 0; pos.col < len; ++pos.col)
663 {
664 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
665 vim_memset(p + pos.col, 0, sizeof(cell));
666 else
667 p[pos.col] = cell;
668 }
669 line->sb_cols = len;
670 line->sb_cells = p;
671 ++term->tl_scrollback.ga_len;
672
673 add_scrollback_line_to_buffer(term);
674 }
675 else
676 vim_free(p);
677 }
678 }
679
680 FOR_ALL_WINDOWS(wp)
681 {
682 if (wp->w_buffer == term->tl_buffer)
683 {
684 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
685 wp->w_cursor.col = 0;
686 wp->w_valid = 0;
687 redraw_win_later(wp, NOT_VALID);
688 }
689 }
690}
691
692 static void
693set_terminal_mode(term_T *term, int on)
694{
695 term->tl_terminal_mode = on;
696 vim_free(term->tl_status_text);
697 term->tl_status_text = NULL;
698 if (term->tl_buffer == curbuf)
699 maketitle();
700}
701
702/*
703 * Called after the job if finished and Terminal mode is not active:
704 * Move the vterm contents into the scrollback buffer and free the vterm.
705 */
706 static void
707cleanup_vterm(term_T *term)
708{
709 move_terminal_to_buffer(term);
710 term_free_vterm(term);
711 set_terminal_mode(term, FALSE);
712}
713
714/*
715 * Switch from sending keys to the job to Terminal-Normal mode.
716 * Suspends updating the terminal window.
717 */
718 static void
719term_enter_terminal_mode()
720{
721 term_T *term = curbuf->b_term;
722
723 /* Append the current terminal contents to the buffer. */
724 move_terminal_to_buffer(term);
725
726 set_terminal_mode(term, TRUE);
727}
728
729/*
730 * Returns TRUE if the current window contains a terminal and we are in
731 * Terminal-Normal mode.
732 */
733 int
734term_in_terminal_mode()
735{
736 term_T *term = curbuf->b_term;
737
738 return term != NULL && term->tl_terminal_mode;
739}
740
741/*
742 * Switch from Terminal-Normal mode to sending keys to the job.
743 * Restores updating the terminal window.
744 */
745 void
746term_leave_terminal_mode()
747{
748 term_T *term = curbuf->b_term;
749 sb_line_T *line;
750 garray_T *gap;
751
752 /* Remove the terminal contents from the scrollback and the buffer. */
753 gap = &term->tl_scrollback;
754 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
755 {
756 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
757 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
758 vim_free(line->sb_cells);
759 --gap->ga_len;
760 if (gap->ga_len == 0)
761 break;
762 }
763 check_cursor();
764
765 set_terminal_mode(term, FALSE);
766
767 if (term->tl_channel_closed)
768 cleanup_vterm(term);
769 redraw_buf_and_status_later(curbuf, NOT_VALID);
770}
771
772/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200773 * Get a key from the user without mapping.
774 * TODO: use terminal mode mappings.
775 */
776 static int
777term_vgetc()
778{
779 int c;
780
781 ++no_mapping;
782 ++allow_keys;
783 got_int = FALSE;
784 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200785 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200786 --no_mapping;
787 --allow_keys;
788 return c;
789}
790
791/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200792 * Send keys to terminal.
793 */
794 static int
795send_keys_to_term(term_T *term, int c, int typed)
796{
797 char msg[KEY_BUF_LEN];
798 size_t len;
799 static int mouse_was_outside = FALSE;
800 int dragging_outside = FALSE;
801
802 /* Catch keys that need to be handled as in Normal mode. */
803 switch (c)
804 {
805 case NUL:
806 case K_ZERO:
807 if (typed)
808 stuffcharReadbuff(c);
809 return FAIL;
810
811 case K_IGNORE:
812 return FAIL;
813
814 case K_LEFTDRAG:
815 case K_MIDDLEDRAG:
816 case K_RIGHTDRAG:
817 case K_X1DRAG:
818 case K_X2DRAG:
819 dragging_outside = mouse_was_outside;
820 /* FALLTHROUGH */
821 case K_LEFTMOUSE:
822 case K_LEFTMOUSE_NM:
823 case K_LEFTRELEASE:
824 case K_LEFTRELEASE_NM:
825 case K_MIDDLEMOUSE:
826 case K_MIDDLERELEASE:
827 case K_RIGHTMOUSE:
828 case K_RIGHTRELEASE:
829 case K_X1MOUSE:
830 case K_X1RELEASE:
831 case K_X2MOUSE:
832 case K_X2RELEASE:
833 if (mouse_row < W_WINROW(curwin)
834 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
835 || mouse_col < W_WINCOL(curwin)
836 || mouse_col >= W_ENDCOL(curwin)
837 || dragging_outside)
838 {
839 /* click outside the current window */
840 if (typed)
841 {
842 stuffcharReadbuff(c);
843 mouse_was_outside = TRUE;
844 }
845 return FAIL;
846 }
847 }
848 if (typed)
849 mouse_was_outside = FALSE;
850
851 /* Convert the typed key to a sequence of bytes for the job. */
852 len = term_convert_key(term, c, msg);
853 if (len > 0)
854 /* TODO: if FAIL is returned, stop? */
855 channel_send(term->tl_job->jv_channel, PART_IN,
856 (char_u *)msg, (int)len, NULL);
857
858 return OK;
859}
860
861/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200862 * Returns TRUE if the current window contains a terminal and we are sending
863 * keys to the job.
864 */
865 int
866term_use_loop()
867{
868 term_T *term = curbuf->b_term;
869
870 return term != NULL
871 && !term->tl_terminal_mode
872 && term->tl_vterm != NULL
873 && term_job_running(term);
874}
875
876/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200877 * Wait for input and send it to the job.
878 * Return when the start of a CTRL-W command is typed or anything else that
879 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200880 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
881 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200882 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200883 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200884terminal_loop(void)
885{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200886 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200887 int termkey = 0;
888
889 if (*curwin->w_p_tk != NUL)
890 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200891
892 for (;;)
893 {
894 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200895 /* Repeat redrawing in case a message is received while redrawing. */
896 while (curwin->w_redr_type != 0)
897 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200898 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200899
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200900 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200901 if (curbuf->b_term->tl_vterm == NULL
902 || !term_job_running(curbuf->b_term))
903 /* job finished while waiting for a character */
904 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200905
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200906 if (c == (termkey == 0 ? Ctrl_W : termkey))
907 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200908#ifdef FEAT_CMDL_INFO
909 if (add_to_showcmd(c))
910 out_flush();
911#endif
912 c = term_vgetc();
913#ifdef FEAT_CMDL_INFO
914 clear_showcmd();
915#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200916 if (curbuf->b_term->tl_vterm == NULL
917 || !term_job_running(curbuf->b_term))
918 /* job finished while waiting for a character */
919 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200920
921 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200922 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200923 /* "CTRL-W .": send CTRL-W to the job */
924 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200925 }
926 else if (termkey == 0 && c == 'N')
927 {
928 term_enter_terminal_mode();
929 return FAIL;
930 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200931 else if (termkey == 0 || c != termkey)
932 {
933 stuffcharReadbuff(Ctrl_W);
934 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200935 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200936 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200937 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200938 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
939 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200940 }
Bram Moolenaard85f2712017-07-28 21:51:57 +0200941 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200942}
943
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200944/*
945 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200946 * This updates the title and status, but does not close the vter, because
947 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200948 */
949 void
950term_job_ended(job_T *job)
951{
952 term_T *term;
953 int did_one = FALSE;
954
955 for (term = first_term; term != NULL; term = term->tl_next)
956 if (term->tl_job == job)
957 {
958 vim_free(term->tl_title);
959 term->tl_title = NULL;
960 vim_free(term->tl_status_text);
961 term->tl_status_text = NULL;
962 redraw_buf_and_status_later(term->tl_buffer, VALID);
963 did_one = TRUE;
964 }
965 if (did_one)
966 redraw_statuslines();
967 if (curbuf->b_term != NULL)
968 {
969 if (curbuf->b_term->tl_job == job)
970 maketitle();
971 update_cursor(curbuf->b_term, TRUE);
972 }
973}
974
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200975 static void
976position_cursor(win_T *wp, VTermPos *pos)
977{
978 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
979 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
980}
981
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200982 static void
983may_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);
1036 update_cursor(term, TRUE);
1037 }
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
1182 if (curbuf->b_term != NULL)
1183 {
1184 if (curbuf->b_term->tl_job == ch->ch_job)
1185 maketitle();
1186 update_cursor(curbuf->b_term, TRUE);
1187 }
1188 }
1189}
1190
1191/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001192 * Reverse engineer the RGB value into a cterm color index.
1193 * First color is 1. Return 0 if no match found.
1194 */
1195 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001196color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001197{
1198 int red = color->red;
1199 int blue = color->blue;
1200 int green = color->green;
1201
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001202 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001203 if (red == 0)
1204 {
1205 if (green == 0)
1206 {
1207 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001208 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001209 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001210 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001211 }
1212 else if (green == 224)
1213 {
1214 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001215 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001216 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001217 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001218 }
1219 }
1220 else if (red == 224)
1221 {
1222 if (green == 0)
1223 {
1224 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001225 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001226 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001227 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001228 }
1229 else if (green == 224)
1230 {
1231 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001232 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001233 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001234 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001235 }
1236 }
1237 else if (red == 128)
1238 {
1239 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001240 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001241 }
1242 else if (red == 255)
1243 {
1244 if (green == 64)
1245 {
1246 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001247 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001248 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001249 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001250 }
1251 else if (green == 255)
1252 {
1253 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001254 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001255 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001256 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001257 }
1258 }
1259 else if (red == 64)
1260 {
1261 if (green == 64)
1262 {
1263 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001264 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001265 }
1266 else if (green == 255)
1267 {
1268 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001269 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001270 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001271 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001272 }
1273 }
1274 if (t_colors >= 256)
1275 {
1276 if (red == blue && red == green)
1277 {
1278 /* 24-color greyscale */
1279 static int cutoff[23] = {
1280 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1281 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1282 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1283 int i;
1284
1285 for (i = 0; i < 23; ++i)
1286 if (red < cutoff[i])
1287 return i + 233;
1288 return 256;
1289 }
1290
1291 /* 216-color cube */
1292 return 17 + ((red + 25) / 0x33) * 36
1293 + ((green + 25) / 0x33) * 6
1294 + (blue + 25) / 0x33;
1295 }
1296 return 0;
1297}
1298
1299/*
1300 * Convert the attributes of a vterm cell into an attribute index.
1301 */
1302 static int
1303cell2attr(VTermScreenCell *cell)
1304{
1305 int attr = 0;
1306
1307 if (cell->attrs.bold)
1308 attr |= HL_BOLD;
1309 if (cell->attrs.underline)
1310 attr |= HL_UNDERLINE;
1311 if (cell->attrs.italic)
1312 attr |= HL_ITALIC;
1313 if (cell->attrs.strike)
1314 attr |= HL_STANDOUT;
1315 if (cell->attrs.reverse)
1316 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001317
1318#ifdef FEAT_GUI
1319 if (gui.in_use)
1320 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001321 guicolor_T fg, bg;
1322
1323 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1324 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1325 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001326 }
1327 else
1328#endif
1329#ifdef FEAT_TERMGUICOLORS
1330 if (p_tgc)
1331 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001332 guicolor_T fg, bg;
1333
1334 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1335 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1336
1337 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001338 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001339 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001340#endif
1341 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001342 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1343 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001344 }
1345 return 0;
1346}
1347
1348/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001349 * Called to update the window that contains a terminal.
1350 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001351 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001352 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001353term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001354{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001355 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001356 VTerm *vterm;
1357 VTermScreen *screen;
1358 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001359 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001360
Bram Moolenaar423802d2017-07-30 16:52:24 +02001361 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001362 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001363
Bram Moolenaard85f2712017-07-28 21:51:57 +02001364 vterm = term->tl_vterm;
1365 screen = vterm_obtain_screen(vterm);
1366 state = vterm_obtain_state(vterm);
1367
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001368 /*
1369 * If the window was resized a redraw will be triggered and we get here.
1370 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1371 */
1372 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1373 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001374 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001375 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1376 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1377 win_T *twp;
1378
1379 FOR_ALL_WINDOWS(twp)
1380 {
1381 /* When more than one window shows the same terminal, use the
1382 * smallest size. */
1383 if (twp->w_buffer == term->tl_buffer)
1384 {
1385 if (!term->tl_rows_fixed && rows > twp->w_height)
1386 rows = twp->w_height;
1387 if (!term->tl_cols_fixed && cols > twp->w_width)
1388 cols = twp->w_width;
1389 }
1390 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001391
1392 vterm_set_size(vterm, rows, cols);
1393 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1394 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001395 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001396 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001397
1398 /* The cursor may have been moved when resizing. */
1399 vterm_state_get_cursorpos(state, &pos);
1400 position_cursor(wp, &pos);
1401
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001402 /* TODO: Only redraw what changed. */
1403 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001404 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001405 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001406 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001407
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001408 if (pos.row < term->tl_rows)
1409 {
1410 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001411 {
1412 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001413 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001414
Bram Moolenaareeac6772017-07-23 15:48:37 +02001415 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1416 vim_memset(&cell, 0, sizeof(cell));
1417
1418 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001419 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001420 if (c == NUL)
1421 {
1422 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001423#if defined(FEAT_MBYTE)
1424 if (enc_utf8)
1425 ScreenLinesUC[off] = NUL;
1426#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001427 }
1428 else
1429 {
1430#if defined(FEAT_MBYTE)
1431 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001432 {
1433 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001434 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001435 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001436 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001437 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001438 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001439 if (enc_utf8)
1440 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001441 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001442#else
1443 ScreenLines[off] = c;
1444#endif
1445 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001446 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001447
1448 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001449 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001450 if (cell.width == 2)
1451 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001452 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001453#if defined(FEAT_MBYTE)
1454 if (enc_utf8)
1455 ScreenLinesUC[off] = NUL;
1456#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001457 ++pos.col;
1458 ++off;
1459 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001460 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001461 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001462 else
1463 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001464
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001465 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1466 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001467 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001468
1469 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001470}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001471
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001472/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001473 * Return TRUE if "wp" is a terminal window where the job has finished.
1474 */
1475 int
1476term_is_finished(buf_T *buf)
1477{
1478 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1479}
1480
1481/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001482 * Return TRUE if "wp" is a terminal window where the job has finished or we
1483 * are in Terminal-Normal mode.
1484 */
1485 int
1486term_show_buffer(buf_T *buf)
1487{
1488 term_T *term = buf->b_term;
1489
1490 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1491}
1492
1493/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001494 * The current buffer is going to be changed. If there is terminal
1495 * highlighting remove it now.
1496 */
1497 void
1498term_change_in_curbuf(void)
1499{
1500 term_T *term = curbuf->b_term;
1501
1502 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1503 {
1504 free_scrollback(term);
1505 redraw_buf_later(term->tl_buffer, NOT_VALID);
1506 }
1507}
1508
1509/*
1510 * Get the screen attribute for a position in the buffer.
1511 */
1512 int
1513term_get_attr(buf_T *buf, linenr_T lnum, int col)
1514{
1515 term_T *term = buf->b_term;
1516 sb_line_T *line;
1517
Bram Moolenaar70229f92017-07-29 16:01:53 +02001518 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001519 return 0;
1520 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1521 if (col >= line->sb_cols)
1522 return 0;
1523 return cell2attr(line->sb_cells + col);
1524}
1525
1526/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001527 * Set job options common for Unix and MS-Windows.
1528 */
1529 static void
1530setup_job_options(jobopt_T *opt, int rows, int cols)
1531{
1532 clear_job_options(opt);
1533 opt->jo_mode = MODE_RAW;
1534 opt->jo_out_mode = MODE_RAW;
1535 opt->jo_err_mode = MODE_RAW;
1536 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001537
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001538 opt->jo_io[PART_OUT] = JIO_BUFFER;
1539 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001540 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1541
1542 opt->jo_modifiable[PART_OUT] = 0;
1543 opt->jo_modifiable[PART_ERR] = 0;
1544 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1545
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001546 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1547 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001548 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001549 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1550
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001551 opt->jo_term_rows = rows;
1552 opt->jo_term_cols = cols;
1553}
1554
1555/*
1556 * Create a new vterm and initialize it.
1557 */
1558 static void
1559create_vterm(term_T *term, int rows, int cols)
1560{
1561 VTerm *vterm;
1562 VTermScreen *screen;
1563
1564 vterm = vterm_new(rows, cols);
1565 term->tl_vterm = vterm;
1566 screen = vterm_obtain_screen(vterm);
1567 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1568 /* TODO: depends on 'encoding'. */
1569 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001570
1571 /* Vterm uses a default black background. Set it to white when
1572 * 'background' is "light". */
1573 if (*p_bg == 'l')
1574 {
1575 VTermColor fg, bg;
1576
1577 fg.red = fg.green = fg.blue = 0;
1578 bg.red = bg.green = bg.blue = 255;
1579 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1580 }
1581
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001582 /* Required to initialize most things. */
1583 vterm_screen_reset(screen, 1 /* hard */);
1584}
1585
Bram Moolenaar21554412017-07-24 21:44:43 +02001586/*
1587 * Return the text to show for the buffer name and status.
1588 */
1589 char_u *
1590term_get_status_text(term_T *term)
1591{
1592 if (term->tl_status_text == NULL)
1593 {
1594 char_u *txt;
1595 size_t len;
1596
Bram Moolenaar423802d2017-07-30 16:52:24 +02001597 if (term->tl_terminal_mode)
1598 {
1599 if (term_job_running(term))
1600 txt = (char_u *)_("Terminal");
1601 else
1602 txt = (char_u *)_("Terminal-finished");
1603 }
1604 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001605 txt = term->tl_title;
1606 else if (term_job_running(term))
1607 txt = (char_u *)_("running");
1608 else
1609 txt = (char_u *)_("finished");
1610 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001611 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001612 if (term->tl_status_text != NULL)
1613 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1614 term->tl_buffer->b_fname, txt);
1615 }
1616 return term->tl_status_text;
1617}
1618
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001619/*
1620 * Mark references in jobs of terminals.
1621 */
1622 int
1623set_ref_in_term(int copyID)
1624{
1625 int abort = FALSE;
1626 term_T *term;
1627 typval_T tv;
1628
1629 for (term = first_term; term != NULL; term = term->tl_next)
1630 if (term->tl_job != NULL)
1631 {
1632 tv.v_type = VAR_JOB;
1633 tv.vval.v_job = term->tl_job;
1634 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1635 }
1636 return abort;
1637}
1638
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001639/*
1640 * "term_getattr(attr, name)" function
1641 */
1642 void
1643f_term_getattr(typval_T *argvars, typval_T *rettv)
1644{
1645 int attr;
1646 size_t i;
1647 char_u *name;
1648
1649 static struct {
1650 char *name;
1651 int attr;
1652 } attrs[] = {
1653 {"bold", HL_BOLD},
1654 {"italic", HL_ITALIC},
1655 {"underline", HL_UNDERLINE},
1656 {"strike", HL_STANDOUT},
1657 {"reverse", HL_INVERSE},
1658 };
1659
1660 attr = get_tv_number(&argvars[0]);
1661 name = get_tv_string_chk(&argvars[1]);
1662 if (name == NULL)
1663 return;
1664
1665 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1666 if (STRCMP(name, attrs[i].name) == 0)
1667 {
1668 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1669 break;
1670 }
1671}
1672
1673/*
1674 * Get the buffer from the first argument in "argvars".
1675 * Returns NULL when the buffer is not for a terminal window.
1676 */
1677 static buf_T *
1678term_get_buf(typval_T *argvars)
1679{
1680 buf_T *buf;
1681
1682 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1683 ++emsg_off;
1684 buf = get_buf_tv(&argvars[0], FALSE);
1685 --emsg_off;
Bram Moolenaarf144a3f2017-07-30 18:02:12 +02001686 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001687 return NULL;
1688 return buf;
1689}
1690
1691/*
1692 * "term_getjob(buf)" function
1693 */
1694 void
1695f_term_getjob(typval_T *argvars, typval_T *rettv)
1696{
1697 buf_T *buf = term_get_buf(argvars);
1698
1699 rettv->v_type = VAR_JOB;
1700 rettv->vval.v_job = NULL;
1701 if (buf == NULL)
1702 return;
1703
1704 rettv->vval.v_job = buf->b_term->tl_job;
1705 if (rettv->vval.v_job != NULL)
1706 ++rettv->vval.v_job->jv_refcount;
1707}
1708
1709/*
1710 * "term_getline(buf, row)" function
1711 */
1712 void
1713f_term_getline(typval_T *argvars, typval_T *rettv)
1714{
1715 buf_T *buf = term_get_buf(argvars);
1716 term_T *term;
1717 int row;
1718
1719 rettv->v_type = VAR_STRING;
1720 if (buf == NULL)
1721 return;
1722 term = buf->b_term;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001723 if (argvars[1].v_type == VAR_UNKNOWN)
1724 row = term->tl_cursor_pos.row;
1725 else
1726 row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001727
1728 if (term->tl_vterm == NULL)
1729 {
1730 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1731
1732 /* vterm is finished, get the text from the buffer */
1733 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1734 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1735 }
1736 else
1737 {
1738 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1739 VTermRect rect;
1740 int len;
1741 char_u *p;
1742
1743 len = term->tl_cols * MB_MAXBYTES + 1;
1744 p = alloc(len);
1745 if (p == NULL)
1746 return;
1747 rettv->vval.v_string = p;
1748
1749 rect.start_col = 0;
1750 rect.end_col = term->tl_cols;
1751 rect.start_row = row;
1752 rect.end_row = row + 1;
1753 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1754 }
1755}
1756
1757/*
1758 * "term_getsize(buf)" function
1759 */
1760 void
1761f_term_getsize(typval_T *argvars, typval_T *rettv)
1762{
1763 buf_T *buf = term_get_buf(argvars);
1764 list_T *l;
1765
1766 if (rettv_list_alloc(rettv) == FAIL)
1767 return;
1768 if (buf == NULL)
1769 return;
1770
1771 l = rettv->vval.v_list;
1772 list_append_number(l, buf->b_term->tl_rows);
1773 list_append_number(l, buf->b_term->tl_cols);
1774}
1775
1776/*
1777 * "term_list()" function
1778 */
1779 void
1780f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1781{
1782 term_T *tp;
1783 list_T *l;
1784
1785 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1786 return;
1787
1788 l = rettv->vval.v_list;
1789 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1790 if (tp != NULL && tp->tl_buffer != NULL)
1791 if (list_append_number(l,
1792 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1793 return;
1794}
1795
1796/*
1797 * "term_scrape(buf, row)" function
1798 */
1799 void
1800f_term_scrape(typval_T *argvars, typval_T *rettv)
1801{
1802 buf_T *buf = term_get_buf(argvars);
1803 VTermScreen *screen = NULL;
1804 VTermPos pos;
1805 list_T *l;
1806 term_T *term;
1807
1808 if (rettv_list_alloc(rettv) == FAIL)
1809 return;
1810 if (buf == NULL)
1811 return;
1812 term = buf->b_term;
1813 if (term->tl_vterm != NULL)
1814 screen = vterm_obtain_screen(term->tl_vterm);
1815
1816 l = rettv->vval.v_list;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001817 if (argvars[1].v_type == VAR_UNKNOWN)
1818 pos.row = term->tl_cursor_pos.row;
1819 else
1820 pos.row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001821 for (pos.col = 0; pos.col < term->tl_cols; )
1822 {
1823 dict_T *dcell;
1824 VTermScreenCell cell;
1825 char_u rgb[8];
1826 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1827 int off = 0;
1828 int i;
1829
1830 if (screen == NULL)
1831 {
1832 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1833 sb_line_T *line;
1834
1835 /* vterm has finished, get the cell from scrollback */
1836 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1837 break;
1838 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1839 if (pos.col >= line->sb_cols)
1840 break;
1841 cell = line->sb_cells[pos.col];
1842 }
1843 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1844 break;
1845 dcell = dict_alloc();
1846 list_append_dict(l, dcell);
1847
1848 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1849 {
1850 if (cell.chars[i] == 0)
1851 break;
1852 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1853 }
1854 mbs[off] = NUL;
1855 dict_add_nr_str(dcell, "chars", 0, mbs);
1856
1857 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1858 cell.fg.red, cell.fg.green, cell.fg.blue);
1859 dict_add_nr_str(dcell, "fg", 0, rgb);
1860 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1861 cell.bg.red, cell.bg.green, cell.bg.blue);
1862 dict_add_nr_str(dcell, "bg", 0, rgb);
1863
1864 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
1865 dict_add_nr_str(dcell, "width", cell.width, NULL);
1866
1867 ++pos.col;
1868 if (cell.width == 2)
1869 ++pos.col;
1870 }
1871}
1872
1873/*
1874 * "term_sendkeys(buf, keys)" function
1875 */
1876 void
1877f_term_sendkeys(typval_T *argvars, typval_T *rettv)
1878{
1879 buf_T *buf = term_get_buf(argvars);
1880 char_u *msg;
1881 term_T *term;
1882
1883 rettv->v_type = VAR_UNKNOWN;
1884 if (buf == NULL)
1885 return;
1886
1887 msg = get_tv_string_chk(&argvars[1]);
1888 if (msg == NULL)
1889 return;
1890 term = buf->b_term;
1891 if (term->tl_vterm == NULL)
1892 return;
1893
1894 while (*msg != NUL)
1895 {
1896 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
1897 msg += MB_PTR2LEN(msg);
1898 }
1899
1900 /* TODO: only update once in a while. */
1901 update_screen(0);
1902 if (buf == curbuf)
1903 update_cursor(term, TRUE);
1904}
1905
1906/*
1907 * "term_start(command, options)" function
1908 */
1909 void
1910f_term_start(typval_T *argvars, typval_T *rettv)
1911{
1912 char_u *cmd = get_tv_string_chk(&argvars[0]);
1913 exarg_T ea;
1914
1915 if (cmd == NULL)
1916 return;
1917 ea.arg = cmd;
1918 ex_terminal(&ea);
1919
1920 if (curbuf->b_term != NULL)
1921 rettv->vval.v_number = curbuf->b_fnum;
1922}
1923
1924/*
1925 * "term_wait" function
1926 */
1927 void
1928f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
1929{
1930 buf_T *buf = term_get_buf(argvars);
1931
1932 if (buf == NULL)
1933 return;
1934
1935 /* Get the job status, this will detect a job that finished. */
1936 if (buf->b_term->tl_job != NULL)
1937 (void)job_status(buf->b_term->tl_job);
1938
1939 /* Check for any pending channel I/O. */
1940 vpeekc_any();
1941 ui_delay(10L, FALSE);
1942
1943 /* Flushing messages on channels is hopefully sufficient.
1944 * TODO: is there a better way? */
1945 parse_queued_messages();
1946}
1947
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001948# ifdef WIN3264
1949
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001950/**************************************
1951 * 2. MS-Windows implementation.
1952 */
1953
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001954#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1955#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1956
Bram Moolenaar8a773062017-07-24 22:29:21 +02001957void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001958void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001959void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001960BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1961void (*winpty_config_set_initial_size)(void*, int, int);
1962LPCWSTR (*winpty_conin_name)(void*);
1963LPCWSTR (*winpty_conout_name)(void*);
1964LPCWSTR (*winpty_conerr_name)(void*);
1965void (*winpty_free)(void*);
1966void (*winpty_config_free)(void*);
1967void (*winpty_spawn_config_free)(void*);
1968void (*winpty_error_free)(void*);
1969LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001970BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001971
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001972#define WINPTY_DLL "winpty.dll"
1973
1974static HINSTANCE hWinPtyDLL = NULL;
1975
1976 int
1977dyn_winpty_init(void)
1978{
1979 int i;
1980 static struct
1981 {
1982 char *name;
1983 FARPROC *ptr;
1984 } winpty_entry[] =
1985 {
1986 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1987 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1988 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1989 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1990 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1991 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1992 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1993 {"winpty_free", (FARPROC*)&winpty_free},
1994 {"winpty_open", (FARPROC*)&winpty_open},
1995 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1996 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1997 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1998 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001999 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002000 {NULL, NULL}
2001 };
2002
2003 /* No need to initialize twice. */
2004 if (hWinPtyDLL)
2005 return 1;
2006 /* Load winpty.dll */
2007 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2008 if (!hWinPtyDLL)
2009 {
2010 EMSG2(_(e_loadlib), WINPTY_DLL);
2011 return 0;
2012 }
2013 for (i = 0; winpty_entry[i].name != NULL
2014 && winpty_entry[i].ptr != NULL; ++i)
2015 {
2016 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2017 winpty_entry[i].name)) == NULL)
2018 {
2019 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2020 return 0;
2021 }
2022 }
2023
2024 return 1;
2025}
2026
2027/*
2028 * Create a new terminal of "rows" by "cols" cells.
2029 * Store a reference in "term".
2030 * Return OK or FAIL.
2031 */
2032 static int
2033term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2034{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002035 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002036 channel_T *channel = NULL;
2037 job_T *job = NULL;
2038 jobopt_T opt;
2039 DWORD error;
2040 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2041 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002042 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002043
2044 if (!dyn_winpty_init())
2045 return FAIL;
2046
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002047 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002048 if (p == NULL)
2049 return FAIL;
2050
2051 job = job_alloc();
2052 if (job == NULL)
2053 goto failed;
2054
2055 channel = add_channel();
2056 if (channel == NULL)
2057 goto failed;
2058
2059 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2060 if (term->tl_winpty_config == NULL)
2061 goto failed;
2062
2063 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2064 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2065 if (term->tl_winpty == NULL)
2066 goto failed;
2067
2068 spawn_config = winpty_spawn_config_new(
2069 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2070 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2071 NULL,
2072 p,
2073 NULL,
2074 NULL,
2075 &winpty_err);
2076 if (spawn_config == NULL)
2077 goto failed;
2078
2079 channel = add_channel();
2080 if (channel == NULL)
2081 goto failed;
2082
2083 job = job_alloc();
2084 if (job == NULL)
2085 goto failed;
2086
2087 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2088 &child_thread_handle, &error, &winpty_err))
2089 goto failed;
2090
2091 channel_set_pipes(channel,
2092 (sock_T) CreateFileW(
2093 winpty_conin_name(term->tl_winpty),
2094 GENERIC_WRITE, 0, NULL,
2095 OPEN_EXISTING, 0, NULL),
2096 (sock_T) CreateFileW(
2097 winpty_conout_name(term->tl_winpty),
2098 GENERIC_READ, 0, NULL,
2099 OPEN_EXISTING, 0, NULL),
2100 (sock_T) CreateFileW(
2101 winpty_conerr_name(term->tl_winpty),
2102 GENERIC_READ, 0, NULL,
2103 OPEN_EXISTING, 0, NULL));
2104
2105 jo = CreateJobObject(NULL, NULL);
2106 if (jo == NULL)
2107 goto failed;
2108
2109 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002110 {
2111 /* Failed, switch the way to terminate process with TerminateProcess. */
2112 CloseHandle(jo);
2113 jo = NULL;
2114 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002115
2116 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002117 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002118
2119 create_vterm(term, rows, cols);
2120
2121 setup_job_options(&opt, rows, cols);
2122 channel_set_job(channel, job, &opt);
2123
2124 job->jv_channel = channel;
2125 job->jv_proc_info.hProcess = child_process_handle;
2126 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2127 job->jv_job_object = jo;
2128 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002129 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002130 term->tl_job = job;
2131
2132 return OK;
2133
2134failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002135 if (spawn_config != NULL)
2136 winpty_spawn_config_free(spawn_config);
2137 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002138 if (channel != NULL)
2139 channel_clear(channel);
2140 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002141 {
2142 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002143 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002144 }
2145 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002146 if (jo != NULL)
2147 CloseHandle(jo);
2148 if (term->tl_winpty != NULL)
2149 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002150 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002151 if (term->tl_winpty_config != NULL)
2152 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002153 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002154 if (winpty_err != NULL)
2155 {
2156 char_u *msg = utf16_to_enc(
2157 (short_u *)winpty_error_msg(winpty_err), NULL);
2158
2159 EMSG(msg);
2160 winpty_error_free(winpty_err);
2161 }
2162 return FAIL;
2163}
2164
2165/*
2166 * Free the terminal emulator part of "term".
2167 */
2168 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002169term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002170{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002171 if (term->tl_winpty != NULL)
2172 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002173 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002174 if (term->tl_winpty_config != NULL)
2175 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002176 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002177 if (term->tl_vterm != NULL)
2178 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002179 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002180}
2181
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002182/*
2183 * Request size to terminal.
2184 */
2185 static void
2186term_report_winsize(term_T *term, int rows, int cols)
2187{
2188 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2189}
2190
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002191# else
2192
2193/**************************************
2194 * 3. Unix-like implementation.
2195 */
2196
2197/*
2198 * Create a new terminal of "rows" by "cols" cells.
2199 * Start job for "cmd".
2200 * Store the pointers in "term".
2201 * Return OK or FAIL.
2202 */
2203 static int
2204term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2205{
2206 typval_T argvars[2];
2207 jobopt_T opt;
2208
2209 create_vterm(term, rows, cols);
2210
2211 argvars[0].v_type = VAR_STRING;
2212 argvars[0].vval.v_string = cmd;
2213 setup_job_options(&opt, rows, cols);
2214 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002215 if (term->tl_job != NULL)
2216 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002217
Bram Moolenaar61a66052017-07-22 18:39:00 +02002218 return term->tl_job != NULL
2219 && term->tl_job->jv_channel != NULL
2220 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002221}
2222
2223/*
2224 * Free the terminal emulator part of "term".
2225 */
2226 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002227term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002228{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002229 if (term->tl_vterm != NULL)
2230 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002231 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002232}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002233
2234/*
2235 * Request size to terminal.
2236 */
2237 static void
2238term_report_winsize(term_T *term, int rows, int cols)
2239{
2240 /* Use an ioctl() to report the new window size to the job. */
2241 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2242 {
2243 int fd = -1;
2244 int part;
2245
2246 for (part = PART_OUT; part < PART_COUNT; ++part)
2247 {
2248 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2249 if (isatty(fd))
2250 break;
2251 }
2252 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2253 mch_stop_job(term->tl_job, (char_u *)"winch");
2254 }
2255}
2256
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002257# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002258
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002259#endif /* FEAT_TERMINAL */