blob: 53edb83c8b53b212f961f1f83d38ff74241b859f [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 Moolenaar94053a52017-08-01 21:44:33 +020039 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar423802d2017-07-30 16:52:24 +020040 * - in bash mouse clicks are inserting characters.
41 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaard85f2712017-07-28 21:51:57 +020042 * - For the scrollback buffer store lines in the buffer, only attributes in
43 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020044 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020045 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020046 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
47 * job finishes).
48 * - add option values to the command:
49 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020050 * - support different cursor shapes, colors and attributes
51 * - make term_getcursor() return type (none/block/bar/underline) and
52 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020053 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020054 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020055 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020056 * - add 't' to mode()
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020057 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar94053a52017-08-01 21:44:33 +020058 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020059 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020060 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020061 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020062 * - implement "term" for job_start(): more job options when starting a
63 * terminal.
Bram Moolenaar423802d2017-07-30 16:52:24 +020064 * - if the job in the terminal does not support the mouse, we can use the
65 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
67 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020068 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020069 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020070 * - Copy text in the vterm to the Vim buffer once in a while, so that
71 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020072 */
73
74#include "vim.h"
75
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020076#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020077
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020078#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079# define MIN(x,y) (x < y ? x : y)
80# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020081#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020083#include "libvterm/include/vterm.h"
84
Bram Moolenaard85f2712017-07-28 21:51:57 +020085typedef struct sb_line_S {
86 int sb_cols; /* can differ per line */
87 VTermScreenCell *sb_cells; /* allocated */
88} sb_line_T;
89
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090/* typedef term_T in structs.h */
91struct terminal_S {
92 term_T *tl_next;
93
Bram Moolenaar423802d2017-07-30 16:52:24 +020094 VTerm *tl_vterm;
95 job_T *tl_job;
96 buf_T *tl_buffer;
97
98 int tl_terminal_mode;
99 int tl_channel_closed;
100
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200101#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200102 void *tl_winpty_config;
103 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200104#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200105
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200106 /* last known vterm size */
107 int tl_rows;
108 int tl_cols;
109 /* vterm size does not follow window size */
110 int tl_rows_fixed;
111 int tl_cols_fixed;
112
Bram Moolenaar21554412017-07-24 21:44:43 +0200113 char_u *tl_title; /* NULL or allocated */
114 char_u *tl_status_text; /* NULL or allocated */
115
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200116 /* Range of screen rows to update. Zero based. */
117 int tl_dirty_row_start; /* -1 if nothing dirty */
118 int tl_dirty_row_end; /* row below last one to update */
119
Bram Moolenaard85f2712017-07-28 21:51:57 +0200120 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200121 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200122
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200123 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200124 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125};
126
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200127/*
128 * List of all active terminals.
129 */
130static term_T *first_term = NULL;
131
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200132
133#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
134#define KEY_BUF_LEN 200
135
136/*
137 * Functions with separate implementation for MS-Windows and Unix-like systems.
138 */
139static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200140static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200141static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200142
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200143/**************************************
144 * 1. Generic code for all systems.
145 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200146
147/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200148 * Determine the terminal size from 'termsize' and the current window.
149 * Assumes term->tl_rows and term->tl_cols are zero.
150 */
151 static void
152set_term_and_win_size(term_T *term)
153{
154 if (*curwin->w_p_tms != NUL)
155 {
156 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
157
158 term->tl_rows = atoi((char *)curwin->w_p_tms);
159 term->tl_cols = atoi((char *)p);
160 }
161 if (term->tl_rows == 0)
162 term->tl_rows = curwin->w_height;
163 else
164 {
165 win_setheight_win(term->tl_rows, curwin);
166 term->tl_rows_fixed = TRUE;
167 }
168 if (term->tl_cols == 0)
169 term->tl_cols = curwin->w_width;
170 else
171 {
172 win_setwidth_win(term->tl_cols, curwin);
173 term->tl_cols_fixed = TRUE;
174 }
175}
176
177/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200178 * ":terminal": open a terminal window and execute a job in it.
179 */
180 void
181ex_terminal(exarg_T *eap)
182{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200183 exarg_T split_ea;
184 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200185 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200186 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200187
188 if (check_restricted() || check_secure())
189 return;
190
191 term = (term_T *)alloc_clear(sizeof(term_T));
192 if (term == NULL)
193 return;
194 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200195 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200196 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200197
198 /* Open a new window or tab. */
199 vim_memset(&split_ea, 0, sizeof(split_ea));
200 split_ea.cmdidx = CMD_new;
201 split_ea.cmd = (char_u *)"new";
202 split_ea.arg = (char_u *)"";
203 ex_splitview(&split_ea);
204 if (curwin == old_curwin)
205 {
206 /* split failed */
207 vim_free(term);
208 return;
209 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200210 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200211 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200212
213 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200214 term->tl_next = first_term;
215 first_term = term;
216
Bram Moolenaar293424c2017-07-26 23:11:01 +0200217 if (cmd == NULL || *cmd == NUL)
218 cmd = p_sh;
219
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200220 {
221 int i;
222 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200223 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200224
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200225 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200226 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200227 /* Prepend a ! to the command name to avoid the buffer name equals
228 * the executable, otherwise ":w!" would overwrite it. */
229 if (i == 0)
230 vim_snprintf((char *)p, len, "!%s", cmd);
231 else
232 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200233 if (buflist_findname(p) == NULL)
234 {
235 curbuf->b_ffname = p;
236 break;
237 }
238 }
239 }
240 curbuf->b_fname = curbuf->b_ffname;
241
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200242 /* Mark the buffer as not modifiable. It can only be made modifiable after
243 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200244 curbuf->b_p_ma = FALSE;
245 set_string_option_direct((char_u *)"buftype", -1,
246 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200248 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200250 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200251 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200252 {
253 /* store the size we ended up with */
254 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200255 }
256 else
257 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200258 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200259
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200260 /* Wiping out the buffer will also close the window and call
261 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200262 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200263 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264}
265
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200266/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200267 * Free the scrollback buffer for "term".
268 */
269 static void
270free_scrollback(term_T *term)
271{
272 int i;
273
274 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
275 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
276 ga_clear(&term->tl_scrollback);
277}
278
279/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200280 * Free a terminal and everything it refers to.
281 * Kills the job if there is one.
282 * Called when wiping out a buffer.
283 */
284 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200285free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200286{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200287 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200288 term_T *tp;
289
290 if (term == NULL)
291 return;
292 if (first_term == term)
293 first_term = term->tl_next;
294 else
295 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
296 if (tp->tl_next == term)
297 {
298 tp->tl_next = term->tl_next;
299 break;
300 }
301
302 if (term->tl_job != NULL)
303 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200304 if (term->tl_job->jv_status != JOB_ENDED
305 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200306 job_stop(term->tl_job, NULL, "kill");
307 job_unref(term->tl_job);
308 }
309
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200310 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200311
312 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200313 vim_free(term->tl_title);
314 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200315 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200316 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200317}
318
319/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200320 * Write job output "msg[len]" to the vterm.
321 */
322 static void
323term_write_job_output(term_T *term, char_u *msg, size_t len)
324{
325 VTerm *vterm = term->tl_vterm;
326 char_u *p;
327 size_t done;
328 size_t len_now;
329
330 for (done = 0; done < len; done += len_now)
331 {
332 for (p = msg + done; p < msg + len; )
333 {
334 if (*p == NL)
335 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200336 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200337 }
338 len_now = p - msg - done;
339 vterm_input_write(vterm, (char *)msg + done, len_now);
340 if (p < msg + len && *p == NL)
341 {
342 /* Convert NL to CR-NL, that appears to work best. */
343 vterm_input_write(vterm, "\r\n", 2);
344 ++len_now;
345 }
346 }
347
348 /* this invokes the damage callbacks */
349 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
350}
351
Bram Moolenaar1c844932017-07-24 23:36:41 +0200352 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200353update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200354{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200355 if (term->tl_terminal_mode)
356 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200357 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200358 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200359 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200360 if (term->tl_cursor_visible)
361 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200362 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200363#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200364 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200365 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200366#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200367 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200368}
369
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200370/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200371 * Invoked when "msg" output from a job was received. Write it to the terminal
372 * of "buffer".
373 */
374 void
375write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
376{
377 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200378 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200379
Bram Moolenaard85f2712017-07-28 21:51:57 +0200380 if (term->tl_vterm == NULL)
381 {
382 ch_logn(channel, "NOT writing %d bytes to terminal", (int)len);
383 return;
384 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200385 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200386 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200387
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200388 if (!term->tl_terminal_mode)
389 {
390 /* TODO: only update once in a while. */
391 update_screen(0);
392 update_cursor(term, TRUE);
393 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200394}
395
396/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200397 * Send a mouse position and click to the vterm
398 */
399 static int
400term_send_mouse(VTerm *vterm, int button, int pressed)
401{
402 VTermModifier mod = VTERM_MOD_NONE;
403
404 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
405 mouse_col - W_WINCOL(curwin), mod);
406 vterm_mouse_button(vterm, button, pressed, mod);
407 return TRUE;
408}
409
410/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200411 * Convert typed key "c" into bytes to send to the job.
412 * Return the number of bytes in "buf".
413 */
414 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200415term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200416{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200417 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200418 VTermKey key = VTERM_KEY_NONE;
419 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200420 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200421
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200422 switch (c)
423 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200424 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200425 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200426 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
427 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200428 case K_DEL: key = VTERM_KEY_DEL; break;
429 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200430 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
431 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200432 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200433 case K_S_END: mod = VTERM_MOD_SHIFT;
434 key = VTERM_KEY_END; break;
435 case K_C_END: mod = VTERM_MOD_CTRL;
436 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200437 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
438 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
439 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
440 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
441 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
442 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
443 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
444 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
445 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
446 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
447 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
448 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
449 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200450 case K_S_HOME: mod = VTERM_MOD_SHIFT;
451 key = VTERM_KEY_HOME; break;
452 case K_C_HOME: mod = VTERM_MOD_CTRL;
453 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200454 case K_INS: key = VTERM_KEY_INS; break;
455 case K_K0: key = VTERM_KEY_KP_0; break;
456 case K_K1: key = VTERM_KEY_KP_1; break;
457 case K_K2: key = VTERM_KEY_KP_2; break;
458 case K_K3: key = VTERM_KEY_KP_3; break;
459 case K_K4: key = VTERM_KEY_KP_4; break;
460 case K_K5: key = VTERM_KEY_KP_5; break;
461 case K_K6: key = VTERM_KEY_KP_6; break;
462 case K_K7: key = VTERM_KEY_KP_7; break;
463 case K_K8: key = VTERM_KEY_KP_8; break;
464 case K_K9: key = VTERM_KEY_KP_9; break;
465 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
466 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
467 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
468 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
469 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
470 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
471 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
472 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
473 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
474 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
475 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
476 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
477 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200478 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
479 key = VTERM_KEY_LEFT; break;
480 case K_C_LEFT: mod = VTERM_MOD_CTRL;
481 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200482 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
483 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
484 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200485 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
486 key = VTERM_KEY_RIGHT; break;
487 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
488 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200489 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200490 case K_S_UP: mod = VTERM_MOD_SHIFT;
491 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200492 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200493
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200494 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
495 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
496 case K_MOUSELEFT: /* TODO */ return 0;
497 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200498
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200499 case K_LEFTMOUSE:
500 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
501 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
502 case K_LEFTRELEASE:
503 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
504 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
505 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
506 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
507 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
508 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
509 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
510 case K_X1MOUSE: /* TODO */ return 0;
511 case K_X1DRAG: /* TODO */ return 0;
512 case K_X1RELEASE: /* TODO */ return 0;
513 case K_X2MOUSE: /* TODO */ return 0;
514 case K_X2DRAG: /* TODO */ return 0;
515 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200516
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200517 case K_IGNORE: return 0;
518 case K_NOP: return 0;
519 case K_UNDO: return 0;
520 case K_HELP: return 0;
521 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
522 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
523 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
524 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
525 case K_SELECT: return 0;
526#ifdef FEAT_GUI
527 case K_VER_SCROLLBAR: return 0;
528 case K_HOR_SCROLLBAR: return 0;
529#endif
530#ifdef FEAT_GUI_TABLINE
531 case K_TABLINE: return 0;
532 case K_TABMENU: return 0;
533#endif
534#ifdef FEAT_NETBEANS_INTG
535 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
536#endif
537#ifdef FEAT_DND
538 case K_DROP: return 0;
539#endif
540#ifdef FEAT_AUTOCMD
541 case K_CURSORHOLD: return 0;
542#endif
543 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
544 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200545 }
546
547 /*
548 * Convert special keys to vterm keys:
549 * - Write keys to vterm: vterm_keyboard_key()
550 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200551 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200552 */
553 if (key != VTERM_KEY_NONE)
554 /* Special key, let vterm convert it. */
555 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200556 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200557 /* Normal character, let vterm convert it. */
558 vterm_keyboard_unichar(vterm, c, mod);
559
560 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200561 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200562}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200563
Bram Moolenaar938783d2017-07-16 20:13:26 +0200564/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200565 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200566 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200567 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200568term_job_running(term_T *term)
569{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200570 /* Also consider the job finished when the channel is closed, to avoid a
571 * race condition when updating the title. */
572 return term->tl_job != NULL
573 && term->tl_job->jv_status == JOB_STARTED
574 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200575}
576
577/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200578 * Add the last line of the scrollback buffer to the buffer in the window.
579 */
580 static void
581add_scrollback_line_to_buffer(term_T *term)
582{
583 linenr_T lnum = term->tl_scrollback.ga_len - 1;
584 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
585 garray_T ga;
586 int c;
587 int col;
588 int i;
589
590 ga_init2(&ga, 1, 100);
591 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
592 {
593 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
594 goto failed;
595 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
596 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
597 (char_u *)ga.ga_data + ga.ga_len);
598 }
599 if (ga_grow(&ga, 1) == FAIL)
600 goto failed;
601 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
602 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
603
604 if (lnum == 0)
605 {
606 /* Delete the empty line that was in the empty buffer. */
607 curbuf = term->tl_buffer;
608 ml_delete(2, FALSE);
609 curbuf = curwin->w_buffer;
610 }
611
612failed:
613 ga_clear(&ga);
614}
615
616/*
617 * Add the current lines of the terminal to scrollback and to the buffer.
618 * Called after the job has ended and when switching to Terminal mode.
619 */
620 static void
621move_terminal_to_buffer(term_T *term)
622{
623 win_T *wp;
624 int len;
625 int lines_skipped = 0;
626 VTermPos pos;
627 VTermScreenCell cell;
628 VTermScreenCell *p;
629 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
630
631 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
632 {
633 len = 0;
634 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
635 if (vterm_screen_get_cell(screen, pos, &cell) != 0
636 && cell.chars[0] != NUL)
637 len = pos.col + 1;
638
639 if (len == 0)
640 ++lines_skipped;
641 else
642 {
643 while (lines_skipped > 0)
644 {
645 /* Line was skipped, add an empty line. */
646 --lines_skipped;
647 if (ga_grow(&term->tl_scrollback, 1) == OK)
648 {
649 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
650 + term->tl_scrollback.ga_len;
651
652 line->sb_cols = 0;
653 line->sb_cells = NULL;
654 ++term->tl_scrollback.ga_len;
655
656 add_scrollback_line_to_buffer(term);
657 }
658 }
659
660 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
661 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
662 {
663 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
664 + term->tl_scrollback.ga_len;
665
666 for (pos.col = 0; pos.col < len; ++pos.col)
667 {
668 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
669 vim_memset(p + pos.col, 0, sizeof(cell));
670 else
671 p[pos.col] = cell;
672 }
673 line->sb_cols = len;
674 line->sb_cells = p;
675 ++term->tl_scrollback.ga_len;
676
677 add_scrollback_line_to_buffer(term);
678 }
679 else
680 vim_free(p);
681 }
682 }
683
684 FOR_ALL_WINDOWS(wp)
685 {
686 if (wp->w_buffer == term->tl_buffer)
687 {
688 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
689 wp->w_cursor.col = 0;
690 wp->w_valid = 0;
691 redraw_win_later(wp, NOT_VALID);
692 }
693 }
694}
695
696 static void
697set_terminal_mode(term_T *term, int on)
698{
699 term->tl_terminal_mode = on;
700 vim_free(term->tl_status_text);
701 term->tl_status_text = NULL;
702 if (term->tl_buffer == curbuf)
703 maketitle();
704}
705
706/*
707 * Called after the job if finished and Terminal mode is not active:
708 * Move the vterm contents into the scrollback buffer and free the vterm.
709 */
710 static void
711cleanup_vterm(term_T *term)
712{
713 move_terminal_to_buffer(term);
714 term_free_vterm(term);
715 set_terminal_mode(term, FALSE);
716}
717
718/*
719 * Switch from sending keys to the job to Terminal-Normal mode.
720 * Suspends updating the terminal window.
721 */
722 static void
723term_enter_terminal_mode()
724{
725 term_T *term = curbuf->b_term;
726
727 /* Append the current terminal contents to the buffer. */
728 move_terminal_to_buffer(term);
729
730 set_terminal_mode(term, TRUE);
731}
732
733/*
734 * Returns TRUE if the current window contains a terminal and we are in
735 * Terminal-Normal mode.
736 */
737 int
738term_in_terminal_mode()
739{
740 term_T *term = curbuf->b_term;
741
742 return term != NULL && term->tl_terminal_mode;
743}
744
745/*
746 * Switch from Terminal-Normal mode to sending keys to the job.
747 * Restores updating the terminal window.
748 */
749 void
750term_leave_terminal_mode()
751{
752 term_T *term = curbuf->b_term;
753 sb_line_T *line;
754 garray_T *gap;
755
756 /* Remove the terminal contents from the scrollback and the buffer. */
757 gap = &term->tl_scrollback;
758 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
759 {
760 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
761 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
762 vim_free(line->sb_cells);
763 --gap->ga_len;
764 if (gap->ga_len == 0)
765 break;
766 }
767 check_cursor();
768
769 set_terminal_mode(term, FALSE);
770
771 if (term->tl_channel_closed)
772 cleanup_vterm(term);
773 redraw_buf_and_status_later(curbuf, NOT_VALID);
774}
775
776/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200777 * Get a key from the user without mapping.
778 * TODO: use terminal mode mappings.
779 */
780 static int
781term_vgetc()
782{
783 int c;
784
785 ++no_mapping;
786 ++allow_keys;
787 got_int = FALSE;
788 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200789 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200790 --no_mapping;
791 --allow_keys;
792 return c;
793}
794
795/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200796 * Send keys to terminal.
797 */
798 static int
799send_keys_to_term(term_T *term, int c, int typed)
800{
801 char msg[KEY_BUF_LEN];
802 size_t len;
803 static int mouse_was_outside = FALSE;
804 int dragging_outside = FALSE;
805
806 /* Catch keys that need to be handled as in Normal mode. */
807 switch (c)
808 {
809 case NUL:
810 case K_ZERO:
811 if (typed)
812 stuffcharReadbuff(c);
813 return FAIL;
814
815 case K_IGNORE:
816 return FAIL;
817
818 case K_LEFTDRAG:
819 case K_MIDDLEDRAG:
820 case K_RIGHTDRAG:
821 case K_X1DRAG:
822 case K_X2DRAG:
823 dragging_outside = mouse_was_outside;
824 /* FALLTHROUGH */
825 case K_LEFTMOUSE:
826 case K_LEFTMOUSE_NM:
827 case K_LEFTRELEASE:
828 case K_LEFTRELEASE_NM:
829 case K_MIDDLEMOUSE:
830 case K_MIDDLERELEASE:
831 case K_RIGHTMOUSE:
832 case K_RIGHTRELEASE:
833 case K_X1MOUSE:
834 case K_X1RELEASE:
835 case K_X2MOUSE:
836 case K_X2RELEASE:
837 if (mouse_row < W_WINROW(curwin)
838 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
839 || mouse_col < W_WINCOL(curwin)
840 || mouse_col >= W_ENDCOL(curwin)
841 || dragging_outside)
842 {
843 /* click outside the current window */
844 if (typed)
845 {
846 stuffcharReadbuff(c);
847 mouse_was_outside = TRUE;
848 }
849 return FAIL;
850 }
851 }
852 if (typed)
853 mouse_was_outside = FALSE;
854
855 /* Convert the typed key to a sequence of bytes for the job. */
856 len = term_convert_key(term, c, msg);
857 if (len > 0)
858 /* TODO: if FAIL is returned, stop? */
859 channel_send(term->tl_job->jv_channel, PART_IN,
860 (char_u *)msg, (int)len, NULL);
861
862 return OK;
863}
864
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200865 static void
866position_cursor(win_T *wp, VTermPos *pos)
867{
868 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
869 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
870 wp->w_valid |= (VALID_WCOL|VALID_WROW);
871}
872
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200873/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200874 * Handle CTRL-W "": send register contents to the job.
875 */
876 static void
877term_paste_register(int prev_c UNUSED)
878{
879 int c;
880 list_T *l;
881 listitem_T *item;
882 long reglen = 0;
883 int type;
884
885#ifdef FEAT_CMDL_INFO
886 if (add_to_showcmd(prev_c))
887 if (add_to_showcmd('"'))
888 out_flush();
889#endif
890 c = term_vgetc();
891#ifdef FEAT_CMDL_INFO
892 clear_showcmd();
893#endif
894
895 /* CTRL-W "= prompt for expression to evaluate. */
896 if (c == '=' && get_expr_register() != '=')
897 return;
898
899 l = (list_T *)get_reg_contents(c, GREG_LIST);
900 if (l != NULL)
901 {
902 type = get_reg_type(c, &reglen);
903 for (item = l->lv_first; item != NULL; item = item->li_next)
904 {
905 char_u *s = get_tv_string(&item->li_tv);
906
907 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
908 s, STRLEN(s), NULL);
909 if (item->li_next != NULL || type == MLINE)
910 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
911 (char_u *)"\r", 1, NULL);
912 }
913 list_free(l);
914 }
915}
916
917/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200918 * Returns TRUE if the current window contains a terminal and we are sending
919 * keys to the job.
920 */
921 int
922term_use_loop()
923{
924 term_T *term = curbuf->b_term;
925
926 return term != NULL
927 && !term->tl_terminal_mode
928 && term->tl_vterm != NULL
929 && term_job_running(term);
930}
931
932/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200933 * Wait for input and send it to the job.
934 * Return when the start of a CTRL-W command is typed or anything else that
935 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200936 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
937 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200938 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200939 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200940terminal_loop(void)
941{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200942 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200943 int termkey = 0;
944
945 if (*curwin->w_p_tk != NUL)
946 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200947 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200948
949 for (;;)
950 {
951 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200952 /* Repeat redrawing in case a message is received while redrawing. */
953 while (curwin->w_redr_type != 0)
954 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200955 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200956
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200957 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200958 if (curbuf->b_term->tl_vterm == NULL
959 || !term_job_running(curbuf->b_term))
960 /* job finished while waiting for a character */
961 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200962
Bram Moolenaarfae42832017-08-01 22:24:26 +0200963#ifdef UNIX
964 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
965#endif
966#ifdef WIN3264
967 if (c == Ctrl_C)
968 /* We don't know if the job can handle CTRL-C itself or not, this
969 * may kill the shell instead of killing the command running in the
970 * shell. */
Bram Moolenaar66cd19f2017-08-02 23:13:27 +0200971 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit")
Bram Moolenaarfae42832017-08-01 22:24:26 +0200972#endif
973
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200974 if (c == (termkey == 0 ? Ctrl_W : termkey))
975 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200976 int prev_c = c;
977
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200978#ifdef FEAT_CMDL_INFO
979 if (add_to_showcmd(c))
980 out_flush();
981#endif
982 c = term_vgetc();
983#ifdef FEAT_CMDL_INFO
984 clear_showcmd();
985#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200986 if (curbuf->b_term->tl_vterm == NULL
987 || !term_job_running(curbuf->b_term))
988 /* job finished while waiting for a character */
989 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200990
991 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200992 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200993 /* "CTRL-W .": send CTRL-W to the job */
994 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200995 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200996 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200997 {
998 term_enter_terminal_mode();
999 return FAIL;
1000 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001001 else if (c == '"')
1002 {
1003 term_paste_register(prev_c);
1004 continue;
1005 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001006 else if (termkey == 0 || c != termkey)
1007 {
1008 stuffcharReadbuff(Ctrl_W);
1009 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001010 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001011 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001012 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001013 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1014 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001015 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001016 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001017}
1018
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001019/*
1020 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001021 * This updates the title and status, but does not close the vter, because
1022 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001023 */
1024 void
1025term_job_ended(job_T *job)
1026{
1027 term_T *term;
1028 int did_one = FALSE;
1029
1030 for (term = first_term; term != NULL; term = term->tl_next)
1031 if (term->tl_job == job)
1032 {
1033 vim_free(term->tl_title);
1034 term->tl_title = NULL;
1035 vim_free(term->tl_status_text);
1036 term->tl_status_text = NULL;
1037 redraw_buf_and_status_later(term->tl_buffer, VALID);
1038 did_one = TRUE;
1039 }
1040 if (did_one)
1041 redraw_statuslines();
1042 if (curbuf->b_term != NULL)
1043 {
1044 if (curbuf->b_term->tl_job == job)
1045 maketitle();
1046 update_cursor(curbuf->b_term, TRUE);
1047 }
1048}
1049
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001050 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001051may_toggle_cursor(term_T *term)
1052{
1053 if (curbuf == term->tl_buffer)
1054 {
1055 if (term->tl_cursor_visible)
1056 cursor_on();
1057 else
1058 cursor_off();
1059 }
1060}
1061
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001062 static int
1063handle_damage(VTermRect rect, void *user)
1064{
1065 term_T *term = (term_T *)user;
1066
1067 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1068 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1069 redraw_buf_later(term->tl_buffer, NOT_VALID);
1070 return 1;
1071}
1072
1073 static int
1074handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1075{
1076 term_T *term = (term_T *)user;
1077
1078 /* TODO */
1079 redraw_buf_later(term->tl_buffer, NOT_VALID);
1080 return 1;
1081}
1082
1083 static int
1084handle_movecursor(
1085 VTermPos pos,
1086 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001087 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001088 void *user)
1089{
1090 term_T *term = (term_T *)user;
1091 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001092
1093 term->tl_cursor_pos = pos;
1094 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001095
1096 FOR_ALL_WINDOWS(wp)
1097 {
1098 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001099 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001100 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001101 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001102 {
1103 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001104 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001105 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001106
1107 return 1;
1108}
1109
Bram Moolenaar21554412017-07-24 21:44:43 +02001110 static int
1111handle_settermprop(
1112 VTermProp prop,
1113 VTermValue *value,
1114 void *user)
1115{
1116 term_T *term = (term_T *)user;
1117
1118 switch (prop)
1119 {
1120 case VTERM_PROP_TITLE:
1121 vim_free(term->tl_title);
1122 term->tl_title = vim_strsave((char_u *)value->string);
1123 vim_free(term->tl_status_text);
1124 term->tl_status_text = NULL;
1125 if (term == curbuf->b_term)
1126 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001127 break;
1128
1129 case VTERM_PROP_CURSORVISIBLE:
1130 term->tl_cursor_visible = value->boolean;
1131 may_toggle_cursor(term);
1132 out_flush();
1133 break;
1134
Bram Moolenaar21554412017-07-24 21:44:43 +02001135 default:
1136 break;
1137 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001138 /* Always return 1, otherwise vterm doesn't store the value internally. */
1139 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001140}
1141
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001142/*
1143 * The job running in the terminal resized the terminal.
1144 */
1145 static int
1146handle_resize(int rows, int cols, void *user)
1147{
1148 term_T *term = (term_T *)user;
1149 win_T *wp;
1150
1151 term->tl_rows = rows;
1152 term->tl_cols = cols;
1153 FOR_ALL_WINDOWS(wp)
1154 {
1155 if (wp->w_buffer == term->tl_buffer)
1156 {
1157 win_setheight_win(rows, wp);
1158 win_setwidth_win(cols, wp);
1159 }
1160 }
1161
1162 redraw_buf_later(term->tl_buffer, NOT_VALID);
1163 return 1;
1164}
1165
Bram Moolenaard85f2712017-07-28 21:51:57 +02001166/*
1167 * Handle a line that is pushed off the top of the screen.
1168 */
1169 static int
1170handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1171{
1172 term_T *term = (term_T *)user;
1173
1174 /* TODO: Limit the number of lines that are stored. */
1175 /* TODO: put the text in the buffer. */
1176 if (ga_grow(&term->tl_scrollback, 1) == OK)
1177 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001178 VTermScreenCell *p = NULL;
1179 int len = 0;
1180 int i;
1181 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001182
1183 /* do not store empty cells at the end */
1184 for (i = 0; i < cols; ++i)
1185 if (cells[i].chars[0] != 0)
1186 len = i + 1;
1187
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001188 if (len > 0)
1189 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001190 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001191 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001192
1193 line = (sb_line_T *)term->tl_scrollback.ga_data
1194 + term->tl_scrollback.ga_len;
1195 line->sb_cols = len;
1196 line->sb_cells = p;
1197 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001198 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001199
1200 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001201 }
1202 return 0; /* ignored */
1203}
1204
Bram Moolenaar21554412017-07-24 21:44:43 +02001205static VTermScreenCallbacks screen_callbacks = {
1206 handle_damage, /* damage */
1207 handle_moverect, /* moverect */
1208 handle_movecursor, /* movecursor */
1209 handle_settermprop, /* settermprop */
1210 NULL, /* bell */
1211 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001212 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001213 NULL /* sb_popline */
1214};
1215
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001216/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001217 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001218 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001219 */
1220 void
1221term_channel_closed(channel_T *ch)
1222{
1223 term_T *term;
1224 int did_one = FALSE;
1225
1226 for (term = first_term; term != NULL; term = term->tl_next)
1227 if (term->tl_job == ch->ch_job)
1228 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001229 term->tl_channel_closed = TRUE;
1230
Bram Moolenaard85f2712017-07-28 21:51:57 +02001231 vim_free(term->tl_title);
1232 term->tl_title = NULL;
1233 vim_free(term->tl_status_text);
1234 term->tl_status_text = NULL;
1235
Bram Moolenaar423802d2017-07-30 16:52:24 +02001236 /* Unless in Terminal-Normal mode: clear the vterm. */
1237 if (!term->tl_terminal_mode)
1238 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001239
1240 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1241 did_one = TRUE;
1242 }
1243 if (did_one)
1244 {
1245 redraw_statuslines();
1246
1247 /* Need to break out of vgetc(). */
1248 ins_char_typebuf(K_IGNORE);
1249
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001250 term = curbuf->b_term;
1251 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001252 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001253 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001254 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001255 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001256 }
1257 }
1258}
1259
1260/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001261 * Reverse engineer the RGB value into a cterm color index.
1262 * First color is 1. Return 0 if no match found.
1263 */
1264 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001265color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001266{
1267 int red = color->red;
1268 int blue = color->blue;
1269 int green = color->green;
1270
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001271 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001272 if (red == 0)
1273 {
1274 if (green == 0)
1275 {
1276 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001277 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001278 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001279 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001280 }
1281 else if (green == 224)
1282 {
1283 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001284 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001285 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001286 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001287 }
1288 }
1289 else if (red == 224)
1290 {
1291 if (green == 0)
1292 {
1293 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001294 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001295 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001296 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001297 }
1298 else if (green == 224)
1299 {
1300 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001301 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001302 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001303 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001304 }
1305 }
1306 else if (red == 128)
1307 {
1308 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001309 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001310 }
1311 else if (red == 255)
1312 {
1313 if (green == 64)
1314 {
1315 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001316 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001317 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001318 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001319 }
1320 else if (green == 255)
1321 {
1322 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001323 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001324 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001325 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001326 }
1327 }
1328 else if (red == 64)
1329 {
1330 if (green == 64)
1331 {
1332 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001333 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001334 }
1335 else if (green == 255)
1336 {
1337 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001338 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001339 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001340 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001341 }
1342 }
1343 if (t_colors >= 256)
1344 {
1345 if (red == blue && red == green)
1346 {
1347 /* 24-color greyscale */
1348 static int cutoff[23] = {
1349 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1350 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1351 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1352 int i;
1353
1354 for (i = 0; i < 23; ++i)
1355 if (red < cutoff[i])
1356 return i + 233;
1357 return 256;
1358 }
1359
1360 /* 216-color cube */
1361 return 17 + ((red + 25) / 0x33) * 36
1362 + ((green + 25) / 0x33) * 6
1363 + (blue + 25) / 0x33;
1364 }
1365 return 0;
1366}
1367
1368/*
1369 * Convert the attributes of a vterm cell into an attribute index.
1370 */
1371 static int
1372cell2attr(VTermScreenCell *cell)
1373{
1374 int attr = 0;
1375
1376 if (cell->attrs.bold)
1377 attr |= HL_BOLD;
1378 if (cell->attrs.underline)
1379 attr |= HL_UNDERLINE;
1380 if (cell->attrs.italic)
1381 attr |= HL_ITALIC;
1382 if (cell->attrs.strike)
1383 attr |= HL_STANDOUT;
1384 if (cell->attrs.reverse)
1385 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001386
1387#ifdef FEAT_GUI
1388 if (gui.in_use)
1389 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001390 guicolor_T fg, bg;
1391
1392 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1393 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1394 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001395 }
1396 else
1397#endif
1398#ifdef FEAT_TERMGUICOLORS
1399 if (p_tgc)
1400 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001401 guicolor_T fg, bg;
1402
1403 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1404 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1405
1406 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001407 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001408 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001409#endif
1410 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001411 int bold = MAYBE;
1412 int fg = color2index(&cell->fg, TRUE, &bold);
1413 int bg = color2index(&cell->bg, FALSE, &bold);
1414
1415 /* with 8 colors set the bold attribute to get a bright foreground */
1416 if (bold == TRUE)
1417 attr |= HL_BOLD;
1418 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001419 }
1420 return 0;
1421}
1422
1423/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001424 * Called to update the window that contains a terminal.
1425 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001426 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001427 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001428term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001429{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001430 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001431 VTerm *vterm;
1432 VTermScreen *screen;
1433 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001434 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001435
Bram Moolenaar423802d2017-07-30 16:52:24 +02001436 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001437 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001438
Bram Moolenaard85f2712017-07-28 21:51:57 +02001439 vterm = term->tl_vterm;
1440 screen = vterm_obtain_screen(vterm);
1441 state = vterm_obtain_state(vterm);
1442
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001443 /*
1444 * If the window was resized a redraw will be triggered and we get here.
1445 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1446 */
1447 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1448 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001449 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001450 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1451 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1452 win_T *twp;
1453
1454 FOR_ALL_WINDOWS(twp)
1455 {
1456 /* When more than one window shows the same terminal, use the
1457 * smallest size. */
1458 if (twp->w_buffer == term->tl_buffer)
1459 {
1460 if (!term->tl_rows_fixed && rows > twp->w_height)
1461 rows = twp->w_height;
1462 if (!term->tl_cols_fixed && cols > twp->w_width)
1463 cols = twp->w_width;
1464 }
1465 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001466
1467 vterm_set_size(vterm, rows, cols);
1468 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1469 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001470 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001471 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001472
1473 /* The cursor may have been moved when resizing. */
1474 vterm_state_get_cursorpos(state, &pos);
1475 position_cursor(wp, &pos);
1476
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001477 /* TODO: Only redraw what changed. */
1478 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001479 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001480 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001481 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001482
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001483 if (pos.row < term->tl_rows)
1484 {
1485 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001486 {
1487 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001488 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001489
Bram Moolenaareeac6772017-07-23 15:48:37 +02001490 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1491 vim_memset(&cell, 0, sizeof(cell));
1492
1493 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001494 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001495 if (c == NUL)
1496 {
1497 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001498#if defined(FEAT_MBYTE)
1499 if (enc_utf8)
1500 ScreenLinesUC[off] = NUL;
1501#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001502 }
1503 else
1504 {
1505#if defined(FEAT_MBYTE)
1506 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001507 {
1508 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001509 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001510 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001511 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001512 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001513 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001514 if (enc_utf8)
1515 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001516 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001517#else
1518 ScreenLines[off] = c;
1519#endif
1520 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001521 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001522
1523 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001524 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001525 if (cell.width == 2)
1526 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001527 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001528#if defined(FEAT_MBYTE)
1529 if (enc_utf8)
1530 ScreenLinesUC[off] = NUL;
1531#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001532 ++pos.col;
1533 ++off;
1534 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001535 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001536 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001537 else
1538 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001539
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001540 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1541 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001542 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001543
1544 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001545}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001546
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001547/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001548 * Return TRUE if "wp" is a terminal window where the job has finished.
1549 */
1550 int
1551term_is_finished(buf_T *buf)
1552{
1553 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1554}
1555
1556/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001557 * Return TRUE if "wp" is a terminal window where the job has finished or we
1558 * are in Terminal-Normal mode.
1559 */
1560 int
1561term_show_buffer(buf_T *buf)
1562{
1563 term_T *term = buf->b_term;
1564
1565 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1566}
1567
1568/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001569 * The current buffer is going to be changed. If there is terminal
1570 * highlighting remove it now.
1571 */
1572 void
1573term_change_in_curbuf(void)
1574{
1575 term_T *term = curbuf->b_term;
1576
1577 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1578 {
1579 free_scrollback(term);
1580 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001581
1582 /* The buffer is now like a normal buffer, it cannot be easily
1583 * abandoned when changed. */
1584 set_string_option_direct((char_u *)"buftype", -1,
1585 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001586 }
1587}
1588
1589/*
1590 * Get the screen attribute for a position in the buffer.
1591 */
1592 int
1593term_get_attr(buf_T *buf, linenr_T lnum, int col)
1594{
1595 term_T *term = buf->b_term;
1596 sb_line_T *line;
1597
Bram Moolenaar70229f92017-07-29 16:01:53 +02001598 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001599 return 0;
1600 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1601 if (col >= line->sb_cols)
1602 return 0;
1603 return cell2attr(line->sb_cells + col);
1604}
1605
1606/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001607 * Set job options common for Unix and MS-Windows.
1608 */
1609 static void
1610setup_job_options(jobopt_T *opt, int rows, int cols)
1611{
1612 clear_job_options(opt);
1613 opt->jo_mode = MODE_RAW;
1614 opt->jo_out_mode = MODE_RAW;
1615 opt->jo_err_mode = MODE_RAW;
1616 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001617
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001618 opt->jo_io[PART_OUT] = JIO_BUFFER;
1619 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001620 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1621
1622 opt->jo_modifiable[PART_OUT] = 0;
1623 opt->jo_modifiable[PART_ERR] = 0;
1624 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1625
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001626 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1627 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001628 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001629 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1630
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001631 opt->jo_term_rows = rows;
1632 opt->jo_term_cols = cols;
1633}
1634
1635/*
1636 * Create a new vterm and initialize it.
1637 */
1638 static void
1639create_vterm(term_T *term, int rows, int cols)
1640{
1641 VTerm *vterm;
1642 VTermScreen *screen;
1643
1644 vterm = vterm_new(rows, cols);
1645 term->tl_vterm = vterm;
1646 screen = vterm_obtain_screen(vterm);
1647 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1648 /* TODO: depends on 'encoding'. */
1649 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001650
1651 /* Vterm uses a default black background. Set it to white when
1652 * 'background' is "light". */
1653 if (*p_bg == 'l')
1654 {
1655 VTermColor fg, bg;
1656
1657 fg.red = fg.green = fg.blue = 0;
1658 bg.red = bg.green = bg.blue = 255;
1659 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1660 }
1661
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001662 /* Required to initialize most things. */
1663 vterm_screen_reset(screen, 1 /* hard */);
1664}
1665
Bram Moolenaar21554412017-07-24 21:44:43 +02001666/*
1667 * Return the text to show for the buffer name and status.
1668 */
1669 char_u *
1670term_get_status_text(term_T *term)
1671{
1672 if (term->tl_status_text == NULL)
1673 {
1674 char_u *txt;
1675 size_t len;
1676
Bram Moolenaar423802d2017-07-30 16:52:24 +02001677 if (term->tl_terminal_mode)
1678 {
1679 if (term_job_running(term))
1680 txt = (char_u *)_("Terminal");
1681 else
1682 txt = (char_u *)_("Terminal-finished");
1683 }
1684 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001685 txt = term->tl_title;
1686 else if (term_job_running(term))
1687 txt = (char_u *)_("running");
1688 else
1689 txt = (char_u *)_("finished");
1690 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001691 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001692 if (term->tl_status_text != NULL)
1693 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1694 term->tl_buffer->b_fname, txt);
1695 }
1696 return term->tl_status_text;
1697}
1698
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001699/*
1700 * Mark references in jobs of terminals.
1701 */
1702 int
1703set_ref_in_term(int copyID)
1704{
1705 int abort = FALSE;
1706 term_T *term;
1707 typval_T tv;
1708
1709 for (term = first_term; term != NULL; term = term->tl_next)
1710 if (term->tl_job != NULL)
1711 {
1712 tv.v_type = VAR_JOB;
1713 tv.vval.v_job = term->tl_job;
1714 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1715 }
1716 return abort;
1717}
1718
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001719/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001720 * Get the buffer from the first argument in "argvars".
1721 * Returns NULL when the buffer is not for a terminal window.
1722 */
1723 static buf_T *
1724term_get_buf(typval_T *argvars)
1725{
1726 buf_T *buf;
1727
1728 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1729 ++emsg_off;
1730 buf = get_buf_tv(&argvars[0], FALSE);
1731 --emsg_off;
1732 if (buf == NULL || buf->b_term == NULL)
1733 return NULL;
1734 return buf;
1735}
1736
1737/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001738 * "term_getattr(attr, name)" function
1739 */
1740 void
1741f_term_getattr(typval_T *argvars, typval_T *rettv)
1742{
1743 int attr;
1744 size_t i;
1745 char_u *name;
1746
1747 static struct {
1748 char *name;
1749 int attr;
1750 } attrs[] = {
1751 {"bold", HL_BOLD},
1752 {"italic", HL_ITALIC},
1753 {"underline", HL_UNDERLINE},
1754 {"strike", HL_STANDOUT},
1755 {"reverse", HL_INVERSE},
1756 };
1757
1758 attr = get_tv_number(&argvars[0]);
1759 name = get_tv_string_chk(&argvars[1]);
1760 if (name == NULL)
1761 return;
1762
1763 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1764 if (STRCMP(name, attrs[i].name) == 0)
1765 {
1766 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1767 break;
1768 }
1769}
1770
1771/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001772 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001773 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001774 void
1775f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001776{
Bram Moolenaar97870002017-07-30 18:28:38 +02001777 buf_T *buf = term_get_buf(argvars);
1778 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001779
Bram Moolenaar97870002017-07-30 18:28:38 +02001780 if (rettv_list_alloc(rettv) == FAIL)
1781 return;
1782 if (buf == NULL)
1783 return;
1784
1785 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001786 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1787 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001788 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001789}
1790
1791/*
1792 * "term_getjob(buf)" function
1793 */
1794 void
1795f_term_getjob(typval_T *argvars, typval_T *rettv)
1796{
1797 buf_T *buf = term_get_buf(argvars);
1798
1799 rettv->v_type = VAR_JOB;
1800 rettv->vval.v_job = NULL;
1801 if (buf == NULL)
1802 return;
1803
1804 rettv->vval.v_job = buf->b_term->tl_job;
1805 if (rettv->vval.v_job != NULL)
1806 ++rettv->vval.v_job->jv_refcount;
1807}
1808
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001809 static int
1810get_row_number(typval_T *tv, term_T *term)
1811{
1812 if (tv->v_type == VAR_STRING
1813 && tv->vval.v_string != NULL
1814 && STRCMP(tv->vval.v_string, ".") == 0)
1815 return term->tl_cursor_pos.row;
1816 return (int)get_tv_number(tv) - 1;
1817}
1818
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001819/*
1820 * "term_getline(buf, row)" function
1821 */
1822 void
1823f_term_getline(typval_T *argvars, typval_T *rettv)
1824{
1825 buf_T *buf = term_get_buf(argvars);
1826 term_T *term;
1827 int row;
1828
1829 rettv->v_type = VAR_STRING;
1830 if (buf == NULL)
1831 return;
1832 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001833 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001834
1835 if (term->tl_vterm == NULL)
1836 {
1837 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1838
1839 /* vterm is finished, get the text from the buffer */
1840 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1841 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1842 }
1843 else
1844 {
1845 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1846 VTermRect rect;
1847 int len;
1848 char_u *p;
1849
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001850 if (row < 0 || row >= term->tl_rows)
1851 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001852 len = term->tl_cols * MB_MAXBYTES + 1;
1853 p = alloc(len);
1854 if (p == NULL)
1855 return;
1856 rettv->vval.v_string = p;
1857
1858 rect.start_col = 0;
1859 rect.end_col = term->tl_cols;
1860 rect.start_row = row;
1861 rect.end_row = row + 1;
1862 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1863 }
1864}
1865
1866/*
1867 * "term_getsize(buf)" function
1868 */
1869 void
1870f_term_getsize(typval_T *argvars, typval_T *rettv)
1871{
1872 buf_T *buf = term_get_buf(argvars);
1873 list_T *l;
1874
1875 if (rettv_list_alloc(rettv) == FAIL)
1876 return;
1877 if (buf == NULL)
1878 return;
1879
1880 l = rettv->vval.v_list;
1881 list_append_number(l, buf->b_term->tl_rows);
1882 list_append_number(l, buf->b_term->tl_cols);
1883}
1884
1885/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001886 * "term_getstatus(buf)" function
1887 */
1888 void
1889f_term_getstatus(typval_T *argvars, typval_T *rettv)
1890{
1891 buf_T *buf = term_get_buf(argvars);
1892 term_T *term;
1893 char_u val[100];
1894
1895 rettv->v_type = VAR_STRING;
1896 if (buf == NULL)
1897 return;
1898 term = buf->b_term;
1899
1900 if (term_job_running(term))
1901 STRCPY(val, "running");
1902 else
1903 STRCPY(val, "finished");
1904 if (term->tl_terminal_mode)
1905 STRCAT(val, ",terminal");
1906 rettv->vval.v_string = vim_strsave(val);
1907}
1908
1909/*
1910 * "term_gettitle(buf)" function
1911 */
1912 void
1913f_term_gettitle(typval_T *argvars, typval_T *rettv)
1914{
1915 buf_T *buf = term_get_buf(argvars);
1916
1917 rettv->v_type = VAR_STRING;
1918 if (buf == NULL)
1919 return;
1920
1921 if (buf->b_term->tl_title != NULL)
1922 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1923}
1924
1925/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001926 * "term_list()" function
1927 */
1928 void
1929f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1930{
1931 term_T *tp;
1932 list_T *l;
1933
1934 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1935 return;
1936
1937 l = rettv->vval.v_list;
1938 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1939 if (tp != NULL && tp->tl_buffer != NULL)
1940 if (list_append_number(l,
1941 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1942 return;
1943}
1944
1945/*
1946 * "term_scrape(buf, row)" function
1947 */
1948 void
1949f_term_scrape(typval_T *argvars, typval_T *rettv)
1950{
1951 buf_T *buf = term_get_buf(argvars);
1952 VTermScreen *screen = NULL;
1953 VTermPos pos;
1954 list_T *l;
1955 term_T *term;
1956
1957 if (rettv_list_alloc(rettv) == FAIL)
1958 return;
1959 if (buf == NULL)
1960 return;
1961 term = buf->b_term;
1962 if (term->tl_vterm != NULL)
1963 screen = vterm_obtain_screen(term->tl_vterm);
1964
1965 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001966 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001967 for (pos.col = 0; pos.col < term->tl_cols; )
1968 {
1969 dict_T *dcell;
1970 VTermScreenCell cell;
1971 char_u rgb[8];
1972 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1973 int off = 0;
1974 int i;
1975
1976 if (screen == NULL)
1977 {
1978 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1979 sb_line_T *line;
1980
1981 /* vterm has finished, get the cell from scrollback */
1982 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1983 break;
1984 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1985 if (pos.col >= line->sb_cols)
1986 break;
1987 cell = line->sb_cells[pos.col];
1988 }
1989 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1990 break;
1991 dcell = dict_alloc();
1992 list_append_dict(l, dcell);
1993
1994 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1995 {
1996 if (cell.chars[i] == 0)
1997 break;
1998 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1999 }
2000 mbs[off] = NUL;
2001 dict_add_nr_str(dcell, "chars", 0, mbs);
2002
2003 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2004 cell.fg.red, cell.fg.green, cell.fg.blue);
2005 dict_add_nr_str(dcell, "fg", 0, rgb);
2006 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2007 cell.bg.red, cell.bg.green, cell.bg.blue);
2008 dict_add_nr_str(dcell, "bg", 0, rgb);
2009
2010 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2011 dict_add_nr_str(dcell, "width", cell.width, NULL);
2012
2013 ++pos.col;
2014 if (cell.width == 2)
2015 ++pos.col;
2016 }
2017}
2018
2019/*
2020 * "term_sendkeys(buf, keys)" function
2021 */
2022 void
2023f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2024{
2025 buf_T *buf = term_get_buf(argvars);
2026 char_u *msg;
2027 term_T *term;
2028
2029 rettv->v_type = VAR_UNKNOWN;
2030 if (buf == NULL)
2031 return;
2032
2033 msg = get_tv_string_chk(&argvars[1]);
2034 if (msg == NULL)
2035 return;
2036 term = buf->b_term;
2037 if (term->tl_vterm == NULL)
2038 return;
2039
2040 while (*msg != NUL)
2041 {
2042 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2043 msg += MB_PTR2LEN(msg);
2044 }
2045
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002046 if (!term->tl_terminal_mode)
2047 {
2048 /* TODO: only update once in a while. */
2049 update_screen(0);
2050 if (buf == curbuf)
2051 update_cursor(term, TRUE);
2052 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002053}
2054
2055/*
2056 * "term_start(command, options)" function
2057 */
2058 void
2059f_term_start(typval_T *argvars, typval_T *rettv)
2060{
2061 char_u *cmd = get_tv_string_chk(&argvars[0]);
2062 exarg_T ea;
2063
2064 if (cmd == NULL)
2065 return;
2066 ea.arg = cmd;
2067 ex_terminal(&ea);
2068
2069 if (curbuf->b_term != NULL)
2070 rettv->vval.v_number = curbuf->b_fnum;
2071}
2072
2073/*
2074 * "term_wait" function
2075 */
2076 void
2077f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2078{
2079 buf_T *buf = term_get_buf(argvars);
2080
2081 if (buf == NULL)
2082 return;
2083
2084 /* Get the job status, this will detect a job that finished. */
2085 if (buf->b_term->tl_job != NULL)
2086 (void)job_status(buf->b_term->tl_job);
2087
2088 /* Check for any pending channel I/O. */
2089 vpeekc_any();
2090 ui_delay(10L, FALSE);
2091
2092 /* Flushing messages on channels is hopefully sufficient.
2093 * TODO: is there a better way? */
2094 parse_queued_messages();
2095}
2096
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002097# ifdef WIN3264
2098
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002099/**************************************
2100 * 2. MS-Windows implementation.
2101 */
2102
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002103#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2104#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2105
Bram Moolenaar8a773062017-07-24 22:29:21 +02002106void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002107void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002108void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002109BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2110void (*winpty_config_set_initial_size)(void*, int, int);
2111LPCWSTR (*winpty_conin_name)(void*);
2112LPCWSTR (*winpty_conout_name)(void*);
2113LPCWSTR (*winpty_conerr_name)(void*);
2114void (*winpty_free)(void*);
2115void (*winpty_config_free)(void*);
2116void (*winpty_spawn_config_free)(void*);
2117void (*winpty_error_free)(void*);
2118LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002119BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002120
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002121#define WINPTY_DLL "winpty.dll"
2122
2123static HINSTANCE hWinPtyDLL = NULL;
2124
2125 int
2126dyn_winpty_init(void)
2127{
2128 int i;
2129 static struct
2130 {
2131 char *name;
2132 FARPROC *ptr;
2133 } winpty_entry[] =
2134 {
2135 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2136 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2137 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2138 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2139 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2140 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2141 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2142 {"winpty_free", (FARPROC*)&winpty_free},
2143 {"winpty_open", (FARPROC*)&winpty_open},
2144 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2145 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2146 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2147 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002148 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002149 {NULL, NULL}
2150 };
2151
2152 /* No need to initialize twice. */
2153 if (hWinPtyDLL)
2154 return 1;
2155 /* Load winpty.dll */
2156 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2157 if (!hWinPtyDLL)
2158 {
2159 EMSG2(_(e_loadlib), WINPTY_DLL);
2160 return 0;
2161 }
2162 for (i = 0; winpty_entry[i].name != NULL
2163 && winpty_entry[i].ptr != NULL; ++i)
2164 {
2165 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2166 winpty_entry[i].name)) == NULL)
2167 {
2168 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2169 return 0;
2170 }
2171 }
2172
2173 return 1;
2174}
2175
2176/*
2177 * Create a new terminal of "rows" by "cols" cells.
2178 * Store a reference in "term".
2179 * Return OK or FAIL.
2180 */
2181 static int
2182term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2183{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002184 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002185 channel_T *channel = NULL;
2186 job_T *job = NULL;
2187 jobopt_T opt;
2188 DWORD error;
2189 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2190 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002191 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002192
2193 if (!dyn_winpty_init())
2194 return FAIL;
2195
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002196 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002197 if (p == NULL)
2198 return FAIL;
2199
2200 job = job_alloc();
2201 if (job == NULL)
2202 goto failed;
2203
2204 channel = add_channel();
2205 if (channel == NULL)
2206 goto failed;
2207
2208 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2209 if (term->tl_winpty_config == NULL)
2210 goto failed;
2211
2212 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2213 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2214 if (term->tl_winpty == NULL)
2215 goto failed;
2216
2217 spawn_config = winpty_spawn_config_new(
2218 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2219 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2220 NULL,
2221 p,
2222 NULL,
2223 NULL,
2224 &winpty_err);
2225 if (spawn_config == NULL)
2226 goto failed;
2227
2228 channel = add_channel();
2229 if (channel == NULL)
2230 goto failed;
2231
2232 job = job_alloc();
2233 if (job == NULL)
2234 goto failed;
2235
2236 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2237 &child_thread_handle, &error, &winpty_err))
2238 goto failed;
2239
2240 channel_set_pipes(channel,
2241 (sock_T) CreateFileW(
2242 winpty_conin_name(term->tl_winpty),
2243 GENERIC_WRITE, 0, NULL,
2244 OPEN_EXISTING, 0, NULL),
2245 (sock_T) CreateFileW(
2246 winpty_conout_name(term->tl_winpty),
2247 GENERIC_READ, 0, NULL,
2248 OPEN_EXISTING, 0, NULL),
2249 (sock_T) CreateFileW(
2250 winpty_conerr_name(term->tl_winpty),
2251 GENERIC_READ, 0, NULL,
2252 OPEN_EXISTING, 0, NULL));
2253
2254 jo = CreateJobObject(NULL, NULL);
2255 if (jo == NULL)
2256 goto failed;
2257
2258 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002259 {
2260 /* Failed, switch the way to terminate process with TerminateProcess. */
2261 CloseHandle(jo);
2262 jo = NULL;
2263 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002264
2265 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002266 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002267
2268 create_vterm(term, rows, cols);
2269
2270 setup_job_options(&opt, rows, cols);
2271 channel_set_job(channel, job, &opt);
2272
2273 job->jv_channel = channel;
2274 job->jv_proc_info.hProcess = child_process_handle;
2275 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2276 job->jv_job_object = jo;
2277 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002278 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002279 term->tl_job = job;
2280
2281 return OK;
2282
2283failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002284 if (spawn_config != NULL)
2285 winpty_spawn_config_free(spawn_config);
2286 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002287 if (channel != NULL)
2288 channel_clear(channel);
2289 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002290 {
2291 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002292 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002293 }
2294 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002295 if (jo != NULL)
2296 CloseHandle(jo);
2297 if (term->tl_winpty != NULL)
2298 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002299 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002300 if (term->tl_winpty_config != NULL)
2301 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002302 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002303 if (winpty_err != NULL)
2304 {
2305 char_u *msg = utf16_to_enc(
2306 (short_u *)winpty_error_msg(winpty_err), NULL);
2307
2308 EMSG(msg);
2309 winpty_error_free(winpty_err);
2310 }
2311 return FAIL;
2312}
2313
2314/*
2315 * Free the terminal emulator part of "term".
2316 */
2317 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002318term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002319{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002320 if (term->tl_winpty != NULL)
2321 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002322 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002323 if (term->tl_winpty_config != NULL)
2324 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002325 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002326 if (term->tl_vterm != NULL)
2327 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002328 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002329}
2330
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002331/*
2332 * Request size to terminal.
2333 */
2334 static void
2335term_report_winsize(term_T *term, int rows, int cols)
2336{
2337 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2338}
2339
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002340# else
2341
2342/**************************************
2343 * 3. Unix-like implementation.
2344 */
2345
2346/*
2347 * Create a new terminal of "rows" by "cols" cells.
2348 * Start job for "cmd".
2349 * Store the pointers in "term".
2350 * Return OK or FAIL.
2351 */
2352 static int
2353term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2354{
2355 typval_T argvars[2];
2356 jobopt_T opt;
2357
2358 create_vterm(term, rows, cols);
2359
2360 argvars[0].v_type = VAR_STRING;
2361 argvars[0].vval.v_string = cmd;
2362 setup_job_options(&opt, rows, cols);
2363 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002364 if (term->tl_job != NULL)
2365 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002366
Bram Moolenaar61a66052017-07-22 18:39:00 +02002367 return term->tl_job != NULL
2368 && term->tl_job->jv_channel != NULL
2369 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002370}
2371
2372/*
2373 * Free the terminal emulator part of "term".
2374 */
2375 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002376term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002377{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002378 if (term->tl_vterm != NULL)
2379 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002380 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002381}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002382
2383/*
2384 * Request size to terminal.
2385 */
2386 static void
2387term_report_winsize(term_T *term, int rows, int cols)
2388{
2389 /* Use an ioctl() to report the new window size to the job. */
2390 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2391 {
2392 int fd = -1;
2393 int part;
2394
2395 for (part = PART_OUT; part < PART_COUNT; ++part)
2396 {
2397 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2398 if (isatty(fd))
2399 break;
2400 }
2401 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2402 mch_stop_job(term->tl_job, (char_u *)"winch");
2403 }
2404}
2405
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002406# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002407
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002408#endif /* FEAT_TERMINAL */