blob: 7f486dd4f65bcf5aeeda900c538400b9cb8df3c6 [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 Moolenaar20e6cd02017-08-01 20:25:22 +020039 * - When closing a window with a terminal buffer where the job has ended, wipe
40 * out the buffer. Like 'bufhidden' is "wipe".
41 * - When a buffer with a terminal is wiped out, kill the job and close the
42 * channel.
Bram Moolenaar423802d2017-07-30 16:52:24 +020043 * - in bash mouse clicks are inserting characters.
44 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020045 * - typing CTRL-C is not sent to the terminal. need to setup controlling tty?
46 * #1910
Bram Moolenaard85f2712017-07-28 21:51:57 +020047 * - For the scrollback buffer store lines in the buffer, only attributes in
48 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020049 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020050 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020051 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
52 * job finishes).
53 * - add option values to the command:
54 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020055 * - support different cursor shapes, colors and attributes
56 * - make term_getcursor() return type (none/block/bar/underline) and
57 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020058 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020059 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020060 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020061 * - add 't' to mode()
Bram Moolenaar12d853f2017-08-01 18:04:04 +020062 * - When making a change after the job has ended, make the buffer a normal
63 * buffer; needs to be written.
Bram Moolenaar938783d2017-07-16 20:13:26 +020064 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaard85f2712017-07-28 21:51:57 +020065 * - when closing window and job has ended, make buffer hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020066 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020067 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020068 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020069 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020070 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020071 * - implement "term" for job_start(): more job options when starting a
72 * terminal.
Bram Moolenaar423802d2017-07-30 16:52:24 +020073 * - if the job in the terminal does not support the mouse, we can use the
74 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020075 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
76 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020077 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020078 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020079 * - Copy text in the vterm to the Vim buffer once in a while, so that
80 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020081 */
82
83#include "vim.h"
84
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020085#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020086
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020087#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020088# define MIN(x,y) (x < y ? x : y)
89# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020090#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020092#include "libvterm/include/vterm.h"
93
Bram Moolenaard85f2712017-07-28 21:51:57 +020094typedef struct sb_line_S {
95 int sb_cols; /* can differ per line */
96 VTermScreenCell *sb_cells; /* allocated */
97} sb_line_T;
98
Bram Moolenaare4f25e42017-07-07 11:54:15 +020099/* typedef term_T in structs.h */
100struct terminal_S {
101 term_T *tl_next;
102
Bram Moolenaar423802d2017-07-30 16:52:24 +0200103 VTerm *tl_vterm;
104 job_T *tl_job;
105 buf_T *tl_buffer;
106
107 int tl_terminal_mode;
108 int tl_channel_closed;
109
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200110#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200111 void *tl_winpty_config;
112 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200113#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200114
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200115 /* last known vterm size */
116 int tl_rows;
117 int tl_cols;
118 /* vterm size does not follow window size */
119 int tl_rows_fixed;
120 int tl_cols_fixed;
121
Bram Moolenaar21554412017-07-24 21:44:43 +0200122 char_u *tl_title; /* NULL or allocated */
123 char_u *tl_status_text; /* NULL or allocated */
124
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125 /* Range of screen rows to update. Zero based. */
126 int tl_dirty_row_start; /* -1 if nothing dirty */
127 int tl_dirty_row_end; /* row below last one to update */
128
Bram Moolenaard85f2712017-07-28 21:51:57 +0200129 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200130 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200131
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200132 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200133 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200134};
135
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200136/*
137 * List of all active terminals.
138 */
139static term_T *first_term = NULL;
140
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200141
142#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
143#define KEY_BUF_LEN 200
144
145/*
146 * Functions with separate implementation for MS-Windows and Unix-like systems.
147 */
148static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200149static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200150static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200151
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200152/**************************************
153 * 1. Generic code for all systems.
154 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200155
156/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200157 * Determine the terminal size from 'termsize' and the current window.
158 * Assumes term->tl_rows and term->tl_cols are zero.
159 */
160 static void
161set_term_and_win_size(term_T *term)
162{
163 if (*curwin->w_p_tms != NUL)
164 {
165 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
166
167 term->tl_rows = atoi((char *)curwin->w_p_tms);
168 term->tl_cols = atoi((char *)p);
169 }
170 if (term->tl_rows == 0)
171 term->tl_rows = curwin->w_height;
172 else
173 {
174 win_setheight_win(term->tl_rows, curwin);
175 term->tl_rows_fixed = TRUE;
176 }
177 if (term->tl_cols == 0)
178 term->tl_cols = curwin->w_width;
179 else
180 {
181 win_setwidth_win(term->tl_cols, curwin);
182 term->tl_cols_fixed = TRUE;
183 }
184}
185
186/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200187 * ":terminal": open a terminal window and execute a job in it.
188 */
189 void
190ex_terminal(exarg_T *eap)
191{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200192 exarg_T split_ea;
193 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200194 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200195 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200196
197 if (check_restricted() || check_secure())
198 return;
199
200 term = (term_T *)alloc_clear(sizeof(term_T));
201 if (term == NULL)
202 return;
203 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200204 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200205 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200206
207 /* Open a new window or tab. */
208 vim_memset(&split_ea, 0, sizeof(split_ea));
209 split_ea.cmdidx = CMD_new;
210 split_ea.cmd = (char_u *)"new";
211 split_ea.arg = (char_u *)"";
212 ex_splitview(&split_ea);
213 if (curwin == old_curwin)
214 {
215 /* split failed */
216 vim_free(term);
217 return;
218 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200219 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200220 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200221
222 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200223 term->tl_next = first_term;
224 first_term = term;
225
Bram Moolenaar293424c2017-07-26 23:11:01 +0200226 if (cmd == NULL || *cmd == NUL)
227 cmd = p_sh;
228
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200229 {
230 int i;
231 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200232 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200233
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200234 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200235 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200236 /* Prepend a ! to the command name to avoid the buffer name equals
237 * the executable, otherwise ":w!" would overwrite it. */
238 if (i == 0)
239 vim_snprintf((char *)p, len, "!%s", cmd);
240 else
241 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200242 if (buflist_findname(p) == NULL)
243 {
244 curbuf->b_ffname = p;
245 break;
246 }
247 }
248 }
249 curbuf->b_fname = curbuf->b_ffname;
250
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200251 /* Mark the buffer as not modifiable. It can only be made modifiable after
252 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200253 curbuf->b_p_ma = FALSE;
254 set_string_option_direct((char_u *)"buftype", -1,
255 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200256
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200257 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200258
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200259 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200260 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200261 {
262 /* store the size we ended up with */
263 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264 }
265 else
266 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200267 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200268
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200269 /* Wiping out the buffer will also close the window and call
270 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200271 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200272 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200273}
274
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200275/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200276 * Free the scrollback buffer for "term".
277 */
278 static void
279free_scrollback(term_T *term)
280{
281 int i;
282
283 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
284 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
285 ga_clear(&term->tl_scrollback);
286}
287
288/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200289 * Free a terminal and everything it refers to.
290 * Kills the job if there is one.
291 * Called when wiping out a buffer.
292 */
293 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200294free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200295{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200296 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200297 term_T *tp;
298
299 if (term == NULL)
300 return;
301 if (first_term == term)
302 first_term = term->tl_next;
303 else
304 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
305 if (tp->tl_next == term)
306 {
307 tp->tl_next = term->tl_next;
308 break;
309 }
310
311 if (term->tl_job != NULL)
312 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200313 if (term->tl_job->jv_status != JOB_ENDED
314 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200315 job_stop(term->tl_job, NULL, "kill");
316 job_unref(term->tl_job);
317 }
318
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200319 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200320
321 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200322 vim_free(term->tl_title);
323 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200324 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200325 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200326}
327
328/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200329 * Write job output "msg[len]" to the vterm.
330 */
331 static void
332term_write_job_output(term_T *term, char_u *msg, size_t len)
333{
334 VTerm *vterm = term->tl_vterm;
335 char_u *p;
336 size_t done;
337 size_t len_now;
338
339 for (done = 0; done < len; done += len_now)
340 {
341 for (p = msg + done; p < msg + len; )
342 {
343 if (*p == NL)
344 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200345 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200346 }
347 len_now = p - msg - done;
348 vterm_input_write(vterm, (char *)msg + done, len_now);
349 if (p < msg + len && *p == NL)
350 {
351 /* Convert NL to CR-NL, that appears to work best. */
352 vterm_input_write(vterm, "\r\n", 2);
353 ++len_now;
354 }
355 }
356
357 /* this invokes the damage callbacks */
358 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
359}
360
Bram Moolenaar1c844932017-07-24 23:36:41 +0200361 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200362update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200363{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200364 if (term->tl_terminal_mode)
365 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200366 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200367 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200368 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200369 if (term->tl_cursor_visible)
370 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200371 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200372#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200373 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200374 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200375#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200376 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200377}
378
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200379/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200380 * Invoked when "msg" output from a job was received. Write it to the terminal
381 * of "buffer".
382 */
383 void
384write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
385{
386 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200387 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200388
Bram Moolenaard85f2712017-07-28 21:51:57 +0200389 if (term->tl_vterm == NULL)
390 {
391 ch_logn(channel, "NOT writing %d bytes to terminal", (int)len);
392 return;
393 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200394 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200395 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200396
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200397 if (!term->tl_terminal_mode)
398 {
399 /* TODO: only update once in a while. */
400 update_screen(0);
401 update_cursor(term, TRUE);
402 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200403}
404
405/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200406 * Send a mouse position and click to the vterm
407 */
408 static int
409term_send_mouse(VTerm *vterm, int button, int pressed)
410{
411 VTermModifier mod = VTERM_MOD_NONE;
412
413 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
414 mouse_col - W_WINCOL(curwin), mod);
415 vterm_mouse_button(vterm, button, pressed, mod);
416 return TRUE;
417}
418
419/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200420 * Convert typed key "c" into bytes to send to the job.
421 * Return the number of bytes in "buf".
422 */
423 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200424term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200425{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200426 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200427 VTermKey key = VTERM_KEY_NONE;
428 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200429 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200430
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200431 switch (c)
432 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200433 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200434 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200435 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
436 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200437 case K_DEL: key = VTERM_KEY_DEL; break;
438 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200439 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
440 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200441 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200442 case K_S_END: mod = VTERM_MOD_SHIFT;
443 key = VTERM_KEY_END; break;
444 case K_C_END: mod = VTERM_MOD_CTRL;
445 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200446 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
447 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
448 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
449 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
450 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
451 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
452 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
453 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
454 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
455 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
456 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
457 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
458 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200459 case K_S_HOME: mod = VTERM_MOD_SHIFT;
460 key = VTERM_KEY_HOME; break;
461 case K_C_HOME: mod = VTERM_MOD_CTRL;
462 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200463 case K_INS: key = VTERM_KEY_INS; break;
464 case K_K0: key = VTERM_KEY_KP_0; break;
465 case K_K1: key = VTERM_KEY_KP_1; break;
466 case K_K2: key = VTERM_KEY_KP_2; break;
467 case K_K3: key = VTERM_KEY_KP_3; break;
468 case K_K4: key = VTERM_KEY_KP_4; break;
469 case K_K5: key = VTERM_KEY_KP_5; break;
470 case K_K6: key = VTERM_KEY_KP_6; break;
471 case K_K7: key = VTERM_KEY_KP_7; break;
472 case K_K8: key = VTERM_KEY_KP_8; break;
473 case K_K9: key = VTERM_KEY_KP_9; break;
474 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
475 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
476 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
477 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
478 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
479 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
480 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
481 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
482 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
483 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
484 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
485 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
486 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200487 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
488 key = VTERM_KEY_LEFT; break;
489 case K_C_LEFT: mod = VTERM_MOD_CTRL;
490 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200491 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
492 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
493 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200494 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
495 key = VTERM_KEY_RIGHT; break;
496 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
497 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200498 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200499 case K_S_UP: mod = VTERM_MOD_SHIFT;
500 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200501 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200502
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200503 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
504 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
505 case K_MOUSELEFT: /* TODO */ return 0;
506 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200507
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200508 case K_LEFTMOUSE:
509 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
510 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
511 case K_LEFTRELEASE:
512 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
513 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
514 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
515 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
516 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
517 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
518 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
519 case K_X1MOUSE: /* TODO */ return 0;
520 case K_X1DRAG: /* TODO */ return 0;
521 case K_X1RELEASE: /* TODO */ return 0;
522 case K_X2MOUSE: /* TODO */ return 0;
523 case K_X2DRAG: /* TODO */ return 0;
524 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200525
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200526 case K_IGNORE: return 0;
527 case K_NOP: return 0;
528 case K_UNDO: return 0;
529 case K_HELP: return 0;
530 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
531 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
532 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
533 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
534 case K_SELECT: return 0;
535#ifdef FEAT_GUI
536 case K_VER_SCROLLBAR: return 0;
537 case K_HOR_SCROLLBAR: return 0;
538#endif
539#ifdef FEAT_GUI_TABLINE
540 case K_TABLINE: return 0;
541 case K_TABMENU: return 0;
542#endif
543#ifdef FEAT_NETBEANS_INTG
544 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
545#endif
546#ifdef FEAT_DND
547 case K_DROP: return 0;
548#endif
549#ifdef FEAT_AUTOCMD
550 case K_CURSORHOLD: return 0;
551#endif
552 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
553 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200554 }
555
556 /*
557 * Convert special keys to vterm keys:
558 * - Write keys to vterm: vterm_keyboard_key()
559 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200560 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200561 */
562 if (key != VTERM_KEY_NONE)
563 /* Special key, let vterm convert it. */
564 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200565 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200566 /* Normal character, let vterm convert it. */
567 vterm_keyboard_unichar(vterm, c, mod);
568
569 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200570 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200571}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200572
Bram Moolenaar938783d2017-07-16 20:13:26 +0200573/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200574 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200575 */
576 static int
577term_job_running(term_T *term)
578{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200579 /* Also consider the job finished when the channel is closed, to avoid a
580 * race condition when updating the title. */
581 return term->tl_job != NULL
582 && term->tl_job->jv_status == JOB_STARTED
583 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200584}
585
586/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200587 * Add the last line of the scrollback buffer to the buffer in the window.
588 */
589 static void
590add_scrollback_line_to_buffer(term_T *term)
591{
592 linenr_T lnum = term->tl_scrollback.ga_len - 1;
593 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
594 garray_T ga;
595 int c;
596 int col;
597 int i;
598
599 ga_init2(&ga, 1, 100);
600 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
601 {
602 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
603 goto failed;
604 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
605 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
606 (char_u *)ga.ga_data + ga.ga_len);
607 }
608 if (ga_grow(&ga, 1) == FAIL)
609 goto failed;
610 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
611 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
612
613 if (lnum == 0)
614 {
615 /* Delete the empty line that was in the empty buffer. */
616 curbuf = term->tl_buffer;
617 ml_delete(2, FALSE);
618 curbuf = curwin->w_buffer;
619 }
620
621failed:
622 ga_clear(&ga);
623}
624
625/*
626 * Add the current lines of the terminal to scrollback and to the buffer.
627 * Called after the job has ended and when switching to Terminal mode.
628 */
629 static void
630move_terminal_to_buffer(term_T *term)
631{
632 win_T *wp;
633 int len;
634 int lines_skipped = 0;
635 VTermPos pos;
636 VTermScreenCell cell;
637 VTermScreenCell *p;
638 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
639
640 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
641 {
642 len = 0;
643 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
644 if (vterm_screen_get_cell(screen, pos, &cell) != 0
645 && cell.chars[0] != NUL)
646 len = pos.col + 1;
647
648 if (len == 0)
649 ++lines_skipped;
650 else
651 {
652 while (lines_skipped > 0)
653 {
654 /* Line was skipped, add an empty line. */
655 --lines_skipped;
656 if (ga_grow(&term->tl_scrollback, 1) == OK)
657 {
658 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
659 + term->tl_scrollback.ga_len;
660
661 line->sb_cols = 0;
662 line->sb_cells = NULL;
663 ++term->tl_scrollback.ga_len;
664
665 add_scrollback_line_to_buffer(term);
666 }
667 }
668
669 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
670 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
671 {
672 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
673 + term->tl_scrollback.ga_len;
674
675 for (pos.col = 0; pos.col < len; ++pos.col)
676 {
677 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
678 vim_memset(p + pos.col, 0, sizeof(cell));
679 else
680 p[pos.col] = cell;
681 }
682 line->sb_cols = len;
683 line->sb_cells = p;
684 ++term->tl_scrollback.ga_len;
685
686 add_scrollback_line_to_buffer(term);
687 }
688 else
689 vim_free(p);
690 }
691 }
692
693 FOR_ALL_WINDOWS(wp)
694 {
695 if (wp->w_buffer == term->tl_buffer)
696 {
697 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
698 wp->w_cursor.col = 0;
699 wp->w_valid = 0;
700 redraw_win_later(wp, NOT_VALID);
701 }
702 }
703}
704
705 static void
706set_terminal_mode(term_T *term, int on)
707{
708 term->tl_terminal_mode = on;
709 vim_free(term->tl_status_text);
710 term->tl_status_text = NULL;
711 if (term->tl_buffer == curbuf)
712 maketitle();
713}
714
715/*
716 * Called after the job if finished and Terminal mode is not active:
717 * Move the vterm contents into the scrollback buffer and free the vterm.
718 */
719 static void
720cleanup_vterm(term_T *term)
721{
722 move_terminal_to_buffer(term);
723 term_free_vterm(term);
724 set_terminal_mode(term, FALSE);
725}
726
727/*
728 * Switch from sending keys to the job to Terminal-Normal mode.
729 * Suspends updating the terminal window.
730 */
731 static void
732term_enter_terminal_mode()
733{
734 term_T *term = curbuf->b_term;
735
736 /* Append the current terminal contents to the buffer. */
737 move_terminal_to_buffer(term);
738
739 set_terminal_mode(term, TRUE);
740}
741
742/*
743 * Returns TRUE if the current window contains a terminal and we are in
744 * Terminal-Normal mode.
745 */
746 int
747term_in_terminal_mode()
748{
749 term_T *term = curbuf->b_term;
750
751 return term != NULL && term->tl_terminal_mode;
752}
753
754/*
755 * Switch from Terminal-Normal mode to sending keys to the job.
756 * Restores updating the terminal window.
757 */
758 void
759term_leave_terminal_mode()
760{
761 term_T *term = curbuf->b_term;
762 sb_line_T *line;
763 garray_T *gap;
764
765 /* Remove the terminal contents from the scrollback and the buffer. */
766 gap = &term->tl_scrollback;
767 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
768 {
769 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
770 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
771 vim_free(line->sb_cells);
772 --gap->ga_len;
773 if (gap->ga_len == 0)
774 break;
775 }
776 check_cursor();
777
778 set_terminal_mode(term, FALSE);
779
780 if (term->tl_channel_closed)
781 cleanup_vterm(term);
782 redraw_buf_and_status_later(curbuf, NOT_VALID);
783}
784
785/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200786 * Get a key from the user without mapping.
787 * TODO: use terminal mode mappings.
788 */
789 static int
790term_vgetc()
791{
792 int c;
793
794 ++no_mapping;
795 ++allow_keys;
796 got_int = FALSE;
797 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200798 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200799 --no_mapping;
800 --allow_keys;
801 return c;
802}
803
804/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200805 * Send keys to terminal.
806 */
807 static int
808send_keys_to_term(term_T *term, int c, int typed)
809{
810 char msg[KEY_BUF_LEN];
811 size_t len;
812 static int mouse_was_outside = FALSE;
813 int dragging_outside = FALSE;
814
815 /* Catch keys that need to be handled as in Normal mode. */
816 switch (c)
817 {
818 case NUL:
819 case K_ZERO:
820 if (typed)
821 stuffcharReadbuff(c);
822 return FAIL;
823
824 case K_IGNORE:
825 return FAIL;
826
827 case K_LEFTDRAG:
828 case K_MIDDLEDRAG:
829 case K_RIGHTDRAG:
830 case K_X1DRAG:
831 case K_X2DRAG:
832 dragging_outside = mouse_was_outside;
833 /* FALLTHROUGH */
834 case K_LEFTMOUSE:
835 case K_LEFTMOUSE_NM:
836 case K_LEFTRELEASE:
837 case K_LEFTRELEASE_NM:
838 case K_MIDDLEMOUSE:
839 case K_MIDDLERELEASE:
840 case K_RIGHTMOUSE:
841 case K_RIGHTRELEASE:
842 case K_X1MOUSE:
843 case K_X1RELEASE:
844 case K_X2MOUSE:
845 case K_X2RELEASE:
846 if (mouse_row < W_WINROW(curwin)
847 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
848 || mouse_col < W_WINCOL(curwin)
849 || mouse_col >= W_ENDCOL(curwin)
850 || dragging_outside)
851 {
852 /* click outside the current window */
853 if (typed)
854 {
855 stuffcharReadbuff(c);
856 mouse_was_outside = TRUE;
857 }
858 return FAIL;
859 }
860 }
861 if (typed)
862 mouse_was_outside = FALSE;
863
864 /* Convert the typed key to a sequence of bytes for the job. */
865 len = term_convert_key(term, c, msg);
866 if (len > 0)
867 /* TODO: if FAIL is returned, stop? */
868 channel_send(term->tl_job->jv_channel, PART_IN,
869 (char_u *)msg, (int)len, NULL);
870
871 return OK;
872}
873
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200874 static void
875position_cursor(win_T *wp, VTermPos *pos)
876{
877 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
878 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
879 wp->w_valid |= (VALID_WCOL|VALID_WROW);
880}
881
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200882/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200883 * Handle CTRL-W "": send register contents to the job.
884 */
885 static void
886term_paste_register(int prev_c UNUSED)
887{
888 int c;
889 list_T *l;
890 listitem_T *item;
891 long reglen = 0;
892 int type;
893
894#ifdef FEAT_CMDL_INFO
895 if (add_to_showcmd(prev_c))
896 if (add_to_showcmd('"'))
897 out_flush();
898#endif
899 c = term_vgetc();
900#ifdef FEAT_CMDL_INFO
901 clear_showcmd();
902#endif
903
904 /* CTRL-W "= prompt for expression to evaluate. */
905 if (c == '=' && get_expr_register() != '=')
906 return;
907
908 l = (list_T *)get_reg_contents(c, GREG_LIST);
909 if (l != NULL)
910 {
911 type = get_reg_type(c, &reglen);
912 for (item = l->lv_first; item != NULL; item = item->li_next)
913 {
914 char_u *s = get_tv_string(&item->li_tv);
915
916 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
917 s, STRLEN(s), NULL);
918 if (item->li_next != NULL || type == MLINE)
919 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
920 (char_u *)"\r", 1, NULL);
921 }
922 list_free(l);
923 }
924}
925
926/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200927 * Returns TRUE if the current window contains a terminal and we are sending
928 * keys to the job.
929 */
930 int
931term_use_loop()
932{
933 term_T *term = curbuf->b_term;
934
935 return term != NULL
936 && !term->tl_terminal_mode
937 && term->tl_vterm != NULL
938 && term_job_running(term);
939}
940
941/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200942 * Wait for input and send it to the job.
943 * Return when the start of a CTRL-W command is typed or anything else that
944 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200945 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
946 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200947 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200948 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200949terminal_loop(void)
950{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200951 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200952 int termkey = 0;
953
954 if (*curwin->w_p_tk != NUL)
955 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200956 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200957
958 for (;;)
959 {
960 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200961 /* Repeat redrawing in case a message is received while redrawing. */
962 while (curwin->w_redr_type != 0)
963 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200964 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200965
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200966 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200967 if (curbuf->b_term->tl_vterm == NULL
968 || !term_job_running(curbuf->b_term))
969 /* job finished while waiting for a character */
970 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200971
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200972 if (c == (termkey == 0 ? Ctrl_W : termkey))
973 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200974 int prev_c = c;
975
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200976#ifdef FEAT_CMDL_INFO
977 if (add_to_showcmd(c))
978 out_flush();
979#endif
980 c = term_vgetc();
981#ifdef FEAT_CMDL_INFO
982 clear_showcmd();
983#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200984 if (curbuf->b_term->tl_vterm == NULL
985 || !term_job_running(curbuf->b_term))
986 /* job finished while waiting for a character */
987 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200988
989 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200990 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200991 /* "CTRL-W .": send CTRL-W to the job */
992 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200993 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200994 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200995 {
996 term_enter_terminal_mode();
997 return FAIL;
998 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200999 else if (c == '"')
1000 {
1001 term_paste_register(prev_c);
1002 continue;
1003 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001004 else if (termkey == 0 || c != termkey)
1005 {
1006 stuffcharReadbuff(Ctrl_W);
1007 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001008 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001009 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001010 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001011 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1012 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001013 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001014 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001015}
1016
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001017/*
1018 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001019 * This updates the title and status, but does not close the vter, because
1020 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001021 */
1022 void
1023term_job_ended(job_T *job)
1024{
1025 term_T *term;
1026 int did_one = FALSE;
1027
1028 for (term = first_term; term != NULL; term = term->tl_next)
1029 if (term->tl_job == job)
1030 {
1031 vim_free(term->tl_title);
1032 term->tl_title = NULL;
1033 vim_free(term->tl_status_text);
1034 term->tl_status_text = NULL;
1035 redraw_buf_and_status_later(term->tl_buffer, VALID);
1036 did_one = TRUE;
1037 }
1038 if (did_one)
1039 redraw_statuslines();
1040 if (curbuf->b_term != NULL)
1041 {
1042 if (curbuf->b_term->tl_job == job)
1043 maketitle();
1044 update_cursor(curbuf->b_term, TRUE);
1045 }
1046}
1047
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001048 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001049may_toggle_cursor(term_T *term)
1050{
1051 if (curbuf == term->tl_buffer)
1052 {
1053 if (term->tl_cursor_visible)
1054 cursor_on();
1055 else
1056 cursor_off();
1057 }
1058}
1059
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001060 static int
1061handle_damage(VTermRect rect, void *user)
1062{
1063 term_T *term = (term_T *)user;
1064
1065 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1066 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1067 redraw_buf_later(term->tl_buffer, NOT_VALID);
1068 return 1;
1069}
1070
1071 static int
1072handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1073{
1074 term_T *term = (term_T *)user;
1075
1076 /* TODO */
1077 redraw_buf_later(term->tl_buffer, NOT_VALID);
1078 return 1;
1079}
1080
1081 static int
1082handle_movecursor(
1083 VTermPos pos,
1084 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001085 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001086 void *user)
1087{
1088 term_T *term = (term_T *)user;
1089 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001090
1091 term->tl_cursor_pos = pos;
1092 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001093
1094 FOR_ALL_WINDOWS(wp)
1095 {
1096 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001097 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001098 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001099 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001100 {
1101 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001102 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001103 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001104
1105 return 1;
1106}
1107
Bram Moolenaar21554412017-07-24 21:44:43 +02001108 static int
1109handle_settermprop(
1110 VTermProp prop,
1111 VTermValue *value,
1112 void *user)
1113{
1114 term_T *term = (term_T *)user;
1115
1116 switch (prop)
1117 {
1118 case VTERM_PROP_TITLE:
1119 vim_free(term->tl_title);
1120 term->tl_title = vim_strsave((char_u *)value->string);
1121 vim_free(term->tl_status_text);
1122 term->tl_status_text = NULL;
1123 if (term == curbuf->b_term)
1124 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001125 break;
1126
1127 case VTERM_PROP_CURSORVISIBLE:
1128 term->tl_cursor_visible = value->boolean;
1129 may_toggle_cursor(term);
1130 out_flush();
1131 break;
1132
Bram Moolenaar21554412017-07-24 21:44:43 +02001133 default:
1134 break;
1135 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001136 /* Always return 1, otherwise vterm doesn't store the value internally. */
1137 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001138}
1139
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001140/*
1141 * The job running in the terminal resized the terminal.
1142 */
1143 static int
1144handle_resize(int rows, int cols, void *user)
1145{
1146 term_T *term = (term_T *)user;
1147 win_T *wp;
1148
1149 term->tl_rows = rows;
1150 term->tl_cols = cols;
1151 FOR_ALL_WINDOWS(wp)
1152 {
1153 if (wp->w_buffer == term->tl_buffer)
1154 {
1155 win_setheight_win(rows, wp);
1156 win_setwidth_win(cols, wp);
1157 }
1158 }
1159
1160 redraw_buf_later(term->tl_buffer, NOT_VALID);
1161 return 1;
1162}
1163
Bram Moolenaard85f2712017-07-28 21:51:57 +02001164/*
1165 * Handle a line that is pushed off the top of the screen.
1166 */
1167 static int
1168handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1169{
1170 term_T *term = (term_T *)user;
1171
1172 /* TODO: Limit the number of lines that are stored. */
1173 /* TODO: put the text in the buffer. */
1174 if (ga_grow(&term->tl_scrollback, 1) == OK)
1175 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001176 VTermScreenCell *p = NULL;
1177 int len = 0;
1178 int i;
1179 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001180
1181 /* do not store empty cells at the end */
1182 for (i = 0; i < cols; ++i)
1183 if (cells[i].chars[0] != 0)
1184 len = i + 1;
1185
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001186 if (len > 0)
1187 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001188 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001189 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001190
1191 line = (sb_line_T *)term->tl_scrollback.ga_data
1192 + term->tl_scrollback.ga_len;
1193 line->sb_cols = len;
1194 line->sb_cells = p;
1195 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001196 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001197
1198 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001199 }
1200 return 0; /* ignored */
1201}
1202
Bram Moolenaar21554412017-07-24 21:44:43 +02001203static VTermScreenCallbacks screen_callbacks = {
1204 handle_damage, /* damage */
1205 handle_moverect, /* moverect */
1206 handle_movecursor, /* movecursor */
1207 handle_settermprop, /* settermprop */
1208 NULL, /* bell */
1209 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001210 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001211 NULL /* sb_popline */
1212};
1213
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001214/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001215 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001216 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001217 */
1218 void
1219term_channel_closed(channel_T *ch)
1220{
1221 term_T *term;
1222 int did_one = FALSE;
1223
1224 for (term = first_term; term != NULL; term = term->tl_next)
1225 if (term->tl_job == ch->ch_job)
1226 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001227 term->tl_channel_closed = TRUE;
1228
Bram Moolenaard85f2712017-07-28 21:51:57 +02001229 vim_free(term->tl_title);
1230 term->tl_title = NULL;
1231 vim_free(term->tl_status_text);
1232 term->tl_status_text = NULL;
1233
Bram Moolenaar423802d2017-07-30 16:52:24 +02001234 /* Unless in Terminal-Normal mode: clear the vterm. */
1235 if (!term->tl_terminal_mode)
1236 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001237
1238 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1239 did_one = TRUE;
1240 }
1241 if (did_one)
1242 {
1243 redraw_statuslines();
1244
1245 /* Need to break out of vgetc(). */
1246 ins_char_typebuf(K_IGNORE);
1247
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001248 term = curbuf->b_term;
1249 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001250 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001251 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001252 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001253 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001254 }
1255 }
1256}
1257
1258/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001259 * Reverse engineer the RGB value into a cterm color index.
1260 * First color is 1. Return 0 if no match found.
1261 */
1262 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001263color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001264{
1265 int red = color->red;
1266 int blue = color->blue;
1267 int green = color->green;
1268
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001269 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001270 if (red == 0)
1271 {
1272 if (green == 0)
1273 {
1274 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001275 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001276 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001277 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001278 }
1279 else if (green == 224)
1280 {
1281 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001282 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001283 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001284 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001285 }
1286 }
1287 else if (red == 224)
1288 {
1289 if (green == 0)
1290 {
1291 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001292 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001293 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001294 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001295 }
1296 else if (green == 224)
1297 {
1298 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001299 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001300 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001301 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001302 }
1303 }
1304 else if (red == 128)
1305 {
1306 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001307 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001308 }
1309 else if (red == 255)
1310 {
1311 if (green == 64)
1312 {
1313 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001314 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001315 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001316 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001317 }
1318 else if (green == 255)
1319 {
1320 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001321 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001322 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001323 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001324 }
1325 }
1326 else if (red == 64)
1327 {
1328 if (green == 64)
1329 {
1330 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001331 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001332 }
1333 else if (green == 255)
1334 {
1335 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001336 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001337 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001338 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001339 }
1340 }
1341 if (t_colors >= 256)
1342 {
1343 if (red == blue && red == green)
1344 {
1345 /* 24-color greyscale */
1346 static int cutoff[23] = {
1347 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1348 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1349 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1350 int i;
1351
1352 for (i = 0; i < 23; ++i)
1353 if (red < cutoff[i])
1354 return i + 233;
1355 return 256;
1356 }
1357
1358 /* 216-color cube */
1359 return 17 + ((red + 25) / 0x33) * 36
1360 + ((green + 25) / 0x33) * 6
1361 + (blue + 25) / 0x33;
1362 }
1363 return 0;
1364}
1365
1366/*
1367 * Convert the attributes of a vterm cell into an attribute index.
1368 */
1369 static int
1370cell2attr(VTermScreenCell *cell)
1371{
1372 int attr = 0;
1373
1374 if (cell->attrs.bold)
1375 attr |= HL_BOLD;
1376 if (cell->attrs.underline)
1377 attr |= HL_UNDERLINE;
1378 if (cell->attrs.italic)
1379 attr |= HL_ITALIC;
1380 if (cell->attrs.strike)
1381 attr |= HL_STANDOUT;
1382 if (cell->attrs.reverse)
1383 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001384
1385#ifdef FEAT_GUI
1386 if (gui.in_use)
1387 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001388 guicolor_T fg, bg;
1389
1390 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1391 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1392 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001393 }
1394 else
1395#endif
1396#ifdef FEAT_TERMGUICOLORS
1397 if (p_tgc)
1398 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001399 guicolor_T fg, bg;
1400
1401 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1402 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1403
1404 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001405 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001406 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001407#endif
1408 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001409 int bold = MAYBE;
1410 int fg = color2index(&cell->fg, TRUE, &bold);
1411 int bg = color2index(&cell->bg, FALSE, &bold);
1412
1413 /* with 8 colors set the bold attribute to get a bright foreground */
1414 if (bold == TRUE)
1415 attr |= HL_BOLD;
1416 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001417 }
1418 return 0;
1419}
1420
1421/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001422 * Called to update the window that contains a terminal.
1423 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001424 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001425 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001426term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001427{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001428 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001429 VTerm *vterm;
1430 VTermScreen *screen;
1431 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001432 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001433
Bram Moolenaar423802d2017-07-30 16:52:24 +02001434 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001435 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001436
Bram Moolenaard85f2712017-07-28 21:51:57 +02001437 vterm = term->tl_vterm;
1438 screen = vterm_obtain_screen(vterm);
1439 state = vterm_obtain_state(vterm);
1440
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001441 /*
1442 * If the window was resized a redraw will be triggered and we get here.
1443 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1444 */
1445 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1446 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001447 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001448 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1449 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1450 win_T *twp;
1451
1452 FOR_ALL_WINDOWS(twp)
1453 {
1454 /* When more than one window shows the same terminal, use the
1455 * smallest size. */
1456 if (twp->w_buffer == term->tl_buffer)
1457 {
1458 if (!term->tl_rows_fixed && rows > twp->w_height)
1459 rows = twp->w_height;
1460 if (!term->tl_cols_fixed && cols > twp->w_width)
1461 cols = twp->w_width;
1462 }
1463 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001464
1465 vterm_set_size(vterm, rows, cols);
1466 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1467 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001468 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001469 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001470
1471 /* The cursor may have been moved when resizing. */
1472 vterm_state_get_cursorpos(state, &pos);
1473 position_cursor(wp, &pos);
1474
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001475 /* TODO: Only redraw what changed. */
1476 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001477 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001478 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001479 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001480
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001481 if (pos.row < term->tl_rows)
1482 {
1483 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001484 {
1485 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001486 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001487
Bram Moolenaareeac6772017-07-23 15:48:37 +02001488 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1489 vim_memset(&cell, 0, sizeof(cell));
1490
1491 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001492 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001493 if (c == NUL)
1494 {
1495 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001496#if defined(FEAT_MBYTE)
1497 if (enc_utf8)
1498 ScreenLinesUC[off] = NUL;
1499#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001500 }
1501 else
1502 {
1503#if defined(FEAT_MBYTE)
1504 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001505 {
1506 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001507 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001508 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001509 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001510 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001511 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001512 if (enc_utf8)
1513 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001514 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001515#else
1516 ScreenLines[off] = c;
1517#endif
1518 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001519 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001520
1521 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001522 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001523 if (cell.width == 2)
1524 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001525 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001526#if defined(FEAT_MBYTE)
1527 if (enc_utf8)
1528 ScreenLinesUC[off] = NUL;
1529#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001530 ++pos.col;
1531 ++off;
1532 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001533 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001534 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001535 else
1536 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001537
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001538 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1539 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001540 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001541
1542 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001543}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001544
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001545/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001546 * Return TRUE if "wp" is a terminal window where the job has finished.
1547 */
1548 int
1549term_is_finished(buf_T *buf)
1550{
1551 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1552}
1553
1554/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001555 * Return TRUE if "wp" is a terminal window where the job has finished or we
1556 * are in Terminal-Normal mode.
1557 */
1558 int
1559term_show_buffer(buf_T *buf)
1560{
1561 term_T *term = buf->b_term;
1562
1563 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1564}
1565
1566/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001567 * The current buffer is going to be changed. If there is terminal
1568 * highlighting remove it now.
1569 */
1570 void
1571term_change_in_curbuf(void)
1572{
1573 term_T *term = curbuf->b_term;
1574
1575 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1576 {
1577 free_scrollback(term);
1578 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001579
1580 /* The buffer is now like a normal buffer, it cannot be easily
1581 * abandoned when changed. */
1582 set_string_option_direct((char_u *)"buftype", -1,
1583 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001584 }
1585}
1586
1587/*
1588 * Get the screen attribute for a position in the buffer.
1589 */
1590 int
1591term_get_attr(buf_T *buf, linenr_T lnum, int col)
1592{
1593 term_T *term = buf->b_term;
1594 sb_line_T *line;
1595
Bram Moolenaar70229f92017-07-29 16:01:53 +02001596 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001597 return 0;
1598 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1599 if (col >= line->sb_cols)
1600 return 0;
1601 return cell2attr(line->sb_cells + col);
1602}
1603
1604/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001605 * Set job options common for Unix and MS-Windows.
1606 */
1607 static void
1608setup_job_options(jobopt_T *opt, int rows, int cols)
1609{
1610 clear_job_options(opt);
1611 opt->jo_mode = MODE_RAW;
1612 opt->jo_out_mode = MODE_RAW;
1613 opt->jo_err_mode = MODE_RAW;
1614 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001615
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001616 opt->jo_io[PART_OUT] = JIO_BUFFER;
1617 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001618 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1619
1620 opt->jo_modifiable[PART_OUT] = 0;
1621 opt->jo_modifiable[PART_ERR] = 0;
1622 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1623
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001624 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1625 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001626 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001627 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1628
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001629 opt->jo_term_rows = rows;
1630 opt->jo_term_cols = cols;
1631}
1632
1633/*
1634 * Create a new vterm and initialize it.
1635 */
1636 static void
1637create_vterm(term_T *term, int rows, int cols)
1638{
1639 VTerm *vterm;
1640 VTermScreen *screen;
1641
1642 vterm = vterm_new(rows, cols);
1643 term->tl_vterm = vterm;
1644 screen = vterm_obtain_screen(vterm);
1645 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1646 /* TODO: depends on 'encoding'. */
1647 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001648
1649 /* Vterm uses a default black background. Set it to white when
1650 * 'background' is "light". */
1651 if (*p_bg == 'l')
1652 {
1653 VTermColor fg, bg;
1654
1655 fg.red = fg.green = fg.blue = 0;
1656 bg.red = bg.green = bg.blue = 255;
1657 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1658 }
1659
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001660 /* Required to initialize most things. */
1661 vterm_screen_reset(screen, 1 /* hard */);
1662}
1663
Bram Moolenaar21554412017-07-24 21:44:43 +02001664/*
1665 * Return the text to show for the buffer name and status.
1666 */
1667 char_u *
1668term_get_status_text(term_T *term)
1669{
1670 if (term->tl_status_text == NULL)
1671 {
1672 char_u *txt;
1673 size_t len;
1674
Bram Moolenaar423802d2017-07-30 16:52:24 +02001675 if (term->tl_terminal_mode)
1676 {
1677 if (term_job_running(term))
1678 txt = (char_u *)_("Terminal");
1679 else
1680 txt = (char_u *)_("Terminal-finished");
1681 }
1682 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001683 txt = term->tl_title;
1684 else if (term_job_running(term))
1685 txt = (char_u *)_("running");
1686 else
1687 txt = (char_u *)_("finished");
1688 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001689 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001690 if (term->tl_status_text != NULL)
1691 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1692 term->tl_buffer->b_fname, txt);
1693 }
1694 return term->tl_status_text;
1695}
1696
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001697/*
1698 * Mark references in jobs of terminals.
1699 */
1700 int
1701set_ref_in_term(int copyID)
1702{
1703 int abort = FALSE;
1704 term_T *term;
1705 typval_T tv;
1706
1707 for (term = first_term; term != NULL; term = term->tl_next)
1708 if (term->tl_job != NULL)
1709 {
1710 tv.v_type = VAR_JOB;
1711 tv.vval.v_job = term->tl_job;
1712 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1713 }
1714 return abort;
1715}
1716
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001717/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001718 * Get the buffer from the first argument in "argvars".
1719 * Returns NULL when the buffer is not for a terminal window.
1720 */
1721 static buf_T *
1722term_get_buf(typval_T *argvars)
1723{
1724 buf_T *buf;
1725
1726 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1727 ++emsg_off;
1728 buf = get_buf_tv(&argvars[0], FALSE);
1729 --emsg_off;
1730 if (buf == NULL || buf->b_term == NULL)
1731 return NULL;
1732 return buf;
1733}
1734
1735/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001736 * "term_getattr(attr, name)" function
1737 */
1738 void
1739f_term_getattr(typval_T *argvars, typval_T *rettv)
1740{
1741 int attr;
1742 size_t i;
1743 char_u *name;
1744
1745 static struct {
1746 char *name;
1747 int attr;
1748 } attrs[] = {
1749 {"bold", HL_BOLD},
1750 {"italic", HL_ITALIC},
1751 {"underline", HL_UNDERLINE},
1752 {"strike", HL_STANDOUT},
1753 {"reverse", HL_INVERSE},
1754 };
1755
1756 attr = get_tv_number(&argvars[0]);
1757 name = get_tv_string_chk(&argvars[1]);
1758 if (name == NULL)
1759 return;
1760
1761 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1762 if (STRCMP(name, attrs[i].name) == 0)
1763 {
1764 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1765 break;
1766 }
1767}
1768
1769/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001770 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001771 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001772 void
1773f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001774{
Bram Moolenaar97870002017-07-30 18:28:38 +02001775 buf_T *buf = term_get_buf(argvars);
1776 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001777
Bram Moolenaar97870002017-07-30 18:28:38 +02001778 if (rettv_list_alloc(rettv) == FAIL)
1779 return;
1780 if (buf == NULL)
1781 return;
1782
1783 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001784 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1785 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001786 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001787}
1788
1789/*
1790 * "term_getjob(buf)" function
1791 */
1792 void
1793f_term_getjob(typval_T *argvars, typval_T *rettv)
1794{
1795 buf_T *buf = term_get_buf(argvars);
1796
1797 rettv->v_type = VAR_JOB;
1798 rettv->vval.v_job = NULL;
1799 if (buf == NULL)
1800 return;
1801
1802 rettv->vval.v_job = buf->b_term->tl_job;
1803 if (rettv->vval.v_job != NULL)
1804 ++rettv->vval.v_job->jv_refcount;
1805}
1806
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001807 static int
1808get_row_number(typval_T *tv, term_T *term)
1809{
1810 if (tv->v_type == VAR_STRING
1811 && tv->vval.v_string != NULL
1812 && STRCMP(tv->vval.v_string, ".") == 0)
1813 return term->tl_cursor_pos.row;
1814 return (int)get_tv_number(tv) - 1;
1815}
1816
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001817/*
1818 * "term_getline(buf, row)" function
1819 */
1820 void
1821f_term_getline(typval_T *argvars, typval_T *rettv)
1822{
1823 buf_T *buf = term_get_buf(argvars);
1824 term_T *term;
1825 int row;
1826
1827 rettv->v_type = VAR_STRING;
1828 if (buf == NULL)
1829 return;
1830 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001831 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001832
1833 if (term->tl_vterm == NULL)
1834 {
1835 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1836
1837 /* vterm is finished, get the text from the buffer */
1838 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1839 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1840 }
1841 else
1842 {
1843 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1844 VTermRect rect;
1845 int len;
1846 char_u *p;
1847
1848 len = term->tl_cols * MB_MAXBYTES + 1;
1849 p = alloc(len);
1850 if (p == NULL)
1851 return;
1852 rettv->vval.v_string = p;
1853
1854 rect.start_col = 0;
1855 rect.end_col = term->tl_cols;
1856 rect.start_row = row;
1857 rect.end_row = row + 1;
1858 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1859 }
1860}
1861
1862/*
1863 * "term_getsize(buf)" function
1864 */
1865 void
1866f_term_getsize(typval_T *argvars, typval_T *rettv)
1867{
1868 buf_T *buf = term_get_buf(argvars);
1869 list_T *l;
1870
1871 if (rettv_list_alloc(rettv) == FAIL)
1872 return;
1873 if (buf == NULL)
1874 return;
1875
1876 l = rettv->vval.v_list;
1877 list_append_number(l, buf->b_term->tl_rows);
1878 list_append_number(l, buf->b_term->tl_cols);
1879}
1880
1881/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001882 * "term_getstatus(buf)" function
1883 */
1884 void
1885f_term_getstatus(typval_T *argvars, typval_T *rettv)
1886{
1887 buf_T *buf = term_get_buf(argvars);
1888 term_T *term;
1889 char_u val[100];
1890
1891 rettv->v_type = VAR_STRING;
1892 if (buf == NULL)
1893 return;
1894 term = buf->b_term;
1895
1896 if (term_job_running(term))
1897 STRCPY(val, "running");
1898 else
1899 STRCPY(val, "finished");
1900 if (term->tl_terminal_mode)
1901 STRCAT(val, ",terminal");
1902 rettv->vval.v_string = vim_strsave(val);
1903}
1904
1905/*
1906 * "term_gettitle(buf)" function
1907 */
1908 void
1909f_term_gettitle(typval_T *argvars, typval_T *rettv)
1910{
1911 buf_T *buf = term_get_buf(argvars);
1912
1913 rettv->v_type = VAR_STRING;
1914 if (buf == NULL)
1915 return;
1916
1917 if (buf->b_term->tl_title != NULL)
1918 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1919}
1920
1921/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001922 * "term_list()" function
1923 */
1924 void
1925f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1926{
1927 term_T *tp;
1928 list_T *l;
1929
1930 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1931 return;
1932
1933 l = rettv->vval.v_list;
1934 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1935 if (tp != NULL && tp->tl_buffer != NULL)
1936 if (list_append_number(l,
1937 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1938 return;
1939}
1940
1941/*
1942 * "term_scrape(buf, row)" function
1943 */
1944 void
1945f_term_scrape(typval_T *argvars, typval_T *rettv)
1946{
1947 buf_T *buf = term_get_buf(argvars);
1948 VTermScreen *screen = NULL;
1949 VTermPos pos;
1950 list_T *l;
1951 term_T *term;
1952
1953 if (rettv_list_alloc(rettv) == FAIL)
1954 return;
1955 if (buf == NULL)
1956 return;
1957 term = buf->b_term;
1958 if (term->tl_vterm != NULL)
1959 screen = vterm_obtain_screen(term->tl_vterm);
1960
1961 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001962 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001963 for (pos.col = 0; pos.col < term->tl_cols; )
1964 {
1965 dict_T *dcell;
1966 VTermScreenCell cell;
1967 char_u rgb[8];
1968 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1969 int off = 0;
1970 int i;
1971
1972 if (screen == NULL)
1973 {
1974 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1975 sb_line_T *line;
1976
1977 /* vterm has finished, get the cell from scrollback */
1978 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1979 break;
1980 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1981 if (pos.col >= line->sb_cols)
1982 break;
1983 cell = line->sb_cells[pos.col];
1984 }
1985 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1986 break;
1987 dcell = dict_alloc();
1988 list_append_dict(l, dcell);
1989
1990 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1991 {
1992 if (cell.chars[i] == 0)
1993 break;
1994 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1995 }
1996 mbs[off] = NUL;
1997 dict_add_nr_str(dcell, "chars", 0, mbs);
1998
1999 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2000 cell.fg.red, cell.fg.green, cell.fg.blue);
2001 dict_add_nr_str(dcell, "fg", 0, rgb);
2002 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2003 cell.bg.red, cell.bg.green, cell.bg.blue);
2004 dict_add_nr_str(dcell, "bg", 0, rgb);
2005
2006 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2007 dict_add_nr_str(dcell, "width", cell.width, NULL);
2008
2009 ++pos.col;
2010 if (cell.width == 2)
2011 ++pos.col;
2012 }
2013}
2014
2015/*
2016 * "term_sendkeys(buf, keys)" function
2017 */
2018 void
2019f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2020{
2021 buf_T *buf = term_get_buf(argvars);
2022 char_u *msg;
2023 term_T *term;
2024
2025 rettv->v_type = VAR_UNKNOWN;
2026 if (buf == NULL)
2027 return;
2028
2029 msg = get_tv_string_chk(&argvars[1]);
2030 if (msg == NULL)
2031 return;
2032 term = buf->b_term;
2033 if (term->tl_vterm == NULL)
2034 return;
2035
2036 while (*msg != NUL)
2037 {
2038 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2039 msg += MB_PTR2LEN(msg);
2040 }
2041
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002042 if (!term->tl_terminal_mode)
2043 {
2044 /* TODO: only update once in a while. */
2045 update_screen(0);
2046 if (buf == curbuf)
2047 update_cursor(term, TRUE);
2048 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002049}
2050
2051/*
2052 * "term_start(command, options)" function
2053 */
2054 void
2055f_term_start(typval_T *argvars, typval_T *rettv)
2056{
2057 char_u *cmd = get_tv_string_chk(&argvars[0]);
2058 exarg_T ea;
2059
2060 if (cmd == NULL)
2061 return;
2062 ea.arg = cmd;
2063 ex_terminal(&ea);
2064
2065 if (curbuf->b_term != NULL)
2066 rettv->vval.v_number = curbuf->b_fnum;
2067}
2068
2069/*
2070 * "term_wait" function
2071 */
2072 void
2073f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2074{
2075 buf_T *buf = term_get_buf(argvars);
2076
2077 if (buf == NULL)
2078 return;
2079
2080 /* Get the job status, this will detect a job that finished. */
2081 if (buf->b_term->tl_job != NULL)
2082 (void)job_status(buf->b_term->tl_job);
2083
2084 /* Check for any pending channel I/O. */
2085 vpeekc_any();
2086 ui_delay(10L, FALSE);
2087
2088 /* Flushing messages on channels is hopefully sufficient.
2089 * TODO: is there a better way? */
2090 parse_queued_messages();
2091}
2092
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002093# ifdef WIN3264
2094
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002095/**************************************
2096 * 2. MS-Windows implementation.
2097 */
2098
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002099#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2100#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2101
Bram Moolenaar8a773062017-07-24 22:29:21 +02002102void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002103void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002104void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002105BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2106void (*winpty_config_set_initial_size)(void*, int, int);
2107LPCWSTR (*winpty_conin_name)(void*);
2108LPCWSTR (*winpty_conout_name)(void*);
2109LPCWSTR (*winpty_conerr_name)(void*);
2110void (*winpty_free)(void*);
2111void (*winpty_config_free)(void*);
2112void (*winpty_spawn_config_free)(void*);
2113void (*winpty_error_free)(void*);
2114LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002115BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002116
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002117#define WINPTY_DLL "winpty.dll"
2118
2119static HINSTANCE hWinPtyDLL = NULL;
2120
2121 int
2122dyn_winpty_init(void)
2123{
2124 int i;
2125 static struct
2126 {
2127 char *name;
2128 FARPROC *ptr;
2129 } winpty_entry[] =
2130 {
2131 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2132 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2133 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2134 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2135 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2136 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2137 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2138 {"winpty_free", (FARPROC*)&winpty_free},
2139 {"winpty_open", (FARPROC*)&winpty_open},
2140 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2141 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2142 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2143 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002144 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002145 {NULL, NULL}
2146 };
2147
2148 /* No need to initialize twice. */
2149 if (hWinPtyDLL)
2150 return 1;
2151 /* Load winpty.dll */
2152 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2153 if (!hWinPtyDLL)
2154 {
2155 EMSG2(_(e_loadlib), WINPTY_DLL);
2156 return 0;
2157 }
2158 for (i = 0; winpty_entry[i].name != NULL
2159 && winpty_entry[i].ptr != NULL; ++i)
2160 {
2161 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2162 winpty_entry[i].name)) == NULL)
2163 {
2164 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2165 return 0;
2166 }
2167 }
2168
2169 return 1;
2170}
2171
2172/*
2173 * Create a new terminal of "rows" by "cols" cells.
2174 * Store a reference in "term".
2175 * Return OK or FAIL.
2176 */
2177 static int
2178term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2179{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002180 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002181 channel_T *channel = NULL;
2182 job_T *job = NULL;
2183 jobopt_T opt;
2184 DWORD error;
2185 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2186 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002187 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002188
2189 if (!dyn_winpty_init())
2190 return FAIL;
2191
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002192 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002193 if (p == NULL)
2194 return FAIL;
2195
2196 job = job_alloc();
2197 if (job == NULL)
2198 goto failed;
2199
2200 channel = add_channel();
2201 if (channel == NULL)
2202 goto failed;
2203
2204 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2205 if (term->tl_winpty_config == NULL)
2206 goto failed;
2207
2208 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2209 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2210 if (term->tl_winpty == NULL)
2211 goto failed;
2212
2213 spawn_config = winpty_spawn_config_new(
2214 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2215 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2216 NULL,
2217 p,
2218 NULL,
2219 NULL,
2220 &winpty_err);
2221 if (spawn_config == NULL)
2222 goto failed;
2223
2224 channel = add_channel();
2225 if (channel == NULL)
2226 goto failed;
2227
2228 job = job_alloc();
2229 if (job == NULL)
2230 goto failed;
2231
2232 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2233 &child_thread_handle, &error, &winpty_err))
2234 goto failed;
2235
2236 channel_set_pipes(channel,
2237 (sock_T) CreateFileW(
2238 winpty_conin_name(term->tl_winpty),
2239 GENERIC_WRITE, 0, NULL,
2240 OPEN_EXISTING, 0, NULL),
2241 (sock_T) CreateFileW(
2242 winpty_conout_name(term->tl_winpty),
2243 GENERIC_READ, 0, NULL,
2244 OPEN_EXISTING, 0, NULL),
2245 (sock_T) CreateFileW(
2246 winpty_conerr_name(term->tl_winpty),
2247 GENERIC_READ, 0, NULL,
2248 OPEN_EXISTING, 0, NULL));
2249
2250 jo = CreateJobObject(NULL, NULL);
2251 if (jo == NULL)
2252 goto failed;
2253
2254 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002255 {
2256 /* Failed, switch the way to terminate process with TerminateProcess. */
2257 CloseHandle(jo);
2258 jo = NULL;
2259 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002260
2261 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002262 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002263
2264 create_vterm(term, rows, cols);
2265
2266 setup_job_options(&opt, rows, cols);
2267 channel_set_job(channel, job, &opt);
2268
2269 job->jv_channel = channel;
2270 job->jv_proc_info.hProcess = child_process_handle;
2271 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2272 job->jv_job_object = jo;
2273 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002274 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002275 term->tl_job = job;
2276
2277 return OK;
2278
2279failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002280 if (spawn_config != NULL)
2281 winpty_spawn_config_free(spawn_config);
2282 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002283 if (channel != NULL)
2284 channel_clear(channel);
2285 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002286 {
2287 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002288 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002289 }
2290 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002291 if (jo != NULL)
2292 CloseHandle(jo);
2293 if (term->tl_winpty != NULL)
2294 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002295 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002296 if (term->tl_winpty_config != NULL)
2297 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002298 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002299 if (winpty_err != NULL)
2300 {
2301 char_u *msg = utf16_to_enc(
2302 (short_u *)winpty_error_msg(winpty_err), NULL);
2303
2304 EMSG(msg);
2305 winpty_error_free(winpty_err);
2306 }
2307 return FAIL;
2308}
2309
2310/*
2311 * Free the terminal emulator part of "term".
2312 */
2313 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002314term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002315{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002316 if (term->tl_winpty != NULL)
2317 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002318 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002319 if (term->tl_winpty_config != NULL)
2320 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002321 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002322 if (term->tl_vterm != NULL)
2323 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002324 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002325}
2326
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002327/*
2328 * Request size to terminal.
2329 */
2330 static void
2331term_report_winsize(term_T *term, int rows, int cols)
2332{
2333 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2334}
2335
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002336# else
2337
2338/**************************************
2339 * 3. Unix-like implementation.
2340 */
2341
2342/*
2343 * Create a new terminal of "rows" by "cols" cells.
2344 * Start job for "cmd".
2345 * Store the pointers in "term".
2346 * Return OK or FAIL.
2347 */
2348 static int
2349term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2350{
2351 typval_T argvars[2];
2352 jobopt_T opt;
2353
2354 create_vterm(term, rows, cols);
2355
2356 argvars[0].v_type = VAR_STRING;
2357 argvars[0].vval.v_string = cmd;
2358 setup_job_options(&opt, rows, cols);
2359 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002360 if (term->tl_job != NULL)
2361 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002362
Bram Moolenaar61a66052017-07-22 18:39:00 +02002363 return term->tl_job != NULL
2364 && term->tl_job->jv_channel != NULL
2365 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002366}
2367
2368/*
2369 * Free the terminal emulator part of "term".
2370 */
2371 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002372term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002373{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002374 if (term->tl_vterm != NULL)
2375 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002376 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002377}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002378
2379/*
2380 * Request size to terminal.
2381 */
2382 static void
2383term_report_winsize(term_T *term, int rows, int cols)
2384{
2385 /* Use an ioctl() to report the new window size to the job. */
2386 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2387 {
2388 int fd = -1;
2389 int part;
2390
2391 for (part = PART_OUT; part < PART_COUNT; ++part)
2392 {
2393 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2394 if (isatty(fd))
2395 break;
2396 }
2397 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2398 mch_stop_job(term->tl_job, (char_u *)"winch");
2399 }
2400}
2401
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002402# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002403
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002404#endif /* FEAT_TERMINAL */