blob: 0ef7ae2aca517fa693c7fab0267af2d542e71a0b [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 Moolenaar20e6cd02017-08-01 20:25:22 +020042 * - typing CTRL-C is not sent to the terminal. need to setup controlling tty?
43 * #1910
Bram Moolenaard85f2712017-07-28 21:51:57 +020044 * - For the scrollback buffer store lines in the buffer, only attributes in
45 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020046 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020048 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
49 * job finishes).
50 * - add option values to the command:
51 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020052 * - support different cursor shapes, colors and attributes
53 * - make term_getcursor() return type (none/block/bar/underline) and
54 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020055 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020056 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020057 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020058 * - add 't' to mode()
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020059 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar94053a52017-08-01 21:44:33 +020060 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020061 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020062 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020063 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020064 * - implement "term" for job_start(): more job options when starting a
65 * terminal.
Bram Moolenaar423802d2017-07-30 16:52:24 +020066 * - if the job in the terminal does not support the mouse, we can use the
67 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020068 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
69 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020070 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020071 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020072 * - Copy text in the vterm to the Vim buffer once in a while, so that
73 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020074 */
75
76#include "vim.h"
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020080#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020081# define MIN(x,y) (x < y ? x : y)
82# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020083#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020084
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020085#include "libvterm/include/vterm.h"
86
Bram Moolenaard85f2712017-07-28 21:51:57 +020087typedef struct sb_line_S {
88 int sb_cols; /* can differ per line */
89 VTermScreenCell *sb_cells; /* allocated */
90} sb_line_T;
91
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092/* typedef term_T in structs.h */
93struct terminal_S {
94 term_T *tl_next;
95
Bram Moolenaar423802d2017-07-30 16:52:24 +020096 VTerm *tl_vterm;
97 job_T *tl_job;
98 buf_T *tl_buffer;
99
100 int tl_terminal_mode;
101 int tl_channel_closed;
102
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200103#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200104 void *tl_winpty_config;
105 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200106#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200107
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200108 /* last known vterm size */
109 int tl_rows;
110 int tl_cols;
111 /* vterm size does not follow window size */
112 int tl_rows_fixed;
113 int tl_cols_fixed;
114
Bram Moolenaar21554412017-07-24 21:44:43 +0200115 char_u *tl_title; /* NULL or allocated */
116 char_u *tl_status_text; /* NULL or allocated */
117
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200118 /* Range of screen rows to update. Zero based. */
119 int tl_dirty_row_start; /* -1 if nothing dirty */
120 int tl_dirty_row_end; /* row below last one to update */
121
Bram Moolenaard85f2712017-07-28 21:51:57 +0200122 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200123 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200124
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200125 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200126 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200127};
128
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200129/*
130 * List of all active terminals.
131 */
132static term_T *first_term = NULL;
133
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200134
135#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
136#define KEY_BUF_LEN 200
137
138/*
139 * Functions with separate implementation for MS-Windows and Unix-like systems.
140 */
141static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200142static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200143static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200144
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200145/**************************************
146 * 1. Generic code for all systems.
147 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200148
149/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200150 * Determine the terminal size from 'termsize' and the current window.
151 * Assumes term->tl_rows and term->tl_cols are zero.
152 */
153 static void
154set_term_and_win_size(term_T *term)
155{
156 if (*curwin->w_p_tms != NUL)
157 {
158 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
159
160 term->tl_rows = atoi((char *)curwin->w_p_tms);
161 term->tl_cols = atoi((char *)p);
162 }
163 if (term->tl_rows == 0)
164 term->tl_rows = curwin->w_height;
165 else
166 {
167 win_setheight_win(term->tl_rows, curwin);
168 term->tl_rows_fixed = TRUE;
169 }
170 if (term->tl_cols == 0)
171 term->tl_cols = curwin->w_width;
172 else
173 {
174 win_setwidth_win(term->tl_cols, curwin);
175 term->tl_cols_fixed = TRUE;
176 }
177}
178
179/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200180 * ":terminal": open a terminal window and execute a job in it.
181 */
182 void
183ex_terminal(exarg_T *eap)
184{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200185 exarg_T split_ea;
186 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200187 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200188 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200189
190 if (check_restricted() || check_secure())
191 return;
192
193 term = (term_T *)alloc_clear(sizeof(term_T));
194 if (term == NULL)
195 return;
196 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200197 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200198 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200199
200 /* Open a new window or tab. */
201 vim_memset(&split_ea, 0, sizeof(split_ea));
202 split_ea.cmdidx = CMD_new;
203 split_ea.cmd = (char_u *)"new";
204 split_ea.arg = (char_u *)"";
205 ex_splitview(&split_ea);
206 if (curwin == old_curwin)
207 {
208 /* split failed */
209 vim_free(term);
210 return;
211 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200212 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200213 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200214
215 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200216 term->tl_next = first_term;
217 first_term = term;
218
Bram Moolenaar293424c2017-07-26 23:11:01 +0200219 if (cmd == NULL || *cmd == NUL)
220 cmd = p_sh;
221
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200222 {
223 int i;
224 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200225 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200226
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200227 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200228 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200229 /* Prepend a ! to the command name to avoid the buffer name equals
230 * the executable, otherwise ":w!" would overwrite it. */
231 if (i == 0)
232 vim_snprintf((char *)p, len, "!%s", cmd);
233 else
234 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200235 if (buflist_findname(p) == NULL)
236 {
237 curbuf->b_ffname = p;
238 break;
239 }
240 }
241 }
242 curbuf->b_fname = curbuf->b_ffname;
243
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200244 /* Mark the buffer as not modifiable. It can only be made modifiable after
245 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200246 curbuf->b_p_ma = FALSE;
247 set_string_option_direct((char_u *)"buftype", -1,
248 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200250 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200252 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200253 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200254 {
255 /* store the size we ended up with */
256 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200257 }
258 else
259 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200260 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200261
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200262 /* Wiping out the buffer will also close the window and call
263 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200264 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200265 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200266}
267
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200268/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200269 * Free the scrollback buffer for "term".
270 */
271 static void
272free_scrollback(term_T *term)
273{
274 int i;
275
276 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
277 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
278 ga_clear(&term->tl_scrollback);
279}
280
281/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200282 * Free a terminal and everything it refers to.
283 * Kills the job if there is one.
284 * Called when wiping out a buffer.
285 */
286 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200287free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200288{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200289 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200290 term_T *tp;
291
292 if (term == NULL)
293 return;
294 if (first_term == term)
295 first_term = term->tl_next;
296 else
297 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
298 if (tp->tl_next == term)
299 {
300 tp->tl_next = term->tl_next;
301 break;
302 }
303
304 if (term->tl_job != NULL)
305 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200306 if (term->tl_job->jv_status != JOB_ENDED
307 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200308 job_stop(term->tl_job, NULL, "kill");
309 job_unref(term->tl_job);
310 }
311
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200312 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200313
314 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200315 vim_free(term->tl_title);
316 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200317 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200318 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200319}
320
321/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200322 * Write job output "msg[len]" to the vterm.
323 */
324 static void
325term_write_job_output(term_T *term, char_u *msg, size_t len)
326{
327 VTerm *vterm = term->tl_vterm;
328 char_u *p;
329 size_t done;
330 size_t len_now;
331
332 for (done = 0; done < len; done += len_now)
333 {
334 for (p = msg + done; p < msg + len; )
335 {
336 if (*p == NL)
337 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200338 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200339 }
340 len_now = p - msg - done;
341 vterm_input_write(vterm, (char *)msg + done, len_now);
342 if (p < msg + len && *p == NL)
343 {
344 /* Convert NL to CR-NL, that appears to work best. */
345 vterm_input_write(vterm, "\r\n", 2);
346 ++len_now;
347 }
348 }
349
350 /* this invokes the damage callbacks */
351 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
352}
353
Bram Moolenaar1c844932017-07-24 23:36:41 +0200354 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200355update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200356{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200357 if (term->tl_terminal_mode)
358 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200359 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200360 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200361 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200362 if (term->tl_cursor_visible)
363 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200364 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200365#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200366 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200367 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200368#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200369 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200370}
371
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200372/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200373 * Invoked when "msg" output from a job was received. Write it to the terminal
374 * of "buffer".
375 */
376 void
377write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
378{
379 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200380 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200381
Bram Moolenaard85f2712017-07-28 21:51:57 +0200382 if (term->tl_vterm == NULL)
383 {
384 ch_logn(channel, "NOT writing %d bytes to terminal", (int)len);
385 return;
386 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200387 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200388 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200389
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200390 if (!term->tl_terminal_mode)
391 {
392 /* TODO: only update once in a while. */
393 update_screen(0);
394 update_cursor(term, TRUE);
395 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200396}
397
398/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200399 * Send a mouse position and click to the vterm
400 */
401 static int
402term_send_mouse(VTerm *vterm, int button, int pressed)
403{
404 VTermModifier mod = VTERM_MOD_NONE;
405
406 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
407 mouse_col - W_WINCOL(curwin), mod);
408 vterm_mouse_button(vterm, button, pressed, mod);
409 return TRUE;
410}
411
412/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200413 * Convert typed key "c" into bytes to send to the job.
414 * Return the number of bytes in "buf".
415 */
416 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200417term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200418{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200419 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200420 VTermKey key = VTERM_KEY_NONE;
421 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200422 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200423
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200424 switch (c)
425 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200426 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200427 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200428 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
429 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200430 case K_DEL: key = VTERM_KEY_DEL; break;
431 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200432 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
433 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200434 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200435 case K_S_END: mod = VTERM_MOD_SHIFT;
436 key = VTERM_KEY_END; break;
437 case K_C_END: mod = VTERM_MOD_CTRL;
438 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200439 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
440 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
441 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
442 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
443 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
444 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
445 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
446 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
447 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
448 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
449 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
450 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
451 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200452 case K_S_HOME: mod = VTERM_MOD_SHIFT;
453 key = VTERM_KEY_HOME; break;
454 case K_C_HOME: mod = VTERM_MOD_CTRL;
455 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200456 case K_INS: key = VTERM_KEY_INS; break;
457 case K_K0: key = VTERM_KEY_KP_0; break;
458 case K_K1: key = VTERM_KEY_KP_1; break;
459 case K_K2: key = VTERM_KEY_KP_2; break;
460 case K_K3: key = VTERM_KEY_KP_3; break;
461 case K_K4: key = VTERM_KEY_KP_4; break;
462 case K_K5: key = VTERM_KEY_KP_5; break;
463 case K_K6: key = VTERM_KEY_KP_6; break;
464 case K_K7: key = VTERM_KEY_KP_7; break;
465 case K_K8: key = VTERM_KEY_KP_8; break;
466 case K_K9: key = VTERM_KEY_KP_9; break;
467 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
468 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
469 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
470 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
471 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
472 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
473 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
474 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
475 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
476 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
477 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
478 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
479 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200480 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
481 key = VTERM_KEY_LEFT; break;
482 case K_C_LEFT: mod = VTERM_MOD_CTRL;
483 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200484 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
485 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
486 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200487 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
488 key = VTERM_KEY_RIGHT; break;
489 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
490 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200491 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200492 case K_S_UP: mod = VTERM_MOD_SHIFT;
493 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200494 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200495
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200496 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
497 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
498 case K_MOUSELEFT: /* TODO */ return 0;
499 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200500
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200501 case K_LEFTMOUSE:
502 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
503 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
504 case K_LEFTRELEASE:
505 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
506 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
507 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
508 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
509 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
510 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
511 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
512 case K_X1MOUSE: /* TODO */ return 0;
513 case K_X1DRAG: /* TODO */ return 0;
514 case K_X1RELEASE: /* TODO */ return 0;
515 case K_X2MOUSE: /* TODO */ return 0;
516 case K_X2DRAG: /* TODO */ return 0;
517 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200518
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200519 case K_IGNORE: return 0;
520 case K_NOP: return 0;
521 case K_UNDO: return 0;
522 case K_HELP: return 0;
523 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
524 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
525 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
526 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
527 case K_SELECT: return 0;
528#ifdef FEAT_GUI
529 case K_VER_SCROLLBAR: return 0;
530 case K_HOR_SCROLLBAR: return 0;
531#endif
532#ifdef FEAT_GUI_TABLINE
533 case K_TABLINE: return 0;
534 case K_TABMENU: return 0;
535#endif
536#ifdef FEAT_NETBEANS_INTG
537 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
538#endif
539#ifdef FEAT_DND
540 case K_DROP: return 0;
541#endif
542#ifdef FEAT_AUTOCMD
543 case K_CURSORHOLD: return 0;
544#endif
545 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
546 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200547 }
548
549 /*
550 * Convert special keys to vterm keys:
551 * - Write keys to vterm: vterm_keyboard_key()
552 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200553 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200554 */
555 if (key != VTERM_KEY_NONE)
556 /* Special key, let vterm convert it. */
557 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200558 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200559 /* Normal character, let vterm convert it. */
560 vterm_keyboard_unichar(vterm, c, mod);
561
562 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200563 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200564}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200565
Bram Moolenaar938783d2017-07-16 20:13:26 +0200566/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200567 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200568 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200569 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200570term_job_running(term_T *term)
571{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200572 /* Also consider the job finished when the channel is closed, to avoid a
573 * race condition when updating the title. */
574 return term->tl_job != NULL
575 && term->tl_job->jv_status == JOB_STARTED
576 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200577}
578
579/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200580 * Add the last line of the scrollback buffer to the buffer in the window.
581 */
582 static void
583add_scrollback_line_to_buffer(term_T *term)
584{
585 linenr_T lnum = term->tl_scrollback.ga_len - 1;
586 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
587 garray_T ga;
588 int c;
589 int col;
590 int i;
591
592 ga_init2(&ga, 1, 100);
593 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
594 {
595 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
596 goto failed;
597 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
598 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
599 (char_u *)ga.ga_data + ga.ga_len);
600 }
601 if (ga_grow(&ga, 1) == FAIL)
602 goto failed;
603 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
604 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
605
606 if (lnum == 0)
607 {
608 /* Delete the empty line that was in the empty buffer. */
609 curbuf = term->tl_buffer;
610 ml_delete(2, FALSE);
611 curbuf = curwin->w_buffer;
612 }
613
614failed:
615 ga_clear(&ga);
616}
617
618/*
619 * Add the current lines of the terminal to scrollback and to the buffer.
620 * Called after the job has ended and when switching to Terminal mode.
621 */
622 static void
623move_terminal_to_buffer(term_T *term)
624{
625 win_T *wp;
626 int len;
627 int lines_skipped = 0;
628 VTermPos pos;
629 VTermScreenCell cell;
630 VTermScreenCell *p;
631 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
632
633 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
634 {
635 len = 0;
636 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
637 if (vterm_screen_get_cell(screen, pos, &cell) != 0
638 && cell.chars[0] != NUL)
639 len = pos.col + 1;
640
641 if (len == 0)
642 ++lines_skipped;
643 else
644 {
645 while (lines_skipped > 0)
646 {
647 /* Line was skipped, add an empty line. */
648 --lines_skipped;
649 if (ga_grow(&term->tl_scrollback, 1) == OK)
650 {
651 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
652 + term->tl_scrollback.ga_len;
653
654 line->sb_cols = 0;
655 line->sb_cells = NULL;
656 ++term->tl_scrollback.ga_len;
657
658 add_scrollback_line_to_buffer(term);
659 }
660 }
661
662 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
663 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
664 {
665 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
666 + term->tl_scrollback.ga_len;
667
668 for (pos.col = 0; pos.col < len; ++pos.col)
669 {
670 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
671 vim_memset(p + pos.col, 0, sizeof(cell));
672 else
673 p[pos.col] = cell;
674 }
675 line->sb_cols = len;
676 line->sb_cells = p;
677 ++term->tl_scrollback.ga_len;
678
679 add_scrollback_line_to_buffer(term);
680 }
681 else
682 vim_free(p);
683 }
684 }
685
686 FOR_ALL_WINDOWS(wp)
687 {
688 if (wp->w_buffer == term->tl_buffer)
689 {
690 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
691 wp->w_cursor.col = 0;
692 wp->w_valid = 0;
693 redraw_win_later(wp, NOT_VALID);
694 }
695 }
696}
697
698 static void
699set_terminal_mode(term_T *term, int on)
700{
701 term->tl_terminal_mode = on;
702 vim_free(term->tl_status_text);
703 term->tl_status_text = NULL;
704 if (term->tl_buffer == curbuf)
705 maketitle();
706}
707
708/*
709 * Called after the job if finished and Terminal mode is not active:
710 * Move the vterm contents into the scrollback buffer and free the vterm.
711 */
712 static void
713cleanup_vterm(term_T *term)
714{
715 move_terminal_to_buffer(term);
716 term_free_vterm(term);
717 set_terminal_mode(term, FALSE);
718}
719
720/*
721 * Switch from sending keys to the job to Terminal-Normal mode.
722 * Suspends updating the terminal window.
723 */
724 static void
725term_enter_terminal_mode()
726{
727 term_T *term = curbuf->b_term;
728
729 /* Append the current terminal contents to the buffer. */
730 move_terminal_to_buffer(term);
731
732 set_terminal_mode(term, TRUE);
733}
734
735/*
736 * Returns TRUE if the current window contains a terminal and we are in
737 * Terminal-Normal mode.
738 */
739 int
740term_in_terminal_mode()
741{
742 term_T *term = curbuf->b_term;
743
744 return term != NULL && term->tl_terminal_mode;
745}
746
747/*
748 * Switch from Terminal-Normal mode to sending keys to the job.
749 * Restores updating the terminal window.
750 */
751 void
752term_leave_terminal_mode()
753{
754 term_T *term = curbuf->b_term;
755 sb_line_T *line;
756 garray_T *gap;
757
758 /* Remove the terminal contents from the scrollback and the buffer. */
759 gap = &term->tl_scrollback;
760 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
761 {
762 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
763 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
764 vim_free(line->sb_cells);
765 --gap->ga_len;
766 if (gap->ga_len == 0)
767 break;
768 }
769 check_cursor();
770
771 set_terminal_mode(term, FALSE);
772
773 if (term->tl_channel_closed)
774 cleanup_vterm(term);
775 redraw_buf_and_status_later(curbuf, NOT_VALID);
776}
777
778/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200779 * Get a key from the user without mapping.
780 * TODO: use terminal mode mappings.
781 */
782 static int
783term_vgetc()
784{
785 int c;
786
787 ++no_mapping;
788 ++allow_keys;
789 got_int = FALSE;
790 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200791 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200792 --no_mapping;
793 --allow_keys;
794 return c;
795}
796
797/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200798 * Send keys to terminal.
799 */
800 static int
801send_keys_to_term(term_T *term, int c, int typed)
802{
803 char msg[KEY_BUF_LEN];
804 size_t len;
805 static int mouse_was_outside = FALSE;
806 int dragging_outside = FALSE;
807
808 /* Catch keys that need to be handled as in Normal mode. */
809 switch (c)
810 {
811 case NUL:
812 case K_ZERO:
813 if (typed)
814 stuffcharReadbuff(c);
815 return FAIL;
816
817 case K_IGNORE:
818 return FAIL;
819
820 case K_LEFTDRAG:
821 case K_MIDDLEDRAG:
822 case K_RIGHTDRAG:
823 case K_X1DRAG:
824 case K_X2DRAG:
825 dragging_outside = mouse_was_outside;
826 /* FALLTHROUGH */
827 case K_LEFTMOUSE:
828 case K_LEFTMOUSE_NM:
829 case K_LEFTRELEASE:
830 case K_LEFTRELEASE_NM:
831 case K_MIDDLEMOUSE:
832 case K_MIDDLERELEASE:
833 case K_RIGHTMOUSE:
834 case K_RIGHTRELEASE:
835 case K_X1MOUSE:
836 case K_X1RELEASE:
837 case K_X2MOUSE:
838 case K_X2RELEASE:
839 if (mouse_row < W_WINROW(curwin)
840 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
841 || mouse_col < W_WINCOL(curwin)
842 || mouse_col >= W_ENDCOL(curwin)
843 || dragging_outside)
844 {
845 /* click outside the current window */
846 if (typed)
847 {
848 stuffcharReadbuff(c);
849 mouse_was_outside = TRUE;
850 }
851 return FAIL;
852 }
853 }
854 if (typed)
855 mouse_was_outside = FALSE;
856
857 /* Convert the typed key to a sequence of bytes for the job. */
858 len = term_convert_key(term, c, msg);
859 if (len > 0)
860 /* TODO: if FAIL is returned, stop? */
861 channel_send(term->tl_job->jv_channel, PART_IN,
862 (char_u *)msg, (int)len, NULL);
863
864 return OK;
865}
866
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200867 static void
868position_cursor(win_T *wp, VTermPos *pos)
869{
870 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
871 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
872 wp->w_valid |= (VALID_WCOL|VALID_WROW);
873}
874
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200875/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200876 * Handle CTRL-W "": send register contents to the job.
877 */
878 static void
879term_paste_register(int prev_c UNUSED)
880{
881 int c;
882 list_T *l;
883 listitem_T *item;
884 long reglen = 0;
885 int type;
886
887#ifdef FEAT_CMDL_INFO
888 if (add_to_showcmd(prev_c))
889 if (add_to_showcmd('"'))
890 out_flush();
891#endif
892 c = term_vgetc();
893#ifdef FEAT_CMDL_INFO
894 clear_showcmd();
895#endif
896
897 /* CTRL-W "= prompt for expression to evaluate. */
898 if (c == '=' && get_expr_register() != '=')
899 return;
900
901 l = (list_T *)get_reg_contents(c, GREG_LIST);
902 if (l != NULL)
903 {
904 type = get_reg_type(c, &reglen);
905 for (item = l->lv_first; item != NULL; item = item->li_next)
906 {
907 char_u *s = get_tv_string(&item->li_tv);
908
909 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
910 s, STRLEN(s), NULL);
911 if (item->li_next != NULL || type == MLINE)
912 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
913 (char_u *)"\r", 1, NULL);
914 }
915 list_free(l);
916 }
917}
918
919/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200920 * Returns TRUE if the current window contains a terminal and we are sending
921 * keys to the job.
922 */
923 int
924term_use_loop()
925{
926 term_T *term = curbuf->b_term;
927
928 return term != NULL
929 && !term->tl_terminal_mode
930 && term->tl_vterm != NULL
931 && term_job_running(term);
932}
933
934/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200935 * Wait for input and send it to the job.
936 * Return when the start of a CTRL-W command is typed or anything else that
937 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200938 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
939 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200940 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200941 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200942terminal_loop(void)
943{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200944 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200945 int termkey = 0;
946
947 if (*curwin->w_p_tk != NUL)
948 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200949 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200950
951 for (;;)
952 {
953 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200954 /* Repeat redrawing in case a message is received while redrawing. */
955 while (curwin->w_redr_type != 0)
956 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200957 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200958
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200959 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200960 if (curbuf->b_term->tl_vterm == NULL
961 || !term_job_running(curbuf->b_term))
962 /* job finished while waiting for a character */
963 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200964
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200965 if (c == (termkey == 0 ? Ctrl_W : termkey))
966 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200967 int prev_c = c;
968
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200969#ifdef FEAT_CMDL_INFO
970 if (add_to_showcmd(c))
971 out_flush();
972#endif
973 c = term_vgetc();
974#ifdef FEAT_CMDL_INFO
975 clear_showcmd();
976#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200977 if (curbuf->b_term->tl_vterm == NULL
978 || !term_job_running(curbuf->b_term))
979 /* job finished while waiting for a character */
980 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200981
982 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200983 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200984 /* "CTRL-W .": send CTRL-W to the job */
985 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200986 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200987 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200988 {
989 term_enter_terminal_mode();
990 return FAIL;
991 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200992 else if (c == '"')
993 {
994 term_paste_register(prev_c);
995 continue;
996 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200997 else if (termkey == 0 || c != termkey)
998 {
999 stuffcharReadbuff(Ctrl_W);
1000 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001001 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001002 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001003 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001004 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1005 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001006 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001007 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001008}
1009
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001010/*
1011 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001012 * This updates the title and status, but does not close the vter, because
1013 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001014 */
1015 void
1016term_job_ended(job_T *job)
1017{
1018 term_T *term;
1019 int did_one = FALSE;
1020
1021 for (term = first_term; term != NULL; term = term->tl_next)
1022 if (term->tl_job == job)
1023 {
1024 vim_free(term->tl_title);
1025 term->tl_title = NULL;
1026 vim_free(term->tl_status_text);
1027 term->tl_status_text = NULL;
1028 redraw_buf_and_status_later(term->tl_buffer, VALID);
1029 did_one = TRUE;
1030 }
1031 if (did_one)
1032 redraw_statuslines();
1033 if (curbuf->b_term != NULL)
1034 {
1035 if (curbuf->b_term->tl_job == job)
1036 maketitle();
1037 update_cursor(curbuf->b_term, TRUE);
1038 }
1039}
1040
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001041 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001042may_toggle_cursor(term_T *term)
1043{
1044 if (curbuf == term->tl_buffer)
1045 {
1046 if (term->tl_cursor_visible)
1047 cursor_on();
1048 else
1049 cursor_off();
1050 }
1051}
1052
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001053 static int
1054handle_damage(VTermRect rect, void *user)
1055{
1056 term_T *term = (term_T *)user;
1057
1058 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1059 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1060 redraw_buf_later(term->tl_buffer, NOT_VALID);
1061 return 1;
1062}
1063
1064 static int
1065handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1066{
1067 term_T *term = (term_T *)user;
1068
1069 /* TODO */
1070 redraw_buf_later(term->tl_buffer, NOT_VALID);
1071 return 1;
1072}
1073
1074 static int
1075handle_movecursor(
1076 VTermPos pos,
1077 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001078 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001079 void *user)
1080{
1081 term_T *term = (term_T *)user;
1082 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001083
1084 term->tl_cursor_pos = pos;
1085 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001086
1087 FOR_ALL_WINDOWS(wp)
1088 {
1089 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001090 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001091 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001092 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001093 {
1094 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001095 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001096 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001097
1098 return 1;
1099}
1100
Bram Moolenaar21554412017-07-24 21:44:43 +02001101 static int
1102handle_settermprop(
1103 VTermProp prop,
1104 VTermValue *value,
1105 void *user)
1106{
1107 term_T *term = (term_T *)user;
1108
1109 switch (prop)
1110 {
1111 case VTERM_PROP_TITLE:
1112 vim_free(term->tl_title);
1113 term->tl_title = vim_strsave((char_u *)value->string);
1114 vim_free(term->tl_status_text);
1115 term->tl_status_text = NULL;
1116 if (term == curbuf->b_term)
1117 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001118 break;
1119
1120 case VTERM_PROP_CURSORVISIBLE:
1121 term->tl_cursor_visible = value->boolean;
1122 may_toggle_cursor(term);
1123 out_flush();
1124 break;
1125
Bram Moolenaar21554412017-07-24 21:44:43 +02001126 default:
1127 break;
1128 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001129 /* Always return 1, otherwise vterm doesn't store the value internally. */
1130 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001131}
1132
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001133/*
1134 * The job running in the terminal resized the terminal.
1135 */
1136 static int
1137handle_resize(int rows, int cols, void *user)
1138{
1139 term_T *term = (term_T *)user;
1140 win_T *wp;
1141
1142 term->tl_rows = rows;
1143 term->tl_cols = cols;
1144 FOR_ALL_WINDOWS(wp)
1145 {
1146 if (wp->w_buffer == term->tl_buffer)
1147 {
1148 win_setheight_win(rows, wp);
1149 win_setwidth_win(cols, wp);
1150 }
1151 }
1152
1153 redraw_buf_later(term->tl_buffer, NOT_VALID);
1154 return 1;
1155}
1156
Bram Moolenaard85f2712017-07-28 21:51:57 +02001157/*
1158 * Handle a line that is pushed off the top of the screen.
1159 */
1160 static int
1161handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1162{
1163 term_T *term = (term_T *)user;
1164
1165 /* TODO: Limit the number of lines that are stored. */
1166 /* TODO: put the text in the buffer. */
1167 if (ga_grow(&term->tl_scrollback, 1) == OK)
1168 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001169 VTermScreenCell *p = NULL;
1170 int len = 0;
1171 int i;
1172 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001173
1174 /* do not store empty cells at the end */
1175 for (i = 0; i < cols; ++i)
1176 if (cells[i].chars[0] != 0)
1177 len = i + 1;
1178
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001179 if (len > 0)
1180 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001181 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001182 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001183
1184 line = (sb_line_T *)term->tl_scrollback.ga_data
1185 + term->tl_scrollback.ga_len;
1186 line->sb_cols = len;
1187 line->sb_cells = p;
1188 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001189 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001190
1191 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001192 }
1193 return 0; /* ignored */
1194}
1195
Bram Moolenaar21554412017-07-24 21:44:43 +02001196static VTermScreenCallbacks screen_callbacks = {
1197 handle_damage, /* damage */
1198 handle_moverect, /* moverect */
1199 handle_movecursor, /* movecursor */
1200 handle_settermprop, /* settermprop */
1201 NULL, /* bell */
1202 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001203 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001204 NULL /* sb_popline */
1205};
1206
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001207/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001208 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001209 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001210 */
1211 void
1212term_channel_closed(channel_T *ch)
1213{
1214 term_T *term;
1215 int did_one = FALSE;
1216
1217 for (term = first_term; term != NULL; term = term->tl_next)
1218 if (term->tl_job == ch->ch_job)
1219 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001220 term->tl_channel_closed = TRUE;
1221
Bram Moolenaard85f2712017-07-28 21:51:57 +02001222 vim_free(term->tl_title);
1223 term->tl_title = NULL;
1224 vim_free(term->tl_status_text);
1225 term->tl_status_text = NULL;
1226
Bram Moolenaar423802d2017-07-30 16:52:24 +02001227 /* Unless in Terminal-Normal mode: clear the vterm. */
1228 if (!term->tl_terminal_mode)
1229 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001230
1231 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1232 did_one = TRUE;
1233 }
1234 if (did_one)
1235 {
1236 redraw_statuslines();
1237
1238 /* Need to break out of vgetc(). */
1239 ins_char_typebuf(K_IGNORE);
1240
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001241 term = curbuf->b_term;
1242 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001243 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001244 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001245 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001246 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001247 }
1248 }
1249}
1250
1251/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001252 * Reverse engineer the RGB value into a cterm color index.
1253 * First color is 1. Return 0 if no match found.
1254 */
1255 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001256color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001257{
1258 int red = color->red;
1259 int blue = color->blue;
1260 int green = color->green;
1261
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001262 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001263 if (red == 0)
1264 {
1265 if (green == 0)
1266 {
1267 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001268 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001269 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001270 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001271 }
1272 else if (green == 224)
1273 {
1274 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001275 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001276 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001277 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001278 }
1279 }
1280 else if (red == 224)
1281 {
1282 if (green == 0)
1283 {
1284 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001285 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001286 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001287 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001288 }
1289 else if (green == 224)
1290 {
1291 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001292 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001293 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001294 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001295 }
1296 }
1297 else if (red == 128)
1298 {
1299 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001300 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001301 }
1302 else if (red == 255)
1303 {
1304 if (green == 64)
1305 {
1306 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001307 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001308 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001309 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001310 }
1311 else if (green == 255)
1312 {
1313 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001314 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001315 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001316 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001317 }
1318 }
1319 else if (red == 64)
1320 {
1321 if (green == 64)
1322 {
1323 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001324 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001325 }
1326 else if (green == 255)
1327 {
1328 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001329 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001330 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001331 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001332 }
1333 }
1334 if (t_colors >= 256)
1335 {
1336 if (red == blue && red == green)
1337 {
1338 /* 24-color greyscale */
1339 static int cutoff[23] = {
1340 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1341 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1342 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1343 int i;
1344
1345 for (i = 0; i < 23; ++i)
1346 if (red < cutoff[i])
1347 return i + 233;
1348 return 256;
1349 }
1350
1351 /* 216-color cube */
1352 return 17 + ((red + 25) / 0x33) * 36
1353 + ((green + 25) / 0x33) * 6
1354 + (blue + 25) / 0x33;
1355 }
1356 return 0;
1357}
1358
1359/*
1360 * Convert the attributes of a vterm cell into an attribute index.
1361 */
1362 static int
1363cell2attr(VTermScreenCell *cell)
1364{
1365 int attr = 0;
1366
1367 if (cell->attrs.bold)
1368 attr |= HL_BOLD;
1369 if (cell->attrs.underline)
1370 attr |= HL_UNDERLINE;
1371 if (cell->attrs.italic)
1372 attr |= HL_ITALIC;
1373 if (cell->attrs.strike)
1374 attr |= HL_STANDOUT;
1375 if (cell->attrs.reverse)
1376 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001377
1378#ifdef FEAT_GUI
1379 if (gui.in_use)
1380 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001381 guicolor_T fg, bg;
1382
1383 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1384 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1385 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001386 }
1387 else
1388#endif
1389#ifdef FEAT_TERMGUICOLORS
1390 if (p_tgc)
1391 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001392 guicolor_T fg, bg;
1393
1394 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1395 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1396
1397 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001398 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001399 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001400#endif
1401 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001402 int bold = MAYBE;
1403 int fg = color2index(&cell->fg, TRUE, &bold);
1404 int bg = color2index(&cell->bg, FALSE, &bold);
1405
1406 /* with 8 colors set the bold attribute to get a bright foreground */
1407 if (bold == TRUE)
1408 attr |= HL_BOLD;
1409 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001410 }
1411 return 0;
1412}
1413
1414/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001415 * Called to update the window that contains a terminal.
1416 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001417 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001418 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001419term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001420{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001421 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001422 VTerm *vterm;
1423 VTermScreen *screen;
1424 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001425 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001426
Bram Moolenaar423802d2017-07-30 16:52:24 +02001427 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001428 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001429
Bram Moolenaard85f2712017-07-28 21:51:57 +02001430 vterm = term->tl_vterm;
1431 screen = vterm_obtain_screen(vterm);
1432 state = vterm_obtain_state(vterm);
1433
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001434 /*
1435 * If the window was resized a redraw will be triggered and we get here.
1436 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1437 */
1438 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1439 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001440 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001441 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1442 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1443 win_T *twp;
1444
1445 FOR_ALL_WINDOWS(twp)
1446 {
1447 /* When more than one window shows the same terminal, use the
1448 * smallest size. */
1449 if (twp->w_buffer == term->tl_buffer)
1450 {
1451 if (!term->tl_rows_fixed && rows > twp->w_height)
1452 rows = twp->w_height;
1453 if (!term->tl_cols_fixed && cols > twp->w_width)
1454 cols = twp->w_width;
1455 }
1456 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001457
1458 vterm_set_size(vterm, rows, cols);
1459 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1460 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001461 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001462 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001463
1464 /* The cursor may have been moved when resizing. */
1465 vterm_state_get_cursorpos(state, &pos);
1466 position_cursor(wp, &pos);
1467
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001468 /* TODO: Only redraw what changed. */
1469 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001470 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001471 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001472 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001473
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001474 if (pos.row < term->tl_rows)
1475 {
1476 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001477 {
1478 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001479 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001480
Bram Moolenaareeac6772017-07-23 15:48:37 +02001481 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1482 vim_memset(&cell, 0, sizeof(cell));
1483
1484 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001485 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001486 if (c == NUL)
1487 {
1488 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001489#if defined(FEAT_MBYTE)
1490 if (enc_utf8)
1491 ScreenLinesUC[off] = NUL;
1492#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001493 }
1494 else
1495 {
1496#if defined(FEAT_MBYTE)
1497 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001498 {
1499 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001500 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001501 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001502 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001503 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001504 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001505 if (enc_utf8)
1506 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001507 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001508#else
1509 ScreenLines[off] = c;
1510#endif
1511 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001512 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001513
1514 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001515 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001516 if (cell.width == 2)
1517 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001518 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001519#if defined(FEAT_MBYTE)
1520 if (enc_utf8)
1521 ScreenLinesUC[off] = NUL;
1522#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001523 ++pos.col;
1524 ++off;
1525 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001526 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001527 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001528 else
1529 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001530
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001531 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1532 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001533 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001534
1535 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001536}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001537
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001538/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001539 * Return TRUE if "wp" is a terminal window where the job has finished.
1540 */
1541 int
1542term_is_finished(buf_T *buf)
1543{
1544 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1545}
1546
1547/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001548 * Return TRUE if "wp" is a terminal window where the job has finished or we
1549 * are in Terminal-Normal mode.
1550 */
1551 int
1552term_show_buffer(buf_T *buf)
1553{
1554 term_T *term = buf->b_term;
1555
1556 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1557}
1558
1559/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001560 * The current buffer is going to be changed. If there is terminal
1561 * highlighting remove it now.
1562 */
1563 void
1564term_change_in_curbuf(void)
1565{
1566 term_T *term = curbuf->b_term;
1567
1568 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1569 {
1570 free_scrollback(term);
1571 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001572
1573 /* The buffer is now like a normal buffer, it cannot be easily
1574 * abandoned when changed. */
1575 set_string_option_direct((char_u *)"buftype", -1,
1576 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001577 }
1578}
1579
1580/*
1581 * Get the screen attribute for a position in the buffer.
1582 */
1583 int
1584term_get_attr(buf_T *buf, linenr_T lnum, int col)
1585{
1586 term_T *term = buf->b_term;
1587 sb_line_T *line;
1588
Bram Moolenaar70229f92017-07-29 16:01:53 +02001589 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001590 return 0;
1591 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1592 if (col >= line->sb_cols)
1593 return 0;
1594 return cell2attr(line->sb_cells + col);
1595}
1596
1597/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001598 * Set job options common for Unix and MS-Windows.
1599 */
1600 static void
1601setup_job_options(jobopt_T *opt, int rows, int cols)
1602{
1603 clear_job_options(opt);
1604 opt->jo_mode = MODE_RAW;
1605 opt->jo_out_mode = MODE_RAW;
1606 opt->jo_err_mode = MODE_RAW;
1607 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001608
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001609 opt->jo_io[PART_OUT] = JIO_BUFFER;
1610 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001611 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1612
1613 opt->jo_modifiable[PART_OUT] = 0;
1614 opt->jo_modifiable[PART_ERR] = 0;
1615 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1616
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001617 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1618 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001619 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001620 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1621
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001622 opt->jo_term_rows = rows;
1623 opt->jo_term_cols = cols;
1624}
1625
1626/*
1627 * Create a new vterm and initialize it.
1628 */
1629 static void
1630create_vterm(term_T *term, int rows, int cols)
1631{
1632 VTerm *vterm;
1633 VTermScreen *screen;
1634
1635 vterm = vterm_new(rows, cols);
1636 term->tl_vterm = vterm;
1637 screen = vterm_obtain_screen(vterm);
1638 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1639 /* TODO: depends on 'encoding'. */
1640 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001641
1642 /* Vterm uses a default black background. Set it to white when
1643 * 'background' is "light". */
1644 if (*p_bg == 'l')
1645 {
1646 VTermColor fg, bg;
1647
1648 fg.red = fg.green = fg.blue = 0;
1649 bg.red = bg.green = bg.blue = 255;
1650 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1651 }
1652
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001653 /* Required to initialize most things. */
1654 vterm_screen_reset(screen, 1 /* hard */);
1655}
1656
Bram Moolenaar21554412017-07-24 21:44:43 +02001657/*
1658 * Return the text to show for the buffer name and status.
1659 */
1660 char_u *
1661term_get_status_text(term_T *term)
1662{
1663 if (term->tl_status_text == NULL)
1664 {
1665 char_u *txt;
1666 size_t len;
1667
Bram Moolenaar423802d2017-07-30 16:52:24 +02001668 if (term->tl_terminal_mode)
1669 {
1670 if (term_job_running(term))
1671 txt = (char_u *)_("Terminal");
1672 else
1673 txt = (char_u *)_("Terminal-finished");
1674 }
1675 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001676 txt = term->tl_title;
1677 else if (term_job_running(term))
1678 txt = (char_u *)_("running");
1679 else
1680 txt = (char_u *)_("finished");
1681 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001682 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001683 if (term->tl_status_text != NULL)
1684 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1685 term->tl_buffer->b_fname, txt);
1686 }
1687 return term->tl_status_text;
1688}
1689
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001690/*
1691 * Mark references in jobs of terminals.
1692 */
1693 int
1694set_ref_in_term(int copyID)
1695{
1696 int abort = FALSE;
1697 term_T *term;
1698 typval_T tv;
1699
1700 for (term = first_term; term != NULL; term = term->tl_next)
1701 if (term->tl_job != NULL)
1702 {
1703 tv.v_type = VAR_JOB;
1704 tv.vval.v_job = term->tl_job;
1705 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1706 }
1707 return abort;
1708}
1709
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001710/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001711 * Get the buffer from the first argument in "argvars".
1712 * Returns NULL when the buffer is not for a terminal window.
1713 */
1714 static buf_T *
1715term_get_buf(typval_T *argvars)
1716{
1717 buf_T *buf;
1718
1719 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1720 ++emsg_off;
1721 buf = get_buf_tv(&argvars[0], FALSE);
1722 --emsg_off;
1723 if (buf == NULL || buf->b_term == NULL)
1724 return NULL;
1725 return buf;
1726}
1727
1728/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001729 * "term_getattr(attr, name)" function
1730 */
1731 void
1732f_term_getattr(typval_T *argvars, typval_T *rettv)
1733{
1734 int attr;
1735 size_t i;
1736 char_u *name;
1737
1738 static struct {
1739 char *name;
1740 int attr;
1741 } attrs[] = {
1742 {"bold", HL_BOLD},
1743 {"italic", HL_ITALIC},
1744 {"underline", HL_UNDERLINE},
1745 {"strike", HL_STANDOUT},
1746 {"reverse", HL_INVERSE},
1747 };
1748
1749 attr = get_tv_number(&argvars[0]);
1750 name = get_tv_string_chk(&argvars[1]);
1751 if (name == NULL)
1752 return;
1753
1754 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1755 if (STRCMP(name, attrs[i].name) == 0)
1756 {
1757 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1758 break;
1759 }
1760}
1761
1762/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001763 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001764 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001765 void
1766f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001767{
Bram Moolenaar97870002017-07-30 18:28:38 +02001768 buf_T *buf = term_get_buf(argvars);
1769 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001770
Bram Moolenaar97870002017-07-30 18:28:38 +02001771 if (rettv_list_alloc(rettv) == FAIL)
1772 return;
1773 if (buf == NULL)
1774 return;
1775
1776 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001777 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1778 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001779 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001780}
1781
1782/*
1783 * "term_getjob(buf)" function
1784 */
1785 void
1786f_term_getjob(typval_T *argvars, typval_T *rettv)
1787{
1788 buf_T *buf = term_get_buf(argvars);
1789
1790 rettv->v_type = VAR_JOB;
1791 rettv->vval.v_job = NULL;
1792 if (buf == NULL)
1793 return;
1794
1795 rettv->vval.v_job = buf->b_term->tl_job;
1796 if (rettv->vval.v_job != NULL)
1797 ++rettv->vval.v_job->jv_refcount;
1798}
1799
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001800 static int
1801get_row_number(typval_T *tv, term_T *term)
1802{
1803 if (tv->v_type == VAR_STRING
1804 && tv->vval.v_string != NULL
1805 && STRCMP(tv->vval.v_string, ".") == 0)
1806 return term->tl_cursor_pos.row;
1807 return (int)get_tv_number(tv) - 1;
1808}
1809
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001810/*
1811 * "term_getline(buf, row)" function
1812 */
1813 void
1814f_term_getline(typval_T *argvars, typval_T *rettv)
1815{
1816 buf_T *buf = term_get_buf(argvars);
1817 term_T *term;
1818 int row;
1819
1820 rettv->v_type = VAR_STRING;
1821 if (buf == NULL)
1822 return;
1823 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001824 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001825
1826 if (term->tl_vterm == NULL)
1827 {
1828 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1829
1830 /* vterm is finished, get the text from the buffer */
1831 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1832 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1833 }
1834 else
1835 {
1836 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1837 VTermRect rect;
1838 int len;
1839 char_u *p;
1840
1841 len = term->tl_cols * MB_MAXBYTES + 1;
1842 p = alloc(len);
1843 if (p == NULL)
1844 return;
1845 rettv->vval.v_string = p;
1846
1847 rect.start_col = 0;
1848 rect.end_col = term->tl_cols;
1849 rect.start_row = row;
1850 rect.end_row = row + 1;
1851 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1852 }
1853}
1854
1855/*
1856 * "term_getsize(buf)" function
1857 */
1858 void
1859f_term_getsize(typval_T *argvars, typval_T *rettv)
1860{
1861 buf_T *buf = term_get_buf(argvars);
1862 list_T *l;
1863
1864 if (rettv_list_alloc(rettv) == FAIL)
1865 return;
1866 if (buf == NULL)
1867 return;
1868
1869 l = rettv->vval.v_list;
1870 list_append_number(l, buf->b_term->tl_rows);
1871 list_append_number(l, buf->b_term->tl_cols);
1872}
1873
1874/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001875 * "term_getstatus(buf)" function
1876 */
1877 void
1878f_term_getstatus(typval_T *argvars, typval_T *rettv)
1879{
1880 buf_T *buf = term_get_buf(argvars);
1881 term_T *term;
1882 char_u val[100];
1883
1884 rettv->v_type = VAR_STRING;
1885 if (buf == NULL)
1886 return;
1887 term = buf->b_term;
1888
1889 if (term_job_running(term))
1890 STRCPY(val, "running");
1891 else
1892 STRCPY(val, "finished");
1893 if (term->tl_terminal_mode)
1894 STRCAT(val, ",terminal");
1895 rettv->vval.v_string = vim_strsave(val);
1896}
1897
1898/*
1899 * "term_gettitle(buf)" function
1900 */
1901 void
1902f_term_gettitle(typval_T *argvars, typval_T *rettv)
1903{
1904 buf_T *buf = term_get_buf(argvars);
1905
1906 rettv->v_type = VAR_STRING;
1907 if (buf == NULL)
1908 return;
1909
1910 if (buf->b_term->tl_title != NULL)
1911 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1912}
1913
1914/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001915 * "term_list()" function
1916 */
1917 void
1918f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1919{
1920 term_T *tp;
1921 list_T *l;
1922
1923 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1924 return;
1925
1926 l = rettv->vval.v_list;
1927 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1928 if (tp != NULL && tp->tl_buffer != NULL)
1929 if (list_append_number(l,
1930 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1931 return;
1932}
1933
1934/*
1935 * "term_scrape(buf, row)" function
1936 */
1937 void
1938f_term_scrape(typval_T *argvars, typval_T *rettv)
1939{
1940 buf_T *buf = term_get_buf(argvars);
1941 VTermScreen *screen = NULL;
1942 VTermPos pos;
1943 list_T *l;
1944 term_T *term;
1945
1946 if (rettv_list_alloc(rettv) == FAIL)
1947 return;
1948 if (buf == NULL)
1949 return;
1950 term = buf->b_term;
1951 if (term->tl_vterm != NULL)
1952 screen = vterm_obtain_screen(term->tl_vterm);
1953
1954 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001955 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001956 for (pos.col = 0; pos.col < term->tl_cols; )
1957 {
1958 dict_T *dcell;
1959 VTermScreenCell cell;
1960 char_u rgb[8];
1961 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1962 int off = 0;
1963 int i;
1964
1965 if (screen == NULL)
1966 {
1967 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1968 sb_line_T *line;
1969
1970 /* vterm has finished, get the cell from scrollback */
1971 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1972 break;
1973 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1974 if (pos.col >= line->sb_cols)
1975 break;
1976 cell = line->sb_cells[pos.col];
1977 }
1978 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1979 break;
1980 dcell = dict_alloc();
1981 list_append_dict(l, dcell);
1982
1983 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1984 {
1985 if (cell.chars[i] == 0)
1986 break;
1987 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1988 }
1989 mbs[off] = NUL;
1990 dict_add_nr_str(dcell, "chars", 0, mbs);
1991
1992 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1993 cell.fg.red, cell.fg.green, cell.fg.blue);
1994 dict_add_nr_str(dcell, "fg", 0, rgb);
1995 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1996 cell.bg.red, cell.bg.green, cell.bg.blue);
1997 dict_add_nr_str(dcell, "bg", 0, rgb);
1998
1999 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2000 dict_add_nr_str(dcell, "width", cell.width, NULL);
2001
2002 ++pos.col;
2003 if (cell.width == 2)
2004 ++pos.col;
2005 }
2006}
2007
2008/*
2009 * "term_sendkeys(buf, keys)" function
2010 */
2011 void
2012f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2013{
2014 buf_T *buf = term_get_buf(argvars);
2015 char_u *msg;
2016 term_T *term;
2017
2018 rettv->v_type = VAR_UNKNOWN;
2019 if (buf == NULL)
2020 return;
2021
2022 msg = get_tv_string_chk(&argvars[1]);
2023 if (msg == NULL)
2024 return;
2025 term = buf->b_term;
2026 if (term->tl_vterm == NULL)
2027 return;
2028
2029 while (*msg != NUL)
2030 {
2031 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2032 msg += MB_PTR2LEN(msg);
2033 }
2034
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002035 if (!term->tl_terminal_mode)
2036 {
2037 /* TODO: only update once in a while. */
2038 update_screen(0);
2039 if (buf == curbuf)
2040 update_cursor(term, TRUE);
2041 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002042}
2043
2044/*
2045 * "term_start(command, options)" function
2046 */
2047 void
2048f_term_start(typval_T *argvars, typval_T *rettv)
2049{
2050 char_u *cmd = get_tv_string_chk(&argvars[0]);
2051 exarg_T ea;
2052
2053 if (cmd == NULL)
2054 return;
2055 ea.arg = cmd;
2056 ex_terminal(&ea);
2057
2058 if (curbuf->b_term != NULL)
2059 rettv->vval.v_number = curbuf->b_fnum;
2060}
2061
2062/*
2063 * "term_wait" function
2064 */
2065 void
2066f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2067{
2068 buf_T *buf = term_get_buf(argvars);
2069
2070 if (buf == NULL)
2071 return;
2072
2073 /* Get the job status, this will detect a job that finished. */
2074 if (buf->b_term->tl_job != NULL)
2075 (void)job_status(buf->b_term->tl_job);
2076
2077 /* Check for any pending channel I/O. */
2078 vpeekc_any();
2079 ui_delay(10L, FALSE);
2080
2081 /* Flushing messages on channels is hopefully sufficient.
2082 * TODO: is there a better way? */
2083 parse_queued_messages();
2084}
2085
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002086# ifdef WIN3264
2087
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002088/**************************************
2089 * 2. MS-Windows implementation.
2090 */
2091
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002092#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2093#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2094
Bram Moolenaar8a773062017-07-24 22:29:21 +02002095void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002096void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002097void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002098BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2099void (*winpty_config_set_initial_size)(void*, int, int);
2100LPCWSTR (*winpty_conin_name)(void*);
2101LPCWSTR (*winpty_conout_name)(void*);
2102LPCWSTR (*winpty_conerr_name)(void*);
2103void (*winpty_free)(void*);
2104void (*winpty_config_free)(void*);
2105void (*winpty_spawn_config_free)(void*);
2106void (*winpty_error_free)(void*);
2107LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002108BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002109
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002110#define WINPTY_DLL "winpty.dll"
2111
2112static HINSTANCE hWinPtyDLL = NULL;
2113
2114 int
2115dyn_winpty_init(void)
2116{
2117 int i;
2118 static struct
2119 {
2120 char *name;
2121 FARPROC *ptr;
2122 } winpty_entry[] =
2123 {
2124 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2125 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2126 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2127 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2128 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2129 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2130 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2131 {"winpty_free", (FARPROC*)&winpty_free},
2132 {"winpty_open", (FARPROC*)&winpty_open},
2133 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2134 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2135 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2136 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002137 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002138 {NULL, NULL}
2139 };
2140
2141 /* No need to initialize twice. */
2142 if (hWinPtyDLL)
2143 return 1;
2144 /* Load winpty.dll */
2145 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2146 if (!hWinPtyDLL)
2147 {
2148 EMSG2(_(e_loadlib), WINPTY_DLL);
2149 return 0;
2150 }
2151 for (i = 0; winpty_entry[i].name != NULL
2152 && winpty_entry[i].ptr != NULL; ++i)
2153 {
2154 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2155 winpty_entry[i].name)) == NULL)
2156 {
2157 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2158 return 0;
2159 }
2160 }
2161
2162 return 1;
2163}
2164
2165/*
2166 * Create a new terminal of "rows" by "cols" cells.
2167 * Store a reference in "term".
2168 * Return OK or FAIL.
2169 */
2170 static int
2171term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2172{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002173 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002174 channel_T *channel = NULL;
2175 job_T *job = NULL;
2176 jobopt_T opt;
2177 DWORD error;
2178 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2179 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002180 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002181
2182 if (!dyn_winpty_init())
2183 return FAIL;
2184
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002185 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002186 if (p == NULL)
2187 return FAIL;
2188
2189 job = job_alloc();
2190 if (job == NULL)
2191 goto failed;
2192
2193 channel = add_channel();
2194 if (channel == NULL)
2195 goto failed;
2196
2197 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2198 if (term->tl_winpty_config == NULL)
2199 goto failed;
2200
2201 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2202 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2203 if (term->tl_winpty == NULL)
2204 goto failed;
2205
2206 spawn_config = winpty_spawn_config_new(
2207 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2208 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2209 NULL,
2210 p,
2211 NULL,
2212 NULL,
2213 &winpty_err);
2214 if (spawn_config == NULL)
2215 goto failed;
2216
2217 channel = add_channel();
2218 if (channel == NULL)
2219 goto failed;
2220
2221 job = job_alloc();
2222 if (job == NULL)
2223 goto failed;
2224
2225 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2226 &child_thread_handle, &error, &winpty_err))
2227 goto failed;
2228
2229 channel_set_pipes(channel,
2230 (sock_T) CreateFileW(
2231 winpty_conin_name(term->tl_winpty),
2232 GENERIC_WRITE, 0, NULL,
2233 OPEN_EXISTING, 0, NULL),
2234 (sock_T) CreateFileW(
2235 winpty_conout_name(term->tl_winpty),
2236 GENERIC_READ, 0, NULL,
2237 OPEN_EXISTING, 0, NULL),
2238 (sock_T) CreateFileW(
2239 winpty_conerr_name(term->tl_winpty),
2240 GENERIC_READ, 0, NULL,
2241 OPEN_EXISTING, 0, NULL));
2242
2243 jo = CreateJobObject(NULL, NULL);
2244 if (jo == NULL)
2245 goto failed;
2246
2247 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002248 {
2249 /* Failed, switch the way to terminate process with TerminateProcess. */
2250 CloseHandle(jo);
2251 jo = NULL;
2252 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002253
2254 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002255 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002256
2257 create_vterm(term, rows, cols);
2258
2259 setup_job_options(&opt, rows, cols);
2260 channel_set_job(channel, job, &opt);
2261
2262 job->jv_channel = channel;
2263 job->jv_proc_info.hProcess = child_process_handle;
2264 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2265 job->jv_job_object = jo;
2266 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002267 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002268 term->tl_job = job;
2269
2270 return OK;
2271
2272failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002273 if (spawn_config != NULL)
2274 winpty_spawn_config_free(spawn_config);
2275 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002276 if (channel != NULL)
2277 channel_clear(channel);
2278 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002279 {
2280 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002281 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002282 }
2283 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002284 if (jo != NULL)
2285 CloseHandle(jo);
2286 if (term->tl_winpty != NULL)
2287 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002288 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002289 if (term->tl_winpty_config != NULL)
2290 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002291 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002292 if (winpty_err != NULL)
2293 {
2294 char_u *msg = utf16_to_enc(
2295 (short_u *)winpty_error_msg(winpty_err), NULL);
2296
2297 EMSG(msg);
2298 winpty_error_free(winpty_err);
2299 }
2300 return FAIL;
2301}
2302
2303/*
2304 * Free the terminal emulator part of "term".
2305 */
2306 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002307term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002308{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002309 if (term->tl_winpty != NULL)
2310 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002311 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002312 if (term->tl_winpty_config != NULL)
2313 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002314 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002315 if (term->tl_vterm != NULL)
2316 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002317 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002318}
2319
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002320/*
2321 * Request size to terminal.
2322 */
2323 static void
2324term_report_winsize(term_T *term, int rows, int cols)
2325{
2326 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2327}
2328
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002329# else
2330
2331/**************************************
2332 * 3. Unix-like implementation.
2333 */
2334
2335/*
2336 * Create a new terminal of "rows" by "cols" cells.
2337 * Start job for "cmd".
2338 * Store the pointers in "term".
2339 * Return OK or FAIL.
2340 */
2341 static int
2342term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2343{
2344 typval_T argvars[2];
2345 jobopt_T opt;
2346
2347 create_vterm(term, rows, cols);
2348
2349 argvars[0].v_type = VAR_STRING;
2350 argvars[0].vval.v_string = cmd;
2351 setup_job_options(&opt, rows, cols);
2352 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002353 if (term->tl_job != NULL)
2354 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002355
Bram Moolenaar61a66052017-07-22 18:39:00 +02002356 return term->tl_job != NULL
2357 && term->tl_job->jv_channel != NULL
2358 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002359}
2360
2361/*
2362 * Free the terminal emulator part of "term".
2363 */
2364 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002365term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002366{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002367 if (term->tl_vterm != NULL)
2368 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002369 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002370}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002371
2372/*
2373 * Request size to terminal.
2374 */
2375 static void
2376term_report_winsize(term_T *term, int rows, int cols)
2377{
2378 /* Use an ioctl() to report the new window size to the job. */
2379 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2380 {
2381 int fd = -1;
2382 int part;
2383
2384 for (part = PART_OUT; part < PART_COUNT; ++part)
2385 {
2386 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2387 if (isatty(fd))
2388 break;
2389 }
2390 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2391 mch_stop_job(term->tl_job, (char_u *)"winch");
2392 }
2393}
2394
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002395# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002396
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002397#endif /* FEAT_TERMINAL */