blob: 1863b598610e69780a9056859c0ecd9c694a0a76 [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 Moolenaard85f2712017-07-28 21:51:57 +020039 * - For the scrollback buffer store lines in the buffer, only attributes in
40 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020041 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020042 * - Need an option or argument to drop the window+buffer right away, to be
43 * used for a shell or Vim.
Bram Moolenaard85f2712017-07-28 21:51:57 +020044 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaard85f2712017-07-28 21:51:57 +020045 * - do not store terminal buffer in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020046 * - add a character in :ls output
Bram Moolenaar938783d2017-07-16 20:13:26 +020047 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaard85f2712017-07-28 21:51:57 +020048 * - when closing window and job has ended, make buffer hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020049 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020050 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020051 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020052 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020053 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020054 * - implement "term" for job_start(): more job options when starting a
55 * terminal.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020056 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
57 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020058 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020059 */
60
61#include "vim.h"
62
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020063#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020064
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020065#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066# define MIN(x,y) (x < y ? x : y)
67# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020068#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020069
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020070#include "libvterm/include/vterm.h"
71
Bram Moolenaard85f2712017-07-28 21:51:57 +020072typedef struct sb_line_S {
73 int sb_cols; /* can differ per line */
74 VTermScreenCell *sb_cells; /* allocated */
75} sb_line_T;
76
Bram Moolenaare4f25e42017-07-07 11:54:15 +020077/* typedef term_T in structs.h */
78struct terminal_S {
79 term_T *tl_next;
80
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020081#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020082 void *tl_winpty_config;
83 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020084#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020085 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020086 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020087 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020088
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020089 /* last known vterm size */
90 int tl_rows;
91 int tl_cols;
92 /* vterm size does not follow window size */
93 int tl_rows_fixed;
94 int tl_cols_fixed;
95
Bram Moolenaar21554412017-07-24 21:44:43 +020096 char_u *tl_title; /* NULL or allocated */
97 char_u *tl_status_text; /* NULL or allocated */
98
Bram Moolenaare4f25e42017-07-07 11:54:15 +020099 /* Range of screen rows to update. Zero based. */
100 int tl_dirty_row_start; /* -1 if nothing dirty */
101 int tl_dirty_row_end; /* row below last one to update */
102
Bram Moolenaard85f2712017-07-28 21:51:57 +0200103 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200104 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200105
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200106 pos_T tl_cursor;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200107 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200108};
109
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200110/*
111 * List of all active terminals.
112 */
113static term_T *first_term = NULL;
114
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200115
116#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
117#define KEY_BUF_LEN 200
118
119/*
120 * Functions with separate implementation for MS-Windows and Unix-like systems.
121 */
122static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200123static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200124static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200125
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200126/**************************************
127 * 1. Generic code for all systems.
128 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200129
130/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200131 * Determine the terminal size from 'termsize' and the current window.
132 * Assumes term->tl_rows and term->tl_cols are zero.
133 */
134 static void
135set_term_and_win_size(term_T *term)
136{
137 if (*curwin->w_p_tms != NUL)
138 {
139 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
140
141 term->tl_rows = atoi((char *)curwin->w_p_tms);
142 term->tl_cols = atoi((char *)p);
143 }
144 if (term->tl_rows == 0)
145 term->tl_rows = curwin->w_height;
146 else
147 {
148 win_setheight_win(term->tl_rows, curwin);
149 term->tl_rows_fixed = TRUE;
150 }
151 if (term->tl_cols == 0)
152 term->tl_cols = curwin->w_width;
153 else
154 {
155 win_setwidth_win(term->tl_cols, curwin);
156 term->tl_cols_fixed = TRUE;
157 }
158}
159
160/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200161 * ":terminal": open a terminal window and execute a job in it.
162 */
163 void
164ex_terminal(exarg_T *eap)
165{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200166 exarg_T split_ea;
167 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200168 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200169 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200170
171 if (check_restricted() || check_secure())
172 return;
173
174 term = (term_T *)alloc_clear(sizeof(term_T));
175 if (term == NULL)
176 return;
177 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200178 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200179 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200180
181 /* Open a new window or tab. */
182 vim_memset(&split_ea, 0, sizeof(split_ea));
183 split_ea.cmdidx = CMD_new;
184 split_ea.cmd = (char_u *)"new";
185 split_ea.arg = (char_u *)"";
186 ex_splitview(&split_ea);
187 if (curwin == old_curwin)
188 {
189 /* split failed */
190 vim_free(term);
191 return;
192 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200193 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200194 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200195
196 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200197 term->tl_next = first_term;
198 first_term = term;
199
Bram Moolenaar293424c2017-07-26 23:11:01 +0200200 if (cmd == NULL || *cmd == NUL)
201 cmd = p_sh;
202
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200203 if (buflist_findname(cmd) == NULL)
204 curbuf->b_ffname = vim_strsave(cmd);
205 else
206 {
207 int i;
208 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200209 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200210
211 for (i = 1; p != NULL; ++i)
212 {
213 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
214 if (buflist_findname(p) == NULL)
215 {
216 curbuf->b_ffname = p;
217 break;
218 }
219 }
220 }
221 curbuf->b_fname = curbuf->b_ffname;
222
223 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
224 curbuf->b_changed = TRUE;
225 curbuf->b_p_ma = FALSE;
226 set_string_option_direct((char_u *)"buftype", -1,
227 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200228
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200229 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200230
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200231 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200232 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200233 {
234 /* store the size we ended up with */
235 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200236 }
237 else
238 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200239 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200240
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200241 /* Wiping out the buffer will also close the window and call
242 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200243 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200244 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200245
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200246 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247}
248
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200249/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200250 * Free the scrollback buffer for "term".
251 */
252 static void
253free_scrollback(term_T *term)
254{
255 int i;
256
257 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
258 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
259 ga_clear(&term->tl_scrollback);
260}
261
262/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200263 * Free a terminal and everything it refers to.
264 * Kills the job if there is one.
265 * Called when wiping out a buffer.
266 */
267 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200268free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200269{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200270 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200271 term_T *tp;
272
273 if (term == NULL)
274 return;
275 if (first_term == term)
276 first_term = term->tl_next;
277 else
278 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
279 if (tp->tl_next == term)
280 {
281 tp->tl_next = term->tl_next;
282 break;
283 }
284
285 if (term->tl_job != NULL)
286 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200287 if (term->tl_job->jv_status != JOB_ENDED
288 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200289 job_stop(term->tl_job, NULL, "kill");
290 job_unref(term->tl_job);
291 }
292
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200293 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200294
295 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200296 vim_free(term->tl_title);
297 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200298 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200299 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200300}
301
302/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200303 * Write job output "msg[len]" to the vterm.
304 */
305 static void
306term_write_job_output(term_T *term, char_u *msg, size_t len)
307{
308 VTerm *vterm = term->tl_vterm;
309 char_u *p;
310 size_t done;
311 size_t len_now;
312
313 for (done = 0; done < len; done += len_now)
314 {
315 for (p = msg + done; p < msg + len; )
316 {
317 if (*p == NL)
318 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200319 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200320 }
321 len_now = p - msg - done;
322 vterm_input_write(vterm, (char *)msg + done, len_now);
323 if (p < msg + len && *p == NL)
324 {
325 /* Convert NL to CR-NL, that appears to work best. */
326 vterm_input_write(vterm, "\r\n", 2);
327 ++len_now;
328 }
329 }
330
331 /* this invokes the damage callbacks */
332 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
333}
334
Bram Moolenaar1c844932017-07-24 23:36:41 +0200335 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200336update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200337{
Bram Moolenaar1c844932017-07-24 23:36:41 +0200338 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200339 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200340 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200341 if (term->tl_cursor_visible)
342 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200343 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200344#ifdef FEAT_GUI
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200345 if (gui.in_use && term->tl_cursor_visible)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200346 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200347#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200348 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200349}
350
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200351/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200352 * Invoked when "msg" output from a job was received. Write it to the terminal
353 * of "buffer".
354 */
355 void
356write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
357{
358 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200359 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200360
Bram Moolenaard85f2712017-07-28 21:51:57 +0200361 if (term->tl_vterm == NULL)
362 {
363 ch_logn(channel, "NOT writing %d bytes to terminal", (int)len);
364 return;
365 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200366 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200367 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200368
369 /* TODO: only update once in a while. */
370 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200371 update_cursor(term, TRUE);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200372}
373
374/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200375 * Send a mouse position and click to the vterm
376 */
377 static int
378term_send_mouse(VTerm *vterm, int button, int pressed)
379{
380 VTermModifier mod = VTERM_MOD_NONE;
381
382 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
383 mouse_col - W_WINCOL(curwin), mod);
384 vterm_mouse_button(vterm, button, pressed, mod);
385 return TRUE;
386}
387
388/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200389 * Convert typed key "c" into bytes to send to the job.
390 * Return the number of bytes in "buf".
391 */
392 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200393term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200394{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200395 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200396 VTermKey key = VTERM_KEY_NONE;
397 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200398 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200399
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200400 switch (c)
401 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200402 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200403 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200404 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
405 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200406 case K_DEL: key = VTERM_KEY_DEL; break;
407 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200408 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
409 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200410 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200411 case K_S_END: mod = VTERM_MOD_SHIFT;
412 key = VTERM_KEY_END; break;
413 case K_C_END: mod = VTERM_MOD_CTRL;
414 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200415 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
416 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
417 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
418 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
419 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
420 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
421 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
422 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
423 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
424 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
425 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
426 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
427 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200428 case K_S_HOME: mod = VTERM_MOD_SHIFT;
429 key = VTERM_KEY_HOME; break;
430 case K_C_HOME: mod = VTERM_MOD_CTRL;
431 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200432 case K_INS: key = VTERM_KEY_INS; break;
433 case K_K0: key = VTERM_KEY_KP_0; break;
434 case K_K1: key = VTERM_KEY_KP_1; break;
435 case K_K2: key = VTERM_KEY_KP_2; break;
436 case K_K3: key = VTERM_KEY_KP_3; break;
437 case K_K4: key = VTERM_KEY_KP_4; break;
438 case K_K5: key = VTERM_KEY_KP_5; break;
439 case K_K6: key = VTERM_KEY_KP_6; break;
440 case K_K7: key = VTERM_KEY_KP_7; break;
441 case K_K8: key = VTERM_KEY_KP_8; break;
442 case K_K9: key = VTERM_KEY_KP_9; break;
443 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
444 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
445 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
446 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
447 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
448 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
449 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
450 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
451 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
452 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
453 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
454 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
455 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200456 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
457 key = VTERM_KEY_LEFT; break;
458 case K_C_LEFT: mod = VTERM_MOD_CTRL;
459 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200460 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
461 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
462 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200463 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
464 key = VTERM_KEY_RIGHT; break;
465 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
466 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200467 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200468 case K_S_UP: mod = VTERM_MOD_SHIFT;
469 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200470 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200471
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200472 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
473 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
474 case K_MOUSELEFT: /* TODO */ return 0;
475 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200476
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200477 case K_LEFTMOUSE:
478 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
479 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
480 case K_LEFTRELEASE:
481 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
482 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
483 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
484 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
485 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
486 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
487 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
488 case K_X1MOUSE: /* TODO */ return 0;
489 case K_X1DRAG: /* TODO */ return 0;
490 case K_X1RELEASE: /* TODO */ return 0;
491 case K_X2MOUSE: /* TODO */ return 0;
492 case K_X2DRAG: /* TODO */ return 0;
493 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200494
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200495 case K_IGNORE: return 0;
496 case K_NOP: return 0;
497 case K_UNDO: return 0;
498 case K_HELP: return 0;
499 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
500 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
501 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
502 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
503 case K_SELECT: return 0;
504#ifdef FEAT_GUI
505 case K_VER_SCROLLBAR: return 0;
506 case K_HOR_SCROLLBAR: return 0;
507#endif
508#ifdef FEAT_GUI_TABLINE
509 case K_TABLINE: return 0;
510 case K_TABMENU: return 0;
511#endif
512#ifdef FEAT_NETBEANS_INTG
513 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
514#endif
515#ifdef FEAT_DND
516 case K_DROP: return 0;
517#endif
518#ifdef FEAT_AUTOCMD
519 case K_CURSORHOLD: return 0;
520#endif
521 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
522 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200523 }
524
525 /*
526 * Convert special keys to vterm keys:
527 * - Write keys to vterm: vterm_keyboard_key()
528 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200529 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200530 */
531 if (key != VTERM_KEY_NONE)
532 /* Special key, let vterm convert it. */
533 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200534 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200535 /* Normal character, let vterm convert it. */
536 vterm_keyboard_unichar(vterm, c, mod);
537
538 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200539 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200540}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200541
Bram Moolenaar938783d2017-07-16 20:13:26 +0200542/*
Bram Moolenaard85f2712017-07-28 21:51:57 +0200543 * Return TRUE if the job for "buf" is still running.
544 */
545 static int
546term_job_running(term_T *term)
547{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200548 /* Also consider the job finished when the channel is closed, to avoid a
549 * race condition when updating the title. */
550 return term->tl_job != NULL
551 && term->tl_job->jv_status == JOB_STARTED
552 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200553}
554
555/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200556 * Get a key from the user without mapping.
557 * TODO: use terminal mode mappings.
558 */
559 static int
560term_vgetc()
561{
562 int c;
563
564 ++no_mapping;
565 ++allow_keys;
566 got_int = FALSE;
567 c = vgetc();
568 --no_mapping;
569 --allow_keys;
570 return c;
571}
572
573/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200574 * Send keys to terminal.
575 */
576 static int
577send_keys_to_term(term_T *term, int c, int typed)
578{
579 char msg[KEY_BUF_LEN];
580 size_t len;
581 static int mouse_was_outside = FALSE;
582 int dragging_outside = FALSE;
583
584 /* Catch keys that need to be handled as in Normal mode. */
585 switch (c)
586 {
587 case NUL:
588 case K_ZERO:
589 if (typed)
590 stuffcharReadbuff(c);
591 return FAIL;
592
593 case K_IGNORE:
594 return FAIL;
595
596 case K_LEFTDRAG:
597 case K_MIDDLEDRAG:
598 case K_RIGHTDRAG:
599 case K_X1DRAG:
600 case K_X2DRAG:
601 dragging_outside = mouse_was_outside;
602 /* FALLTHROUGH */
603 case K_LEFTMOUSE:
604 case K_LEFTMOUSE_NM:
605 case K_LEFTRELEASE:
606 case K_LEFTRELEASE_NM:
607 case K_MIDDLEMOUSE:
608 case K_MIDDLERELEASE:
609 case K_RIGHTMOUSE:
610 case K_RIGHTRELEASE:
611 case K_X1MOUSE:
612 case K_X1RELEASE:
613 case K_X2MOUSE:
614 case K_X2RELEASE:
615 if (mouse_row < W_WINROW(curwin)
616 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
617 || mouse_col < W_WINCOL(curwin)
618 || mouse_col >= W_ENDCOL(curwin)
619 || dragging_outside)
620 {
621 /* click outside the current window */
622 if (typed)
623 {
624 stuffcharReadbuff(c);
625 mouse_was_outside = TRUE;
626 }
627 return FAIL;
628 }
629 }
630 if (typed)
631 mouse_was_outside = FALSE;
632
633 /* Convert the typed key to a sequence of bytes for the job. */
634 len = term_convert_key(term, c, msg);
635 if (len > 0)
636 /* TODO: if FAIL is returned, stop? */
637 channel_send(term->tl_job->jv_channel, PART_IN,
638 (char_u *)msg, (int)len, NULL);
639
640 return OK;
641}
642
643/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200644 * Wait for input and send it to the job.
645 * Return when the start of a CTRL-W command is typed or anything else that
646 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200647 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
648 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200649 */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200650 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200651terminal_loop(void)
652{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200653 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200654 int termkey = 0;
655
Bram Moolenaard85f2712017-07-28 21:51:57 +0200656 if (curbuf->b_term->tl_vterm == NULL || !term_job_running(curbuf->b_term))
657 /* job finished */
658 return OK;
659
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200660 if (*curwin->w_p_tk != NUL)
661 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200662
663 for (;;)
664 {
665 /* TODO: skip screen update when handling a sequence of keys. */
666 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200667 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200668 c = term_vgetc();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200669 if (curbuf->b_term->tl_vterm == NULL
670 || !term_job_running(curbuf->b_term))
671 /* job finished while waiting for a character */
672 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200673
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200674 if (c == (termkey == 0 ? Ctrl_W : termkey))
675 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200676#ifdef FEAT_CMDL_INFO
677 if (add_to_showcmd(c))
678 out_flush();
679#endif
680 c = term_vgetc();
681#ifdef FEAT_CMDL_INFO
682 clear_showcmd();
683#endif
Bram Moolenaard85f2712017-07-28 21:51:57 +0200684 if (curbuf->b_term->tl_vterm == NULL
685 || !term_job_running(curbuf->b_term))
686 /* job finished while waiting for a character */
687 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200688
689 if (termkey == 0 && c == '.')
690 /* "CTRL-W .": send CTRL-W to the job */
691 c = Ctrl_W;
692 else if (termkey == 0 || c != termkey)
693 {
694 stuffcharReadbuff(Ctrl_W);
695 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200696 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200697 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200698 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200699 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
700 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200701 }
Bram Moolenaard85f2712017-07-28 21:51:57 +0200702 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200703}
704
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200705/*
706 * Called when a job has finished.
707 */
708 void
709term_job_ended(job_T *job)
710{
711 term_T *term;
712 int did_one = FALSE;
713
714 for (term = first_term; term != NULL; term = term->tl_next)
715 if (term->tl_job == job)
716 {
717 vim_free(term->tl_title);
718 term->tl_title = NULL;
719 vim_free(term->tl_status_text);
720 term->tl_status_text = NULL;
721 redraw_buf_and_status_later(term->tl_buffer, VALID);
722 did_one = TRUE;
723 }
724 if (did_one)
725 redraw_statuslines();
726 if (curbuf->b_term != NULL)
727 {
728 if (curbuf->b_term->tl_job == job)
729 maketitle();
730 update_cursor(curbuf->b_term, TRUE);
731 }
732}
733
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200734 static void
735position_cursor(win_T *wp, VTermPos *pos)
736{
737 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
738 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
739}
740
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200741 static void
742may_toggle_cursor(term_T *term)
743{
744 if (curbuf == term->tl_buffer)
745 {
746 if (term->tl_cursor_visible)
747 cursor_on();
748 else
749 cursor_off();
750 }
751}
752
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200753 static int
754handle_damage(VTermRect rect, void *user)
755{
756 term_T *term = (term_T *)user;
757
758 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
759 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
760 redraw_buf_later(term->tl_buffer, NOT_VALID);
761 return 1;
762}
763
764 static int
765handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
766{
767 term_T *term = (term_T *)user;
768
769 /* TODO */
770 redraw_buf_later(term->tl_buffer, NOT_VALID);
771 return 1;
772}
773
774 static int
775handle_movecursor(
776 VTermPos pos,
777 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200778 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200779 void *user)
780{
781 term_T *term = (term_T *)user;
782 win_T *wp;
783 int is_current = FALSE;
784
785 FOR_ALL_WINDOWS(wp)
786 {
787 if (wp->w_buffer == term->tl_buffer)
788 {
789 position_cursor(wp, &pos);
790 if (wp == curwin)
791 is_current = TRUE;
792 }
793 }
794
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200795 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200796 if (is_current)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200797 {
798 may_toggle_cursor(term);
799 update_cursor(term, TRUE);
800 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200801
802 return 1;
803}
804
Bram Moolenaar21554412017-07-24 21:44:43 +0200805 static int
806handle_settermprop(
807 VTermProp prop,
808 VTermValue *value,
809 void *user)
810{
811 term_T *term = (term_T *)user;
812
813 switch (prop)
814 {
815 case VTERM_PROP_TITLE:
816 vim_free(term->tl_title);
817 term->tl_title = vim_strsave((char_u *)value->string);
818 vim_free(term->tl_status_text);
819 term->tl_status_text = NULL;
820 if (term == curbuf->b_term)
821 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200822 break;
823
824 case VTERM_PROP_CURSORVISIBLE:
825 term->tl_cursor_visible = value->boolean;
826 may_toggle_cursor(term);
827 out_flush();
828 break;
829
Bram Moolenaar21554412017-07-24 21:44:43 +0200830 default:
831 break;
832 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200833 /* Always return 1, otherwise vterm doesn't store the value internally. */
834 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +0200835}
836
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200837/*
838 * The job running in the terminal resized the terminal.
839 */
840 static int
841handle_resize(int rows, int cols, void *user)
842{
843 term_T *term = (term_T *)user;
844 win_T *wp;
845
846 term->tl_rows = rows;
847 term->tl_cols = cols;
848 FOR_ALL_WINDOWS(wp)
849 {
850 if (wp->w_buffer == term->tl_buffer)
851 {
852 win_setheight_win(rows, wp);
853 win_setwidth_win(cols, wp);
854 }
855 }
856
857 redraw_buf_later(term->tl_buffer, NOT_VALID);
858 return 1;
859}
860
Bram Moolenaard85f2712017-07-28 21:51:57 +0200861/*
862 * Handle a line that is pushed off the top of the screen.
863 */
864 static int
865handle_pushline(int cols, const VTermScreenCell *cells, void *user)
866{
867 term_T *term = (term_T *)user;
868
869 /* TODO: Limit the number of lines that are stored. */
870 /* TODO: put the text in the buffer. */
871 if (ga_grow(&term->tl_scrollback, 1) == OK)
872 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200873 VTermScreenCell *p = NULL;
874 int len = 0;
875 int i;
876 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200877
878 /* do not store empty cells at the end */
879 for (i = 0; i < cols; ++i)
880 if (cells[i].chars[0] != 0)
881 len = i + 1;
882
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200883 if (len > 0)
884 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200885 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +0200886 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200887
888 line = (sb_line_T *)term->tl_scrollback.ga_data
889 + term->tl_scrollback.ga_len;
890 line->sb_cols = len;
891 line->sb_cells = p;
892 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200893 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200894 }
895 return 0; /* ignored */
896}
897
898/*
899 * Fill the buffer with the scrollback lines and current lines of the terminal.
900 * Called after the job has ended.
901 */
902 static void
903move_scrollback_to_buffer(term_T *term)
904{
905 linenr_T lnum;
906 garray_T ga;
907 int c;
908 int col;
909 int i;
910 win_T *wp;
911 int len;
912 int lines_skipped = 0;
913 VTermPos pos;
914 VTermScreenCell cell;
915 VTermScreenCell *p;
916 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
917
918 /* Append the the visible lines to the scrollback. */
919 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
920 {
921 len = 0;
922 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
923 if (vterm_screen_get_cell(screen, pos, &cell) != 0
924 && cell.chars[0] != NUL)
925 len = pos.col + 1;
926
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200927 if (len == 0)
928 ++lines_skipped;
929 else
Bram Moolenaard85f2712017-07-28 21:51:57 +0200930 {
931 while (lines_skipped > 0)
932 {
933 /* Line was skipped, add an empty line. */
934 --lines_skipped;
935 if (ga_grow(&term->tl_scrollback, 1) == OK)
936 {
937 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
938 + term->tl_scrollback.ga_len;
939
940 line->sb_cols = 0;
941 line->sb_cells = NULL;
942 ++term->tl_scrollback.ga_len;
943 }
944 }
945
946 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
947 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
948 {
949 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
950 + term->tl_scrollback.ga_len;
951
952 for (pos.col = 0; pos.col < len; ++pos.col)
953 {
954 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
955 vim_memset(p + pos.col, 0, sizeof(cell));
956 else
957 p[pos.col] = cell;
958 }
959 line->sb_cols = len;
960 line->sb_cells = p;
961 ++term->tl_scrollback.ga_len;
962 }
963 else
964 vim_free(p);
965 }
966 }
967
968 /* Add the text to the buffer. */
969 ga_init2(&ga, 1, 100);
970 for (lnum = 0; lnum < term->tl_scrollback.ga_len; ++lnum)
971 {
972 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
973
974 ga.ga_len = 0;
975 for (col = 0; col < line->sb_cols; ++col)
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200976 {
977 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
978 goto failed;
979 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
Bram Moolenaard85f2712017-07-28 21:51:57 +0200980 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
981 (char_u *)ga.ga_data + ga.ga_len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +0200982 }
983 if (ga_grow(&ga, 1) == FAIL)
984 goto failed;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200985 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
986 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
987 }
988
989 /* Delete the empty line that was in the empty buffer. */
990 curbuf = term->tl_buffer;
991 ml_delete(lnum + 1, FALSE);
992 curbuf = curwin->w_buffer;
993
994failed:
995 ga_clear(&ga);
996
997 FOR_ALL_WINDOWS(wp)
998 {
999 if (wp->w_buffer == term->tl_buffer)
1000 {
1001 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1002 wp->w_cursor.col = 0;
1003 wp->w_valid = 0;
1004 }
1005 }
1006}
1007
Bram Moolenaar21554412017-07-24 21:44:43 +02001008static VTermScreenCallbacks screen_callbacks = {
1009 handle_damage, /* damage */
1010 handle_moverect, /* moverect */
1011 handle_movecursor, /* movecursor */
1012 handle_settermprop, /* settermprop */
1013 NULL, /* bell */
1014 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001015 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001016 NULL /* sb_popline */
1017};
1018
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001019/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001020 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001021 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001022 */
1023 void
1024term_channel_closed(channel_T *ch)
1025{
1026 term_T *term;
1027 int did_one = FALSE;
1028
1029 for (term = first_term; term != NULL; term = term->tl_next)
1030 if (term->tl_job == ch->ch_job)
1031 {
1032 vim_free(term->tl_title);
1033 term->tl_title = NULL;
1034 vim_free(term->tl_status_text);
1035 term->tl_status_text = NULL;
1036
1037 /* move the lines into the buffer and free the vterm */
1038 move_scrollback_to_buffer(term);
1039 term_free_vterm(term);
1040
1041 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1042 did_one = TRUE;
1043 }
1044 if (did_one)
1045 {
1046 redraw_statuslines();
1047
1048 /* Need to break out of vgetc(). */
1049 ins_char_typebuf(K_IGNORE);
1050
1051 if (curbuf->b_term != NULL)
1052 {
1053 if (curbuf->b_term->tl_job == ch->ch_job)
1054 maketitle();
1055 update_cursor(curbuf->b_term, TRUE);
1056 }
1057 }
1058}
1059
1060/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001061 * Reverse engineer the RGB value into a cterm color index.
1062 * First color is 1. Return 0 if no match found.
1063 */
1064 static int
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001065color2index(VTermColor *color, int foreground)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001066{
1067 int red = color->red;
1068 int blue = color->blue;
1069 int green = color->green;
1070
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001071 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001072 if (red == 0)
1073 {
1074 if (green == 0)
1075 {
1076 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001077 return lookup_color(0, foreground) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001078 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001079 return lookup_color(1, foreground) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001080 }
1081 else if (green == 224)
1082 {
1083 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001084 return lookup_color(2, foreground) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001085 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001086 return lookup_color(3, foreground) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001087 }
1088 }
1089 else if (red == 224)
1090 {
1091 if (green == 0)
1092 {
1093 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001094 return lookup_color(4, foreground) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001095 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001096 return lookup_color(5, foreground) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001097 }
1098 else if (green == 224)
1099 {
1100 if (blue == 0)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001101 return lookup_color(6, foreground) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001102 if (blue == 224)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001103 return lookup_color(8, foreground) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001104 }
1105 }
1106 else if (red == 128)
1107 {
1108 if (green == 128 && blue == 128)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001109 return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001110 }
1111 else if (red == 255)
1112 {
1113 if (green == 64)
1114 {
1115 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001116 return lookup_color(20, foreground) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001117 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001118 return lookup_color(22, foreground) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001119 }
1120 else if (green == 255)
1121 {
1122 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001123 return lookup_color(24, foreground) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001124 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001125 return lookup_color(26, foreground) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001126 }
1127 }
1128 else if (red == 64)
1129 {
1130 if (green == 64)
1131 {
1132 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001133 return lookup_color(14, foreground) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001134 }
1135 else if (green == 255)
1136 {
1137 if (blue == 64)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001138 return lookup_color(16, foreground) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001139 if (blue == 255)
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001140 return lookup_color(18, foreground) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001141 }
1142 }
1143 if (t_colors >= 256)
1144 {
1145 if (red == blue && red == green)
1146 {
1147 /* 24-color greyscale */
1148 static int cutoff[23] = {
1149 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1150 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1151 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1152 int i;
1153
1154 for (i = 0; i < 23; ++i)
1155 if (red < cutoff[i])
1156 return i + 233;
1157 return 256;
1158 }
1159
1160 /* 216-color cube */
1161 return 17 + ((red + 25) / 0x33) * 36
1162 + ((green + 25) / 0x33) * 6
1163 + (blue + 25) / 0x33;
1164 }
1165 return 0;
1166}
1167
1168/*
1169 * Convert the attributes of a vterm cell into an attribute index.
1170 */
1171 static int
1172cell2attr(VTermScreenCell *cell)
1173{
1174 int attr = 0;
1175
1176 if (cell->attrs.bold)
1177 attr |= HL_BOLD;
1178 if (cell->attrs.underline)
1179 attr |= HL_UNDERLINE;
1180 if (cell->attrs.italic)
1181 attr |= HL_ITALIC;
1182 if (cell->attrs.strike)
1183 attr |= HL_STANDOUT;
1184 if (cell->attrs.reverse)
1185 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001186
1187#ifdef FEAT_GUI
1188 if (gui.in_use)
1189 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001190 guicolor_T fg, bg;
1191
1192 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1193 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1194 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001195 }
1196 else
1197#endif
1198#ifdef FEAT_TERMGUICOLORS
1199 if (p_tgc)
1200 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001201 guicolor_T fg, bg;
1202
1203 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1204 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1205
1206 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001207 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001208 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001209#endif
1210 {
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001211 return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
1212 color2index(&cell->bg, FALSE));
Bram Moolenaareeac6772017-07-23 15:48:37 +02001213 }
1214 return 0;
1215}
1216
1217/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001218 * Called to update the window that contains a terminal.
1219 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001220 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001221 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001222term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001223{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001224 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001225 VTerm *vterm;
1226 VTermScreen *screen;
1227 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001228 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001229
Bram Moolenaard85f2712017-07-28 21:51:57 +02001230 if (term == NULL || term->tl_vterm == NULL)
1231 return FAIL;
1232 vterm = term->tl_vterm;
1233 screen = vterm_obtain_screen(vterm);
1234 state = vterm_obtain_state(vterm);
1235
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001236 /*
1237 * If the window was resized a redraw will be triggered and we get here.
1238 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1239 */
1240 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1241 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001242 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001243 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1244 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1245 win_T *twp;
1246
1247 FOR_ALL_WINDOWS(twp)
1248 {
1249 /* When more than one window shows the same terminal, use the
1250 * smallest size. */
1251 if (twp->w_buffer == term->tl_buffer)
1252 {
1253 if (!term->tl_rows_fixed && rows > twp->w_height)
1254 rows = twp->w_height;
1255 if (!term->tl_cols_fixed && cols > twp->w_width)
1256 cols = twp->w_width;
1257 }
1258 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001259
1260 vterm_set_size(vterm, rows, cols);
1261 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
1262 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001263 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001264 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001265
1266 /* The cursor may have been moved when resizing. */
1267 vterm_state_get_cursorpos(state, &pos);
1268 position_cursor(wp, &pos);
1269
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001270 /* TODO: Only redraw what changed. */
1271 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001272 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001273 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001274 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001275
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001276 if (pos.row < term->tl_rows)
1277 {
1278 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001279 {
1280 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001281 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001282
Bram Moolenaareeac6772017-07-23 15:48:37 +02001283 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1284 vim_memset(&cell, 0, sizeof(cell));
1285
1286 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001287 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001288 if (c == NUL)
1289 {
1290 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001291#if defined(FEAT_MBYTE)
1292 if (enc_utf8)
1293 ScreenLinesUC[off] = NUL;
1294#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001295 }
1296 else
1297 {
1298#if defined(FEAT_MBYTE)
1299 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001300 {
1301 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001302 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001303 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001304 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001305 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001306 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001307 if (enc_utf8)
1308 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001309 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001310#else
1311 ScreenLines[off] = c;
1312#endif
1313 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001314 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001315
1316 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001317 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001318 if (cell.width == 2)
1319 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001320 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001321#if defined(FEAT_MBYTE)
1322 if (enc_utf8)
1323 ScreenLinesUC[off] = NUL;
1324#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001325 ++pos.col;
1326 ++off;
1327 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001328 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001329 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001330 else
1331 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001332
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001333 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1334 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001335 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001336
1337 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001338}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001339
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001340/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001341 * Return TRUE if "wp" is a terminal window where the job has finished.
1342 */
1343 int
1344term_is_finished(buf_T *buf)
1345{
1346 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1347}
1348
1349/*
1350 * The current buffer is going to be changed. If there is terminal
1351 * highlighting remove it now.
1352 */
1353 void
1354term_change_in_curbuf(void)
1355{
1356 term_T *term = curbuf->b_term;
1357
1358 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1359 {
1360 free_scrollback(term);
1361 redraw_buf_later(term->tl_buffer, NOT_VALID);
1362 }
1363}
1364
1365/*
1366 * Get the screen attribute for a position in the buffer.
1367 */
1368 int
1369term_get_attr(buf_T *buf, linenr_T lnum, int col)
1370{
1371 term_T *term = buf->b_term;
1372 sb_line_T *line;
1373
Bram Moolenaar70229f92017-07-29 16:01:53 +02001374 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001375 return 0;
1376 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1377 if (col >= line->sb_cols)
1378 return 0;
1379 return cell2attr(line->sb_cells + col);
1380}
1381
1382/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001383 * Set job options common for Unix and MS-Windows.
1384 */
1385 static void
1386setup_job_options(jobopt_T *opt, int rows, int cols)
1387{
1388 clear_job_options(opt);
1389 opt->jo_mode = MODE_RAW;
1390 opt->jo_out_mode = MODE_RAW;
1391 opt->jo_err_mode = MODE_RAW;
1392 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001393
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001394 opt->jo_io[PART_OUT] = JIO_BUFFER;
1395 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001396 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
1397
1398 opt->jo_modifiable[PART_OUT] = 0;
1399 opt->jo_modifiable[PART_ERR] = 0;
1400 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
1401
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001402 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
1403 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001404 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001405 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
1406
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001407 opt->jo_term_rows = rows;
1408 opt->jo_term_cols = cols;
1409}
1410
1411/*
1412 * Create a new vterm and initialize it.
1413 */
1414 static void
1415create_vterm(term_T *term, int rows, int cols)
1416{
1417 VTerm *vterm;
1418 VTermScreen *screen;
1419
1420 vterm = vterm_new(rows, cols);
1421 term->tl_vterm = vterm;
1422 screen = vterm_obtain_screen(vterm);
1423 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1424 /* TODO: depends on 'encoding'. */
1425 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001426
1427 /* Vterm uses a default black background. Set it to white when
1428 * 'background' is "light". */
1429 if (*p_bg == 'l')
1430 {
1431 VTermColor fg, bg;
1432
1433 fg.red = fg.green = fg.blue = 0;
1434 bg.red = bg.green = bg.blue = 255;
1435 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1436 }
1437
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001438 /* Required to initialize most things. */
1439 vterm_screen_reset(screen, 1 /* hard */);
1440}
1441
Bram Moolenaar21554412017-07-24 21:44:43 +02001442/*
1443 * Return the text to show for the buffer name and status.
1444 */
1445 char_u *
1446term_get_status_text(term_T *term)
1447{
1448 if (term->tl_status_text == NULL)
1449 {
1450 char_u *txt;
1451 size_t len;
1452
1453 if (term->tl_title != NULL)
1454 txt = term->tl_title;
1455 else if (term_job_running(term))
1456 txt = (char_u *)_("running");
1457 else
1458 txt = (char_u *)_("finished");
1459 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001460 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001461 if (term->tl_status_text != NULL)
1462 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1463 term->tl_buffer->b_fname, txt);
1464 }
1465 return term->tl_status_text;
1466}
1467
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001468/*
1469 * Mark references in jobs of terminals.
1470 */
1471 int
1472set_ref_in_term(int copyID)
1473{
1474 int abort = FALSE;
1475 term_T *term;
1476 typval_T tv;
1477
1478 for (term = first_term; term != NULL; term = term->tl_next)
1479 if (term->tl_job != NULL)
1480 {
1481 tv.v_type = VAR_JOB;
1482 tv.vval.v_job = term->tl_job;
1483 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1484 }
1485 return abort;
1486}
1487
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001488/*
1489 * "term_getattr(attr, name)" function
1490 */
1491 void
1492f_term_getattr(typval_T *argvars, typval_T *rettv)
1493{
1494 int attr;
1495 size_t i;
1496 char_u *name;
1497
1498 static struct {
1499 char *name;
1500 int attr;
1501 } attrs[] = {
1502 {"bold", HL_BOLD},
1503 {"italic", HL_ITALIC},
1504 {"underline", HL_UNDERLINE},
1505 {"strike", HL_STANDOUT},
1506 {"reverse", HL_INVERSE},
1507 };
1508
1509 attr = get_tv_number(&argvars[0]);
1510 name = get_tv_string_chk(&argvars[1]);
1511 if (name == NULL)
1512 return;
1513
1514 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1515 if (STRCMP(name, attrs[i].name) == 0)
1516 {
1517 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1518 break;
1519 }
1520}
1521
1522/*
1523 * Get the buffer from the first argument in "argvars".
1524 * Returns NULL when the buffer is not for a terminal window.
1525 */
1526 static buf_T *
1527term_get_buf(typval_T *argvars)
1528{
1529 buf_T *buf;
1530
1531 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1532 ++emsg_off;
1533 buf = get_buf_tv(&argvars[0], FALSE);
1534 --emsg_off;
1535 if (buf->b_term == NULL)
1536 return NULL;
1537 return buf;
1538}
1539
1540/*
1541 * "term_getjob(buf)" function
1542 */
1543 void
1544f_term_getjob(typval_T *argvars, typval_T *rettv)
1545{
1546 buf_T *buf = term_get_buf(argvars);
1547
1548 rettv->v_type = VAR_JOB;
1549 rettv->vval.v_job = NULL;
1550 if (buf == NULL)
1551 return;
1552
1553 rettv->vval.v_job = buf->b_term->tl_job;
1554 if (rettv->vval.v_job != NULL)
1555 ++rettv->vval.v_job->jv_refcount;
1556}
1557
1558/*
1559 * "term_getline(buf, row)" function
1560 */
1561 void
1562f_term_getline(typval_T *argvars, typval_T *rettv)
1563{
1564 buf_T *buf = term_get_buf(argvars);
1565 term_T *term;
1566 int row;
1567
1568 rettv->v_type = VAR_STRING;
1569 if (buf == NULL)
1570 return;
1571 term = buf->b_term;
1572 row = (int)get_tv_number(&argvars[1]);
1573
1574 if (term->tl_vterm == NULL)
1575 {
1576 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1577
1578 /* vterm is finished, get the text from the buffer */
1579 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1580 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1581 }
1582 else
1583 {
1584 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1585 VTermRect rect;
1586 int len;
1587 char_u *p;
1588
1589 len = term->tl_cols * MB_MAXBYTES + 1;
1590 p = alloc(len);
1591 if (p == NULL)
1592 return;
1593 rettv->vval.v_string = p;
1594
1595 rect.start_col = 0;
1596 rect.end_col = term->tl_cols;
1597 rect.start_row = row;
1598 rect.end_row = row + 1;
1599 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1600 }
1601}
1602
1603/*
1604 * "term_getsize(buf)" function
1605 */
1606 void
1607f_term_getsize(typval_T *argvars, typval_T *rettv)
1608{
1609 buf_T *buf = term_get_buf(argvars);
1610 list_T *l;
1611
1612 if (rettv_list_alloc(rettv) == FAIL)
1613 return;
1614 if (buf == NULL)
1615 return;
1616
1617 l = rettv->vval.v_list;
1618 list_append_number(l, buf->b_term->tl_rows);
1619 list_append_number(l, buf->b_term->tl_cols);
1620}
1621
1622/*
1623 * "term_list()" function
1624 */
1625 void
1626f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
1627{
1628 term_T *tp;
1629 list_T *l;
1630
1631 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
1632 return;
1633
1634 l = rettv->vval.v_list;
1635 for (tp = first_term; tp != NULL; tp = tp->tl_next)
1636 if (tp != NULL && tp->tl_buffer != NULL)
1637 if (list_append_number(l,
1638 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
1639 return;
1640}
1641
1642/*
1643 * "term_scrape(buf, row)" function
1644 */
1645 void
1646f_term_scrape(typval_T *argvars, typval_T *rettv)
1647{
1648 buf_T *buf = term_get_buf(argvars);
1649 VTermScreen *screen = NULL;
1650 VTermPos pos;
1651 list_T *l;
1652 term_T *term;
1653
1654 if (rettv_list_alloc(rettv) == FAIL)
1655 return;
1656 if (buf == NULL)
1657 return;
1658 term = buf->b_term;
1659 if (term->tl_vterm != NULL)
1660 screen = vterm_obtain_screen(term->tl_vterm);
1661
1662 l = rettv->vval.v_list;
1663 pos.row = (int)get_tv_number(&argvars[1]);
1664 for (pos.col = 0; pos.col < term->tl_cols; )
1665 {
1666 dict_T *dcell;
1667 VTermScreenCell cell;
1668 char_u rgb[8];
1669 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
1670 int off = 0;
1671 int i;
1672
1673 if (screen == NULL)
1674 {
1675 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
1676 sb_line_T *line;
1677
1678 /* vterm has finished, get the cell from scrollback */
1679 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
1680 break;
1681 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
1682 if (pos.col >= line->sb_cols)
1683 break;
1684 cell = line->sb_cells[pos.col];
1685 }
1686 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1687 break;
1688 dcell = dict_alloc();
1689 list_append_dict(l, dcell);
1690
1691 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
1692 {
1693 if (cell.chars[i] == 0)
1694 break;
1695 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
1696 }
1697 mbs[off] = NUL;
1698 dict_add_nr_str(dcell, "chars", 0, mbs);
1699
1700 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1701 cell.fg.red, cell.fg.green, cell.fg.blue);
1702 dict_add_nr_str(dcell, "fg", 0, rgb);
1703 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
1704 cell.bg.red, cell.bg.green, cell.bg.blue);
1705 dict_add_nr_str(dcell, "bg", 0, rgb);
1706
1707 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
1708 dict_add_nr_str(dcell, "width", cell.width, NULL);
1709
1710 ++pos.col;
1711 if (cell.width == 2)
1712 ++pos.col;
1713 }
1714}
1715
1716/*
1717 * "term_sendkeys(buf, keys)" function
1718 */
1719 void
1720f_term_sendkeys(typval_T *argvars, typval_T *rettv)
1721{
1722 buf_T *buf = term_get_buf(argvars);
1723 char_u *msg;
1724 term_T *term;
1725
1726 rettv->v_type = VAR_UNKNOWN;
1727 if (buf == NULL)
1728 return;
1729
1730 msg = get_tv_string_chk(&argvars[1]);
1731 if (msg == NULL)
1732 return;
1733 term = buf->b_term;
1734 if (term->tl_vterm == NULL)
1735 return;
1736
1737 while (*msg != NUL)
1738 {
1739 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
1740 msg += MB_PTR2LEN(msg);
1741 }
1742
1743 /* TODO: only update once in a while. */
1744 update_screen(0);
1745 if (buf == curbuf)
1746 update_cursor(term, TRUE);
1747}
1748
1749/*
1750 * "term_start(command, options)" function
1751 */
1752 void
1753f_term_start(typval_T *argvars, typval_T *rettv)
1754{
1755 char_u *cmd = get_tv_string_chk(&argvars[0]);
1756 exarg_T ea;
1757
1758 if (cmd == NULL)
1759 return;
1760 ea.arg = cmd;
1761 ex_terminal(&ea);
1762
1763 if (curbuf->b_term != NULL)
1764 rettv->vval.v_number = curbuf->b_fnum;
1765}
1766
1767/*
1768 * "term_wait" function
1769 */
1770 void
1771f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
1772{
1773 buf_T *buf = term_get_buf(argvars);
1774
1775 if (buf == NULL)
1776 return;
1777
1778 /* Get the job status, this will detect a job that finished. */
1779 if (buf->b_term->tl_job != NULL)
1780 (void)job_status(buf->b_term->tl_job);
1781
1782 /* Check for any pending channel I/O. */
1783 vpeekc_any();
1784 ui_delay(10L, FALSE);
1785
1786 /* Flushing messages on channels is hopefully sufficient.
1787 * TODO: is there a better way? */
1788 parse_queued_messages();
1789}
1790
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001791# ifdef WIN3264
1792
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001793/**************************************
1794 * 2. MS-Windows implementation.
1795 */
1796
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001797#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1798#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1799
Bram Moolenaar8a773062017-07-24 22:29:21 +02001800void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001801void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001802void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001803BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1804void (*winpty_config_set_initial_size)(void*, int, int);
1805LPCWSTR (*winpty_conin_name)(void*);
1806LPCWSTR (*winpty_conout_name)(void*);
1807LPCWSTR (*winpty_conerr_name)(void*);
1808void (*winpty_free)(void*);
1809void (*winpty_config_free)(void*);
1810void (*winpty_spawn_config_free)(void*);
1811void (*winpty_error_free)(void*);
1812LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001813BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001814
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001815#define WINPTY_DLL "winpty.dll"
1816
1817static HINSTANCE hWinPtyDLL = NULL;
1818
1819 int
1820dyn_winpty_init(void)
1821{
1822 int i;
1823 static struct
1824 {
1825 char *name;
1826 FARPROC *ptr;
1827 } winpty_entry[] =
1828 {
1829 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1830 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1831 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1832 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1833 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1834 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1835 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1836 {"winpty_free", (FARPROC*)&winpty_free},
1837 {"winpty_open", (FARPROC*)&winpty_open},
1838 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1839 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1840 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1841 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001842 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001843 {NULL, NULL}
1844 };
1845
1846 /* No need to initialize twice. */
1847 if (hWinPtyDLL)
1848 return 1;
1849 /* Load winpty.dll */
1850 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1851 if (!hWinPtyDLL)
1852 {
1853 EMSG2(_(e_loadlib), WINPTY_DLL);
1854 return 0;
1855 }
1856 for (i = 0; winpty_entry[i].name != NULL
1857 && winpty_entry[i].ptr != NULL; ++i)
1858 {
1859 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1860 winpty_entry[i].name)) == NULL)
1861 {
1862 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1863 return 0;
1864 }
1865 }
1866
1867 return 1;
1868}
1869
1870/*
1871 * Create a new terminal of "rows" by "cols" cells.
1872 * Store a reference in "term".
1873 * Return OK or FAIL.
1874 */
1875 static int
1876term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1877{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001878 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001879 channel_T *channel = NULL;
1880 job_T *job = NULL;
1881 jobopt_T opt;
1882 DWORD error;
1883 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1884 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001885 void *spawn_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001886
1887 if (!dyn_winpty_init())
1888 return FAIL;
1889
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001890 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001891 if (p == NULL)
1892 return FAIL;
1893
1894 job = job_alloc();
1895 if (job == NULL)
1896 goto failed;
1897
1898 channel = add_channel();
1899 if (channel == NULL)
1900 goto failed;
1901
1902 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1903 if (term->tl_winpty_config == NULL)
1904 goto failed;
1905
1906 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1907 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1908 if (term->tl_winpty == NULL)
1909 goto failed;
1910
1911 spawn_config = winpty_spawn_config_new(
1912 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1913 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1914 NULL,
1915 p,
1916 NULL,
1917 NULL,
1918 &winpty_err);
1919 if (spawn_config == NULL)
1920 goto failed;
1921
1922 channel = add_channel();
1923 if (channel == NULL)
1924 goto failed;
1925
1926 job = job_alloc();
1927 if (job == NULL)
1928 goto failed;
1929
1930 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1931 &child_thread_handle, &error, &winpty_err))
1932 goto failed;
1933
1934 channel_set_pipes(channel,
1935 (sock_T) CreateFileW(
1936 winpty_conin_name(term->tl_winpty),
1937 GENERIC_WRITE, 0, NULL,
1938 OPEN_EXISTING, 0, NULL),
1939 (sock_T) CreateFileW(
1940 winpty_conout_name(term->tl_winpty),
1941 GENERIC_READ, 0, NULL,
1942 OPEN_EXISTING, 0, NULL),
1943 (sock_T) CreateFileW(
1944 winpty_conerr_name(term->tl_winpty),
1945 GENERIC_READ, 0, NULL,
1946 OPEN_EXISTING, 0, NULL));
1947
1948 jo = CreateJobObject(NULL, NULL);
1949 if (jo == NULL)
1950 goto failed;
1951
1952 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001953 {
1954 /* Failed, switch the way to terminate process with TerminateProcess. */
1955 CloseHandle(jo);
1956 jo = NULL;
1957 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001958
1959 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001960 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001961
1962 create_vterm(term, rows, cols);
1963
1964 setup_job_options(&opt, rows, cols);
1965 channel_set_job(channel, job, &opt);
1966
1967 job->jv_channel = channel;
1968 job->jv_proc_info.hProcess = child_process_handle;
1969 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1970 job->jv_job_object = jo;
1971 job->jv_status = JOB_STARTED;
Bram Moolenaar0e83f022017-07-27 22:07:35 +02001972 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001973 term->tl_job = job;
1974
1975 return OK;
1976
1977failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02001978 if (spawn_config != NULL)
1979 winpty_spawn_config_free(spawn_config);
1980 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001981 if (channel != NULL)
1982 channel_clear(channel);
1983 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001984 {
1985 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001986 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001987 }
1988 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001989 if (jo != NULL)
1990 CloseHandle(jo);
1991 if (term->tl_winpty != NULL)
1992 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001993 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001994 if (term->tl_winpty_config != NULL)
1995 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001996 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001997 if (winpty_err != NULL)
1998 {
1999 char_u *msg = utf16_to_enc(
2000 (short_u *)winpty_error_msg(winpty_err), NULL);
2001
2002 EMSG(msg);
2003 winpty_error_free(winpty_err);
2004 }
2005 return FAIL;
2006}
2007
2008/*
2009 * Free the terminal emulator part of "term".
2010 */
2011 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002012term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002013{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002014 if (term->tl_winpty != NULL)
2015 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002016 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002017 if (term->tl_winpty_config != NULL)
2018 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002019 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002020 if (term->tl_vterm != NULL)
2021 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002022 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002023}
2024
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002025/*
2026 * Request size to terminal.
2027 */
2028 static void
2029term_report_winsize(term_T *term, int rows, int cols)
2030{
2031 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2032}
2033
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002034# else
2035
2036/**************************************
2037 * 3. Unix-like implementation.
2038 */
2039
2040/*
2041 * Create a new terminal of "rows" by "cols" cells.
2042 * Start job for "cmd".
2043 * Store the pointers in "term".
2044 * Return OK or FAIL.
2045 */
2046 static int
2047term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
2048{
2049 typval_T argvars[2];
2050 jobopt_T opt;
2051
2052 create_vterm(term, rows, cols);
2053
2054 argvars[0].v_type = VAR_STRING;
2055 argvars[0].vval.v_string = cmd;
2056 setup_job_options(&opt, rows, cols);
2057 term->tl_job = job_start(argvars, &opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002058 if (term->tl_job != NULL)
2059 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002060
Bram Moolenaar61a66052017-07-22 18:39:00 +02002061 return term->tl_job != NULL
2062 && term->tl_job->jv_channel != NULL
2063 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002064}
2065
2066/*
2067 * Free the terminal emulator part of "term".
2068 */
2069 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002070term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002071{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002072 if (term->tl_vterm != NULL)
2073 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002074 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002075}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002076
2077/*
2078 * Request size to terminal.
2079 */
2080 static void
2081term_report_winsize(term_T *term, int rows, int cols)
2082{
2083 /* Use an ioctl() to report the new window size to the job. */
2084 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2085 {
2086 int fd = -1;
2087 int part;
2088
2089 for (part = PART_OUT; part < PART_COUNT; ++part)
2090 {
2091 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2092 if (isatty(fd))
2093 break;
2094 }
2095 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2096 mch_stop_job(term->tl_job, (char_u *)"winch");
2097 }
2098}
2099
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002100# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002101
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002102#endif /* FEAT_TERMINAL */