blob: 1423c5f55fb15d5545026cefeb2b4fac033e4e2b [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 Moolenaar392d1bf2017-07-31 21:18:58 +020039 * - Use "." for current line instead of optional.
40 * - make row and cols one-based instead of zero-based in term_ functions.
41 * - Add StatusLineTerm highlighting
Bram Moolenaar423802d2017-07-30 16:52:24 +020042 * - in bash mouse clicks are inserting characters.
43 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaard85f2712017-07-28 21:51:57 +020044 * - For the scrollback buffer store lines in the buffer, only attributes in
45 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020046 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020048 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
49 * job finishes).
50 * - add option values to the command:
51 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020052 * - support different cursor shapes, colors and attributes
53 * - make term_getcursor() return type (none/block/bar/underline) and
54 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020055 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar423802d2017-07-30 16:52:24 +020056 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020057 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020058 * - add 't' to mode()
Bram Moolenaar938783d2017-07-16 20:13:26 +020059 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaard85f2712017-07-28 21:51:57 +020060 * - when closing window and job has ended, make buffer hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020061 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020062 * - use win_del_lines() to make scroll-up efficient.
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 Moolenaar423802d2017-07-30 16:52:24 +020068 * - if the job in the terminal does not support the mouse, we can use the
69 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020070 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
71 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020072 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020073 * - In the GUI use a terminal emulator for :!cmd.
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 if (buflist_findname(cmd) == NULL)
223 curbuf->b_ffname = vim_strsave(cmd);
224 else
225 {
226 int i;
227 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200228 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200229
230 for (i = 1; p != NULL; ++i)
231 {
232 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
233 if (buflist_findname(p) == NULL)
234 {
235 curbuf->b_ffname = p;
236 break;
237 }
238 }
239 }
240 curbuf->b_fname = curbuf->b_ffname;
241
242 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
243 curbuf->b_changed = TRUE;
244 curbuf->b_p_ma = FALSE;
245 set_string_option_direct((char_u *)"buftype", -1,
246 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200248 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200250 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200251 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200252 {
253 /* store the size we ended up with */
254 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200255 }
256 else
257 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200258 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200259
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200260 /* Wiping out the buffer will also close the window and call
261 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200262 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200263 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200264
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200265 /* TODO: Setup pty, see mch_call_shell(). */
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 */
569 static int
570term_job_running(term_T *term)
571{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200572 /* Also consider the job finished when the channel is closed, to avoid a
573 * race condition when updating the title. */
574 return term->tl_job != NULL
575 && term->tl_job->jv_status == JOB_STARTED
576 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200577}
578
579/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200580 * Add the last line of the scrollback buffer to the buffer in the window.
581 */
582 static void
583add_scrollback_line_to_buffer(term_T *term)
584{
585 linenr_T lnum = term->tl_scrollback.ga_len - 1;
586 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
587 garray_T ga;
588 int c;
589 int col;
590 int i;
591
592 ga_init2(&ga, 1, 100);
593 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
594 {
595 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
596 goto failed;
597 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
598 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
599 (char_u *)ga.ga_data + ga.ga_len);
600 }
601 if (ga_grow(&ga, 1) == FAIL)
602 goto failed;
603 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
604 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
605
606 if (lnum == 0)
607 {
608 /* Delete the empty line that was in the empty buffer. */
609 curbuf = term->tl_buffer;
610 ml_delete(2, FALSE);
611 curbuf = curwin->w_buffer;
612 }
613
614failed:
615 ga_clear(&ga);
616}
617
618/*
619 * Add the current lines of the terminal to scrollback and to the buffer.
620 * Called after the job has ended and when switching to Terminal mode.
621 */
622 static void
623move_terminal_to_buffer(term_T *term)
624{
625 win_T *wp;
626 int len;
627 int lines_skipped = 0;
628 VTermPos pos;
629 VTermScreenCell cell;
630 VTermScreenCell *p;
631 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
632
633 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
634 {
635 len = 0;
636 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
637 if (vterm_screen_get_cell(screen, pos, &cell) != 0
638 && cell.chars[0] != NUL)
639 len = pos.col + 1;
640
641 if (len == 0)
642 ++lines_skipped;
643 else
644 {
645 while (lines_skipped > 0)
646 {
647 /* Line was skipped, add an empty line. */
648 --lines_skipped;
649 if (ga_grow(&term->tl_scrollback, 1) == OK)
650 {
651 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
652 + term->tl_scrollback.ga_len;
653
654 line->sb_cols = 0;
655 line->sb_cells = NULL;
656 ++term->tl_scrollback.ga_len;
657
658 add_scrollback_line_to_buffer(term);
659 }
660 }
661
662 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
663 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
664 {
665 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
666 + term->tl_scrollback.ga_len;
667
668 for (pos.col = 0; pos.col < len; ++pos.col)
669 {
670 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
671 vim_memset(p + pos.col, 0, sizeof(cell));
672 else
673 p[pos.col] = cell;
674 }
675 line->sb_cols = len;
676 line->sb_cells = p;
677 ++term->tl_scrollback.ga_len;
678
679 add_scrollback_line_to_buffer(term);
680 }
681 else
682 vim_free(p);
683 }
684 }
685
686 FOR_ALL_WINDOWS(wp)
687 {
688 if (wp->w_buffer == term->tl_buffer)
689 {
690 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
691 wp->w_cursor.col = 0;
692 wp->w_valid = 0;
693 redraw_win_later(wp, NOT_VALID);
694 }
695 }
696}
697
698 static void
699set_terminal_mode(term_T *term, int on)
700{
701 term->tl_terminal_mode = on;
702 vim_free(term->tl_status_text);
703 term->tl_status_text = NULL;
704 if (term->tl_buffer == curbuf)
705 maketitle();
706}
707
708/*
709 * Called after the job if finished and Terminal mode is not active:
710 * Move the vterm contents into the scrollback buffer and free the vterm.
711 */
712 static void
713cleanup_vterm(term_T *term)
714{
715 move_terminal_to_buffer(term);
716 term_free_vterm(term);
717 set_terminal_mode(term, FALSE);
718}
719
720/*
721 * Switch from sending keys to the job to Terminal-Normal mode.
722 * Suspends updating the terminal window.
723 */
724 static void
725term_enter_terminal_mode()
726{
727 term_T *term = curbuf->b_term;
728
729 /* Append the current terminal contents to the buffer. */
730 move_terminal_to_buffer(term);
731
732 set_terminal_mode(term, TRUE);
733}
734
735/*
736 * Returns TRUE if the current window contains a terminal and we are in
737 * Terminal-Normal mode.
738 */
739 int
740term_in_terminal_mode()
741{
742 term_T *term = curbuf->b_term;
743
744 return term != NULL && term->tl_terminal_mode;
745}
746
747/*
748 * Switch from Terminal-Normal mode to sending keys to the job.
749 * Restores updating the terminal window.
750 */
751 void
752term_leave_terminal_mode()
753{
754 term_T *term = curbuf->b_term;
755 sb_line_T *line;
756 garray_T *gap;
757
758 /* Remove the terminal contents from the scrollback and the buffer. */
759 gap = &term->tl_scrollback;
760 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
761 {
762 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
763 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
764 vim_free(line->sb_cells);
765 --gap->ga_len;
766 if (gap->ga_len == 0)
767 break;
768 }
769 check_cursor();
770
771 set_terminal_mode(term, FALSE);
772
773 if (term->tl_channel_closed)
774 cleanup_vterm(term);
775 redraw_buf_and_status_later(curbuf, NOT_VALID);
776}
777
778/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200779 * Get a key from the user without mapping.
780 * TODO: use terminal mode mappings.
781 */
782 static int
783term_vgetc()
784{
785 int c;
786
787 ++no_mapping;
788 ++allow_keys;
789 got_int = FALSE;
790 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200791 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200792 --no_mapping;
793 --allow_keys;
794 return c;
795}
796
797/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200798 * Send keys to terminal.
799 */
800 static int
801send_keys_to_term(term_T *term, int c, int typed)
802{
803 char msg[KEY_BUF_LEN];
804 size_t len;
805 static int mouse_was_outside = FALSE;
806 int dragging_outside = FALSE;
807
808 /* Catch keys that need to be handled as in Normal mode. */
809 switch (c)
810 {
811 case NUL:
812 case K_ZERO:
813 if (typed)
814 stuffcharReadbuff(c);
815 return FAIL;
816
817 case K_IGNORE:
818 return FAIL;
819
820 case K_LEFTDRAG:
821 case K_MIDDLEDRAG:
822 case K_RIGHTDRAG:
823 case K_X1DRAG:
824 case K_X2DRAG:
825 dragging_outside = mouse_was_outside;
826 /* FALLTHROUGH */
827 case K_LEFTMOUSE:
828 case K_LEFTMOUSE_NM:
829 case K_LEFTRELEASE:
830 case K_LEFTRELEASE_NM:
831 case K_MIDDLEMOUSE:
832 case K_MIDDLERELEASE:
833 case K_RIGHTMOUSE:
834 case K_RIGHTRELEASE:
835 case K_X1MOUSE:
836 case K_X1RELEASE:
837 case K_X2MOUSE:
838 case K_X2RELEASE:
839 if (mouse_row < W_WINROW(curwin)
840 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
841 || mouse_col < W_WINCOL(curwin)
842 || mouse_col >= W_ENDCOL(curwin)
843 || dragging_outside)
844 {
845 /* click outside the current window */
846 if (typed)
847 {
848 stuffcharReadbuff(c);
849 mouse_was_outside = TRUE;
850 }
851 return FAIL;
852 }
853 }
854 if (typed)
855 mouse_was_outside = FALSE;
856
857 /* Convert the typed key to a sequence of bytes for the job. */
858 len = term_convert_key(term, c, msg);
859 if (len > 0)
860 /* TODO: if FAIL is returned, stop? */
861 channel_send(term->tl_job->jv_channel, PART_IN,
862 (char_u *)msg, (int)len, NULL);
863
864 return OK;
865}
866
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200867 static void
868position_cursor(win_T *wp, VTermPos *pos)
869{
870 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
871 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
872 wp->w_valid |= (VALID_WCOL|VALID_WROW);
873}
874
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200875/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200876 * Handle CTRL-W "": send register contents to the job.
877 */
878 static void
879term_paste_register(int prev_c UNUSED)
880{
881 int c;
882 list_T *l;
883 listitem_T *item;
884 long reglen = 0;
885 int type;
886
887#ifdef FEAT_CMDL_INFO
888 if (add_to_showcmd(prev_c))
889 if (add_to_showcmd('"'))
890 out_flush();
891#endif
892 c = term_vgetc();
893#ifdef FEAT_CMDL_INFO
894 clear_showcmd();
895#endif
896
897 /* CTRL-W "= prompt for expression to evaluate. */
898 if (c == '=' && get_expr_register() != '=')
899 return;
900
901 l = (list_T *)get_reg_contents(c, GREG_LIST);
902 if (l != NULL)
903 {
904 type = get_reg_type(c, &reglen);
905 for (item = l->lv_first; item != NULL; item = item->li_next)
906 {
907 char_u *s = get_tv_string(&item->li_tv);
908
909 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
910 s, STRLEN(s), NULL);
911 if (item->li_next != NULL || type == MLINE)
912 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
913 (char_u *)"\r", 1, NULL);
914 }
915 list_free(l);
916 }
917}
918
919/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200920 * Returns TRUE if the current window contains a terminal and we are sending
921 * keys to the job.
922 */
923 int
924term_use_loop()
925{
926 term_T *term = curbuf->b_term;
927
928 return term != NULL
929 && !term->tl_terminal_mode
930 && term->tl_vterm != NULL
931 && term_job_running(term);
932}
933
934/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200935 * Wait for input and send it to the job.
936 * Return when the start of a CTRL-W command is typed or anything else that
937 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200938 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
939 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200940 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200941 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200942terminal_loop(void)
943{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200944 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200945 int termkey = 0;
946
947 if (*curwin->w_p_tk != NUL)
948 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200949 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200950
951 for (;;)
952 {
953 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200954 /* Repeat redrawing in case a message is received while redrawing. */
955 while (curwin->w_redr_type != 0)
956 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200957 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200958
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200959 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200960 if (curbuf->b_term->tl_vterm == NULL
961 || !term_job_running(curbuf->b_term))
962 /* job finished while waiting for a character */
963 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200964
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200965 if (c == (termkey == 0 ? Ctrl_W : termkey))
966 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200967 int prev_c = c;
968
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200969#ifdef FEAT_CMDL_INFO
970 if (add_to_showcmd(c))
971 out_flush();
972#endif
973 c = term_vgetc();
974#ifdef FEAT_CMDL_INFO
975 clear_showcmd();
976#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200977 if (curbuf->b_term->tl_vterm == NULL
978 || !term_job_running(curbuf->b_term))
979 /* job finished while waiting for a character */
980 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200981
982 if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200983 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200984 /* "CTRL-W .": send CTRL-W to the job */
985 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200986 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200987 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +0200988 {
989 term_enter_terminal_mode();
990 return FAIL;
991 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200992 else if (c == '"')
993 {
994 term_paste_register(prev_c);
995 continue;
996 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200997 else if (termkey == 0 || c != termkey)
998 {
999 stuffcharReadbuff(Ctrl_W);
1000 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001001 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001002 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001003 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001004 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1005 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001006 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001007 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001008}
1009
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001010/*
1011 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001012 * This updates the title and status, but does not close the vter, because
1013 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001014 */
1015 void
1016term_job_ended(job_T *job)
1017{
1018 term_T *term;
1019 int did_one = FALSE;
1020
1021 for (term = first_term; term != NULL; term = term->tl_next)
1022 if (term->tl_job == job)
1023 {
1024 vim_free(term->tl_title);
1025 term->tl_title = NULL;
1026 vim_free(term->tl_status_text);
1027 term->tl_status_text = NULL;
1028 redraw_buf_and_status_later(term->tl_buffer, VALID);
1029 did_one = TRUE;
1030 }
1031 if (did_one)
1032 redraw_statuslines();
1033 if (curbuf->b_term != NULL)
1034 {
1035 if (curbuf->b_term->tl_job == job)
1036 maketitle();
1037 update_cursor(curbuf->b_term, TRUE);
1038 }
1039}
1040
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001041 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001042may_toggle_cursor(term_T *term)
1043{
1044 if (curbuf == term->tl_buffer)
1045 {
1046 if (term->tl_cursor_visible)
1047 cursor_on();
1048 else
1049 cursor_off();
1050 }
1051}
1052
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001053 static int
1054handle_damage(VTermRect rect, void *user)
1055{
1056 term_T *term = (term_T *)user;
1057
1058 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1059 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1060 redraw_buf_later(term->tl_buffer, NOT_VALID);
1061 return 1;
1062}
1063
1064 static int
1065handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1066{
1067 term_T *term = (term_T *)user;
1068
1069 /* TODO */
1070 redraw_buf_later(term->tl_buffer, NOT_VALID);
1071 return 1;
1072}
1073
1074 static int
1075handle_movecursor(
1076 VTermPos pos,
1077 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001078 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001079 void *user)
1080{
1081 term_T *term = (term_T *)user;
1082 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001083
1084 term->tl_cursor_pos = pos;
1085 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001086
1087 FOR_ALL_WINDOWS(wp)
1088 {
1089 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001090 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001091 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001092 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001093 {
1094 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001095 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001096 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001097
1098 return 1;
1099}
1100
Bram Moolenaar21554412017-07-24 21:44:43 +02001101 static int
1102handle_settermprop(
1103 VTermProp prop,
1104 VTermValue *value,
1105 void *user)
1106{
1107 term_T *term = (term_T *)user;
1108
1109 switch (prop)
1110 {
1111 case VTERM_PROP_TITLE:
1112 vim_free(term->tl_title);
1113 term->tl_title = vim_strsave((char_u *)value->string);
1114 vim_free(term->tl_status_text);
1115 term->tl_status_text = NULL;
1116 if (term == curbuf->b_term)
1117 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001118 break;
1119
1120 case VTERM_PROP_CURSORVISIBLE:
1121 term->tl_cursor_visible = value->boolean;
1122 may_toggle_cursor(term);
1123 out_flush();
1124 break;
1125
Bram Moolenaar21554412017-07-24 21:44:43 +02001126 default:
1127 break;
1128 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001129 /* Always return 1, otherwise vterm doesn't store the value internally. */
1130 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001131}
1132
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001133/*
1134 * The job running in the terminal resized the terminal.
1135 */
1136 static int
1137handle_resize(int rows, int cols, void *user)
1138{
1139 term_T *term = (term_T *)user;
1140 win_T *wp;
1141
1142 term->tl_rows = rows;
1143 term->tl_cols = cols;
1144 FOR_ALL_WINDOWS(wp)
1145 {
1146 if (wp->w_buffer == term->tl_buffer)
1147 {
1148 win_setheight_win(rows, wp);
1149 win_setwidth_win(cols, wp);
1150 }
1151 }
1152
1153 redraw_buf_later(term->tl_buffer, NOT_VALID);
1154 return 1;
1155}
1156
Bram Moolenaard85f2712017-07-28 21:51:57 +02001157/*
1158 * Handle a line that is pushed off the top of the screen.
1159 */
1160 static int
1161handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1162{
1163 term_T *term = (term_T *)user;
1164
1165 /* TODO: Limit the number of lines that are stored. */
1166 /* TODO: put the text in the buffer. */
1167 if (ga_grow(&term->tl_scrollback, 1) == OK)
1168 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001169 VTermScreenCell *p = NULL;
1170 int len = 0;
1171 int i;
1172 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001173
1174 /* do not store empty cells at the end */
1175 for (i = 0; i < cols; ++i)
1176 if (cells[i].chars[0] != 0)
1177 len = i + 1;
1178
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001179 if (len > 0)
1180 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001181 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001182 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001183
1184 line = (sb_line_T *)term->tl_scrollback.ga_data
1185 + term->tl_scrollback.ga_len;
1186 line->sb_cols = len;
1187 line->sb_cells = p;
1188 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001189 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001190
1191 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001192 }
1193 return 0; /* ignored */
1194}
1195
Bram Moolenaar21554412017-07-24 21:44:43 +02001196static VTermScreenCallbacks screen_callbacks = {
1197 handle_damage, /* damage */
1198 handle_moverect, /* moverect */
1199 handle_movecursor, /* movecursor */
1200 handle_settermprop, /* settermprop */
1201 NULL, /* bell */
1202 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001203 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001204 NULL /* sb_popline */
1205};
1206
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001207/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001208 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001209 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001210 */
1211 void
1212term_channel_closed(channel_T *ch)
1213{
1214 term_T *term;
1215 int did_one = FALSE;
1216
1217 for (term = first_term; term != NULL; term = term->tl_next)
1218 if (term->tl_job == ch->ch_job)
1219 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001220 term->tl_channel_closed = TRUE;
1221
Bram Moolenaard85f2712017-07-28 21:51:57 +02001222 vim_free(term->tl_title);
1223 term->tl_title = NULL;
1224 vim_free(term->tl_status_text);
1225 term->tl_status_text = NULL;
1226
Bram Moolenaar423802d2017-07-30 16:52:24 +02001227 /* Unless in Terminal-Normal mode: clear the vterm. */
1228 if (!term->tl_terminal_mode)
1229 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001230
1231 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1232 did_one = TRUE;
1233 }
1234 if (did_one)
1235 {
1236 redraw_statuslines();
1237
1238 /* Need to break out of vgetc(). */
1239 ins_char_typebuf(K_IGNORE);
1240
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001241 term = curbuf->b_term;
1242 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001243 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001244 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001245 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001246 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001247 }
1248 }
1249}
1250
1251/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001252 * Reverse engineer the RGB value into a cterm color index.
1253 * First color is 1. Return 0 if no match found.
1254 */
1255 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001256color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001257{
1258 int red = color->red;
1259 int blue = color->blue;
1260 int green = color->green;
1261
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001262 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001263 if (red == 0)
1264 {
1265 if (green == 0)
1266 {
1267 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001268 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001269 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001270 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001271 }
1272 else if (green == 224)
1273 {
1274 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001275 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001276 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001277 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001278 }
1279 }
1280 else if (red == 224)
1281 {
1282 if (green == 0)
1283 {
1284 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001285 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001286 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001287 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001288 }
1289 else if (green == 224)
1290 {
1291 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001292 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001293 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001294 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001295 }
1296 }
1297 else if (red == 128)
1298 {
1299 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001300 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001301 }
1302 else if (red == 255)
1303 {
1304 if (green == 64)
1305 {
1306 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001307 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001308 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001309 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001310 }
1311 else if (green == 255)
1312 {
1313 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001314 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001315 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001316 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001317 }
1318 }
1319 else if (red == 64)
1320 {
1321 if (green == 64)
1322 {
1323 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001324 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001325 }
1326 else if (green == 255)
1327 {
1328 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001329 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001330 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001331 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001332 }
1333 }
1334 if (t_colors >= 256)
1335 {
1336 if (red == blue && red == green)
1337 {
1338 /* 24-color greyscale */
1339 static int cutoff[23] = {
1340 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1341 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1342 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1343 int i;
1344
1345 for (i = 0; i < 23; ++i)
1346 if (red < cutoff[i])
1347 return i + 233;
1348 return 256;
1349 }
1350
1351 /* 216-color cube */
1352 return 17 + ((red + 25) / 0x33) * 36
1353 + ((green + 25) / 0x33) * 6
1354 + (blue + 25) / 0x33;
1355 }
1356 return 0;
1357}
1358
1359/*
1360 * Convert the attributes of a vterm cell into an attribute index.
1361 */
1362 static int
1363cell2attr(VTermScreenCell *cell)
1364{
1365 int attr = 0;
1366
1367 if (cell->attrs.bold)
1368 attr |= HL_BOLD;
1369 if (cell->attrs.underline)
1370 attr |= HL_UNDERLINE;
1371 if (cell->attrs.italic)
1372 attr |= HL_ITALIC;
1373 if (cell->attrs.strike)
1374 attr |= HL_STANDOUT;
1375 if (cell->attrs.reverse)
1376 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001377
1378#ifdef FEAT_GUI
1379 if (gui.in_use)
1380 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001381 guicolor_T fg, bg;
1382
1383 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1384 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1385 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001386 }
1387 else
1388#endif
1389#ifdef FEAT_TERMGUICOLORS
1390 if (p_tgc)
1391 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001392 guicolor_T fg, bg;
1393
1394 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1395 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1396
1397 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001398 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001399 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001400#endif
1401 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001402 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1403 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001404 }
1405 return 0;
1406}
1407
1408/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001409 * Called to update the window that contains a terminal.
1410 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001411 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001412 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001413term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001414{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001415 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001416 VTerm *vterm;
1417 VTermScreen *screen;
1418 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001419 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001420
Bram Moolenaar423802d2017-07-30 16:52:24 +02001421 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001422 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001423
Bram Moolenaard85f2712017-07-28 21:51:57 +02001424 vterm = term->tl_vterm;
1425 screen = vterm_obtain_screen(vterm);
1426 state = vterm_obtain_state(vterm);
1427
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001428 /*
1429 * If the window was resized a redraw will be triggered and we get here.
1430 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1431 */
1432 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1433 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001434 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001435 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1436 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1437 win_T *twp;
1438
1439 FOR_ALL_WINDOWS(twp)
1440 {
1441 /* When more than one window shows the same terminal, use the
1442 * smallest size. */
1443 if (twp->w_buffer == term->tl_buffer)
1444 {
1445 if (!term->tl_rows_fixed && rows > twp->w_height)
1446 rows = twp->w_height;
1447 if (!term->tl_cols_fixed && cols > twp->w_width)
1448 cols = twp->w_width;
1449 }
1450 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001451
1452 vterm_set_size(vterm, rows, cols);
1453 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1454 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001455 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001456 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001457
1458 /* The cursor may have been moved when resizing. */
1459 vterm_state_get_cursorpos(state, &pos);
1460 position_cursor(wp, &pos);
1461
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001462 /* TODO: Only redraw what changed. */
1463 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001464 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001465 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001466 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001467
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001468 if (pos.row < term->tl_rows)
1469 {
1470 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001471 {
1472 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001473 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001474
Bram Moolenaareeac6772017-07-23 15:48:37 +02001475 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1476 vim_memset(&cell, 0, sizeof(cell));
1477
1478 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001479 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001480 if (c == NUL)
1481 {
1482 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001483#if defined(FEAT_MBYTE)
1484 if (enc_utf8)
1485 ScreenLinesUC[off] = NUL;
1486#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001487 }
1488 else
1489 {
1490#if defined(FEAT_MBYTE)
1491 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001492 {
1493 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001494 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001495 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001496 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001497 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001498 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001499 if (enc_utf8)
1500 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001501 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001502#else
1503 ScreenLines[off] = c;
1504#endif
1505 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001506 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001507
1508 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001509 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001510 if (cell.width == 2)
1511 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001512 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001513#if defined(FEAT_MBYTE)
1514 if (enc_utf8)
1515 ScreenLinesUC[off] = NUL;
1516#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001517 ++pos.col;
1518 ++off;
1519 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001520 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001521 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001522 else
1523 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001524
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001525 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1526 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001527 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001528
1529 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001530}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001531
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001532/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001533 * Return TRUE if "wp" is a terminal window where the job has finished.
1534 */
1535 int
1536term_is_finished(buf_T *buf)
1537{
1538 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1539}
1540
1541/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001542 * Return TRUE if "wp" is a terminal window where the job has finished or we
1543 * are in Terminal-Normal mode.
1544 */
1545 int
1546term_show_buffer(buf_T *buf)
1547{
1548 term_T *term = buf->b_term;
1549
1550 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1551}
1552
1553/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001554 * The current buffer is going to be changed. If there is terminal
1555 * highlighting remove it now.
1556 */
1557 void
1558term_change_in_curbuf(void)
1559{
1560 term_T *term = curbuf->b_term;
1561
1562 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1563 {
1564 free_scrollback(term);
1565 redraw_buf_later(term->tl_buffer, NOT_VALID);
1566 }
1567}
1568
1569/*
1570 * Get the screen attribute for a position in the buffer.
1571 */
1572 int
1573term_get_attr(buf_T *buf, linenr_T lnum, int col)
1574{
1575 term_T *term = buf->b_term;
1576 sb_line_T *line;
1577
Bram Moolenaar70229f92017-07-29 16:01:53 +02001578 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001579 return 0;
1580 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1581 if (col >= line->sb_cols)
1582 return 0;
1583 return cell2attr(line->sb_cells + col);
1584}
1585
1586/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001587 * Set job options common for Unix and MS-Windows.
1588 */
1589 static void
1590setup_job_options(jobopt_T *opt, int rows, int cols)
1591{
1592 clear_job_options(opt);
1593 opt->jo_mode = MODE_RAW;
1594 opt->jo_out_mode = MODE_RAW;
1595 opt->jo_err_mode = MODE_RAW;
1596 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001597
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001598 opt->jo_io[PART_OUT] = JIO_BUFFER;
1599 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001600 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1601
1602 opt->jo_modifiable[PART_OUT] = 0;
1603 opt->jo_modifiable[PART_ERR] = 0;
1604 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1605
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001606 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1607 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001608 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001609 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1610
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001611 opt->jo_term_rows = rows;
1612 opt->jo_term_cols = cols;
1613}
1614
1615/*
1616 * Create a new vterm and initialize it.
1617 */
1618 static void
1619create_vterm(term_T *term, int rows, int cols)
1620{
1621 VTerm *vterm;
1622 VTermScreen *screen;
1623
1624 vterm = vterm_new(rows, cols);
1625 term->tl_vterm = vterm;
1626 screen = vterm_obtain_screen(vterm);
1627 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1628 /* TODO: depends on 'encoding'. */
1629 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001630
1631 /* Vterm uses a default black background. Set it to white when
1632 * 'background' is "light". */
1633 if (*p_bg == 'l')
1634 {
1635 VTermColor fg, bg;
1636
1637 fg.red = fg.green = fg.blue = 0;
1638 bg.red = bg.green = bg.blue = 255;
1639 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1640 }
1641
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001642 /* Required to initialize most things. */
1643 vterm_screen_reset(screen, 1 /* hard */);
1644}
1645
Bram Moolenaar21554412017-07-24 21:44:43 +02001646/*
1647 * Return the text to show for the buffer name and status.
1648 */
1649 char_u *
1650term_get_status_text(term_T *term)
1651{
1652 if (term->tl_status_text == NULL)
1653 {
1654 char_u *txt;
1655 size_t len;
1656
Bram Moolenaar423802d2017-07-30 16:52:24 +02001657 if (term->tl_terminal_mode)
1658 {
1659 if (term_job_running(term))
1660 txt = (char_u *)_("Terminal");
1661 else
1662 txt = (char_u *)_("Terminal-finished");
1663 }
1664 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001665 txt = term->tl_title;
1666 else if (term_job_running(term))
1667 txt = (char_u *)_("running");
1668 else
1669 txt = (char_u *)_("finished");
1670 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001671 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001672 if (term->tl_status_text != NULL)
1673 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1674 term->tl_buffer->b_fname, txt);
1675 }
1676 return term->tl_status_text;
1677}
1678
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001679/*
1680 * Mark references in jobs of terminals.
1681 */
1682 int
1683set_ref_in_term(int copyID)
1684{
1685 int abort = FALSE;
1686 term_T *term;
1687 typval_T tv;
1688
1689 for (term = first_term; term != NULL; term = term->tl_next)
1690 if (term->tl_job != NULL)
1691 {
1692 tv.v_type = VAR_JOB;
1693 tv.vval.v_job = term->tl_job;
1694 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1695 }
1696 return abort;
1697}
1698
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001699/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001700 * Get the buffer from the first argument in "argvars".
1701 * Returns NULL when the buffer is not for a terminal window.
1702 */
1703 static buf_T *
1704term_get_buf(typval_T *argvars)
1705{
1706 buf_T *buf;
1707
1708 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1709 ++emsg_off;
1710 buf = get_buf_tv(&argvars[0], FALSE);
1711 --emsg_off;
1712 if (buf == NULL || buf->b_term == NULL)
1713 return NULL;
1714 return buf;
1715}
1716
1717/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001718 * "term_getattr(attr, name)" function
1719 */
1720 void
1721f_term_getattr(typval_T *argvars, typval_T *rettv)
1722{
1723 int attr;
1724 size_t i;
1725 char_u *name;
1726
1727 static struct {
1728 char *name;
1729 int attr;
1730 } attrs[] = {
1731 {"bold", HL_BOLD},
1732 {"italic", HL_ITALIC},
1733 {"underline", HL_UNDERLINE},
1734 {"strike", HL_STANDOUT},
1735 {"reverse", HL_INVERSE},
1736 };
1737
1738 attr = get_tv_number(&argvars[0]);
1739 name = get_tv_string_chk(&argvars[1]);
1740 if (name == NULL)
1741 return;
1742
1743 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1744 if (STRCMP(name, attrs[i].name) == 0)
1745 {
1746 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1747 break;
1748 }
1749}
1750
1751/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001752 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001753 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001754 void
1755f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001756{
Bram Moolenaar97870002017-07-30 18:28:38 +02001757 buf_T *buf = term_get_buf(argvars);
1758 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001759
Bram Moolenaar97870002017-07-30 18:28:38 +02001760 if (rettv_list_alloc(rettv) == FAIL)
1761 return;
1762 if (buf == NULL)
1763 return;
1764
1765 l = rettv->vval.v_list;
1766 list_append_number(l, buf->b_term->tl_cursor_pos.row);
1767 list_append_number(l, buf->b_term->tl_cursor_pos.col);
1768 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001769}
1770
1771/*
1772 * "term_getjob(buf)" function
1773 */
1774 void
1775f_term_getjob(typval_T *argvars, typval_T *rettv)
1776{
1777 buf_T *buf = term_get_buf(argvars);
1778
1779 rettv->v_type = VAR_JOB;
1780 rettv->vval.v_job = NULL;
1781 if (buf == NULL)
1782 return;
1783
1784 rettv->vval.v_job = buf->b_term->tl_job;
1785 if (rettv->vval.v_job != NULL)
1786 ++rettv->vval.v_job->jv_refcount;
1787}
1788
1789/*
1790 * "term_getline(buf, row)" function
1791 */
1792 void
1793f_term_getline(typval_T *argvars, typval_T *rettv)
1794{
1795 buf_T *buf = term_get_buf(argvars);
1796 term_T *term;
1797 int row;
1798
1799 rettv->v_type = VAR_STRING;
1800 if (buf == NULL)
1801 return;
1802 term = buf->b_term;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001803 if (argvars[1].v_type == VAR_UNKNOWN)
1804 row = term->tl_cursor_pos.row;
1805 else
1806 row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001807
1808 if (term->tl_vterm == NULL)
1809 {
1810 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1811
1812 /* vterm is finished, get the text from the buffer */
1813 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1814 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1815 }
1816 else
1817 {
1818 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1819 VTermRect rect;
1820 int len;
1821 char_u *p;
1822
1823 len = term->tl_cols * MB_MAXBYTES + 1;
1824 p = alloc(len);
1825 if (p == NULL)
1826 return;
1827 rettv->vval.v_string = p;
1828
1829 rect.start_col = 0;
1830 rect.end_col = term->tl_cols;
1831 rect.start_row = row;
1832 rect.end_row = row + 1;
1833 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1834 }
1835}
1836
1837/*
1838 * "term_getsize(buf)" function
1839 */
1840 void
1841f_term_getsize(typval_T *argvars, typval_T *rettv)
1842{
1843 buf_T *buf = term_get_buf(argvars);
1844 list_T *l;
1845
1846 if (rettv_list_alloc(rettv) == FAIL)
1847 return;
1848 if (buf == NULL)
1849 return;
1850
1851 l = rettv->vval.v_list;
1852 list_append_number(l, buf->b_term->tl_rows);
1853 list_append_number(l, buf->b_term->tl_cols);
1854}
1855
1856/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001857 * "term_getstatus(buf)" function
1858 */
1859 void
1860f_term_getstatus(typval_T *argvars, typval_T *rettv)
1861{
1862 buf_T *buf = term_get_buf(argvars);
1863 term_T *term;
1864 char_u val[100];
1865
1866 rettv->v_type = VAR_STRING;
1867 if (buf == NULL)
1868 return;
1869 term = buf->b_term;
1870
1871 if (term_job_running(term))
1872 STRCPY(val, "running");
1873 else
1874 STRCPY(val, "finished");
1875 if (term->tl_terminal_mode)
1876 STRCAT(val, ",terminal");
1877 rettv->vval.v_string = vim_strsave(val);
1878}
1879
1880/*
1881 * "term_gettitle(buf)" function
1882 */
1883 void
1884f_term_gettitle(typval_T *argvars, typval_T *rettv)
1885{
1886 buf_T *buf = term_get_buf(argvars);
1887
1888 rettv->v_type = VAR_STRING;
1889 if (buf == NULL)
1890 return;
1891
1892 if (buf->b_term->tl_title != NULL)
1893 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
1894}
1895
1896/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001897 * "term_list()" function
1898 */
1899 void
1900f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1901{
1902 term_T *tp;
1903 list_T *l;
1904
1905 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1906 return;
1907
1908 l = rettv->vval.v_list;
1909 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1910 if (tp != NULL && tp->tl_buffer != NULL)
1911 if (list_append_number(l,
1912 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1913 return;
1914}
1915
1916/*
1917 * "term_scrape(buf, row)" function
1918 */
1919 void
1920f_term_scrape(typval_T *argvars, typval_T *rettv)
1921{
1922 buf_T *buf = term_get_buf(argvars);
1923 VTermScreen *screen = NULL;
1924 VTermPos pos;
1925 list_T *l;
1926 term_T *term;
1927
1928 if (rettv_list_alloc(rettv) == FAIL)
1929 return;
1930 if (buf == NULL)
1931 return;
1932 term = buf->b_term;
1933 if (term->tl_vterm != NULL)
1934 screen = vterm_obtain_screen(term->tl_vterm);
1935
1936 l = rettv->vval.v_list;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001937 if (argvars[1].v_type == VAR_UNKNOWN)
1938 pos.row = term->tl_cursor_pos.row;
1939 else
1940 pos.row = (int)get_tv_number(&argvars[1]);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001941 for (pos.col = 0; pos.col < term->tl_cols; )
1942 {
1943 dict_T *dcell;
1944 VTermScreenCell cell;
1945 char_u rgb[8];
1946 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1947 int off = 0;
1948 int i;
1949
1950 if (screen == NULL)
1951 {
1952 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1953 sb_line_T *line;
1954
1955 /* vterm has finished, get the cell from scrollback */
1956 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1957 break;
1958 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1959 if (pos.col >= line->sb_cols)
1960 break;
1961 cell = line->sb_cells[pos.col];
1962 }
1963 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1964 break;
1965 dcell = dict_alloc();
1966 list_append_dict(l, dcell);
1967
1968 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1969 {
1970 if (cell.chars[i] == 0)
1971 break;
1972 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1973 }
1974 mbs[off] = NUL;
1975 dict_add_nr_str(dcell, "chars", 0, mbs);
1976
1977 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1978 cell.fg.red, cell.fg.green, cell.fg.blue);
1979 dict_add_nr_str(dcell, "fg", 0, rgb);
1980 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1981 cell.bg.red, cell.bg.green, cell.bg.blue);
1982 dict_add_nr_str(dcell, "bg", 0, rgb);
1983
1984 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
1985 dict_add_nr_str(dcell, "width", cell.width, NULL);
1986
1987 ++pos.col;
1988 if (cell.width == 2)
1989 ++pos.col;
1990 }
1991}
1992
1993/*
1994 * "term_sendkeys(buf, keys)" function
1995 */
1996 void
1997f_term_sendkeys(typval_T *argvars, typval_T *rettv)
1998{
1999 buf_T *buf = term_get_buf(argvars);
2000 char_u *msg;
2001 term_T *term;
2002
2003 rettv->v_type = VAR_UNKNOWN;
2004 if (buf == NULL)
2005 return;
2006
2007 msg = get_tv_string_chk(&argvars[1]);
2008 if (msg == NULL)
2009 return;
2010 term = buf->b_term;
2011 if (term->tl_vterm == NULL)
2012 return;
2013
2014 while (*msg != NUL)
2015 {
2016 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2017 msg += MB_PTR2LEN(msg);
2018 }
2019
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002020 if (!term->tl_terminal_mode)
2021 {
2022 /* TODO: only update once in a while. */
2023 update_screen(0);
2024 if (buf == curbuf)
2025 update_cursor(term, TRUE);
2026 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002027}
2028
2029/*
2030 * "term_start(command, options)" function
2031 */
2032 void
2033f_term_start(typval_T *argvars, typval_T *rettv)
2034{
2035 char_u *cmd = get_tv_string_chk(&argvars[0]);
2036 exarg_T ea;
2037
2038 if (cmd == NULL)
2039 return;
2040 ea.arg = cmd;
2041 ex_terminal(&ea);
2042
2043 if (curbuf->b_term != NULL)
2044 rettv->vval.v_number = curbuf->b_fnum;
2045}
2046
2047/*
2048 * "term_wait" function
2049 */
2050 void
2051f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2052{
2053 buf_T *buf = term_get_buf(argvars);
2054
2055 if (buf == NULL)
2056 return;
2057
2058 /* Get the job status, this will detect a job that finished. */
2059 if (buf->b_term->tl_job != NULL)
2060 (void)job_status(buf->b_term->tl_job);
2061
2062 /* Check for any pending channel I/O. */
2063 vpeekc_any();
2064 ui_delay(10L, FALSE);
2065
2066 /* Flushing messages on channels is hopefully sufficient.
2067 * TODO: is there a better way? */
2068 parse_queued_messages();
2069}
2070
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002071# ifdef WIN3264
2072
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002073/**************************************
2074 * 2. MS-Windows implementation.
2075 */
2076
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002077#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2078#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2079
Bram Moolenaar8a773062017-07-24 22:29:21 +02002080void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002081void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002082void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002083BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2084void (*winpty_config_set_initial_size)(void*, int, int);
2085LPCWSTR (*winpty_conin_name)(void*);
2086LPCWSTR (*winpty_conout_name)(void*);
2087LPCWSTR (*winpty_conerr_name)(void*);
2088void (*winpty_free)(void*);
2089void (*winpty_config_free)(void*);
2090void (*winpty_spawn_config_free)(void*);
2091void (*winpty_error_free)(void*);
2092LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002093BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002094
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002095#define WINPTY_DLL "winpty.dll"
2096
2097static HINSTANCE hWinPtyDLL = NULL;
2098
2099 int
2100dyn_winpty_init(void)
2101{
2102 int i;
2103 static struct
2104 {
2105 char *name;
2106 FARPROC *ptr;
2107 } winpty_entry[] =
2108 {
2109 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2110 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2111 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2112 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2113 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2114 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2115 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2116 {"winpty_free", (FARPROC*)&winpty_free},
2117 {"winpty_open", (FARPROC*)&winpty_open},
2118 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2119 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2120 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2121 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002122 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002123 {NULL, NULL}
2124 };
2125
2126 /* No need to initialize twice. */
2127 if (hWinPtyDLL)
2128 return 1;
2129 /* Load winpty.dll */
2130 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2131 if (!hWinPtyDLL)
2132 {
2133 EMSG2(_(e_loadlib), WINPTY_DLL);
2134 return 0;
2135 }
2136 for (i = 0; winpty_entry[i].name != NULL
2137 && winpty_entry[i].ptr != NULL; ++i)
2138 {
2139 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2140 winpty_entry[i].name)) == NULL)
2141 {
2142 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2143 return 0;
2144 }
2145 }
2146
2147 return 1;
2148}
2149
2150/*
2151 * Create a new terminal of "rows" by "cols" cells.
2152 * Store a reference in "term".
2153 * Return OK or FAIL.
2154 */
2155 static int
2156term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2157{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002158 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002159 channel_T *channel = NULL;
2160 job_T *job = NULL;
2161 jobopt_T opt;
2162 DWORD error;
2163 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2164 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002165 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002166
2167 if (!dyn_winpty_init())
2168 return FAIL;
2169
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002170 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002171 if (p == NULL)
2172 return FAIL;
2173
2174 job = job_alloc();
2175 if (job == NULL)
2176 goto failed;
2177
2178 channel = add_channel();
2179 if (channel == NULL)
2180 goto failed;
2181
2182 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2183 if (term->tl_winpty_config == NULL)
2184 goto failed;
2185
2186 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2187 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2188 if (term->tl_winpty == NULL)
2189 goto failed;
2190
2191 spawn_config = winpty_spawn_config_new(
2192 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2193 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2194 NULL,
2195 p,
2196 NULL,
2197 NULL,
2198 &winpty_err);
2199 if (spawn_config == NULL)
2200 goto failed;
2201
2202 channel = add_channel();
2203 if (channel == NULL)
2204 goto failed;
2205
2206 job = job_alloc();
2207 if (job == NULL)
2208 goto failed;
2209
2210 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2211 &child_thread_handle, &error, &winpty_err))
2212 goto failed;
2213
2214 channel_set_pipes(channel,
2215 (sock_T) CreateFileW(
2216 winpty_conin_name(term->tl_winpty),
2217 GENERIC_WRITE, 0, NULL,
2218 OPEN_EXISTING, 0, NULL),
2219 (sock_T) CreateFileW(
2220 winpty_conout_name(term->tl_winpty),
2221 GENERIC_READ, 0, NULL,
2222 OPEN_EXISTING, 0, NULL),
2223 (sock_T) CreateFileW(
2224 winpty_conerr_name(term->tl_winpty),
2225 GENERIC_READ, 0, NULL,
2226 OPEN_EXISTING, 0, NULL));
2227
2228 jo = CreateJobObject(NULL, NULL);
2229 if (jo == NULL)
2230 goto failed;
2231
2232 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002233 {
2234 /* Failed, switch the way to terminate process with TerminateProcess. */
2235 CloseHandle(jo);
2236 jo = NULL;
2237 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002238
2239 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002240 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002241
2242 create_vterm(term, rows, cols);
2243
2244 setup_job_options(&opt, rows, cols);
2245 channel_set_job(channel, job, &opt);
2246
2247 job->jv_channel = channel;
2248 job->jv_proc_info.hProcess = child_process_handle;
2249 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2250 job->jv_job_object = jo;
2251 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002252 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002253 term->tl_job = job;
2254
2255 return OK;
2256
2257failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002258 if (spawn_config != NULL)
2259 winpty_spawn_config_free(spawn_config);
2260 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002261 if (channel != NULL)
2262 channel_clear(channel);
2263 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002264 {
2265 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002266 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002267 }
2268 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002269 if (jo != NULL)
2270 CloseHandle(jo);
2271 if (term->tl_winpty != NULL)
2272 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002273 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002274 if (term->tl_winpty_config != NULL)
2275 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002276 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002277 if (winpty_err != NULL)
2278 {
2279 char_u *msg = utf16_to_enc(
2280 (short_u *)winpty_error_msg(winpty_err), NULL);
2281
2282 EMSG(msg);
2283 winpty_error_free(winpty_err);
2284 }
2285 return FAIL;
2286}
2287
2288/*
2289 * Free the terminal emulator part of "term".
2290 */
2291 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002292term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002293{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002294 if (term->tl_winpty != NULL)
2295 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002296 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002297 if (term->tl_winpty_config != NULL)
2298 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002299 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002300 if (term->tl_vterm != NULL)
2301 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002302 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002303}
2304
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002305/*
2306 * Request size to terminal.
2307 */
2308 static void
2309term_report_winsize(term_T *term, int rows, int cols)
2310{
2311 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2312}
2313
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002314# else
2315
2316/**************************************
2317 * 3. Unix-like implementation.
2318 */
2319
2320/*
2321 * Create a new terminal of "rows" by "cols" cells.
2322 * Start job for "cmd".
2323 * Store the pointers in "term".
2324 * Return OK or FAIL.
2325 */
2326 static int
2327term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2328{
2329 typval_T argvars[2];
2330 jobopt_T opt;
2331
2332 create_vterm(term, rows, cols);
2333
2334 argvars[0].v_type = VAR_STRING;
2335 argvars[0].vval.v_string = cmd;
2336 setup_job_options(&opt, rows, cols);
2337 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002338 if (term->tl_job != NULL)
2339 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002340
Bram Moolenaar61a66052017-07-22 18:39:00 +02002341 return term->tl_job != NULL
2342 && term->tl_job->jv_channel != NULL
2343 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002344}
2345
2346/*
2347 * Free the terminal emulator part of "term".
2348 */
2349 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002350term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002351{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002352 if (term->tl_vterm != NULL)
2353 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002354 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002355}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002356
2357/*
2358 * Request size to terminal.
2359 */
2360 static void
2361term_report_winsize(term_T *term, int rows, int cols)
2362{
2363 /* Use an ioctl() to report the new window size to the job. */
2364 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2365 {
2366 int fd = -1;
2367 int part;
2368
2369 for (part = PART_OUT; part < PART_COUNT; ++part)
2370 {
2371 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2372 if (isatty(fd))
2373 break;
2374 }
2375 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2376 mch_stop_job(term->tl_job, (char_u *)"winch");
2377 }
2378}
2379
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002380# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002381
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002382#endif /* FEAT_TERMINAL */