blob: 1f155dbab620c0c37dc49b8c648157e62b01f62b [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 * - in bash mouse clicks are inserting characters.
40 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaard85f2712017-07-28 21:51:57 +020041 * - For the scrollback buffer store lines in the buffer, only attributes in
42 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020043 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020044 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020045 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
46 * job finishes).
47 * - add option values to the command:
48 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020049 * - support different cursor shapes, colors and attributes
50 * - make term_getcursor() return type (none/block/bar/underline) and
51 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020052 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020053 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020054 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020055 * - add 't' to mode()
Bram Moolenaar938783d2017-07-16 20:13:26 +020056 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaard85f2712017-07-28 21:51:57 +020057 * - when closing window and job has ended, make buffer hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020058 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020059 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020060 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020061 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020062 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020063 * - implement "term" for job_start(): more job options when starting a
64 * terminal.
Bram Moolenaar423802d2017-07-30 16:52:24 +020065 * - if the job in the terminal does not support the mouse, we can use the
66 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020067 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
68 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020069 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020070 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020071 */
72
73#include "vim.h"
74
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020075#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020076
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020077#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020078# define MIN(x,y) (x < y ? x : y)
79# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020080#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020081
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020082#include "libvterm/include/vterm.h"
83
Bram Moolenaard85f2712017-07-28 21:51:57 +020084typedef struct sb_line_S {
85 int sb_cols; /* can differ per line */
86 VTermScreenCell *sb_cells; /* allocated */
87} sb_line_T;
88
Bram Moolenaare4f25e42017-07-07 11:54:15 +020089/* typedef term_T in structs.h */
90struct terminal_S {
91 term_T *tl_next;
92
Bram Moolenaar423802d2017-07-30 16:52:24 +020093 VTerm *tl_vterm;
94 job_T *tl_job;
95 buf_T *tl_buffer;
96
97 int tl_terminal_mode;
98 int tl_channel_closed;
99
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200100#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200101 void *tl_winpty_config;
102 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200103#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200104
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200105 /* last known vterm size */
106 int tl_rows;
107 int tl_cols;
108 /* vterm size does not follow window size */
109 int tl_rows_fixed;
110 int tl_cols_fixed;
111
Bram Moolenaar21554412017-07-24 21:44:43 +0200112 char_u *tl_title; /* NULL or allocated */
113 char_u *tl_status_text; /* NULL or allocated */
114
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200115 /* Range of screen rows to update. Zero based. */
116 int tl_dirty_row_start; /* -1 if nothing dirty */
117 int tl_dirty_row_end; /* row below last one to update */
118
Bram Moolenaard85f2712017-07-28 21:51:57 +0200119 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200120 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200121
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200122 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200123 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200124};
125
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126/*
127 * List of all active terminals.
128 */
129static term_T *first_term = NULL;
130
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200131
132#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
133#define KEY_BUF_LEN 200
134
135/*
136 * Functions with separate implementation for MS-Windows and Unix-like systems.
137 */
138static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200139static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200140static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200141
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200142/**************************************
143 * 1. Generic code for all systems.
144 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200145
146/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200147 * Determine the terminal size from 'termsize' and the current window.
148 * Assumes term->tl_rows and term->tl_cols are zero.
149 */
150 static void
151set_term_and_win_size(term_T *term)
152{
153 if (*curwin->w_p_tms != NUL)
154 {
155 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
156
157 term->tl_rows = atoi((char *)curwin->w_p_tms);
158 term->tl_cols = atoi((char *)p);
159 }
160 if (term->tl_rows == 0)
161 term->tl_rows = curwin->w_height;
162 else
163 {
164 win_setheight_win(term->tl_rows, curwin);
165 term->tl_rows_fixed = TRUE;
166 }
167 if (term->tl_cols == 0)
168 term->tl_cols = curwin->w_width;
169 else
170 {
171 win_setwidth_win(term->tl_cols, curwin);
172 term->tl_cols_fixed = TRUE;
173 }
174}
175
176/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200177 * ":terminal": open a terminal window and execute a job in it.
178 */
179 void
180ex_terminal(exarg_T *eap)
181{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200182 exarg_T split_ea;
183 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200184 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200185 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200186
187 if (check_restricted() || check_secure())
188 return;
189
190 term = (term_T *)alloc_clear(sizeof(term_T));
191 if (term == NULL)
192 return;
193 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200194 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200195 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200196
197 /* Open a new window or tab. */
198 vim_memset(&split_ea, 0, sizeof(split_ea));
199 split_ea.cmdidx = CMD_new;
200 split_ea.cmd = (char_u *)"new";
201 split_ea.arg = (char_u *)"";
202 ex_splitview(&split_ea);
203 if (curwin == old_curwin)
204 {
205 /* split failed */
206 vim_free(term);
207 return;
208 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200209 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200210 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200211
212 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200213 term->tl_next = first_term;
214 first_term = term;
215
Bram Moolenaar293424c2017-07-26 23:11:01 +0200216 if (cmd == NULL || *cmd == NUL)
217 cmd = p_sh;
218
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200219 if (buflist_findname(cmd) == NULL)
220 curbuf->b_ffname = vim_strsave(cmd);
221 else
222 {
223 int i;
224 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200225 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200226
227 for (i = 1; p != NULL; ++i)
228 {
229 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
230 if (buflist_findname(p) == NULL)
231 {
232 curbuf->b_ffname = p;
233 break;
234 }
235 }
236 }
237 curbuf->b_fname = curbuf->b_ffname;
238
239 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
240 curbuf->b_changed = TRUE;
241 curbuf->b_p_ma = FALSE;
242 set_string_option_direct((char_u *)"buftype", -1,
243 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200244
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200245 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200247 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200248 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200249 {
250 /* store the size we ended up with */
251 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252 }
253 else
254 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200255 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200256
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200257 /* Wiping out the buffer will also close the window and call
258 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200259 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200260 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200261
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200262 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263}
264
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200265/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200266 * Free the scrollback buffer for "term".
267 */
268 static void
269free_scrollback(term_T *term)
270{
271 int i;
272
273 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
274 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
275 ga_clear(&term->tl_scrollback);
276}
277
278/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200279 * Free a terminal and everything it refers to.
280 * Kills the job if there is one.
281 * Called when wiping out a buffer.
282 */
283 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200284free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200285{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200286 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200287 term_T *tp;
288
289 if (term == NULL)
290 return;
291 if (first_term == term)
292 first_term = term->tl_next;
293 else
294 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
295 if (tp->tl_next == term)
296 {
297 tp->tl_next = term->tl_next;
298 break;
299 }
300
301 if (term->tl_job != NULL)
302 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200303 if (term->tl_job->jv_status != JOB_ENDED
304 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200305 job_stop(term->tl_job, NULL, "kill");
306 job_unref(term->tl_job);
307 }
308
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200309 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200310
311 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200312 vim_free(term->tl_title);
313 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200314 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200315 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200316}
317
318/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200319 * Write job output "msg[len]" to the vterm.
320 */
321 static void
322term_write_job_output(term_T *term, char_u *msg, size_t len)
323{
324 VTerm *vterm = term->tl_vterm;
325 char_u *p;
326 size_t done;
327 size_t len_now;
328
329 for (done = 0; done < len; done += len_now)
330 {
331 for (p = msg + done; p < msg + len; )
332 {
333 if (*p == NL)
334 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200335 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200336 }
337 len_now = p - msg - done;
338 vterm_input_write(vterm, (char *)msg + done, len_now);
339 if (p < msg + len && *p == NL)
340 {
341 /* Convert NL to CR-NL, that appears to work best. */
342 vterm_input_write(vterm, "\r\n", 2);
343 ++len_now;
344 }
345 }
346
347 /* this invokes the damage callbacks */
348 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
349}
350
Bram Moolenaar1c844932017-07-24 23:36:41 +0200351 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200352update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200353{
Bram Moolenaar1c844932017-07-24 23:36:41 +0200354 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200355 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200356 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200357 if (term->tl_cursor_visible)
358 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200359 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200360#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200361 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200362 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200363#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200364 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200365}
366
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200367/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200368 * Invoked when "msg" output from a job was received. Write it to the terminal
369 * of "buffer".
370 */
371 void
372write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
373{
374 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200375 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200376
Bram Moolenaard85f2712017-07-28 21:51:57 +0200377 if (term->tl_vterm == NULL)
378 {
379 ch_logn(channel, "NOT writing %d bytes to terminal", (int)len);
380 return;
381 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200382 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200383 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200384
385 /* TODO: only update once in a while. */
386 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200387 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200388}
389
390/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200391 * Send a mouse position and click to the vterm
392 */
393 static int
394term_send_mouse(VTerm *vterm, int button, int pressed)
395{
396 VTermModifier mod = VTERM_MOD_NONE;
397
398 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
399 mouse_col - W_WINCOL(curwin), mod);
400 vterm_mouse_button(vterm, button, pressed, mod);
401 return TRUE;
402}
403
404/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200405 * Convert typed key "c" into bytes to send to the job.
406 * Return the number of bytes in "buf".
407 */
408 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200409term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200410{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200411 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200412 VTermKey key = VTERM_KEY_NONE;
413 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200414 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200415
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200416 switch (c)
417 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200418 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200419 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200420 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
421 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200422 case K_DEL: key = VTERM_KEY_DEL; break;
423 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200424 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
425 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200426 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200427 case K_S_END: mod = VTERM_MOD_SHIFT;
428 key = VTERM_KEY_END; break;
429 case K_C_END: mod = VTERM_MOD_CTRL;
430 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200431 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
432 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
433 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
434 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
435 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
436 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
437 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
438 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
439 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
440 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
441 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
442 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
443 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200444 case K_S_HOME: mod = VTERM_MOD_SHIFT;
445 key = VTERM_KEY_HOME; break;
446 case K_C_HOME: mod = VTERM_MOD_CTRL;
447 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200448 case K_INS: key = VTERM_KEY_INS; break;
449 case K_K0: key = VTERM_KEY_KP_0; break;
450 case K_K1: key = VTERM_KEY_KP_1; break;
451 case K_K2: key = VTERM_KEY_KP_2; break;
452 case K_K3: key = VTERM_KEY_KP_3; break;
453 case K_K4: key = VTERM_KEY_KP_4; break;
454 case K_K5: key = VTERM_KEY_KP_5; break;
455 case K_K6: key = VTERM_KEY_KP_6; break;
456 case K_K7: key = VTERM_KEY_KP_7; break;
457 case K_K8: key = VTERM_KEY_KP_8; break;
458 case K_K9: key = VTERM_KEY_KP_9; break;
459 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
460 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
461 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
462 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
463 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
464 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
465 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
466 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
467 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
468 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
469 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
470 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
471 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200472 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
473 key = VTERM_KEY_LEFT; break;
474 case K_C_LEFT: mod = VTERM_MOD_CTRL;
475 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200476 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
477 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
478 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200479 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
480 key = VTERM_KEY_RIGHT; break;
481 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
482 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200483 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200484 case K_S_UP: mod = VTERM_MOD_SHIFT;
485 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200486 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200487
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200488 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
489 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
490 case K_MOUSELEFT: /* TODO */ return 0;
491 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200492
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200493 case K_LEFTMOUSE:
494 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
495 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
496 case K_LEFTRELEASE:
497 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
498 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
499 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
500 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
501 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
502 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
503 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
504 case K_X1MOUSE: /* TODO */ return 0;
505 case K_X1DRAG: /* TODO */ return 0;
506 case K_X1RELEASE: /* TODO */ return 0;
507 case K_X2MOUSE: /* TODO */ return 0;
508 case K_X2DRAG: /* TODO */ return 0;
509 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200510
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200511 case K_IGNORE: return 0;
512 case K_NOP: return 0;
513 case K_UNDO: return 0;
514 case K_HELP: return 0;
515 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
516 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
517 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
518 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
519 case K_SELECT: return 0;
520#ifdef FEAT_GUI
521 case K_VER_SCROLLBAR: return 0;
522 case K_HOR_SCROLLBAR: return 0;
523#endif
524#ifdef FEAT_GUI_TABLINE
525 case K_TABLINE: return 0;
526 case K_TABMENU: return 0;
527#endif
528#ifdef FEAT_NETBEANS_INTG
529 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
530#endif
531#ifdef FEAT_DND
532 case K_DROP: return 0;
533#endif
534#ifdef FEAT_AUTOCMD
535 case K_CURSORHOLD: return 0;
536#endif
537 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
538 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200539 }
540
541 /*
542 * Convert special keys to vterm keys:
543 * - Write keys to vterm: vterm_keyboard_key()
544 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200545 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200546 */
547 if (key != VTERM_KEY_NONE)
548 /* Special key, let vterm convert it. */
549 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200550 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200551 /* Normal character, let vterm convert it. */
552 vterm_keyboard_unichar(vterm, c, mod);
553
554 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200555 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200556}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200557
Bram Moolenaar938783d2017-07-16 20:13:26 +0200558/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200559 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200560 */
561 static int
562term_job_running(term_T *term)
563{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200564 /* Also consider the job finished when the channel is closed, to avoid a
565 * race condition when updating the title. */
566 return term->tl_job != NULL
567 && term->tl_job->jv_status == JOB_STARTED
568 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200569}
570
571/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200572 * Add the last line of the scrollback buffer to the buffer in the window.
573 */
574 static void
575add_scrollback_line_to_buffer(term_T *term)
576{
577 linenr_T lnum = term->tl_scrollback.ga_len - 1;
578 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
579 garray_T ga;
580 int c;
581 int col;
582 int i;
583
584 ga_init2(&ga, 1, 100);
585 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
586 {
587 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
588 goto failed;
589 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
590 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
591 (char_u *)ga.ga_data + ga.ga_len);
592 }
593 if (ga_grow(&ga, 1) == FAIL)
594 goto failed;
595 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
596 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
597
598 if (lnum == 0)
599 {
600 /* Delete the empty line that was in the empty buffer. */
601 curbuf = term->tl_buffer;
602 ml_delete(2, FALSE);
603 curbuf = curwin->w_buffer;
604 }
605
606failed:
607 ga_clear(&ga);
608}
609
610/*
611 * Add the current lines of the terminal to scrollback and to the buffer.
612 * Called after the job has ended and when switching to Terminal mode.
613 */
614 static void
615move_terminal_to_buffer(term_T *term)
616{
617 win_T *wp;
618 int len;
619 int lines_skipped = 0;
620 VTermPos pos;
621 VTermScreenCell cell;
622 VTermScreenCell *p;
623 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
624
625 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
626 {
627 len = 0;
628 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
629 if (vterm_screen_get_cell(screen, pos, &cell) != 0
630 && cell.chars[0] != NUL)
631 len = pos.col + 1;
632
633 if (len == 0)
634 ++lines_skipped;
635 else
636 {
637 while (lines_skipped > 0)
638 {
639 /* Line was skipped, add an empty line. */
640 --lines_skipped;
641 if (ga_grow(&term->tl_scrollback, 1) == OK)
642 {
643 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
644 + term->tl_scrollback.ga_len;
645
646 line->sb_cols = 0;
647 line->sb_cells = NULL;
648 ++term->tl_scrollback.ga_len;
649
650 add_scrollback_line_to_buffer(term);
651 }
652 }
653
654 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
655 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
656 {
657 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
658 + term->tl_scrollback.ga_len;
659
660 for (pos.col = 0; pos.col < len; ++pos.col)
661 {
662 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
663 vim_memset(p + pos.col, 0, sizeof(cell));
664 else
665 p[pos.col] = cell;
666 }
667 line->sb_cols = len;
668 line->sb_cells = p;
669 ++term->tl_scrollback.ga_len;
670
671 add_scrollback_line_to_buffer(term);
672 }
673 else
674 vim_free(p);
675 }
676 }
677
678 FOR_ALL_WINDOWS(wp)
679 {
680 if (wp->w_buffer == term->tl_buffer)
681 {
682 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
683 wp->w_cursor.col = 0;
684 wp->w_valid = 0;
685 redraw_win_later(wp, NOT_VALID);
686 }
687 }
688}
689
690 static void
691set_terminal_mode(term_T *term, int on)
692{
693 term->tl_terminal_mode = on;
694 vim_free(term->tl_status_text);
695 term->tl_status_text = NULL;
696 if (term->tl_buffer == curbuf)
697 maketitle();
698}
699
700/*
701 * Called after the job if finished and Terminal mode is not active:
702 * Move the vterm contents into the scrollback buffer and free the vterm.
703 */
704 static void
705cleanup_vterm(term_T *term)
706{
707 move_terminal_to_buffer(term);
708 term_free_vterm(term);
709 set_terminal_mode(term, FALSE);
710}
711
712/*
713 * Switch from sending keys to the job to Terminal-Normal mode.
714 * Suspends updating the terminal window.
715 */
716 static void
717term_enter_terminal_mode()
718{
719 term_T *term = curbuf->b_term;
720
721 /* Append the current terminal contents to the buffer. */
722 move_terminal_to_buffer(term);
723
724 set_terminal_mode(term, TRUE);
725}
726
727/*
728 * Returns TRUE if the current window contains a terminal and we are in
729 * Terminal-Normal mode.
730 */
731 int
732term_in_terminal_mode()
733{
734 term_T *term = curbuf->b_term;
735
736 return term != NULL && term->tl_terminal_mode;
737}
738
739/*
740 * Switch from Terminal-Normal mode to sending keys to the job.
741 * Restores updating the terminal window.
742 */
743 void
744term_leave_terminal_mode()
745{
746 term_T *term = curbuf->b_term;
747 sb_line_T *line;
748 garray_T *gap;
749
750 /* Remove the terminal contents from the scrollback and the buffer. */
751 gap = &term->tl_scrollback;
752 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
753 {
754 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
755 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
756 vim_free(line->sb_cells);
757 --gap->ga_len;
758 if (gap->ga_len == 0)
759 break;
760 }
761 check_cursor();
762
763 set_terminal_mode(term, FALSE);
764
765 if (term->tl_channel_closed)
766 cleanup_vterm(term);
767 redraw_buf_and_status_later(curbuf, NOT_VALID);
768}
769
770/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200771 * Get a key from the user without mapping.
772 * TODO: use terminal mode mappings.
773 */
774 static int
775term_vgetc()
776{
777 int c;
778
779 ++no_mapping;
780 ++allow_keys;
781 got_int = FALSE;
782 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200783 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200784 --no_mapping;
785 --allow_keys;
786 return c;
787}
788
789/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200790 * Send keys to terminal.
791 */
792 static int
793send_keys_to_term(term_T *term, int c, int typed)
794{
795 char msg[KEY_BUF_LEN];
796 size_t len;
797 static int mouse_was_outside = FALSE;
798 int dragging_outside = FALSE;
799
800 /* Catch keys that need to be handled as in Normal mode. */
801 switch (c)
802 {
803 case NUL:
804 case K_ZERO:
805 if (typed)
806 stuffcharReadbuff(c);
807 return FAIL;
808
809 case K_IGNORE:
810 return FAIL;
811
812 case K_LEFTDRAG:
813 case K_MIDDLEDRAG:
814 case K_RIGHTDRAG:
815 case K_X1DRAG:
816 case K_X2DRAG:
817 dragging_outside = mouse_was_outside;
818 /* FALLTHROUGH */
819 case K_LEFTMOUSE:
820 case K_LEFTMOUSE_NM:
821 case K_LEFTRELEASE:
822 case K_LEFTRELEASE_NM:
823 case K_MIDDLEMOUSE:
824 case K_MIDDLERELEASE:
825 case K_RIGHTMOUSE:
826 case K_RIGHTRELEASE:
827 case K_X1MOUSE:
828 case K_X1RELEASE:
829 case K_X2MOUSE:
830 case K_X2RELEASE:
831 if (mouse_row < W_WINROW(curwin)
832 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
833 || mouse_col < W_WINCOL(curwin)
834 || mouse_col >= W_ENDCOL(curwin)
835 || dragging_outside)
836 {
837 /* click outside the current window */
838 if (typed)
839 {
840 stuffcharReadbuff(c);
841 mouse_was_outside = TRUE;
842 }
843 return FAIL;
844 }
845 }
846 if (typed)
847 mouse_was_outside = FALSE;
848
849 /* Convert the typed key to a sequence of bytes for the job. */
850 len = term_convert_key(term, c, msg);
851 if (len > 0)
852 /* TODO: if FAIL is returned, stop? */
853 channel_send(term->tl_job->jv_channel, PART_IN,
854 (char_u *)msg, (int)len, NULL);
855
856 return OK;
857}
858
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200859 static void
860position_cursor(win_T *wp, VTermPos *pos)
861{
862 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
863 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
864 wp->w_valid |= (VALID_WCOL|VALID_WROW);
865}
866
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200867/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200868 * Handle CTRL-W "": send register contents to the job.
869 */
870 static void
871term_paste_register(int prev_c UNUSED)
872{
873 int c;
874 list_T *l;
875 listitem_T *item;
876 long reglen = 0;
877 int type;
878
879#ifdef FEAT_CMDL_INFO
880 if (add_to_showcmd(prev_c))
881 if (add_to_showcmd('"'))
882 out_flush();
883#endif
884 c = term_vgetc();
885#ifdef FEAT_CMDL_INFO
886 clear_showcmd();
887#endif
888
889 /* CTRL-W "= prompt for expression to evaluate. */
890 if (c == '=' && get_expr_register() != '=')
891 return;
892
893 l = (list_T *)get_reg_contents(c, GREG_LIST);
894 if (l != NULL)
895 {
896 type = get_reg_type(c, &reglen);
897 for (item = l->lv_first; item != NULL; item = item->li_next)
898 {
899 char_u *s = get_tv_string(&item->li_tv);
900
901 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
902 s, STRLEN(s), NULL);
903 if (item->li_next != NULL || type == MLINE)
904 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
905 (char_u *)"\r", 1, NULL);
906 }
907 list_free(l);
908 }
909}
910
911/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200912 * Returns TRUE if the current window contains a terminal and we are sending
913 * keys to the job.
914 */
915 int
916term_use_loop()
917{
918 term_T *term = curbuf->b_term;
919
920 return term != NULL
921 && !term->tl_terminal_mode
922 && term->tl_vterm != NULL
923 && term_job_running(term);
924}
925
926/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200927 * Wait for input and send it to the job.
928 * Return when the start of a CTRL-W command is typed or anything else that
929 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200930 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
931 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200932 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200933 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200934terminal_loop(void)
935{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200936 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200937 int termkey = 0;
938
939 if (*curwin->w_p_tk != NUL)
940 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200941 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200942
943 for (;;)
944 {
945 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200946 /* Repeat redrawing in case a message is received while redrawing. */
947 while (curwin->w_redr_type != 0)
948 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200949 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200950
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200951 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200952 if (curbuf->b_term->tl_vterm == NULL
953 || !term_job_running(curbuf->b_term))
954 /* job finished while waiting for a character */
955 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200956
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200957 if (c == (termkey == 0 ? Ctrl_W : termkey))
958 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200959 int prev_c = c;
960
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200961#ifdef FEAT_CMDL_INFO
962 if (add_to_showcmd(c))
963 out_flush();
964#endif
965 c = term_vgetc();
966#ifdef FEAT_CMDL_INFO
967 clear_showcmd();
968#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200969 if (curbuf->b_term->tl_vterm == NULL
970 || !term_job_running(curbuf->b_term))
971 /* job finished while waiting for a character */
972 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200973
974 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200975 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200976 /* "CTRL-W .": send CTRL-W to the job */
977 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200978 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200979 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200980 {
981 term_enter_terminal_mode();
982 return FAIL;
983 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200984 else if (c == '"')
985 {
986 term_paste_register(prev_c);
987 continue;
988 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200989 else if (termkey == 0 || c != termkey)
990 {
991 stuffcharReadbuff(Ctrl_W);
992 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200993 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200994 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200995 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200996 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
997 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200998 }
Bram Moolenaard85f2712017-07-28 21:51:57 +0200999 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001000}
1001
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001002/*
1003 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001004 * This updates the title and status, but does not close the vter, because
1005 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001006 */
1007 void
1008term_job_ended(job_T *job)
1009{
1010 term_T *term;
1011 int did_one = FALSE;
1012
1013 for (term = first_term; term != NULL; term = term->tl_next)
1014 if (term->tl_job == job)
1015 {
1016 vim_free(term->tl_title);
1017 term->tl_title = NULL;
1018 vim_free(term->tl_status_text);
1019 term->tl_status_text = NULL;
1020 redraw_buf_and_status_later(term->tl_buffer, VALID);
1021 did_one = TRUE;
1022 }
1023 if (did_one)
1024 redraw_statuslines();
1025 if (curbuf->b_term != NULL)
1026 {
1027 if (curbuf->b_term->tl_job == job)
1028 maketitle();
1029 update_cursor(curbuf->b_term, TRUE);
1030 }
1031}
1032
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001033 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001034may_toggle_cursor(term_T *term)
1035{
1036 if (curbuf == term->tl_buffer)
1037 {
1038 if (term->tl_cursor_visible)
1039 cursor_on();
1040 else
1041 cursor_off();
1042 }
1043}
1044
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001045 static int
1046handle_damage(VTermRect rect, void *user)
1047{
1048 term_T *term = (term_T *)user;
1049
1050 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1051 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1052 redraw_buf_later(term->tl_buffer, NOT_VALID);
1053 return 1;
1054}
1055
1056 static int
1057handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1058{
1059 term_T *term = (term_T *)user;
1060
1061 /* TODO */
1062 redraw_buf_later(term->tl_buffer, NOT_VALID);
1063 return 1;
1064}
1065
1066 static int
1067handle_movecursor(
1068 VTermPos pos,
1069 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001070 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001071 void *user)
1072{
1073 term_T *term = (term_T *)user;
1074 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001075
1076 term->tl_cursor_pos = pos;
1077 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001078
1079 FOR_ALL_WINDOWS(wp)
1080 {
1081 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001082 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001083 }
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001084 if (term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001085 {
1086 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001087 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001088 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001089
1090 return 1;
1091}
1092
Bram Moolenaar21554412017-07-24 21:44:43 +02001093 static int
1094handle_settermprop(
1095 VTermProp prop,
1096 VTermValue *value,
1097 void *user)
1098{
1099 term_T *term = (term_T *)user;
1100
1101 switch (prop)
1102 {
1103 case VTERM_PROP_TITLE:
1104 vim_free(term->tl_title);
1105 term->tl_title = vim_strsave((char_u *)value->string);
1106 vim_free(term->tl_status_text);
1107 term->tl_status_text = NULL;
1108 if (term == curbuf->b_term)
1109 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001110 break;
1111
1112 case VTERM_PROP_CURSORVISIBLE:
1113 term->tl_cursor_visible = value->boolean;
1114 may_toggle_cursor(term);
1115 out_flush();
1116 break;
1117
Bram Moolenaar21554412017-07-24 21:44:43 +02001118 default:
1119 break;
1120 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001121 /* Always return 1, otherwise vterm doesn't store the value internally. */
1122 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001123}
1124
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001125/*
1126 * The job running in the terminal resized the terminal.
1127 */
1128 static int
1129handle_resize(int rows, int cols, void *user)
1130{
1131 term_T *term = (term_T *)user;
1132 win_T *wp;
1133
1134 term->tl_rows = rows;
1135 term->tl_cols = cols;
1136 FOR_ALL_WINDOWS(wp)
1137 {
1138 if (wp->w_buffer == term->tl_buffer)
1139 {
1140 win_setheight_win(rows, wp);
1141 win_setwidth_win(cols, wp);
1142 }
1143 }
1144
1145 redraw_buf_later(term->tl_buffer, NOT_VALID);
1146 return 1;
1147}
1148
Bram Moolenaard85f2712017-07-28 21:51:57 +02001149/*
1150 * Handle a line that is pushed off the top of the screen.
1151 */
1152 static int
1153handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1154{
1155 term_T *term = (term_T *)user;
1156
1157 /* TODO: Limit the number of lines that are stored. */
1158 /* TODO: put the text in the buffer. */
1159 if (ga_grow(&term->tl_scrollback, 1) == OK)
1160 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001161 VTermScreenCell *p = NULL;
1162 int len = 0;
1163 int i;
1164 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001165
1166 /* do not store empty cells at the end */
1167 for (i = 0; i < cols; ++i)
1168 if (cells[i].chars[0] != 0)
1169 len = i + 1;
1170
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001171 if (len > 0)
1172 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001173 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001174 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001175
1176 line = (sb_line_T *)term->tl_scrollback.ga_data
1177 + term->tl_scrollback.ga_len;
1178 line->sb_cols = len;
1179 line->sb_cells = p;
1180 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001181 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001182
1183 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001184 }
1185 return 0; /* ignored */
1186}
1187
Bram Moolenaar21554412017-07-24 21:44:43 +02001188static VTermScreenCallbacks screen_callbacks = {
1189 handle_damage, /* damage */
1190 handle_moverect, /* moverect */
1191 handle_movecursor, /* movecursor */
1192 handle_settermprop, /* settermprop */
1193 NULL, /* bell */
1194 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001195 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001196 NULL /* sb_popline */
1197};
1198
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001199/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001200 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001201 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001202 */
1203 void
1204term_channel_closed(channel_T *ch)
1205{
1206 term_T *term;
1207 int did_one = FALSE;
1208
1209 for (term = first_term; term != NULL; term = term->tl_next)
1210 if (term->tl_job == ch->ch_job)
1211 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001212 term->tl_channel_closed = TRUE;
1213
Bram Moolenaard85f2712017-07-28 21:51:57 +02001214 vim_free(term->tl_title);
1215 term->tl_title = NULL;
1216 vim_free(term->tl_status_text);
1217 term->tl_status_text = NULL;
1218
Bram Moolenaar423802d2017-07-30 16:52:24 +02001219 /* Unless in Terminal-Normal mode: clear the vterm. */
1220 if (!term->tl_terminal_mode)
1221 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001222
1223 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1224 did_one = TRUE;
1225 }
1226 if (did_one)
1227 {
1228 redraw_statuslines();
1229
1230 /* Need to break out of vgetc(). */
1231 ins_char_typebuf(K_IGNORE);
1232
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001233 term = curbuf->b_term;
1234 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001235 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001236 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001237 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001238 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001239 }
1240 }
1241}
1242
1243/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001244 * Reverse engineer the RGB value into a cterm color index.
1245 * First color is 1. Return 0 if no match found.
1246 */
1247 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001248color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001249{
1250 int red = color->red;
1251 int blue = color->blue;
1252 int green = color->green;
1253
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001254 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001255 if (red == 0)
1256 {
1257 if (green == 0)
1258 {
1259 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001260 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001261 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001262 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001263 }
1264 else if (green == 224)
1265 {
1266 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001267 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001268 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001269 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001270 }
1271 }
1272 else if (red == 224)
1273 {
1274 if (green == 0)
1275 {
1276 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001277 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001278 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001279 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001280 }
1281 else if (green == 224)
1282 {
1283 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001284 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001285 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001286 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001287 }
1288 }
1289 else if (red == 128)
1290 {
1291 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001292 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001293 }
1294 else if (red == 255)
1295 {
1296 if (green == 64)
1297 {
1298 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001299 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001300 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001301 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001302 }
1303 else if (green == 255)
1304 {
1305 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001306 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001307 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001308 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001309 }
1310 }
1311 else if (red == 64)
1312 {
1313 if (green == 64)
1314 {
1315 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001316 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001317 }
1318 else if (green == 255)
1319 {
1320 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001321 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001322 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001323 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001324 }
1325 }
1326 if (t_colors >= 256)
1327 {
1328 if (red == blue && red == green)
1329 {
1330 /* 24-color greyscale */
1331 static int cutoff[23] = {
1332 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1333 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1334 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1335 int i;
1336
1337 for (i = 0; i < 23; ++i)
1338 if (red < cutoff[i])
1339 return i + 233;
1340 return 256;
1341 }
1342
1343 /* 216-color cube */
1344 return 17 + ((red + 25) / 0x33) * 36
1345 + ((green + 25) / 0x33) * 6
1346 + (blue + 25) / 0x33;
1347 }
1348 return 0;
1349}
1350
1351/*
1352 * Convert the attributes of a vterm cell into an attribute index.
1353 */
1354 static int
1355cell2attr(VTermScreenCell *cell)
1356{
1357 int attr = 0;
1358
1359 if (cell->attrs.bold)
1360 attr |= HL_BOLD;
1361 if (cell->attrs.underline)
1362 attr |= HL_UNDERLINE;
1363 if (cell->attrs.italic)
1364 attr |= HL_ITALIC;
1365 if (cell->attrs.strike)
1366 attr |= HL_STANDOUT;
1367 if (cell->attrs.reverse)
1368 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001369
1370#ifdef FEAT_GUI
1371 if (gui.in_use)
1372 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001373 guicolor_T fg, bg;
1374
1375 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1376 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1377 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001378 }
1379 else
1380#endif
1381#ifdef FEAT_TERMGUICOLORS
1382 if (p_tgc)
1383 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001384 guicolor_T fg, bg;
1385
1386 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1387 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1388
1389 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001390 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001391 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001392#endif
1393 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001394 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1395 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001396 }
1397 return 0;
1398}
1399
1400/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001401 * Called to update the window that contains a terminal.
1402 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001403 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001404 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001405term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001406{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001407 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001408 VTerm *vterm;
1409 VTermScreen *screen;
1410 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001411 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001412
Bram Moolenaar423802d2017-07-30 16:52:24 +02001413 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001414 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001415
Bram Moolenaard85f2712017-07-28 21:51:57 +02001416 vterm = term->tl_vterm;
1417 screen = vterm_obtain_screen(vterm);
1418 state = vterm_obtain_state(vterm);
1419
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001420 /*
1421 * If the window was resized a redraw will be triggered and we get here.
1422 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1423 */
1424 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1425 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001426 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001427 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1428 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1429 win_T *twp;
1430
1431 FOR_ALL_WINDOWS(twp)
1432 {
1433 /* When more than one window shows the same terminal, use the
1434 * smallest size. */
1435 if (twp->w_buffer == term->tl_buffer)
1436 {
1437 if (!term->tl_rows_fixed && rows > twp->w_height)
1438 rows = twp->w_height;
1439 if (!term->tl_cols_fixed && cols > twp->w_width)
1440 cols = twp->w_width;
1441 }
1442 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001443
1444 vterm_set_size(vterm, rows, cols);
1445 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1446 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001447 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001448 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001449
1450 /* The cursor may have been moved when resizing. */
1451 vterm_state_get_cursorpos(state, &pos);
1452 position_cursor(wp, &pos);
1453
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001454 /* TODO: Only redraw what changed. */
1455 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001456 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001457 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001458 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001459
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001460 if (pos.row < term->tl_rows)
1461 {
1462 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001463 {
1464 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001465 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001466
Bram Moolenaareeac6772017-07-23 15:48:37 +02001467 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1468 vim_memset(&cell, 0, sizeof(cell));
1469
1470 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001471 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001472 if (c == NUL)
1473 {
1474 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001475#if defined(FEAT_MBYTE)
1476 if (enc_utf8)
1477 ScreenLinesUC[off] = NUL;
1478#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001479 }
1480 else
1481 {
1482#if defined(FEAT_MBYTE)
1483 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001484 {
1485 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001486 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001487 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001488 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001489 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001490 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001491 if (enc_utf8)
1492 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001493 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001494#else
1495 ScreenLines[off] = c;
1496#endif
1497 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001498 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001499
1500 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001501 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001502 if (cell.width == 2)
1503 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001504 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001505#if defined(FEAT_MBYTE)
1506 if (enc_utf8)
1507 ScreenLinesUC[off] = NUL;
1508#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001509 ++pos.col;
1510 ++off;
1511 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001512 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001513 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001514 else
1515 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001516
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001517 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1518 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001519 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001520
1521 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001522}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001523
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001524/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001525 * Return TRUE if "wp" is a terminal window where the job has finished.
1526 */
1527 int
1528term_is_finished(buf_T *buf)
1529{
1530 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1531}
1532
1533/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001534 * Return TRUE if "wp" is a terminal window where the job has finished or we
1535 * are in Terminal-Normal mode.
1536 */
1537 int
1538term_show_buffer(buf_T *buf)
1539{
1540 term_T *term = buf->b_term;
1541
1542 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1543}
1544
1545/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001546 * The current buffer is going to be changed. If there is terminal
1547 * highlighting remove it now.
1548 */
1549 void
1550term_change_in_curbuf(void)
1551{
1552 term_T *term = curbuf->b_term;
1553
1554 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1555 {
1556 free_scrollback(term);
1557 redraw_buf_later(term->tl_buffer, NOT_VALID);
1558 }
1559}
1560
1561/*
1562 * Get the screen attribute for a position in the buffer.
1563 */
1564 int
1565term_get_attr(buf_T *buf, linenr_T lnum, int col)
1566{
1567 term_T *term = buf->b_term;
1568 sb_line_T *line;
1569
Bram Moolenaar70229f92017-07-29 16:01:53 +02001570 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001571 return 0;
1572 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1573 if (col >= line->sb_cols)
1574 return 0;
1575 return cell2attr(line->sb_cells + col);
1576}
1577
1578/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001579 * Set job options common for Unix and MS-Windows.
1580 */
1581 static void
1582setup_job_options(jobopt_T *opt, int rows, int cols)
1583{
1584 clear_job_options(opt);
1585 opt->jo_mode = MODE_RAW;
1586 opt->jo_out_mode = MODE_RAW;
1587 opt->jo_err_mode = MODE_RAW;
1588 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001589
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001590 opt->jo_io[PART_OUT] = JIO_BUFFER;
1591 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001592 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1593
1594 opt->jo_modifiable[PART_OUT] = 0;
1595 opt->jo_modifiable[PART_ERR] = 0;
1596 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1597
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001598 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1599 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001600 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001601 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1602
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001603 opt->jo_term_rows = rows;
1604 opt->jo_term_cols = cols;
1605}
1606
1607/*
1608 * Create a new vterm and initialize it.
1609 */
1610 static void
1611create_vterm(term_T *term, int rows, int cols)
1612{
1613 VTerm *vterm;
1614 VTermScreen *screen;
1615
1616 vterm = vterm_new(rows, cols);
1617 term->tl_vterm = vterm;
1618 screen = vterm_obtain_screen(vterm);
1619 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1620 /* TODO: depends on 'encoding'. */
1621 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001622
1623 /* Vterm uses a default black background. Set it to white when
1624 * 'background' is "light". */
1625 if (*p_bg == 'l')
1626 {
1627 VTermColor fg, bg;
1628
1629 fg.red = fg.green = fg.blue = 0;
1630 bg.red = bg.green = bg.blue = 255;
1631 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1632 }
1633
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001634 /* Required to initialize most things. */
1635 vterm_screen_reset(screen, 1 /* hard */);
1636}
1637
Bram Moolenaar21554412017-07-24 21:44:43 +02001638/*
1639 * Return the text to show for the buffer name and status.
1640 */
1641 char_u *
1642term_get_status_text(term_T *term)
1643{
1644 if (term->tl_status_text == NULL)
1645 {
1646 char_u *txt;
1647 size_t len;
1648
Bram Moolenaar423802d2017-07-30 16:52:24 +02001649 if (term->tl_terminal_mode)
1650 {
1651 if (term_job_running(term))
1652 txt = (char_u *)_("Terminal");
1653 else
1654 txt = (char_u *)_("Terminal-finished");
1655 }
1656 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001657 txt = term->tl_title;
1658 else if (term_job_running(term))
1659 txt = (char_u *)_("running");
1660 else
1661 txt = (char_u *)_("finished");
1662 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001663 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001664 if (term->tl_status_text != NULL)
1665 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1666 term->tl_buffer->b_fname, txt);
1667 }
1668 return term->tl_status_text;
1669}
1670
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001671/*
1672 * Mark references in jobs of terminals.
1673 */
1674 int
1675set_ref_in_term(int copyID)
1676{
1677 int abort = FALSE;
1678 term_T *term;
1679 typval_T tv;
1680
1681 for (term = first_term; term != NULL; term = term->tl_next)
1682 if (term->tl_job != NULL)
1683 {
1684 tv.v_type = VAR_JOB;
1685 tv.vval.v_job = term->tl_job;
1686 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1687 }
1688 return abort;
1689}
1690
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001691/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001692 * Get the buffer from the first argument in "argvars".
1693 * Returns NULL when the buffer is not for a terminal window.
1694 */
1695 static buf_T *
1696term_get_buf(typval_T *argvars)
1697{
1698 buf_T *buf;
1699
1700 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1701 ++emsg_off;
1702 buf = get_buf_tv(&argvars[0], FALSE);
1703 --emsg_off;
1704 if (buf == NULL || buf->b_term == NULL)
1705 return NULL;
1706 return buf;
1707}
1708
1709/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001710 * "term_getattr(attr, name)" function
1711 */
1712 void
1713f_term_getattr(typval_T *argvars, typval_T *rettv)
1714{
1715 int attr;
1716 size_t i;
1717 char_u *name;
1718
1719 static struct {
1720 char *name;
1721 int attr;
1722 } attrs[] = {
1723 {"bold", HL_BOLD},
1724 {"italic", HL_ITALIC},
1725 {"underline", HL_UNDERLINE},
1726 {"strike", HL_STANDOUT},
1727 {"reverse", HL_INVERSE},
1728 };
1729
1730 attr = get_tv_number(&argvars[0]);
1731 name = get_tv_string_chk(&argvars[1]);
1732 if (name == NULL)
1733 return;
1734
1735 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1736 if (STRCMP(name, attrs[i].name) == 0)
1737 {
1738 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1739 break;
1740 }
1741}
1742
1743/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001744 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001745 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001746 void
1747f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001748{
Bram Moolenaar97870002017-07-30 18:28:38 +02001749 buf_T *buf = term_get_buf(argvars);
1750 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001751
Bram Moolenaar97870002017-07-30 18:28:38 +02001752 if (rettv_list_alloc(rettv) == FAIL)
1753 return;
1754 if (buf == NULL)
1755 return;
1756
1757 l = rettv->vval.v_list;
1758 list_append_number(l, buf->b_term->tl_cursor_pos.row);
1759 list_append_number(l, buf->b_term->tl_cursor_pos.col);
1760 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001761}
1762
1763/*
1764 * "term_getjob(buf)" function
1765 */
1766 void
1767f_term_getjob(typval_T *argvars, typval_T *rettv)
1768{
1769 buf_T *buf = term_get_buf(argvars);
1770
1771 rettv->v_type = VAR_JOB;
1772 rettv->vval.v_job = NULL;
1773 if (buf == NULL)
1774 return;
1775
1776 rettv->vval.v_job = buf->b_term->tl_job;
1777 if (rettv->vval.v_job != NULL)
1778 ++rettv->vval.v_job->jv_refcount;
1779}
1780
1781/*
1782 * "term_getline(buf, row)" function
1783 */
1784 void
1785f_term_getline(typval_T *argvars, typval_T *rettv)
1786{
1787 buf_T *buf = term_get_buf(argvars);
1788 term_T *term;
1789 int row;
1790
1791 rettv->v_type = VAR_STRING;
1792 if (buf == NULL)
1793 return;
1794 term = buf->b_term;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001795 if (argvars[1].v_type == VAR_UNKNOWN)
1796 row = term->tl_cursor_pos.row;
1797 else
1798 row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001799
1800 if (term->tl_vterm == NULL)
1801 {
1802 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1803
1804 /* vterm is finished, get the text from the buffer */
1805 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1806 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1807 }
1808 else
1809 {
1810 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1811 VTermRect rect;
1812 int len;
1813 char_u *p;
1814
1815 len = term->tl_cols * MB_MAXBYTES + 1;
1816 p = alloc(len);
1817 if (p == NULL)
1818 return;
1819 rettv->vval.v_string = p;
1820
1821 rect.start_col = 0;
1822 rect.end_col = term->tl_cols;
1823 rect.start_row = row;
1824 rect.end_row = row + 1;
1825 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1826 }
1827}
1828
1829/*
1830 * "term_getsize(buf)" function
1831 */
1832 void
1833f_term_getsize(typval_T *argvars, typval_T *rettv)
1834{
1835 buf_T *buf = term_get_buf(argvars);
1836 list_T *l;
1837
1838 if (rettv_list_alloc(rettv) == FAIL)
1839 return;
1840 if (buf == NULL)
1841 return;
1842
1843 l = rettv->vval.v_list;
1844 list_append_number(l, buf->b_term->tl_rows);
1845 list_append_number(l, buf->b_term->tl_cols);
1846}
1847
1848/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001849 * "term_getstatus(buf)" function
1850 */
1851 void
1852f_term_getstatus(typval_T *argvars, typval_T *rettv)
1853{
1854 buf_T *buf = term_get_buf(argvars);
1855 term_T *term;
1856 char_u val[100];
1857
1858 rettv->v_type = VAR_STRING;
1859 if (buf == NULL)
1860 return;
1861 term = buf->b_term;
1862
1863 if (term_job_running(term))
1864 STRCPY(val, "running");
1865 else
1866 STRCPY(val, "finished");
1867 if (term->tl_terminal_mode)
1868 STRCAT(val, ",terminal");
1869 rettv->vval.v_string = vim_strsave(val);
1870}
1871
1872/*
1873 * "term_gettitle(buf)" function
1874 */
1875 void
1876f_term_gettitle(typval_T *argvars, typval_T *rettv)
1877{
1878 buf_T *buf = term_get_buf(argvars);
1879
1880 rettv->v_type = VAR_STRING;
1881 if (buf == NULL)
1882 return;
1883
1884 if (buf->b_term->tl_title != NULL)
1885 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1886}
1887
1888/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001889 * "term_list()" function
1890 */
1891 void
1892f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1893{
1894 term_T *tp;
1895 list_T *l;
1896
1897 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1898 return;
1899
1900 l = rettv->vval.v_list;
1901 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1902 if (tp != NULL && tp->tl_buffer != NULL)
1903 if (list_append_number(l,
1904 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1905 return;
1906}
1907
1908/*
1909 * "term_scrape(buf, row)" function
1910 */
1911 void
1912f_term_scrape(typval_T *argvars, typval_T *rettv)
1913{
1914 buf_T *buf = term_get_buf(argvars);
1915 VTermScreen *screen = NULL;
1916 VTermPos pos;
1917 list_T *l;
1918 term_T *term;
1919
1920 if (rettv_list_alloc(rettv) == FAIL)
1921 return;
1922 if (buf == NULL)
1923 return;
1924 term = buf->b_term;
1925 if (term->tl_vterm != NULL)
1926 screen = vterm_obtain_screen(term->tl_vterm);
1927
1928 l = rettv->vval.v_list;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001929 if (argvars[1].v_type == VAR_UNKNOWN)
1930 pos.row = term->tl_cursor_pos.row;
1931 else
1932 pos.row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001933 for (pos.col = 0; pos.col < term->tl_cols; )
1934 {
1935 dict_T *dcell;
1936 VTermScreenCell cell;
1937 char_u rgb[8];
1938 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1939 int off = 0;
1940 int i;
1941
1942 if (screen == NULL)
1943 {
1944 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1945 sb_line_T *line;
1946
1947 /* vterm has finished, get the cell from scrollback */
1948 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1949 break;
1950 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1951 if (pos.col >= line->sb_cols)
1952 break;
1953 cell = line->sb_cells[pos.col];
1954 }
1955 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1956 break;
1957 dcell = dict_alloc();
1958 list_append_dict(l, dcell);
1959
1960 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1961 {
1962 if (cell.chars[i] == 0)
1963 break;
1964 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1965 }
1966 mbs[off] = NUL;
1967 dict_add_nr_str(dcell, "chars", 0, mbs);
1968
1969 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1970 cell.fg.red, cell.fg.green, cell.fg.blue);
1971 dict_add_nr_str(dcell, "fg", 0, rgb);
1972 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1973 cell.bg.red, cell.bg.green, cell.bg.blue);
1974 dict_add_nr_str(dcell, "bg", 0, rgb);
1975
1976 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
1977 dict_add_nr_str(dcell, "width", cell.width, NULL);
1978
1979 ++pos.col;
1980 if (cell.width == 2)
1981 ++pos.col;
1982 }
1983}
1984
1985/*
1986 * "term_sendkeys(buf, keys)" function
1987 */
1988 void
1989f_term_sendkeys(typval_T *argvars, typval_T *rettv)
1990{
1991 buf_T *buf = term_get_buf(argvars);
1992 char_u *msg;
1993 term_T *term;
1994
1995 rettv->v_type = VAR_UNKNOWN;
1996 if (buf == NULL)
1997 return;
1998
1999 msg = get_tv_string_chk(&argvars[1]);
2000 if (msg == NULL)
2001 return;
2002 term = buf->b_term;
2003 if (term->tl_vterm == NULL)
2004 return;
2005
2006 while (*msg != NUL)
2007 {
2008 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2009 msg += MB_PTR2LEN(msg);
2010 }
2011
2012 /* TODO: only update once in a while. */
2013 update_screen(0);
2014 if (buf == curbuf)
2015 update_cursor(term, TRUE);
2016}
2017
2018/*
2019 * "term_start(command, options)" function
2020 */
2021 void
2022f_term_start(typval_T *argvars, typval_T *rettv)
2023{
2024 char_u *cmd = get_tv_string_chk(&argvars[0]);
2025 exarg_T ea;
2026
2027 if (cmd == NULL)
2028 return;
2029 ea.arg = cmd;
2030 ex_terminal(&ea);
2031
2032 if (curbuf->b_term != NULL)
2033 rettv->vval.v_number = curbuf->b_fnum;
2034}
2035
2036/*
2037 * "term_wait" function
2038 */
2039 void
2040f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2041{
2042 buf_T *buf = term_get_buf(argvars);
2043
2044 if (buf == NULL)
2045 return;
2046
2047 /* Get the job status, this will detect a job that finished. */
2048 if (buf->b_term->tl_job != NULL)
2049 (void)job_status(buf->b_term->tl_job);
2050
2051 /* Check for any pending channel I/O. */
2052 vpeekc_any();
2053 ui_delay(10L, FALSE);
2054
2055 /* Flushing messages on channels is hopefully sufficient.
2056 * TODO: is there a better way? */
2057 parse_queued_messages();
2058}
2059
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002060# ifdef WIN3264
2061
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002062/**************************************
2063 * 2. MS-Windows implementation.
2064 */
2065
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002066#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2067#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2068
Bram Moolenaar8a773062017-07-24 22:29:21 +02002069void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002070void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002071void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002072BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2073void (*winpty_config_set_initial_size)(void*, int, int);
2074LPCWSTR (*winpty_conin_name)(void*);
2075LPCWSTR (*winpty_conout_name)(void*);
2076LPCWSTR (*winpty_conerr_name)(void*);
2077void (*winpty_free)(void*);
2078void (*winpty_config_free)(void*);
2079void (*winpty_spawn_config_free)(void*);
2080void (*winpty_error_free)(void*);
2081LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002082BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002083
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002084#define WINPTY_DLL "winpty.dll"
2085
2086static HINSTANCE hWinPtyDLL = NULL;
2087
2088 int
2089dyn_winpty_init(void)
2090{
2091 int i;
2092 static struct
2093 {
2094 char *name;
2095 FARPROC *ptr;
2096 } winpty_entry[] =
2097 {
2098 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2099 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2100 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2101 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2102 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2103 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2104 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2105 {"winpty_free", (FARPROC*)&winpty_free},
2106 {"winpty_open", (FARPROC*)&winpty_open},
2107 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2108 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2109 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2110 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002111 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002112 {NULL, NULL}
2113 };
2114
2115 /* No need to initialize twice. */
2116 if (hWinPtyDLL)
2117 return 1;
2118 /* Load winpty.dll */
2119 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2120 if (!hWinPtyDLL)
2121 {
2122 EMSG2(_(e_loadlib), WINPTY_DLL);
2123 return 0;
2124 }
2125 for (i = 0; winpty_entry[i].name != NULL
2126 && winpty_entry[i].ptr != NULL; ++i)
2127 {
2128 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2129 winpty_entry[i].name)) == NULL)
2130 {
2131 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2132 return 0;
2133 }
2134 }
2135
2136 return 1;
2137}
2138
2139/*
2140 * Create a new terminal of "rows" by "cols" cells.
2141 * Store a reference in "term".
2142 * Return OK or FAIL.
2143 */
2144 static int
2145term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2146{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002147 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002148 channel_T *channel = NULL;
2149 job_T *job = NULL;
2150 jobopt_T opt;
2151 DWORD error;
2152 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2153 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002154 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002155
2156 if (!dyn_winpty_init())
2157 return FAIL;
2158
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002159 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002160 if (p == NULL)
2161 return FAIL;
2162
2163 job = job_alloc();
2164 if (job == NULL)
2165 goto failed;
2166
2167 channel = add_channel();
2168 if (channel == NULL)
2169 goto failed;
2170
2171 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2172 if (term->tl_winpty_config == NULL)
2173 goto failed;
2174
2175 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2176 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2177 if (term->tl_winpty == NULL)
2178 goto failed;
2179
2180 spawn_config = winpty_spawn_config_new(
2181 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2182 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2183 NULL,
2184 p,
2185 NULL,
2186 NULL,
2187 &winpty_err);
2188 if (spawn_config == NULL)
2189 goto failed;
2190
2191 channel = add_channel();
2192 if (channel == NULL)
2193 goto failed;
2194
2195 job = job_alloc();
2196 if (job == NULL)
2197 goto failed;
2198
2199 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2200 &child_thread_handle, &error, &winpty_err))
2201 goto failed;
2202
2203 channel_set_pipes(channel,
2204 (sock_T) CreateFileW(
2205 winpty_conin_name(term->tl_winpty),
2206 GENERIC_WRITE, 0, NULL,
2207 OPEN_EXISTING, 0, NULL),
2208 (sock_T) CreateFileW(
2209 winpty_conout_name(term->tl_winpty),
2210 GENERIC_READ, 0, NULL,
2211 OPEN_EXISTING, 0, NULL),
2212 (sock_T) CreateFileW(
2213 winpty_conerr_name(term->tl_winpty),
2214 GENERIC_READ, 0, NULL,
2215 OPEN_EXISTING, 0, NULL));
2216
2217 jo = CreateJobObject(NULL, NULL);
2218 if (jo == NULL)
2219 goto failed;
2220
2221 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002222 {
2223 /* Failed, switch the way to terminate process with TerminateProcess. */
2224 CloseHandle(jo);
2225 jo = NULL;
2226 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002227
2228 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002229 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002230
2231 create_vterm(term, rows, cols);
2232
2233 setup_job_options(&opt, rows, cols);
2234 channel_set_job(channel, job, &opt);
2235
2236 job->jv_channel = channel;
2237 job->jv_proc_info.hProcess = child_process_handle;
2238 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2239 job->jv_job_object = jo;
2240 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002241 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002242 term->tl_job = job;
2243
2244 return OK;
2245
2246failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002247 if (spawn_config != NULL)
2248 winpty_spawn_config_free(spawn_config);
2249 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002250 if (channel != NULL)
2251 channel_clear(channel);
2252 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002253 {
2254 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002255 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002256 }
2257 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002258 if (jo != NULL)
2259 CloseHandle(jo);
2260 if (term->tl_winpty != NULL)
2261 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002262 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002263 if (term->tl_winpty_config != NULL)
2264 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002265 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002266 if (winpty_err != NULL)
2267 {
2268 char_u *msg = utf16_to_enc(
2269 (short_u *)winpty_error_msg(winpty_err), NULL);
2270
2271 EMSG(msg);
2272 winpty_error_free(winpty_err);
2273 }
2274 return FAIL;
2275}
2276
2277/*
2278 * Free the terminal emulator part of "term".
2279 */
2280 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002281term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002282{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002283 if (term->tl_winpty != NULL)
2284 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002285 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002286 if (term->tl_winpty_config != NULL)
2287 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002288 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002289 if (term->tl_vterm != NULL)
2290 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002291 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002292}
2293
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002294/*
2295 * Request size to terminal.
2296 */
2297 static void
2298term_report_winsize(term_T *term, int rows, int cols)
2299{
2300 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2301}
2302
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002303# else
2304
2305/**************************************
2306 * 3. Unix-like implementation.
2307 */
2308
2309/*
2310 * Create a new terminal of "rows" by "cols" cells.
2311 * Start job for "cmd".
2312 * Store the pointers in "term".
2313 * Return OK or FAIL.
2314 */
2315 static int
2316term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2317{
2318 typval_T argvars[2];
2319 jobopt_T opt;
2320
2321 create_vterm(term, rows, cols);
2322
2323 argvars[0].v_type = VAR_STRING;
2324 argvars[0].vval.v_string = cmd;
2325 setup_job_options(&opt, rows, cols);
2326 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002327 if (term->tl_job != NULL)
2328 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002329
Bram Moolenaar61a66052017-07-22 18:39:00 +02002330 return term->tl_job != NULL
2331 && term->tl_job->jv_channel != NULL
2332 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002333}
2334
2335/*
2336 * Free the terminal emulator part of "term".
2337 */
2338 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002339term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002340{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002341 if (term->tl_vterm != NULL)
2342 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002343 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002344}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002345
2346/*
2347 * Request size to terminal.
2348 */
2349 static void
2350term_report_winsize(term_T *term, int rows, int cols)
2351{
2352 /* Use an ioctl() to report the new window size to the job. */
2353 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2354 {
2355 int fd = -1;
2356 int part;
2357
2358 for (part = PART_OUT; part < PART_COUNT; ++part)
2359 {
2360 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2361 if (isatty(fd))
2362 break;
2363 }
2364 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2365 mch_stop_job(term->tl_job, (char_u *)"winch");
2366 }
2367}
2368
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002369# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002370
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002371#endif /* FEAT_TERMINAL */