blob: 2c0879d560d974f3cb208c1d1971b3c0d22b9fbe [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 Moolenaar7c9aec42017-08-03 13:51:25 +020060 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
61 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020062 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020063 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020064 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020066 * - implement "term" for job_start(): more job options when starting a
67 * terminal.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020068 * - support ":term NONE" to open a terminal with a pty but not running a job
69 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020070 * - if the job in the terminal does not support the mouse, we can use the
71 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020072 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
73 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020074 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020075 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020076 * - Copy text in the vterm to the Vim buffer once in a while, so that
77 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078 */
79
80#include "vim.h"
81
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020082#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020083
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020084#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020085# define MIN(x,y) (x < y ? x : y)
86# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020087#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020088
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020089#include "libvterm/include/vterm.h"
90
Bram Moolenaard85f2712017-07-28 21:51:57 +020091typedef struct sb_line_S {
92 int sb_cols; /* can differ per line */
93 VTermScreenCell *sb_cells; /* allocated */
94} sb_line_T;
95
Bram Moolenaare4f25e42017-07-07 11:54:15 +020096/* typedef term_T in structs.h */
97struct terminal_S {
98 term_T *tl_next;
99
Bram Moolenaar423802d2017-07-30 16:52:24 +0200100 VTerm *tl_vterm;
101 job_T *tl_job;
102 buf_T *tl_buffer;
103
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200104 /* used when tl_job is NULL and only a pty was created */
105 int tl_tty_fd;
106 char_u *tl_tty_name;
107
Bram Moolenaar423802d2017-07-30 16:52:24 +0200108 int tl_terminal_mode;
109 int tl_channel_closed;
110
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200111#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200112 void *tl_winpty_config;
113 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200114#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200115
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200116 /* last known vterm size */
117 int tl_rows;
118 int tl_cols;
119 /* vterm size does not follow window size */
120 int tl_rows_fixed;
121 int tl_cols_fixed;
122
Bram Moolenaar21554412017-07-24 21:44:43 +0200123 char_u *tl_title; /* NULL or allocated */
124 char_u *tl_status_text; /* NULL or allocated */
125
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126 /* Range of screen rows to update. Zero based. */
127 int tl_dirty_row_start; /* -1 if nothing dirty */
128 int tl_dirty_row_end; /* row below last one to update */
129
Bram Moolenaard85f2712017-07-28 21:51:57 +0200130 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200131 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200132
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200133 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200134 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200135};
136
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200137/*
138 * List of all active terminals.
139 */
140static term_T *first_term = NULL;
141
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200142
143#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
144#define KEY_BUF_LEN 200
145
146/*
147 * Functions with separate implementation for MS-Windows and Unix-like systems.
148 */
149static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200150static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200151static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200152
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200153/**************************************
154 * 1. Generic code for all systems.
155 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200156
157/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200158 * Determine the terminal size from 'termsize' and the current window.
159 * Assumes term->tl_rows and term->tl_cols are zero.
160 */
161 static void
162set_term_and_win_size(term_T *term)
163{
164 if (*curwin->w_p_tms != NUL)
165 {
166 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
167
168 term->tl_rows = atoi((char *)curwin->w_p_tms);
169 term->tl_cols = atoi((char *)p);
170 }
171 if (term->tl_rows == 0)
172 term->tl_rows = curwin->w_height;
173 else
174 {
175 win_setheight_win(term->tl_rows, curwin);
176 term->tl_rows_fixed = TRUE;
177 }
178 if (term->tl_cols == 0)
179 term->tl_cols = curwin->w_width;
180 else
181 {
182 win_setwidth_win(term->tl_cols, curwin);
183 term->tl_cols_fixed = TRUE;
184 }
185}
186
187/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200188 * ":terminal": open a terminal window and execute a job in it.
189 */
190 void
191ex_terminal(exarg_T *eap)
192{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200193 exarg_T split_ea;
194 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200195 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200196 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200197
198 if (check_restricted() || check_secure())
199 return;
200
201 term = (term_T *)alloc_clear(sizeof(term_T));
202 if (term == NULL)
203 return;
204 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200205 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200206 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200207
208 /* Open a new window or tab. */
209 vim_memset(&split_ea, 0, sizeof(split_ea));
210 split_ea.cmdidx = CMD_new;
211 split_ea.cmd = (char_u *)"new";
212 split_ea.arg = (char_u *)"";
213 ex_splitview(&split_ea);
214 if (curwin == old_curwin)
215 {
216 /* split failed */
217 vim_free(term);
218 return;
219 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200220 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200221 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200222
223 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200224 term->tl_next = first_term;
225 first_term = term;
226
Bram Moolenaar293424c2017-07-26 23:11:01 +0200227 if (cmd == NULL || *cmd == NUL)
228 cmd = p_sh;
229
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200230 {
231 int i;
232 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200233 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200234
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200235 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200236 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200237 /* Prepend a ! to the command name to avoid the buffer name equals
238 * the executable, otherwise ":w!" would overwrite it. */
239 if (i == 0)
240 vim_snprintf((char *)p, len, "!%s", cmd);
241 else
242 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200243 if (buflist_findname(p) == NULL)
244 {
245 curbuf->b_ffname = p;
246 break;
247 }
248 }
249 }
250 curbuf->b_fname = curbuf->b_ffname;
251
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200252 /* Mark the buffer as not modifiable. It can only be made modifiable after
253 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200254 curbuf->b_p_ma = FALSE;
255 set_string_option_direct((char_u *)"buftype", -1,
256 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200257
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200258 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200259
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200260 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200261 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200262 {
263 /* store the size we ended up with */
264 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200265 }
266 else
267 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200268 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200269
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200270 /* Wiping out the buffer will also close the window and call
271 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200272 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200273 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200274}
275
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200276/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200277 * Free the scrollback buffer for "term".
278 */
279 static void
280free_scrollback(term_T *term)
281{
282 int i;
283
284 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
285 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
286 ga_clear(&term->tl_scrollback);
287}
288
289/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200290 * Free a terminal and everything it refers to.
291 * Kills the job if there is one.
292 * Called when wiping out a buffer.
293 */
294 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200295free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200296{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200297 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200298 term_T *tp;
299
300 if (term == NULL)
301 return;
302 if (first_term == term)
303 first_term = term->tl_next;
304 else
305 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
306 if (tp->tl_next == term)
307 {
308 tp->tl_next = term->tl_next;
309 break;
310 }
311
312 if (term->tl_job != NULL)
313 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200314 if (term->tl_job->jv_status != JOB_ENDED
315 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200316 job_stop(term->tl_job, NULL, "kill");
317 job_unref(term->tl_job);
318 }
319
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200320 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200321
322 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200323 vim_free(term->tl_title);
324 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200325 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200326 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200327}
328
329/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200330 * Write job output "msg[len]" to the vterm.
331 */
332 static void
333term_write_job_output(term_T *term, char_u *msg, size_t len)
334{
335 VTerm *vterm = term->tl_vterm;
336 char_u *p;
337 size_t done;
338 size_t len_now;
339
340 for (done = 0; done < len; done += len_now)
341 {
342 for (p = msg + done; p < msg + len; )
343 {
344 if (*p == NL)
345 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200346 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200347 }
348 len_now = p - msg - done;
349 vterm_input_write(vterm, (char *)msg + done, len_now);
350 if (p < msg + len && *p == NL)
351 {
352 /* Convert NL to CR-NL, that appears to work best. */
353 vterm_input_write(vterm, "\r\n", 2);
354 ++len_now;
355 }
356 }
357
358 /* this invokes the damage callbacks */
359 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
360}
361
Bram Moolenaar1c844932017-07-24 23:36:41 +0200362 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200363update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200364{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200365 if (term->tl_terminal_mode)
366 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200367 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200368 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200369 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200370 if (term->tl_cursor_visible)
371 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200372 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200373#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200374 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200375 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200376#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200377 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200378}
379
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200380/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200381 * Invoked when "msg" output from a job was received. Write it to the terminal
382 * of "buffer".
383 */
384 void
385write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
386{
387 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200388 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200389
Bram Moolenaard85f2712017-07-28 21:51:57 +0200390 if (term->tl_vterm == NULL)
391 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200392 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200393 return;
394 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200395 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200396 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200397
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200398 if (!term->tl_terminal_mode)
399 {
400 /* TODO: only update once in a while. */
401 update_screen(0);
402 update_cursor(term, TRUE);
403 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200404}
405
406/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200407 * Send a mouse position and click to the vterm
408 */
409 static int
410term_send_mouse(VTerm *vterm, int button, int pressed)
411{
412 VTermModifier mod = VTERM_MOD_NONE;
413
414 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
415 mouse_col - W_WINCOL(curwin), mod);
416 vterm_mouse_button(vterm, button, pressed, mod);
417 return TRUE;
418}
419
420/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200421 * Convert typed key "c" into bytes to send to the job.
422 * Return the number of bytes in "buf".
423 */
424 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200425term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200426{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200427 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200428 VTermKey key = VTERM_KEY_NONE;
429 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200430 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200431
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200432 switch (c)
433 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200434 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200435 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200436 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
437 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200438 case K_DEL: key = VTERM_KEY_DEL; break;
439 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200440 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
441 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200442 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200443 case K_S_END: mod = VTERM_MOD_SHIFT;
444 key = VTERM_KEY_END; break;
445 case K_C_END: mod = VTERM_MOD_CTRL;
446 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200447 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
448 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
449 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
450 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
451 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
452 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
453 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
454 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
455 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
456 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
457 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
458 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
459 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200460 case K_S_HOME: mod = VTERM_MOD_SHIFT;
461 key = VTERM_KEY_HOME; break;
462 case K_C_HOME: mod = VTERM_MOD_CTRL;
463 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200464 case K_INS: key = VTERM_KEY_INS; break;
465 case K_K0: key = VTERM_KEY_KP_0; break;
466 case K_K1: key = VTERM_KEY_KP_1; break;
467 case K_K2: key = VTERM_KEY_KP_2; break;
468 case K_K3: key = VTERM_KEY_KP_3; break;
469 case K_K4: key = VTERM_KEY_KP_4; break;
470 case K_K5: key = VTERM_KEY_KP_5; break;
471 case K_K6: key = VTERM_KEY_KP_6; break;
472 case K_K7: key = VTERM_KEY_KP_7; break;
473 case K_K8: key = VTERM_KEY_KP_8; break;
474 case K_K9: key = VTERM_KEY_KP_9; break;
475 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
476 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
477 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
478 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
479 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
480 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
481 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
482 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
483 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
484 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
485 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
486 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
487 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200488 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
489 key = VTERM_KEY_LEFT; break;
490 case K_C_LEFT: mod = VTERM_MOD_CTRL;
491 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200492 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
493 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
494 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200495 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
496 key = VTERM_KEY_RIGHT; break;
497 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
498 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200499 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200500 case K_S_UP: mod = VTERM_MOD_SHIFT;
501 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200502 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200503
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200504 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
505 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
506 case K_MOUSELEFT: /* TODO */ return 0;
507 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200508
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200509 case K_LEFTMOUSE:
510 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
511 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
512 case K_LEFTRELEASE:
513 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
514 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
515 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
516 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
517 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
518 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
519 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
520 case K_X1MOUSE: /* TODO */ return 0;
521 case K_X1DRAG: /* TODO */ return 0;
522 case K_X1RELEASE: /* TODO */ return 0;
523 case K_X2MOUSE: /* TODO */ return 0;
524 case K_X2DRAG: /* TODO */ return 0;
525 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200526
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200527 case K_IGNORE: return 0;
528 case K_NOP: return 0;
529 case K_UNDO: return 0;
530 case K_HELP: return 0;
531 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
532 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
533 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
534 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
535 case K_SELECT: return 0;
536#ifdef FEAT_GUI
537 case K_VER_SCROLLBAR: return 0;
538 case K_HOR_SCROLLBAR: return 0;
539#endif
540#ifdef FEAT_GUI_TABLINE
541 case K_TABLINE: return 0;
542 case K_TABMENU: return 0;
543#endif
544#ifdef FEAT_NETBEANS_INTG
545 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
546#endif
547#ifdef FEAT_DND
548 case K_DROP: return 0;
549#endif
550#ifdef FEAT_AUTOCMD
551 case K_CURSORHOLD: return 0;
552#endif
553 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
554 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200555 }
556
557 /*
558 * Convert special keys to vterm keys:
559 * - Write keys to vterm: vterm_keyboard_key()
560 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200561 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200562 */
563 if (key != VTERM_KEY_NONE)
564 /* Special key, let vterm convert it. */
565 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200566 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200567 /* Normal character, let vterm convert it. */
568 vterm_keyboard_unichar(vterm, c, mod);
569
570 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200571 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200572}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200573
Bram Moolenaar938783d2017-07-16 20:13:26 +0200574/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200575 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200576 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200577 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200578term_job_running(term_T *term)
579{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200580 /* Also consider the job finished when the channel is closed, to avoid a
581 * race condition when updating the title. */
582 return term->tl_job != NULL
583 && term->tl_job->jv_status == JOB_STARTED
584 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200585}
586
587/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200588 * Add the last line of the scrollback buffer to the buffer in the window.
589 */
590 static void
591add_scrollback_line_to_buffer(term_T *term)
592{
593 linenr_T lnum = term->tl_scrollback.ga_len - 1;
594 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
595 garray_T ga;
596 int c;
597 int col;
598 int i;
599
600 ga_init2(&ga, 1, 100);
601 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
602 {
603 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
604 goto failed;
605 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
606 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
607 (char_u *)ga.ga_data + ga.ga_len);
608 }
609 if (ga_grow(&ga, 1) == FAIL)
610 goto failed;
611 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
612 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
613
614 if (lnum == 0)
615 {
616 /* Delete the empty line that was in the empty buffer. */
617 curbuf = term->tl_buffer;
618 ml_delete(2, FALSE);
619 curbuf = curwin->w_buffer;
620 }
621
622failed:
623 ga_clear(&ga);
624}
625
626/*
627 * Add the current lines of the terminal to scrollback and to the buffer.
628 * Called after the job has ended and when switching to Terminal mode.
629 */
630 static void
631move_terminal_to_buffer(term_T *term)
632{
633 win_T *wp;
634 int len;
635 int lines_skipped = 0;
636 VTermPos pos;
637 VTermScreenCell cell;
638 VTermScreenCell *p;
639 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
640
641 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
642 {
643 len = 0;
644 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
645 if (vterm_screen_get_cell(screen, pos, &cell) != 0
646 && cell.chars[0] != NUL)
647 len = pos.col + 1;
648
649 if (len == 0)
650 ++lines_skipped;
651 else
652 {
653 while (lines_skipped > 0)
654 {
655 /* Line was skipped, add an empty line. */
656 --lines_skipped;
657 if (ga_grow(&term->tl_scrollback, 1) == OK)
658 {
659 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
660 + term->tl_scrollback.ga_len;
661
662 line->sb_cols = 0;
663 line->sb_cells = NULL;
664 ++term->tl_scrollback.ga_len;
665
666 add_scrollback_line_to_buffer(term);
667 }
668 }
669
670 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
671 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
672 {
673 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
674 + term->tl_scrollback.ga_len;
675
676 for (pos.col = 0; pos.col < len; ++pos.col)
677 {
678 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
679 vim_memset(p + pos.col, 0, sizeof(cell));
680 else
681 p[pos.col] = cell;
682 }
683 line->sb_cols = len;
684 line->sb_cells = p;
685 ++term->tl_scrollback.ga_len;
686
687 add_scrollback_line_to_buffer(term);
688 }
689 else
690 vim_free(p);
691 }
692 }
693
694 FOR_ALL_WINDOWS(wp)
695 {
696 if (wp->w_buffer == term->tl_buffer)
697 {
698 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
699 wp->w_cursor.col = 0;
700 wp->w_valid = 0;
701 redraw_win_later(wp, NOT_VALID);
702 }
703 }
704}
705
706 static void
707set_terminal_mode(term_T *term, int on)
708{
709 term->tl_terminal_mode = on;
710 vim_free(term->tl_status_text);
711 term->tl_status_text = NULL;
712 if (term->tl_buffer == curbuf)
713 maketitle();
714}
715
716/*
717 * Called after the job if finished and Terminal mode is not active:
718 * Move the vterm contents into the scrollback buffer and free the vterm.
719 */
720 static void
721cleanup_vterm(term_T *term)
722{
723 move_terminal_to_buffer(term);
724 term_free_vterm(term);
725 set_terminal_mode(term, FALSE);
726}
727
728/*
729 * Switch from sending keys to the job to Terminal-Normal mode.
730 * Suspends updating the terminal window.
731 */
732 static void
733term_enter_terminal_mode()
734{
735 term_T *term = curbuf->b_term;
736
737 /* Append the current terminal contents to the buffer. */
738 move_terminal_to_buffer(term);
739
740 set_terminal_mode(term, TRUE);
741}
742
743/*
744 * Returns TRUE if the current window contains a terminal and we are in
745 * Terminal-Normal mode.
746 */
747 int
748term_in_terminal_mode()
749{
750 term_T *term = curbuf->b_term;
751
752 return term != NULL && term->tl_terminal_mode;
753}
754
755/*
756 * Switch from Terminal-Normal mode to sending keys to the job.
757 * Restores updating the terminal window.
758 */
759 void
760term_leave_terminal_mode()
761{
762 term_T *term = curbuf->b_term;
763 sb_line_T *line;
764 garray_T *gap;
765
766 /* Remove the terminal contents from the scrollback and the buffer. */
767 gap = &term->tl_scrollback;
768 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
769 {
770 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
771 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
772 vim_free(line->sb_cells);
773 --gap->ga_len;
774 if (gap->ga_len == 0)
775 break;
776 }
777 check_cursor();
778
779 set_terminal_mode(term, FALSE);
780
781 if (term->tl_channel_closed)
782 cleanup_vterm(term);
783 redraw_buf_and_status_later(curbuf, NOT_VALID);
784}
785
786/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200787 * Get a key from the user without mapping.
788 * TODO: use terminal mode mappings.
789 */
790 static int
791term_vgetc()
792{
793 int c;
794
795 ++no_mapping;
796 ++allow_keys;
797 got_int = FALSE;
798 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200799 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200800 --no_mapping;
801 --allow_keys;
802 return c;
803}
804
805/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200806 * Send keys to terminal.
807 */
808 static int
809send_keys_to_term(term_T *term, int c, int typed)
810{
811 char msg[KEY_BUF_LEN];
812 size_t len;
813 static int mouse_was_outside = FALSE;
814 int dragging_outside = FALSE;
815
816 /* Catch keys that need to be handled as in Normal mode. */
817 switch (c)
818 {
819 case NUL:
820 case K_ZERO:
821 if (typed)
822 stuffcharReadbuff(c);
823 return FAIL;
824
825 case K_IGNORE:
826 return FAIL;
827
828 case K_LEFTDRAG:
829 case K_MIDDLEDRAG:
830 case K_RIGHTDRAG:
831 case K_X1DRAG:
832 case K_X2DRAG:
833 dragging_outside = mouse_was_outside;
834 /* FALLTHROUGH */
835 case K_LEFTMOUSE:
836 case K_LEFTMOUSE_NM:
837 case K_LEFTRELEASE:
838 case K_LEFTRELEASE_NM:
839 case K_MIDDLEMOUSE:
840 case K_MIDDLERELEASE:
841 case K_RIGHTMOUSE:
842 case K_RIGHTRELEASE:
843 case K_X1MOUSE:
844 case K_X1RELEASE:
845 case K_X2MOUSE:
846 case K_X2RELEASE:
847 if (mouse_row < W_WINROW(curwin)
848 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
849 || mouse_col < W_WINCOL(curwin)
850 || mouse_col >= W_ENDCOL(curwin)
851 || dragging_outside)
852 {
853 /* click outside the current window */
854 if (typed)
855 {
856 stuffcharReadbuff(c);
857 mouse_was_outside = TRUE;
858 }
859 return FAIL;
860 }
861 }
862 if (typed)
863 mouse_was_outside = FALSE;
864
865 /* Convert the typed key to a sequence of bytes for the job. */
866 len = term_convert_key(term, c, msg);
867 if (len > 0)
868 /* TODO: if FAIL is returned, stop? */
869 channel_send(term->tl_job->jv_channel, PART_IN,
870 (char_u *)msg, (int)len, NULL);
871
872 return OK;
873}
874
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200875 static void
876position_cursor(win_T *wp, VTermPos *pos)
877{
878 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
879 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
880 wp->w_valid |= (VALID_WCOL|VALID_WROW);
881}
882
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200883/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200884 * Handle CTRL-W "": send register contents to the job.
885 */
886 static void
887term_paste_register(int prev_c UNUSED)
888{
889 int c;
890 list_T *l;
891 listitem_T *item;
892 long reglen = 0;
893 int type;
894
895#ifdef FEAT_CMDL_INFO
896 if (add_to_showcmd(prev_c))
897 if (add_to_showcmd('"'))
898 out_flush();
899#endif
900 c = term_vgetc();
901#ifdef FEAT_CMDL_INFO
902 clear_showcmd();
903#endif
904
905 /* CTRL-W "= prompt for expression to evaluate. */
906 if (c == '=' && get_expr_register() != '=')
907 return;
908
909 l = (list_T *)get_reg_contents(c, GREG_LIST);
910 if (l != NULL)
911 {
912 type = get_reg_type(c, &reglen);
913 for (item = l->lv_first; item != NULL; item = item->li_next)
914 {
915 char_u *s = get_tv_string(&item->li_tv);
916
917 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
918 s, STRLEN(s), NULL);
919 if (item->li_next != NULL || type == MLINE)
920 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
921 (char_u *)"\r", 1, NULL);
922 }
923 list_free(l);
924 }
925}
926
927/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200928 * Returns TRUE if the current window contains a terminal and we are sending
929 * keys to the job.
930 */
931 int
932term_use_loop()
933{
934 term_T *term = curbuf->b_term;
935
936 return term != NULL
937 && !term->tl_terminal_mode
938 && term->tl_vterm != NULL
939 && term_job_running(term);
940}
941
942/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200943 * Wait for input and send it to the job.
944 * Return when the start of a CTRL-W command is typed or anything else that
945 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200946 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
947 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200948 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200949 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200950terminal_loop(void)
951{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200952 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200953 int termkey = 0;
954
955 if (*curwin->w_p_tk != NUL)
956 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200957 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200958
959 for (;;)
960 {
961 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200962 /* Repeat redrawing in case a message is received while redrawing. */
963 while (curwin->w_redr_type != 0)
964 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200965 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200966
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200967 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200968 if (curbuf->b_term->tl_vterm == NULL
969 || !term_job_running(curbuf->b_term))
970 /* job finished while waiting for a character */
971 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200972
Bram Moolenaarfae42832017-08-01 22:24:26 +0200973#ifdef UNIX
974 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
975#endif
976#ifdef WIN3264
977 if (c == Ctrl_C)
978 /* We don't know if the job can handle CTRL-C itself or not, this
979 * may kill the shell instead of killing the command running in the
980 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +0200981 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +0200982#endif
983
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200984 if (c == (termkey == 0 ? Ctrl_W : termkey))
985 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200986 int prev_c = c;
987
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200988#ifdef FEAT_CMDL_INFO
989 if (add_to_showcmd(c))
990 out_flush();
991#endif
992 c = term_vgetc();
993#ifdef FEAT_CMDL_INFO
994 clear_showcmd();
995#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200996 if (curbuf->b_term->tl_vterm == NULL
997 || !term_job_running(curbuf->b_term))
998 /* job finished while waiting for a character */
999 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001000
1001 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001002 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001003 /* "CTRL-W .": send CTRL-W to the job */
1004 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001005 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001006 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001007 {
1008 term_enter_terminal_mode();
1009 return FAIL;
1010 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001011 else if (c == '"')
1012 {
1013 term_paste_register(prev_c);
1014 continue;
1015 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001016 else if (termkey == 0 || c != termkey)
1017 {
1018 stuffcharReadbuff(Ctrl_W);
1019 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001020 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001021 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001022 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001023 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1024 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001025 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001026 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001027}
1028
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001029/*
1030 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001031 * This updates the title and status, but does not close the vter, because
1032 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001033 */
1034 void
1035term_job_ended(job_T *job)
1036{
1037 term_T *term;
1038 int did_one = FALSE;
1039
1040 for (term = first_term; term != NULL; term = term->tl_next)
1041 if (term->tl_job == job)
1042 {
1043 vim_free(term->tl_title);
1044 term->tl_title = NULL;
1045 vim_free(term->tl_status_text);
1046 term->tl_status_text = NULL;
1047 redraw_buf_and_status_later(term->tl_buffer, VALID);
1048 did_one = TRUE;
1049 }
1050 if (did_one)
1051 redraw_statuslines();
1052 if (curbuf->b_term != NULL)
1053 {
1054 if (curbuf->b_term->tl_job == job)
1055 maketitle();
1056 update_cursor(curbuf->b_term, TRUE);
1057 }
1058}
1059
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001060 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001061may_toggle_cursor(term_T *term)
1062{
1063 if (curbuf == term->tl_buffer)
1064 {
1065 if (term->tl_cursor_visible)
1066 cursor_on();
1067 else
1068 cursor_off();
1069 }
1070}
1071
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001072 static int
1073handle_damage(VTermRect rect, void *user)
1074{
1075 term_T *term = (term_T *)user;
1076
1077 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1078 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1079 redraw_buf_later(term->tl_buffer, NOT_VALID);
1080 return 1;
1081}
1082
1083 static int
1084handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1085{
1086 term_T *term = (term_T *)user;
1087
1088 /* TODO */
1089 redraw_buf_later(term->tl_buffer, NOT_VALID);
1090 return 1;
1091}
1092
1093 static int
1094handle_movecursor(
1095 VTermPos pos,
1096 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001097 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001098 void *user)
1099{
1100 term_T *term = (term_T *)user;
1101 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001102
1103 term->tl_cursor_pos = pos;
1104 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001105
1106 FOR_ALL_WINDOWS(wp)
1107 {
1108 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001109 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001110 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001111 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001112 {
1113 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001114 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001115 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001116
1117 return 1;
1118}
1119
Bram Moolenaar21554412017-07-24 21:44:43 +02001120 static int
1121handle_settermprop(
1122 VTermProp prop,
1123 VTermValue *value,
1124 void *user)
1125{
1126 term_T *term = (term_T *)user;
1127
1128 switch (prop)
1129 {
1130 case VTERM_PROP_TITLE:
1131 vim_free(term->tl_title);
1132 term->tl_title = vim_strsave((char_u *)value->string);
1133 vim_free(term->tl_status_text);
1134 term->tl_status_text = NULL;
1135 if (term == curbuf->b_term)
1136 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001137 break;
1138
1139 case VTERM_PROP_CURSORVISIBLE:
1140 term->tl_cursor_visible = value->boolean;
1141 may_toggle_cursor(term);
1142 out_flush();
1143 break;
1144
Bram Moolenaar21554412017-07-24 21:44:43 +02001145 default:
1146 break;
1147 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001148 /* Always return 1, otherwise vterm doesn't store the value internally. */
1149 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001150}
1151
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001152/*
1153 * The job running in the terminal resized the terminal.
1154 */
1155 static int
1156handle_resize(int rows, int cols, void *user)
1157{
1158 term_T *term = (term_T *)user;
1159 win_T *wp;
1160
1161 term->tl_rows = rows;
1162 term->tl_cols = cols;
1163 FOR_ALL_WINDOWS(wp)
1164 {
1165 if (wp->w_buffer == term->tl_buffer)
1166 {
1167 win_setheight_win(rows, wp);
1168 win_setwidth_win(cols, wp);
1169 }
1170 }
1171
1172 redraw_buf_later(term->tl_buffer, NOT_VALID);
1173 return 1;
1174}
1175
Bram Moolenaard85f2712017-07-28 21:51:57 +02001176/*
1177 * Handle a line that is pushed off the top of the screen.
1178 */
1179 static int
1180handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1181{
1182 term_T *term = (term_T *)user;
1183
1184 /* TODO: Limit the number of lines that are stored. */
1185 /* TODO: put the text in the buffer. */
1186 if (ga_grow(&term->tl_scrollback, 1) == OK)
1187 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001188 VTermScreenCell *p = NULL;
1189 int len = 0;
1190 int i;
1191 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001192
1193 /* do not store empty cells at the end */
1194 for (i = 0; i < cols; ++i)
1195 if (cells[i].chars[0] != 0)
1196 len = i + 1;
1197
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001198 if (len > 0)
1199 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001200 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001201 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001202
1203 line = (sb_line_T *)term->tl_scrollback.ga_data
1204 + term->tl_scrollback.ga_len;
1205 line->sb_cols = len;
1206 line->sb_cells = p;
1207 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001208 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001209
1210 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001211 }
1212 return 0; /* ignored */
1213}
1214
Bram Moolenaar21554412017-07-24 21:44:43 +02001215static VTermScreenCallbacks screen_callbacks = {
1216 handle_damage, /* damage */
1217 handle_moverect, /* moverect */
1218 handle_movecursor, /* movecursor */
1219 handle_settermprop, /* settermprop */
1220 NULL, /* bell */
1221 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001222 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001223 NULL /* sb_popline */
1224};
1225
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001226/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001227 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001228 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001229 */
1230 void
1231term_channel_closed(channel_T *ch)
1232{
1233 term_T *term;
1234 int did_one = FALSE;
1235
1236 for (term = first_term; term != NULL; term = term->tl_next)
1237 if (term->tl_job == ch->ch_job)
1238 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001239 term->tl_channel_closed = TRUE;
1240
Bram Moolenaard85f2712017-07-28 21:51:57 +02001241 vim_free(term->tl_title);
1242 term->tl_title = NULL;
1243 vim_free(term->tl_status_text);
1244 term->tl_status_text = NULL;
1245
Bram Moolenaar423802d2017-07-30 16:52:24 +02001246 /* Unless in Terminal-Normal mode: clear the vterm. */
1247 if (!term->tl_terminal_mode)
1248 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001249
1250 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1251 did_one = TRUE;
1252 }
1253 if (did_one)
1254 {
1255 redraw_statuslines();
1256
1257 /* Need to break out of vgetc(). */
1258 ins_char_typebuf(K_IGNORE);
1259
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001260 term = curbuf->b_term;
1261 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001262 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001263 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001264 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001265 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001266 }
1267 }
1268}
1269
1270/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001271 * Reverse engineer the RGB value into a cterm color index.
1272 * First color is 1. Return 0 if no match found.
1273 */
1274 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001275color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001276{
1277 int red = color->red;
1278 int blue = color->blue;
1279 int green = color->green;
1280
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001281 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001282 if (red == 0)
1283 {
1284 if (green == 0)
1285 {
1286 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001287 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001288 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001289 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001290 }
1291 else if (green == 224)
1292 {
1293 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001294 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001295 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001296 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001297 }
1298 }
1299 else if (red == 224)
1300 {
1301 if (green == 0)
1302 {
1303 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001304 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001305 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001306 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001307 }
1308 else if (green == 224)
1309 {
1310 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001311 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001312 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001313 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001314 }
1315 }
1316 else if (red == 128)
1317 {
1318 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001319 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001320 }
1321 else if (red == 255)
1322 {
1323 if (green == 64)
1324 {
1325 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001326 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001327 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001328 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001329 }
1330 else if (green == 255)
1331 {
1332 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001333 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001334 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001335 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001336 }
1337 }
1338 else if (red == 64)
1339 {
1340 if (green == 64)
1341 {
1342 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001343 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001344 }
1345 else if (green == 255)
1346 {
1347 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001348 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001349 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001350 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001351 }
1352 }
1353 if (t_colors >= 256)
1354 {
1355 if (red == blue && red == green)
1356 {
1357 /* 24-color greyscale */
1358 static int cutoff[23] = {
1359 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1360 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1361 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1362 int i;
1363
1364 for (i = 0; i < 23; ++i)
1365 if (red < cutoff[i])
1366 return i + 233;
1367 return 256;
1368 }
1369
1370 /* 216-color cube */
1371 return 17 + ((red + 25) / 0x33) * 36
1372 + ((green + 25) / 0x33) * 6
1373 + (blue + 25) / 0x33;
1374 }
1375 return 0;
1376}
1377
1378/*
1379 * Convert the attributes of a vterm cell into an attribute index.
1380 */
1381 static int
1382cell2attr(VTermScreenCell *cell)
1383{
1384 int attr = 0;
1385
1386 if (cell->attrs.bold)
1387 attr |= HL_BOLD;
1388 if (cell->attrs.underline)
1389 attr |= HL_UNDERLINE;
1390 if (cell->attrs.italic)
1391 attr |= HL_ITALIC;
1392 if (cell->attrs.strike)
1393 attr |= HL_STANDOUT;
1394 if (cell->attrs.reverse)
1395 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001396
1397#ifdef FEAT_GUI
1398 if (gui.in_use)
1399 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001400 guicolor_T fg, bg;
1401
1402 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1403 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1404 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001405 }
1406 else
1407#endif
1408#ifdef FEAT_TERMGUICOLORS
1409 if (p_tgc)
1410 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001411 guicolor_T fg, bg;
1412
1413 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1414 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1415
1416 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001417 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001418 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001419#endif
1420 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001421 int bold = MAYBE;
1422 int fg = color2index(&cell->fg, TRUE, &bold);
1423 int bg = color2index(&cell->bg, FALSE, &bold);
1424
1425 /* with 8 colors set the bold attribute to get a bright foreground */
1426 if (bold == TRUE)
1427 attr |= HL_BOLD;
1428 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001429 }
1430 return 0;
1431}
1432
1433/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001434 * Called to update the window that contains a terminal.
1435 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001436 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001437 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001438term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001439{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001440 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001441 VTerm *vterm;
1442 VTermScreen *screen;
1443 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001444 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001445
Bram Moolenaar423802d2017-07-30 16:52:24 +02001446 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001447 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001448
Bram Moolenaard85f2712017-07-28 21:51:57 +02001449 vterm = term->tl_vterm;
1450 screen = vterm_obtain_screen(vterm);
1451 state = vterm_obtain_state(vterm);
1452
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001453 /*
1454 * If the window was resized a redraw will be triggered and we get here.
1455 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1456 */
1457 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1458 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001459 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001460 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1461 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1462 win_T *twp;
1463
1464 FOR_ALL_WINDOWS(twp)
1465 {
1466 /* When more than one window shows the same terminal, use the
1467 * smallest size. */
1468 if (twp->w_buffer == term->tl_buffer)
1469 {
1470 if (!term->tl_rows_fixed && rows > twp->w_height)
1471 rows = twp->w_height;
1472 if (!term->tl_cols_fixed && cols > twp->w_width)
1473 cols = twp->w_width;
1474 }
1475 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001476
1477 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001478 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001479 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001480 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001481 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001482
1483 /* The cursor may have been moved when resizing. */
1484 vterm_state_get_cursorpos(state, &pos);
1485 position_cursor(wp, &pos);
1486
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001487 /* TODO: Only redraw what changed. */
1488 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001489 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001490 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001491 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001492
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001493 if (pos.row < term->tl_rows)
1494 {
1495 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001496 {
1497 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001498 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001499
Bram Moolenaareeac6772017-07-23 15:48:37 +02001500 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1501 vim_memset(&cell, 0, sizeof(cell));
1502
1503 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001504 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001505 if (c == NUL)
1506 {
1507 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001508#if defined(FEAT_MBYTE)
1509 if (enc_utf8)
1510 ScreenLinesUC[off] = NUL;
1511#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001512 }
1513 else
1514 {
1515#if defined(FEAT_MBYTE)
1516 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001517 {
1518 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001519 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001520 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001521 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001522 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001523 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001524 if (enc_utf8)
1525 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001526 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001527#else
1528 ScreenLines[off] = c;
1529#endif
1530 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001531 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001532
1533 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001534 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001535 if (cell.width == 2)
1536 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001537 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001538#if defined(FEAT_MBYTE)
1539 if (enc_utf8)
1540 ScreenLinesUC[off] = NUL;
1541#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001542 ++pos.col;
1543 ++off;
1544 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001545 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001546 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001547 else
1548 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001549
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001550 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1551 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001552 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001553
1554 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001555}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001556
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001557/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001558 * Return TRUE if "wp" is a terminal window where the job has finished.
1559 */
1560 int
1561term_is_finished(buf_T *buf)
1562{
1563 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1564}
1565
1566/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001567 * Return TRUE if "wp" is a terminal window where the job has finished or we
1568 * are in Terminal-Normal mode.
1569 */
1570 int
1571term_show_buffer(buf_T *buf)
1572{
1573 term_T *term = buf->b_term;
1574
1575 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1576}
1577
1578/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001579 * The current buffer is going to be changed. If there is terminal
1580 * highlighting remove it now.
1581 */
1582 void
1583term_change_in_curbuf(void)
1584{
1585 term_T *term = curbuf->b_term;
1586
1587 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1588 {
1589 free_scrollback(term);
1590 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001591
1592 /* The buffer is now like a normal buffer, it cannot be easily
1593 * abandoned when changed. */
1594 set_string_option_direct((char_u *)"buftype", -1,
1595 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001596 }
1597}
1598
1599/*
1600 * Get the screen attribute for a position in the buffer.
1601 */
1602 int
1603term_get_attr(buf_T *buf, linenr_T lnum, int col)
1604{
1605 term_T *term = buf->b_term;
1606 sb_line_T *line;
1607
Bram Moolenaar70229f92017-07-29 16:01:53 +02001608 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001609 return 0;
1610 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1611 if (col >= line->sb_cols)
1612 return 0;
1613 return cell2attr(line->sb_cells + col);
1614}
1615
1616/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001617 * Set job options common for Unix and MS-Windows.
1618 */
1619 static void
1620setup_job_options(jobopt_T *opt, int rows, int cols)
1621{
1622 clear_job_options(opt);
1623 opt->jo_mode = MODE_RAW;
1624 opt->jo_out_mode = MODE_RAW;
1625 opt->jo_err_mode = MODE_RAW;
1626 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001627
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001628 opt->jo_io[PART_OUT] = JIO_BUFFER;
1629 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001630 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1631
1632 opt->jo_modifiable[PART_OUT] = 0;
1633 opt->jo_modifiable[PART_ERR] = 0;
1634 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1635
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001636 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1637 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001638 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001639 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1640
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001641 opt->jo_term_rows = rows;
1642 opt->jo_term_cols = cols;
1643}
1644
1645/*
1646 * Create a new vterm and initialize it.
1647 */
1648 static void
1649create_vterm(term_T *term, int rows, int cols)
1650{
1651 VTerm *vterm;
1652 VTermScreen *screen;
1653
1654 vterm = vterm_new(rows, cols);
1655 term->tl_vterm = vterm;
1656 screen = vterm_obtain_screen(vterm);
1657 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1658 /* TODO: depends on 'encoding'. */
1659 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001660
1661 /* Vterm uses a default black background. Set it to white when
1662 * 'background' is "light". */
1663 if (*p_bg == 'l')
1664 {
1665 VTermColor fg, bg;
1666
1667 fg.red = fg.green = fg.blue = 0;
1668 bg.red = bg.green = bg.blue = 255;
1669 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1670 }
1671
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001672 /* Required to initialize most things. */
1673 vterm_screen_reset(screen, 1 /* hard */);
1674}
1675
Bram Moolenaar21554412017-07-24 21:44:43 +02001676/*
1677 * Return the text to show for the buffer name and status.
1678 */
1679 char_u *
1680term_get_status_text(term_T *term)
1681{
1682 if (term->tl_status_text == NULL)
1683 {
1684 char_u *txt;
1685 size_t len;
1686
Bram Moolenaar423802d2017-07-30 16:52:24 +02001687 if (term->tl_terminal_mode)
1688 {
1689 if (term_job_running(term))
1690 txt = (char_u *)_("Terminal");
1691 else
1692 txt = (char_u *)_("Terminal-finished");
1693 }
1694 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001695 txt = term->tl_title;
1696 else if (term_job_running(term))
1697 txt = (char_u *)_("running");
1698 else
1699 txt = (char_u *)_("finished");
1700 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001701 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001702 if (term->tl_status_text != NULL)
1703 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1704 term->tl_buffer->b_fname, txt);
1705 }
1706 return term->tl_status_text;
1707}
1708
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001709/*
1710 * Mark references in jobs of terminals.
1711 */
1712 int
1713set_ref_in_term(int copyID)
1714{
1715 int abort = FALSE;
1716 term_T *term;
1717 typval_T tv;
1718
1719 for (term = first_term; term != NULL; term = term->tl_next)
1720 if (term->tl_job != NULL)
1721 {
1722 tv.v_type = VAR_JOB;
1723 tv.vval.v_job = term->tl_job;
1724 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1725 }
1726 return abort;
1727}
1728
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001729/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001730 * Get the buffer from the first argument in "argvars".
1731 * Returns NULL when the buffer is not for a terminal window.
1732 */
1733 static buf_T *
1734term_get_buf(typval_T *argvars)
1735{
1736 buf_T *buf;
1737
1738 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1739 ++emsg_off;
1740 buf = get_buf_tv(&argvars[0], FALSE);
1741 --emsg_off;
1742 if (buf == NULL || buf->b_term == NULL)
1743 return NULL;
1744 return buf;
1745}
1746
1747/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001748 * "term_getattr(attr, name)" function
1749 */
1750 void
1751f_term_getattr(typval_T *argvars, typval_T *rettv)
1752{
1753 int attr;
1754 size_t i;
1755 char_u *name;
1756
1757 static struct {
1758 char *name;
1759 int attr;
1760 } attrs[] = {
1761 {"bold", HL_BOLD},
1762 {"italic", HL_ITALIC},
1763 {"underline", HL_UNDERLINE},
1764 {"strike", HL_STANDOUT},
1765 {"reverse", HL_INVERSE},
1766 };
1767
1768 attr = get_tv_number(&argvars[0]);
1769 name = get_tv_string_chk(&argvars[1]);
1770 if (name == NULL)
1771 return;
1772
1773 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1774 if (STRCMP(name, attrs[i].name) == 0)
1775 {
1776 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1777 break;
1778 }
1779}
1780
1781/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001782 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001783 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001784 void
1785f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001786{
Bram Moolenaar97870002017-07-30 18:28:38 +02001787 buf_T *buf = term_get_buf(argvars);
1788 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001789
Bram Moolenaar97870002017-07-30 18:28:38 +02001790 if (rettv_list_alloc(rettv) == FAIL)
1791 return;
1792 if (buf == NULL)
1793 return;
1794
1795 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001796 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1797 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001798 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001799}
1800
1801/*
1802 * "term_getjob(buf)" function
1803 */
1804 void
1805f_term_getjob(typval_T *argvars, typval_T *rettv)
1806{
1807 buf_T *buf = term_get_buf(argvars);
1808
1809 rettv->v_type = VAR_JOB;
1810 rettv->vval.v_job = NULL;
1811 if (buf == NULL)
1812 return;
1813
1814 rettv->vval.v_job = buf->b_term->tl_job;
1815 if (rettv->vval.v_job != NULL)
1816 ++rettv->vval.v_job->jv_refcount;
1817}
1818
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001819 static int
1820get_row_number(typval_T *tv, term_T *term)
1821{
1822 if (tv->v_type == VAR_STRING
1823 && tv->vval.v_string != NULL
1824 && STRCMP(tv->vval.v_string, ".") == 0)
1825 return term->tl_cursor_pos.row;
1826 return (int)get_tv_number(tv) - 1;
1827}
1828
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001829/*
1830 * "term_getline(buf, row)" function
1831 */
1832 void
1833f_term_getline(typval_T *argvars, typval_T *rettv)
1834{
1835 buf_T *buf = term_get_buf(argvars);
1836 term_T *term;
1837 int row;
1838
1839 rettv->v_type = VAR_STRING;
1840 if (buf == NULL)
1841 return;
1842 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001843 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001844
1845 if (term->tl_vterm == NULL)
1846 {
1847 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1848
1849 /* vterm is finished, get the text from the buffer */
1850 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1851 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1852 }
1853 else
1854 {
1855 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1856 VTermRect rect;
1857 int len;
1858 char_u *p;
1859
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001860 if (row < 0 || row >= term->tl_rows)
1861 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001862 len = term->tl_cols * MB_MAXBYTES + 1;
1863 p = alloc(len);
1864 if (p == NULL)
1865 return;
1866 rettv->vval.v_string = p;
1867
1868 rect.start_col = 0;
1869 rect.end_col = term->tl_cols;
1870 rect.start_row = row;
1871 rect.end_row = row + 1;
1872 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1873 }
1874}
1875
1876/*
1877 * "term_getsize(buf)" function
1878 */
1879 void
1880f_term_getsize(typval_T *argvars, typval_T *rettv)
1881{
1882 buf_T *buf = term_get_buf(argvars);
1883 list_T *l;
1884
1885 if (rettv_list_alloc(rettv) == FAIL)
1886 return;
1887 if (buf == NULL)
1888 return;
1889
1890 l = rettv->vval.v_list;
1891 list_append_number(l, buf->b_term->tl_rows);
1892 list_append_number(l, buf->b_term->tl_cols);
1893}
1894
1895/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001896 * "term_getstatus(buf)" function
1897 */
1898 void
1899f_term_getstatus(typval_T *argvars, typval_T *rettv)
1900{
1901 buf_T *buf = term_get_buf(argvars);
1902 term_T *term;
1903 char_u val[100];
1904
1905 rettv->v_type = VAR_STRING;
1906 if (buf == NULL)
1907 return;
1908 term = buf->b_term;
1909
1910 if (term_job_running(term))
1911 STRCPY(val, "running");
1912 else
1913 STRCPY(val, "finished");
1914 if (term->tl_terminal_mode)
1915 STRCAT(val, ",terminal");
1916 rettv->vval.v_string = vim_strsave(val);
1917}
1918
1919/*
1920 * "term_gettitle(buf)" function
1921 */
1922 void
1923f_term_gettitle(typval_T *argvars, typval_T *rettv)
1924{
1925 buf_T *buf = term_get_buf(argvars);
1926
1927 rettv->v_type = VAR_STRING;
1928 if (buf == NULL)
1929 return;
1930
1931 if (buf->b_term->tl_title != NULL)
1932 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1933}
1934
1935/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001936 * "term_gettty(buf)" function
1937 */
1938 void
1939f_term_gettty(typval_T *argvars, typval_T *rettv)
1940{
1941 buf_T *buf = term_get_buf(argvars);
1942 char_u *p;
1943
1944 rettv->v_type = VAR_STRING;
1945 if (buf == NULL)
1946 return;
1947 if (buf->b_term->tl_job != NULL)
1948 p = buf->b_term->tl_job->jv_tty_name;
1949 else
1950 p = buf->b_term->tl_tty_name;
1951 if (p != NULL)
1952 rettv->vval.v_string = vim_strsave(p);
1953}
1954
1955/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001956 * "term_list()" function
1957 */
1958 void
1959f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1960{
1961 term_T *tp;
1962 list_T *l;
1963
1964 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1965 return;
1966
1967 l = rettv->vval.v_list;
1968 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1969 if (tp != NULL && tp->tl_buffer != NULL)
1970 if (list_append_number(l,
1971 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1972 return;
1973}
1974
1975/*
1976 * "term_scrape(buf, row)" function
1977 */
1978 void
1979f_term_scrape(typval_T *argvars, typval_T *rettv)
1980{
1981 buf_T *buf = term_get_buf(argvars);
1982 VTermScreen *screen = NULL;
1983 VTermPos pos;
1984 list_T *l;
1985 term_T *term;
1986
1987 if (rettv_list_alloc(rettv) == FAIL)
1988 return;
1989 if (buf == NULL)
1990 return;
1991 term = buf->b_term;
1992 if (term->tl_vterm != NULL)
1993 screen = vterm_obtain_screen(term->tl_vterm);
1994
1995 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001996 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001997 for (pos.col = 0; pos.col < term->tl_cols; )
1998 {
1999 dict_T *dcell;
2000 VTermScreenCell cell;
2001 char_u rgb[8];
2002 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2003 int off = 0;
2004 int i;
2005
2006 if (screen == NULL)
2007 {
2008 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2009 sb_line_T *line;
2010
2011 /* vterm has finished, get the cell from scrollback */
2012 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2013 break;
2014 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2015 if (pos.col >= line->sb_cols)
2016 break;
2017 cell = line->sb_cells[pos.col];
2018 }
2019 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2020 break;
2021 dcell = dict_alloc();
2022 list_append_dict(l, dcell);
2023
2024 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2025 {
2026 if (cell.chars[i] == 0)
2027 break;
2028 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2029 }
2030 mbs[off] = NUL;
2031 dict_add_nr_str(dcell, "chars", 0, mbs);
2032
2033 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2034 cell.fg.red, cell.fg.green, cell.fg.blue);
2035 dict_add_nr_str(dcell, "fg", 0, rgb);
2036 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2037 cell.bg.red, cell.bg.green, cell.bg.blue);
2038 dict_add_nr_str(dcell, "bg", 0, rgb);
2039
2040 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2041 dict_add_nr_str(dcell, "width", cell.width, NULL);
2042
2043 ++pos.col;
2044 if (cell.width == 2)
2045 ++pos.col;
2046 }
2047}
2048
2049/*
2050 * "term_sendkeys(buf, keys)" function
2051 */
2052 void
2053f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2054{
2055 buf_T *buf = term_get_buf(argvars);
2056 char_u *msg;
2057 term_T *term;
2058
2059 rettv->v_type = VAR_UNKNOWN;
2060 if (buf == NULL)
2061 return;
2062
2063 msg = get_tv_string_chk(&argvars[1]);
2064 if (msg == NULL)
2065 return;
2066 term = buf->b_term;
2067 if (term->tl_vterm == NULL)
2068 return;
2069
2070 while (*msg != NUL)
2071 {
2072 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2073 msg += MB_PTR2LEN(msg);
2074 }
2075
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002076 if (!term->tl_terminal_mode)
2077 {
2078 /* TODO: only update once in a while. */
2079 update_screen(0);
2080 if (buf == curbuf)
2081 update_cursor(term, TRUE);
2082 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002083}
2084
2085/*
2086 * "term_start(command, options)" function
2087 */
2088 void
2089f_term_start(typval_T *argvars, typval_T *rettv)
2090{
2091 char_u *cmd = get_tv_string_chk(&argvars[0]);
2092 exarg_T ea;
2093
2094 if (cmd == NULL)
2095 return;
2096 ea.arg = cmd;
2097 ex_terminal(&ea);
2098
2099 if (curbuf->b_term != NULL)
2100 rettv->vval.v_number = curbuf->b_fnum;
2101}
2102
2103/*
2104 * "term_wait" function
2105 */
2106 void
2107f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2108{
2109 buf_T *buf = term_get_buf(argvars);
2110
2111 if (buf == NULL)
2112 return;
2113
2114 /* Get the job status, this will detect a job that finished. */
2115 if (buf->b_term->tl_job != NULL)
2116 (void)job_status(buf->b_term->tl_job);
2117
2118 /* Check for any pending channel I/O. */
2119 vpeekc_any();
2120 ui_delay(10L, FALSE);
2121
2122 /* Flushing messages on channels is hopefully sufficient.
2123 * TODO: is there a better way? */
2124 parse_queued_messages();
2125}
2126
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002127# ifdef WIN3264
2128
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002129/**************************************
2130 * 2. MS-Windows implementation.
2131 */
2132
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002133#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2134#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2135
Bram Moolenaar8a773062017-07-24 22:29:21 +02002136void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002137void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002138void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002139BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2140void (*winpty_config_set_initial_size)(void*, int, int);
2141LPCWSTR (*winpty_conin_name)(void*);
2142LPCWSTR (*winpty_conout_name)(void*);
2143LPCWSTR (*winpty_conerr_name)(void*);
2144void (*winpty_free)(void*);
2145void (*winpty_config_free)(void*);
2146void (*winpty_spawn_config_free)(void*);
2147void (*winpty_error_free)(void*);
2148LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002149BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002150
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002151#define WINPTY_DLL "winpty.dll"
2152
2153static HINSTANCE hWinPtyDLL = NULL;
2154
2155 int
2156dyn_winpty_init(void)
2157{
2158 int i;
2159 static struct
2160 {
2161 char *name;
2162 FARPROC *ptr;
2163 } winpty_entry[] =
2164 {
2165 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2166 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2167 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2168 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2169 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2170 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2171 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2172 {"winpty_free", (FARPROC*)&winpty_free},
2173 {"winpty_open", (FARPROC*)&winpty_open},
2174 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2175 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2176 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2177 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002178 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002179 {NULL, NULL}
2180 };
2181
2182 /* No need to initialize twice. */
2183 if (hWinPtyDLL)
2184 return 1;
2185 /* Load winpty.dll */
2186 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2187 if (!hWinPtyDLL)
2188 {
2189 EMSG2(_(e_loadlib), WINPTY_DLL);
2190 return 0;
2191 }
2192 for (i = 0; winpty_entry[i].name != NULL
2193 && winpty_entry[i].ptr != NULL; ++i)
2194 {
2195 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2196 winpty_entry[i].name)) == NULL)
2197 {
2198 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2199 return 0;
2200 }
2201 }
2202
2203 return 1;
2204}
2205
2206/*
2207 * Create a new terminal of "rows" by "cols" cells.
2208 * Store a reference in "term".
2209 * Return OK or FAIL.
2210 */
2211 static int
2212term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2213{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002214 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002215 channel_T *channel = NULL;
2216 job_T *job = NULL;
2217 jobopt_T opt;
2218 DWORD error;
2219 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2220 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002221 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002222
2223 if (!dyn_winpty_init())
2224 return FAIL;
2225
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002226 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002227 if (p == NULL)
2228 return FAIL;
2229
2230 job = job_alloc();
2231 if (job == NULL)
2232 goto failed;
2233
2234 channel = add_channel();
2235 if (channel == NULL)
2236 goto failed;
2237
2238 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2239 if (term->tl_winpty_config == NULL)
2240 goto failed;
2241
2242 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2243 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2244 if (term->tl_winpty == NULL)
2245 goto failed;
2246
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002247 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002248 spawn_config = winpty_spawn_config_new(
2249 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2250 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2251 NULL,
2252 p,
2253 NULL,
2254 NULL,
2255 &winpty_err);
2256 if (spawn_config == NULL)
2257 goto failed;
2258
2259 channel = add_channel();
2260 if (channel == NULL)
2261 goto failed;
2262
2263 job = job_alloc();
2264 if (job == NULL)
2265 goto failed;
2266
2267 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2268 &child_thread_handle, &error, &winpty_err))
2269 goto failed;
2270
2271 channel_set_pipes(channel,
2272 (sock_T) CreateFileW(
2273 winpty_conin_name(term->tl_winpty),
2274 GENERIC_WRITE, 0, NULL,
2275 OPEN_EXISTING, 0, NULL),
2276 (sock_T) CreateFileW(
2277 winpty_conout_name(term->tl_winpty),
2278 GENERIC_READ, 0, NULL,
2279 OPEN_EXISTING, 0, NULL),
2280 (sock_T) CreateFileW(
2281 winpty_conerr_name(term->tl_winpty),
2282 GENERIC_READ, 0, NULL,
2283 OPEN_EXISTING, 0, NULL));
2284
2285 jo = CreateJobObject(NULL, NULL);
2286 if (jo == NULL)
2287 goto failed;
2288
2289 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002290 {
2291 /* Failed, switch the way to terminate process with TerminateProcess. */
2292 CloseHandle(jo);
2293 jo = NULL;
2294 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002295
2296 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002297 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002298
2299 create_vterm(term, rows, cols);
2300
2301 setup_job_options(&opt, rows, cols);
2302 channel_set_job(channel, job, &opt);
2303
2304 job->jv_channel = channel;
2305 job->jv_proc_info.hProcess = child_process_handle;
2306 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2307 job->jv_job_object = jo;
2308 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002309 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002310 term->tl_job = job;
2311
2312 return OK;
2313
2314failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002315 if (spawn_config != NULL)
2316 winpty_spawn_config_free(spawn_config);
2317 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002318 if (channel != NULL)
2319 channel_clear(channel);
2320 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002321 {
2322 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002323 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002324 }
2325 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002326 if (jo != NULL)
2327 CloseHandle(jo);
2328 if (term->tl_winpty != NULL)
2329 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002330 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002331 if (term->tl_winpty_config != NULL)
2332 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002333 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002334 if (winpty_err != NULL)
2335 {
2336 char_u *msg = utf16_to_enc(
2337 (short_u *)winpty_error_msg(winpty_err), NULL);
2338
2339 EMSG(msg);
2340 winpty_error_free(winpty_err);
2341 }
2342 return FAIL;
2343}
2344
2345/*
2346 * Free the terminal emulator part of "term".
2347 */
2348 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002349term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002350{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002351 if (term->tl_winpty != NULL)
2352 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002353 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002354 if (term->tl_winpty_config != NULL)
2355 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002356 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002357 if (term->tl_vterm != NULL)
2358 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002359 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002360}
2361
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002362/*
2363 * Request size to terminal.
2364 */
2365 static void
2366term_report_winsize(term_T *term, int rows, int cols)
2367{
2368 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2369}
2370
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002371# else
2372
2373/**************************************
2374 * 3. Unix-like implementation.
2375 */
2376
2377/*
2378 * Create a new terminal of "rows" by "cols" cells.
2379 * Start job for "cmd".
2380 * Store the pointers in "term".
2381 * Return OK or FAIL.
2382 */
2383 static int
2384term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2385{
2386 typval_T argvars[2];
2387 jobopt_T opt;
2388
2389 create_vterm(term, rows, cols);
2390
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002391 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002392 argvars[0].v_type = VAR_STRING;
2393 argvars[0].vval.v_string = cmd;
2394 setup_job_options(&opt, rows, cols);
2395 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002396 if (term->tl_job != NULL)
2397 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002398
Bram Moolenaar61a66052017-07-22 18:39:00 +02002399 return term->tl_job != NULL
2400 && term->tl_job->jv_channel != NULL
2401 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002402}
2403
2404/*
2405 * Free the terminal emulator part of "term".
2406 */
2407 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002408term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002409{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002410 if (term->tl_vterm != NULL)
2411 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002412 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002413}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002414
2415/*
2416 * Request size to terminal.
2417 */
2418 static void
2419term_report_winsize(term_T *term, int rows, int cols)
2420{
2421 /* Use an ioctl() to report the new window size to the job. */
2422 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2423 {
2424 int fd = -1;
2425 int part;
2426
2427 for (part = PART_OUT; part < PART_COUNT; ++part)
2428 {
2429 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2430 if (isatty(fd))
2431 break;
2432 }
2433 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2434 mch_stop_job(term->tl_job, (char_u *)"winch");
2435 }
2436}
2437
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002438# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002439
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002440#endif /* FEAT_TERMINAL */