blob: edb4b75583d1860f3f806b76f27b09c629455848 [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 Moolenaard8dc1792017-08-03 11:55:21 +020040 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar423802d2017-07-30 16:52:24 +020041 * - in bash mouse clicks are inserting characters.
42 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaard85f2712017-07-28 21:51:57 +020043 * - For the scrollback buffer store lines in the buffer, only attributes in
44 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020045 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020046 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020047 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
48 * job finishes).
49 * - add option values to the command:
50 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020051 * - support different cursor shapes, colors and attributes
52 * - make term_getcursor() return type (none/block/bar/underline) and
53 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020054 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020055 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020056 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020057 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020058 * - set 'filetype' to "terminal"?
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 Moolenaarfae42832017-08-01 22:24:26 +0200965#ifdef UNIX
966 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
967#endif
968#ifdef WIN3264
969 if (c == Ctrl_C)
970 /* We don't know if the job can handle CTRL-C itself or not, this
971 * may kill the shell instead of killing the command running in the
972 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +0200973 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +0200974#endif
975
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200976 if (c == (termkey == 0 ? Ctrl_W : termkey))
977 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200978 int prev_c = c;
979
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200980#ifdef FEAT_CMDL_INFO
981 if (add_to_showcmd(c))
982 out_flush();
983#endif
984 c = term_vgetc();
985#ifdef FEAT_CMDL_INFO
986 clear_showcmd();
987#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200988 if (curbuf->b_term->tl_vterm == NULL
989 || !term_job_running(curbuf->b_term))
990 /* job finished while waiting for a character */
991 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200992
993 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200994 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200995 /* "CTRL-W .": send CTRL-W to the job */
996 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200997 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200998 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200999 {
1000 term_enter_terminal_mode();
1001 return FAIL;
1002 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001003 else if (c == '"')
1004 {
1005 term_paste_register(prev_c);
1006 continue;
1007 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001008 else if (termkey == 0 || c != termkey)
1009 {
1010 stuffcharReadbuff(Ctrl_W);
1011 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001012 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001013 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001014 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001015 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1016 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001017 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001018 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001019}
1020
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001021/*
1022 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001023 * This updates the title and status, but does not close the vter, because
1024 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001025 */
1026 void
1027term_job_ended(job_T *job)
1028{
1029 term_T *term;
1030 int did_one = FALSE;
1031
1032 for (term = first_term; term != NULL; term = term->tl_next)
1033 if (term->tl_job == job)
1034 {
1035 vim_free(term->tl_title);
1036 term->tl_title = NULL;
1037 vim_free(term->tl_status_text);
1038 term->tl_status_text = NULL;
1039 redraw_buf_and_status_later(term->tl_buffer, VALID);
1040 did_one = TRUE;
1041 }
1042 if (did_one)
1043 redraw_statuslines();
1044 if (curbuf->b_term != NULL)
1045 {
1046 if (curbuf->b_term->tl_job == job)
1047 maketitle();
1048 update_cursor(curbuf->b_term, TRUE);
1049 }
1050}
1051
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001052 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001053may_toggle_cursor(term_T *term)
1054{
1055 if (curbuf == term->tl_buffer)
1056 {
1057 if (term->tl_cursor_visible)
1058 cursor_on();
1059 else
1060 cursor_off();
1061 }
1062}
1063
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001064 static int
1065handle_damage(VTermRect rect, void *user)
1066{
1067 term_T *term = (term_T *)user;
1068
1069 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1070 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1071 redraw_buf_later(term->tl_buffer, NOT_VALID);
1072 return 1;
1073}
1074
1075 static int
1076handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1077{
1078 term_T *term = (term_T *)user;
1079
1080 /* TODO */
1081 redraw_buf_later(term->tl_buffer, NOT_VALID);
1082 return 1;
1083}
1084
1085 static int
1086handle_movecursor(
1087 VTermPos pos,
1088 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001089 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001090 void *user)
1091{
1092 term_T *term = (term_T *)user;
1093 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001094
1095 term->tl_cursor_pos = pos;
1096 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001097
1098 FOR_ALL_WINDOWS(wp)
1099 {
1100 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001101 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001102 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001103 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001104 {
1105 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001106 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001107 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001108
1109 return 1;
1110}
1111
Bram Moolenaar21554412017-07-24 21:44:43 +02001112 static int
1113handle_settermprop(
1114 VTermProp prop,
1115 VTermValue *value,
1116 void *user)
1117{
1118 term_T *term = (term_T *)user;
1119
1120 switch (prop)
1121 {
1122 case VTERM_PROP_TITLE:
1123 vim_free(term->tl_title);
1124 term->tl_title = vim_strsave((char_u *)value->string);
1125 vim_free(term->tl_status_text);
1126 term->tl_status_text = NULL;
1127 if (term == curbuf->b_term)
1128 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001129 break;
1130
1131 case VTERM_PROP_CURSORVISIBLE:
1132 term->tl_cursor_visible = value->boolean;
1133 may_toggle_cursor(term);
1134 out_flush();
1135 break;
1136
Bram Moolenaar21554412017-07-24 21:44:43 +02001137 default:
1138 break;
1139 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001140 /* Always return 1, otherwise vterm doesn't store the value internally. */
1141 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001142}
1143
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001144/*
1145 * The job running in the terminal resized the terminal.
1146 */
1147 static int
1148handle_resize(int rows, int cols, void *user)
1149{
1150 term_T *term = (term_T *)user;
1151 win_T *wp;
1152
1153 term->tl_rows = rows;
1154 term->tl_cols = cols;
1155 FOR_ALL_WINDOWS(wp)
1156 {
1157 if (wp->w_buffer == term->tl_buffer)
1158 {
1159 win_setheight_win(rows, wp);
1160 win_setwidth_win(cols, wp);
1161 }
1162 }
1163
1164 redraw_buf_later(term->tl_buffer, NOT_VALID);
1165 return 1;
1166}
1167
Bram Moolenaard85f2712017-07-28 21:51:57 +02001168/*
1169 * Handle a line that is pushed off the top of the screen.
1170 */
1171 static int
1172handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1173{
1174 term_T *term = (term_T *)user;
1175
1176 /* TODO: Limit the number of lines that are stored. */
1177 /* TODO: put the text in the buffer. */
1178 if (ga_grow(&term->tl_scrollback, 1) == OK)
1179 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001180 VTermScreenCell *p = NULL;
1181 int len = 0;
1182 int i;
1183 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001184
1185 /* do not store empty cells at the end */
1186 for (i = 0; i < cols; ++i)
1187 if (cells[i].chars[0] != 0)
1188 len = i + 1;
1189
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001190 if (len > 0)
1191 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001192 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001193 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001194
1195 line = (sb_line_T *)term->tl_scrollback.ga_data
1196 + term->tl_scrollback.ga_len;
1197 line->sb_cols = len;
1198 line->sb_cells = p;
1199 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001200 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001201
1202 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001203 }
1204 return 0; /* ignored */
1205}
1206
Bram Moolenaar21554412017-07-24 21:44:43 +02001207static VTermScreenCallbacks screen_callbacks = {
1208 handle_damage, /* damage */
1209 handle_moverect, /* moverect */
1210 handle_movecursor, /* movecursor */
1211 handle_settermprop, /* settermprop */
1212 NULL, /* bell */
1213 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001214 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001215 NULL /* sb_popline */
1216};
1217
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001218/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001219 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001220 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001221 */
1222 void
1223term_channel_closed(channel_T *ch)
1224{
1225 term_T *term;
1226 int did_one = FALSE;
1227
1228 for (term = first_term; term != NULL; term = term->tl_next)
1229 if (term->tl_job == ch->ch_job)
1230 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001231 term->tl_channel_closed = TRUE;
1232
Bram Moolenaard85f2712017-07-28 21:51:57 +02001233 vim_free(term->tl_title);
1234 term->tl_title = NULL;
1235 vim_free(term->tl_status_text);
1236 term->tl_status_text = NULL;
1237
Bram Moolenaar423802d2017-07-30 16:52:24 +02001238 /* Unless in Terminal-Normal mode: clear the vterm. */
1239 if (!term->tl_terminal_mode)
1240 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001241
1242 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1243 did_one = TRUE;
1244 }
1245 if (did_one)
1246 {
1247 redraw_statuslines();
1248
1249 /* Need to break out of vgetc(). */
1250 ins_char_typebuf(K_IGNORE);
1251
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001252 term = curbuf->b_term;
1253 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001254 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001255 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001256 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001257 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001258 }
1259 }
1260}
1261
1262/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001263 * Reverse engineer the RGB value into a cterm color index.
1264 * First color is 1. Return 0 if no match found.
1265 */
1266 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001267color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001268{
1269 int red = color->red;
1270 int blue = color->blue;
1271 int green = color->green;
1272
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001273 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001274 if (red == 0)
1275 {
1276 if (green == 0)
1277 {
1278 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001279 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001280 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001281 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001282 }
1283 else if (green == 224)
1284 {
1285 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001286 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001287 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001288 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001289 }
1290 }
1291 else if (red == 224)
1292 {
1293 if (green == 0)
1294 {
1295 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001296 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001297 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001298 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001299 }
1300 else if (green == 224)
1301 {
1302 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001303 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001304 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001305 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001306 }
1307 }
1308 else if (red == 128)
1309 {
1310 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001311 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001312 }
1313 else if (red == 255)
1314 {
1315 if (green == 64)
1316 {
1317 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001318 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001319 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001320 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001321 }
1322 else if (green == 255)
1323 {
1324 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001325 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001326 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001327 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001328 }
1329 }
1330 else if (red == 64)
1331 {
1332 if (green == 64)
1333 {
1334 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001335 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001336 }
1337 else if (green == 255)
1338 {
1339 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001340 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001341 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001342 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001343 }
1344 }
1345 if (t_colors >= 256)
1346 {
1347 if (red == blue && red == green)
1348 {
1349 /* 24-color greyscale */
1350 static int cutoff[23] = {
1351 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1352 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1353 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1354 int i;
1355
1356 for (i = 0; i < 23; ++i)
1357 if (red < cutoff[i])
1358 return i + 233;
1359 return 256;
1360 }
1361
1362 /* 216-color cube */
1363 return 17 + ((red + 25) / 0x33) * 36
1364 + ((green + 25) / 0x33) * 6
1365 + (blue + 25) / 0x33;
1366 }
1367 return 0;
1368}
1369
1370/*
1371 * Convert the attributes of a vterm cell into an attribute index.
1372 */
1373 static int
1374cell2attr(VTermScreenCell *cell)
1375{
1376 int attr = 0;
1377
1378 if (cell->attrs.bold)
1379 attr |= HL_BOLD;
1380 if (cell->attrs.underline)
1381 attr |= HL_UNDERLINE;
1382 if (cell->attrs.italic)
1383 attr |= HL_ITALIC;
1384 if (cell->attrs.strike)
1385 attr |= HL_STANDOUT;
1386 if (cell->attrs.reverse)
1387 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001388
1389#ifdef FEAT_GUI
1390 if (gui.in_use)
1391 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001392 guicolor_T fg, bg;
1393
1394 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1395 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1396 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001397 }
1398 else
1399#endif
1400#ifdef FEAT_TERMGUICOLORS
1401 if (p_tgc)
1402 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001403 guicolor_T fg, bg;
1404
1405 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1406 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1407
1408 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001409 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001410 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001411#endif
1412 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001413 int bold = MAYBE;
1414 int fg = color2index(&cell->fg, TRUE, &bold);
1415 int bg = color2index(&cell->bg, FALSE, &bold);
1416
1417 /* with 8 colors set the bold attribute to get a bright foreground */
1418 if (bold == TRUE)
1419 attr |= HL_BOLD;
1420 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001421 }
1422 return 0;
1423}
1424
1425/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001426 * Called to update the window that contains a terminal.
1427 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001428 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001429 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001430term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001431{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001432 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001433 VTerm *vterm;
1434 VTermScreen *screen;
1435 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001436 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001437
Bram Moolenaar423802d2017-07-30 16:52:24 +02001438 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001439 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001440
Bram Moolenaard85f2712017-07-28 21:51:57 +02001441 vterm = term->tl_vterm;
1442 screen = vterm_obtain_screen(vterm);
1443 state = vterm_obtain_state(vterm);
1444
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001445 /*
1446 * If the window was resized a redraw will be triggered and we get here.
1447 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1448 */
1449 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1450 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001451 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001452 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1453 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1454 win_T *twp;
1455
1456 FOR_ALL_WINDOWS(twp)
1457 {
1458 /* When more than one window shows the same terminal, use the
1459 * smallest size. */
1460 if (twp->w_buffer == term->tl_buffer)
1461 {
1462 if (!term->tl_rows_fixed && rows > twp->w_height)
1463 rows = twp->w_height;
1464 if (!term->tl_cols_fixed && cols > twp->w_width)
1465 cols = twp->w_width;
1466 }
1467 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001468
1469 vterm_set_size(vterm, rows, cols);
1470 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1471 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001472 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001473 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001474
1475 /* The cursor may have been moved when resizing. */
1476 vterm_state_get_cursorpos(state, &pos);
1477 position_cursor(wp, &pos);
1478
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001479 /* TODO: Only redraw what changed. */
1480 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001481 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001482 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001483 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001484
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001485 if (pos.row < term->tl_rows)
1486 {
1487 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001488 {
1489 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001490 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001491
Bram Moolenaareeac6772017-07-23 15:48:37 +02001492 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1493 vim_memset(&cell, 0, sizeof(cell));
1494
1495 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001496 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001497 if (c == NUL)
1498 {
1499 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001500#if defined(FEAT_MBYTE)
1501 if (enc_utf8)
1502 ScreenLinesUC[off] = NUL;
1503#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001504 }
1505 else
1506 {
1507#if defined(FEAT_MBYTE)
1508 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001509 {
1510 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001511 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001512 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001513 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001514 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001515 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001516 if (enc_utf8)
1517 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001518 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001519#else
1520 ScreenLines[off] = c;
1521#endif
1522 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001523 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001524
1525 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001526 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001527 if (cell.width == 2)
1528 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001529 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001530#if defined(FEAT_MBYTE)
1531 if (enc_utf8)
1532 ScreenLinesUC[off] = NUL;
1533#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001534 ++pos.col;
1535 ++off;
1536 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001537 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001538 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001539 else
1540 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001541
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001542 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1543 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001544 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001545
1546 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001547}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001548
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001549/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001550 * Return TRUE if "wp" is a terminal window where the job has finished.
1551 */
1552 int
1553term_is_finished(buf_T *buf)
1554{
1555 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1556}
1557
1558/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001559 * Return TRUE if "wp" is a terminal window where the job has finished or we
1560 * are in Terminal-Normal mode.
1561 */
1562 int
1563term_show_buffer(buf_T *buf)
1564{
1565 term_T *term = buf->b_term;
1566
1567 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1568}
1569
1570/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001571 * The current buffer is going to be changed. If there is terminal
1572 * highlighting remove it now.
1573 */
1574 void
1575term_change_in_curbuf(void)
1576{
1577 term_T *term = curbuf->b_term;
1578
1579 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1580 {
1581 free_scrollback(term);
1582 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001583
1584 /* The buffer is now like a normal buffer, it cannot be easily
1585 * abandoned when changed. */
1586 set_string_option_direct((char_u *)"buftype", -1,
1587 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001588 }
1589}
1590
1591/*
1592 * Get the screen attribute for a position in the buffer.
1593 */
1594 int
1595term_get_attr(buf_T *buf, linenr_T lnum, int col)
1596{
1597 term_T *term = buf->b_term;
1598 sb_line_T *line;
1599
Bram Moolenaar70229f92017-07-29 16:01:53 +02001600 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001601 return 0;
1602 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1603 if (col >= line->sb_cols)
1604 return 0;
1605 return cell2attr(line->sb_cells + col);
1606}
1607
1608/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001609 * Set job options common for Unix and MS-Windows.
1610 */
1611 static void
1612setup_job_options(jobopt_T *opt, int rows, int cols)
1613{
1614 clear_job_options(opt);
1615 opt->jo_mode = MODE_RAW;
1616 opt->jo_out_mode = MODE_RAW;
1617 opt->jo_err_mode = MODE_RAW;
1618 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001619
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001620 opt->jo_io[PART_OUT] = JIO_BUFFER;
1621 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001622 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1623
1624 opt->jo_modifiable[PART_OUT] = 0;
1625 opt->jo_modifiable[PART_ERR] = 0;
1626 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1627
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001628 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1629 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001630 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001631 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1632
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001633 opt->jo_term_rows = rows;
1634 opt->jo_term_cols = cols;
1635}
1636
1637/*
1638 * Create a new vterm and initialize it.
1639 */
1640 static void
1641create_vterm(term_T *term, int rows, int cols)
1642{
1643 VTerm *vterm;
1644 VTermScreen *screen;
1645
1646 vterm = vterm_new(rows, cols);
1647 term->tl_vterm = vterm;
1648 screen = vterm_obtain_screen(vterm);
1649 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1650 /* TODO: depends on 'encoding'. */
1651 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001652
1653 /* Vterm uses a default black background. Set it to white when
1654 * 'background' is "light". */
1655 if (*p_bg == 'l')
1656 {
1657 VTermColor fg, bg;
1658
1659 fg.red = fg.green = fg.blue = 0;
1660 bg.red = bg.green = bg.blue = 255;
1661 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1662 }
1663
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001664 /* Required to initialize most things. */
1665 vterm_screen_reset(screen, 1 /* hard */);
1666}
1667
Bram Moolenaar21554412017-07-24 21:44:43 +02001668/*
1669 * Return the text to show for the buffer name and status.
1670 */
1671 char_u *
1672term_get_status_text(term_T *term)
1673{
1674 if (term->tl_status_text == NULL)
1675 {
1676 char_u *txt;
1677 size_t len;
1678
Bram Moolenaar423802d2017-07-30 16:52:24 +02001679 if (term->tl_terminal_mode)
1680 {
1681 if (term_job_running(term))
1682 txt = (char_u *)_("Terminal");
1683 else
1684 txt = (char_u *)_("Terminal-finished");
1685 }
1686 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001687 txt = term->tl_title;
1688 else if (term_job_running(term))
1689 txt = (char_u *)_("running");
1690 else
1691 txt = (char_u *)_("finished");
1692 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001693 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001694 if (term->tl_status_text != NULL)
1695 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1696 term->tl_buffer->b_fname, txt);
1697 }
1698 return term->tl_status_text;
1699}
1700
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001701/*
1702 * Mark references in jobs of terminals.
1703 */
1704 int
1705set_ref_in_term(int copyID)
1706{
1707 int abort = FALSE;
1708 term_T *term;
1709 typval_T tv;
1710
1711 for (term = first_term; term != NULL; term = term->tl_next)
1712 if (term->tl_job != NULL)
1713 {
1714 tv.v_type = VAR_JOB;
1715 tv.vval.v_job = term->tl_job;
1716 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1717 }
1718 return abort;
1719}
1720
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001721/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001722 * Get the buffer from the first argument in "argvars".
1723 * Returns NULL when the buffer is not for a terminal window.
1724 */
1725 static buf_T *
1726term_get_buf(typval_T *argvars)
1727{
1728 buf_T *buf;
1729
1730 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1731 ++emsg_off;
1732 buf = get_buf_tv(&argvars[0], FALSE);
1733 --emsg_off;
1734 if (buf == NULL || buf->b_term == NULL)
1735 return NULL;
1736 return buf;
1737}
1738
1739/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001740 * "term_getattr(attr, name)" function
1741 */
1742 void
1743f_term_getattr(typval_T *argvars, typval_T *rettv)
1744{
1745 int attr;
1746 size_t i;
1747 char_u *name;
1748
1749 static struct {
1750 char *name;
1751 int attr;
1752 } attrs[] = {
1753 {"bold", HL_BOLD},
1754 {"italic", HL_ITALIC},
1755 {"underline", HL_UNDERLINE},
1756 {"strike", HL_STANDOUT},
1757 {"reverse", HL_INVERSE},
1758 };
1759
1760 attr = get_tv_number(&argvars[0]);
1761 name = get_tv_string_chk(&argvars[1]);
1762 if (name == NULL)
1763 return;
1764
1765 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1766 if (STRCMP(name, attrs[i].name) == 0)
1767 {
1768 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1769 break;
1770 }
1771}
1772
1773/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001774 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001775 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001776 void
1777f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001778{
Bram Moolenaar97870002017-07-30 18:28:38 +02001779 buf_T *buf = term_get_buf(argvars);
1780 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001781
Bram Moolenaar97870002017-07-30 18:28:38 +02001782 if (rettv_list_alloc(rettv) == FAIL)
1783 return;
1784 if (buf == NULL)
1785 return;
1786
1787 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001788 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1789 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001790 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001791}
1792
1793/*
1794 * "term_getjob(buf)" function
1795 */
1796 void
1797f_term_getjob(typval_T *argvars, typval_T *rettv)
1798{
1799 buf_T *buf = term_get_buf(argvars);
1800
1801 rettv->v_type = VAR_JOB;
1802 rettv->vval.v_job = NULL;
1803 if (buf == NULL)
1804 return;
1805
1806 rettv->vval.v_job = buf->b_term->tl_job;
1807 if (rettv->vval.v_job != NULL)
1808 ++rettv->vval.v_job->jv_refcount;
1809}
1810
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001811 static int
1812get_row_number(typval_T *tv, term_T *term)
1813{
1814 if (tv->v_type == VAR_STRING
1815 && tv->vval.v_string != NULL
1816 && STRCMP(tv->vval.v_string, ".") == 0)
1817 return term->tl_cursor_pos.row;
1818 return (int)get_tv_number(tv) - 1;
1819}
1820
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001821/*
1822 * "term_getline(buf, row)" function
1823 */
1824 void
1825f_term_getline(typval_T *argvars, typval_T *rettv)
1826{
1827 buf_T *buf = term_get_buf(argvars);
1828 term_T *term;
1829 int row;
1830
1831 rettv->v_type = VAR_STRING;
1832 if (buf == NULL)
1833 return;
1834 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001835 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001836
1837 if (term->tl_vterm == NULL)
1838 {
1839 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1840
1841 /* vterm is finished, get the text from the buffer */
1842 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1843 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1844 }
1845 else
1846 {
1847 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1848 VTermRect rect;
1849 int len;
1850 char_u *p;
1851
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001852 if (row < 0 || row >= term->tl_rows)
1853 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001854 len = term->tl_cols * MB_MAXBYTES + 1;
1855 p = alloc(len);
1856 if (p == NULL)
1857 return;
1858 rettv->vval.v_string = p;
1859
1860 rect.start_col = 0;
1861 rect.end_col = term->tl_cols;
1862 rect.start_row = row;
1863 rect.end_row = row + 1;
1864 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1865 }
1866}
1867
1868/*
1869 * "term_getsize(buf)" function
1870 */
1871 void
1872f_term_getsize(typval_T *argvars, typval_T *rettv)
1873{
1874 buf_T *buf = term_get_buf(argvars);
1875 list_T *l;
1876
1877 if (rettv_list_alloc(rettv) == FAIL)
1878 return;
1879 if (buf == NULL)
1880 return;
1881
1882 l = rettv->vval.v_list;
1883 list_append_number(l, buf->b_term->tl_rows);
1884 list_append_number(l, buf->b_term->tl_cols);
1885}
1886
1887/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001888 * "term_getstatus(buf)" function
1889 */
1890 void
1891f_term_getstatus(typval_T *argvars, typval_T *rettv)
1892{
1893 buf_T *buf = term_get_buf(argvars);
1894 term_T *term;
1895 char_u val[100];
1896
1897 rettv->v_type = VAR_STRING;
1898 if (buf == NULL)
1899 return;
1900 term = buf->b_term;
1901
1902 if (term_job_running(term))
1903 STRCPY(val, "running");
1904 else
1905 STRCPY(val, "finished");
1906 if (term->tl_terminal_mode)
1907 STRCAT(val, ",terminal");
1908 rettv->vval.v_string = vim_strsave(val);
1909}
1910
1911/*
1912 * "term_gettitle(buf)" function
1913 */
1914 void
1915f_term_gettitle(typval_T *argvars, typval_T *rettv)
1916{
1917 buf_T *buf = term_get_buf(argvars);
1918
1919 rettv->v_type = VAR_STRING;
1920 if (buf == NULL)
1921 return;
1922
1923 if (buf->b_term->tl_title != NULL)
1924 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1925}
1926
1927/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001928 * "term_list()" function
1929 */
1930 void
1931f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1932{
1933 term_T *tp;
1934 list_T *l;
1935
1936 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1937 return;
1938
1939 l = rettv->vval.v_list;
1940 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1941 if (tp != NULL && tp->tl_buffer != NULL)
1942 if (list_append_number(l,
1943 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1944 return;
1945}
1946
1947/*
1948 * "term_scrape(buf, row)" function
1949 */
1950 void
1951f_term_scrape(typval_T *argvars, typval_T *rettv)
1952{
1953 buf_T *buf = term_get_buf(argvars);
1954 VTermScreen *screen = NULL;
1955 VTermPos pos;
1956 list_T *l;
1957 term_T *term;
1958
1959 if (rettv_list_alloc(rettv) == FAIL)
1960 return;
1961 if (buf == NULL)
1962 return;
1963 term = buf->b_term;
1964 if (term->tl_vterm != NULL)
1965 screen = vterm_obtain_screen(term->tl_vterm);
1966
1967 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001968 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001969 for (pos.col = 0; pos.col < term->tl_cols; )
1970 {
1971 dict_T *dcell;
1972 VTermScreenCell cell;
1973 char_u rgb[8];
1974 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1975 int off = 0;
1976 int i;
1977
1978 if (screen == NULL)
1979 {
1980 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1981 sb_line_T *line;
1982
1983 /* vterm has finished, get the cell from scrollback */
1984 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1985 break;
1986 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1987 if (pos.col >= line->sb_cols)
1988 break;
1989 cell = line->sb_cells[pos.col];
1990 }
1991 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1992 break;
1993 dcell = dict_alloc();
1994 list_append_dict(l, dcell);
1995
1996 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1997 {
1998 if (cell.chars[i] == 0)
1999 break;
2000 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2001 }
2002 mbs[off] = NUL;
2003 dict_add_nr_str(dcell, "chars", 0, mbs);
2004
2005 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2006 cell.fg.red, cell.fg.green, cell.fg.blue);
2007 dict_add_nr_str(dcell, "fg", 0, rgb);
2008 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2009 cell.bg.red, cell.bg.green, cell.bg.blue);
2010 dict_add_nr_str(dcell, "bg", 0, rgb);
2011
2012 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2013 dict_add_nr_str(dcell, "width", cell.width, NULL);
2014
2015 ++pos.col;
2016 if (cell.width == 2)
2017 ++pos.col;
2018 }
2019}
2020
2021/*
2022 * "term_sendkeys(buf, keys)" function
2023 */
2024 void
2025f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2026{
2027 buf_T *buf = term_get_buf(argvars);
2028 char_u *msg;
2029 term_T *term;
2030
2031 rettv->v_type = VAR_UNKNOWN;
2032 if (buf == NULL)
2033 return;
2034
2035 msg = get_tv_string_chk(&argvars[1]);
2036 if (msg == NULL)
2037 return;
2038 term = buf->b_term;
2039 if (term->tl_vterm == NULL)
2040 return;
2041
2042 while (*msg != NUL)
2043 {
2044 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2045 msg += MB_PTR2LEN(msg);
2046 }
2047
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002048 if (!term->tl_terminal_mode)
2049 {
2050 /* TODO: only update once in a while. */
2051 update_screen(0);
2052 if (buf == curbuf)
2053 update_cursor(term, TRUE);
2054 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002055}
2056
2057/*
2058 * "term_start(command, options)" function
2059 */
2060 void
2061f_term_start(typval_T *argvars, typval_T *rettv)
2062{
2063 char_u *cmd = get_tv_string_chk(&argvars[0]);
2064 exarg_T ea;
2065
2066 if (cmd == NULL)
2067 return;
2068 ea.arg = cmd;
2069 ex_terminal(&ea);
2070
2071 if (curbuf->b_term != NULL)
2072 rettv->vval.v_number = curbuf->b_fnum;
2073}
2074
2075/*
2076 * "term_wait" function
2077 */
2078 void
2079f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2080{
2081 buf_T *buf = term_get_buf(argvars);
2082
2083 if (buf == NULL)
2084 return;
2085
2086 /* Get the job status, this will detect a job that finished. */
2087 if (buf->b_term->tl_job != NULL)
2088 (void)job_status(buf->b_term->tl_job);
2089
2090 /* Check for any pending channel I/O. */
2091 vpeekc_any();
2092 ui_delay(10L, FALSE);
2093
2094 /* Flushing messages on channels is hopefully sufficient.
2095 * TODO: is there a better way? */
2096 parse_queued_messages();
2097}
2098
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002099# ifdef WIN3264
2100
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002101/**************************************
2102 * 2. MS-Windows implementation.
2103 */
2104
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002105#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2106#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2107
Bram Moolenaar8a773062017-07-24 22:29:21 +02002108void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002109void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002110void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002111BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2112void (*winpty_config_set_initial_size)(void*, int, int);
2113LPCWSTR (*winpty_conin_name)(void*);
2114LPCWSTR (*winpty_conout_name)(void*);
2115LPCWSTR (*winpty_conerr_name)(void*);
2116void (*winpty_free)(void*);
2117void (*winpty_config_free)(void*);
2118void (*winpty_spawn_config_free)(void*);
2119void (*winpty_error_free)(void*);
2120LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002121BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002122
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002123#define WINPTY_DLL "winpty.dll"
2124
2125static HINSTANCE hWinPtyDLL = NULL;
2126
2127 int
2128dyn_winpty_init(void)
2129{
2130 int i;
2131 static struct
2132 {
2133 char *name;
2134 FARPROC *ptr;
2135 } winpty_entry[] =
2136 {
2137 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2138 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2139 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2140 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2141 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2142 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2143 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2144 {"winpty_free", (FARPROC*)&winpty_free},
2145 {"winpty_open", (FARPROC*)&winpty_open},
2146 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2147 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2148 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2149 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002150 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002151 {NULL, NULL}
2152 };
2153
2154 /* No need to initialize twice. */
2155 if (hWinPtyDLL)
2156 return 1;
2157 /* Load winpty.dll */
2158 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2159 if (!hWinPtyDLL)
2160 {
2161 EMSG2(_(e_loadlib), WINPTY_DLL);
2162 return 0;
2163 }
2164 for (i = 0; winpty_entry[i].name != NULL
2165 && winpty_entry[i].ptr != NULL; ++i)
2166 {
2167 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2168 winpty_entry[i].name)) == NULL)
2169 {
2170 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2171 return 0;
2172 }
2173 }
2174
2175 return 1;
2176}
2177
2178/*
2179 * Create a new terminal of "rows" by "cols" cells.
2180 * Store a reference in "term".
2181 * Return OK or FAIL.
2182 */
2183 static int
2184term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2185{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002186 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002187 channel_T *channel = NULL;
2188 job_T *job = NULL;
2189 jobopt_T opt;
2190 DWORD error;
2191 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2192 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002193 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002194
2195 if (!dyn_winpty_init())
2196 return FAIL;
2197
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002198 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002199 if (p == NULL)
2200 return FAIL;
2201
2202 job = job_alloc();
2203 if (job == NULL)
2204 goto failed;
2205
2206 channel = add_channel();
2207 if (channel == NULL)
2208 goto failed;
2209
2210 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2211 if (term->tl_winpty_config == NULL)
2212 goto failed;
2213
2214 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2215 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2216 if (term->tl_winpty == NULL)
2217 goto failed;
2218
2219 spawn_config = winpty_spawn_config_new(
2220 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2221 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2222 NULL,
2223 p,
2224 NULL,
2225 NULL,
2226 &winpty_err);
2227 if (spawn_config == NULL)
2228 goto failed;
2229
2230 channel = add_channel();
2231 if (channel == NULL)
2232 goto failed;
2233
2234 job = job_alloc();
2235 if (job == NULL)
2236 goto failed;
2237
2238 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2239 &child_thread_handle, &error, &winpty_err))
2240 goto failed;
2241
2242 channel_set_pipes(channel,
2243 (sock_T) CreateFileW(
2244 winpty_conin_name(term->tl_winpty),
2245 GENERIC_WRITE, 0, NULL,
2246 OPEN_EXISTING, 0, NULL),
2247 (sock_T) CreateFileW(
2248 winpty_conout_name(term->tl_winpty),
2249 GENERIC_READ, 0, NULL,
2250 OPEN_EXISTING, 0, NULL),
2251 (sock_T) CreateFileW(
2252 winpty_conerr_name(term->tl_winpty),
2253 GENERIC_READ, 0, NULL,
2254 OPEN_EXISTING, 0, NULL));
2255
2256 jo = CreateJobObject(NULL, NULL);
2257 if (jo == NULL)
2258 goto failed;
2259
2260 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002261 {
2262 /* Failed, switch the way to terminate process with TerminateProcess. */
2263 CloseHandle(jo);
2264 jo = NULL;
2265 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002266
2267 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002268 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002269
2270 create_vterm(term, rows, cols);
2271
2272 setup_job_options(&opt, rows, cols);
2273 channel_set_job(channel, job, &opt);
2274
2275 job->jv_channel = channel;
2276 job->jv_proc_info.hProcess = child_process_handle;
2277 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2278 job->jv_job_object = jo;
2279 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002280 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002281 term->tl_job = job;
2282
2283 return OK;
2284
2285failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002286 if (spawn_config != NULL)
2287 winpty_spawn_config_free(spawn_config);
2288 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002289 if (channel != NULL)
2290 channel_clear(channel);
2291 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002292 {
2293 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002294 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002295 }
2296 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002297 if (jo != NULL)
2298 CloseHandle(jo);
2299 if (term->tl_winpty != NULL)
2300 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002301 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002302 if (term->tl_winpty_config != NULL)
2303 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002304 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002305 if (winpty_err != NULL)
2306 {
2307 char_u *msg = utf16_to_enc(
2308 (short_u *)winpty_error_msg(winpty_err), NULL);
2309
2310 EMSG(msg);
2311 winpty_error_free(winpty_err);
2312 }
2313 return FAIL;
2314}
2315
2316/*
2317 * Free the terminal emulator part of "term".
2318 */
2319 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002320term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002321{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002322 if (term->tl_winpty != NULL)
2323 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002324 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002325 if (term->tl_winpty_config != NULL)
2326 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002327 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002328 if (term->tl_vterm != NULL)
2329 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002330 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002331}
2332
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002333/*
2334 * Request size to terminal.
2335 */
2336 static void
2337term_report_winsize(term_T *term, int rows, int cols)
2338{
2339 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2340}
2341
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002342# else
2343
2344/**************************************
2345 * 3. Unix-like implementation.
2346 */
2347
2348/*
2349 * Create a new terminal of "rows" by "cols" cells.
2350 * Start job for "cmd".
2351 * Store the pointers in "term".
2352 * Return OK or FAIL.
2353 */
2354 static int
2355term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2356{
2357 typval_T argvars[2];
2358 jobopt_T opt;
2359
2360 create_vterm(term, rows, cols);
2361
2362 argvars[0].v_type = VAR_STRING;
2363 argvars[0].vval.v_string = cmd;
2364 setup_job_options(&opt, rows, cols);
2365 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002366 if (term->tl_job != NULL)
2367 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002368
Bram Moolenaar61a66052017-07-22 18:39:00 +02002369 return term->tl_job != NULL
2370 && term->tl_job->jv_channel != NULL
2371 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002372}
2373
2374/*
2375 * Free the terminal emulator part of "term".
2376 */
2377 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002378term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002379{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002380 if (term->tl_vterm != NULL)
2381 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002382 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002383}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002384
2385/*
2386 * Request size to terminal.
2387 */
2388 static void
2389term_report_winsize(term_T *term, int rows, int cols)
2390{
2391 /* Use an ioctl() to report the new window size to the job. */
2392 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2393 {
2394 int fd = -1;
2395 int part;
2396
2397 for (part = PART_OUT; part < PART_COUNT; ++part)
2398 {
2399 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2400 if (isatty(fd))
2401 break;
2402 }
2403 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2404 mch_stop_job(term->tl_job, (char_u *)"winch");
2405 }
2406}
2407
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002408# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002409
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002410#endif /* FEAT_TERMINAL */