blob: 82aa2c21b95d28c04feb86ae5b78152cc63b39e5 [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.
15 * 2. The MS-Windows implementation.
16 * Uses a hidden console for the terminal emulator.
17 * 3. The Unix-like implementation.
18 * Uses libvterm for the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020019 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020020 * When a terminal window is opened, a job is started that will be connected to
21 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020022 *
23 * If the terminal window has keyboard focus, typed keys are converted to the
24 * terminal encoding and writting to the job over a channel.
25 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020026 * If the job produces output, it is written to the terminal emulator. The
27 * terminal emulator invokes callbacks when its screen content changes. The
28 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
29 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020030 *
31 * TODO:
Bram Moolenaare4f25e42017-07-07 11:54:15 +020032 * - set buffer options to be scratch, hidden, nomodifiable, etc.
33 * - set buffer name to command, add (1) to avoid duplicates.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020034 * - If [command] is not given the 'shell' option is used.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020035 * - Add a scrollback buffer (contains lines to scroll off the top).
36 * Can use the buf_T lines, store attributes somewhere else?
37 * - When the job ends:
38 * - Write "-- JOB ENDED --" in the terminal.
39 * - Put the terminal contents in the scrollback buffer.
40 * - Free the terminal emulator.
41 * - Display the scrollback buffer (but with attributes).
42 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar938783d2017-07-16 20:13:26 +020043 * - when closing window and job has not ended, make terminal hidden?
44 * - Use a pty for I/O with the job.
45 * - Windows implementation:
46 * (WiP): https://github.com/mattn/vim/tree/terminal
47 * src/os_win32.c mch_open_terminal()
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020048 * Using winpty ?
49 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020050 * - command line completion for :terminal
Bram Moolenaare4f25e42017-07-07 11:54:15 +020051 * - support fixed size when 'termsize' is "rowsXcols".
52 * - support minimal size when 'termsize' is "rows*cols".
53 * - support minimal size when 'termsize' is empty.
54 * - implement ":buf {term-buf-name}"
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020055 * - implement term_list() list of buffers with a terminal
56 * - implement term_getsize(buf)
57 * - implement term_setsize(buf)
58 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
59 * - implement term_wait(buf) wait for screen to be updated
60 * - implement term_scrape(buf, row) inspect terminal screen
61 * - implement term_open(command, options) open terminal window
62 * - implement term_getjob(buf)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020063 * - implement 'termkey'
Bram Moolenaare4f25e42017-07-07 11:54:15 +020064 */
65
66#include "vim.h"
67
68#ifdef FEAT_TERMINAL
69
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020070#ifdef WIN3264
71/* MS-Windows: use a native console. */
72#else
73/* Unix-like: use libvterm. */
74# include "libvterm/include/vterm.h"
75#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020076
77/* 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
82 /* console handle? */
83#else
Bram Moolenaare4f25e42017-07-07 11:54:15 +020084 VTerm *tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020085#endif
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
89 /* Range of screen rows to update. Zero based. */
90 int tl_dirty_row_start; /* -1 if nothing dirty */
91 int tl_dirty_row_end; /* row below last one to update */
92
93 pos_T tl_cursor;
94};
95
96#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020097#define KEY_BUF_LEN 200
98
99/* Functions implemented for MS-Windows and Unix-like systems. */
100static int term_init(term_T *term, int rows, int cols);
101static void term_free(term_T *term);
102static void term_write_job_output(term_T *term, char_u *msg, size_t len);
103static int term_convert_key(int c, char *buf);
104static void term_update_lines(win_T *wp);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200105
106/*
107 * List of all active terminals.
108 */
109static term_T *first_term = NULL;
110
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200111/**************************************
112 * 1. Generic code for all systems.
113 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200114
115/*
116 * ":terminal": open a terminal window and execute a job in it.
117 */
118 void
119ex_terminal(exarg_T *eap)
120{
121 int rows;
122 int cols;
123 exarg_T split_ea;
124 win_T *old_curwin = curwin;
125 typval_T argvars[2];
126 term_T *term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200127 jobopt_T opt;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200128
129 if (check_restricted() || check_secure())
130 return;
131
132 term = (term_T *)alloc_clear(sizeof(term_T));
133 if (term == NULL)
134 return;
135 term->tl_dirty_row_end = MAX_ROW;
136
137 /* Open a new window or tab. */
138 vim_memset(&split_ea, 0, sizeof(split_ea));
139 split_ea.cmdidx = CMD_new;
140 split_ea.cmd = (char_u *)"new";
141 split_ea.arg = (char_u *)"";
142 ex_splitview(&split_ea);
143 if (curwin == old_curwin)
144 {
145 /* split failed */
146 vim_free(term);
147 return;
148 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200149 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200150
151 curbuf->b_term = term;
152 term->tl_next = first_term;
153 first_term = term;
154
155 /* TODO: set buffer type, hidden, etc. */
156
157 if (*curwin->w_p_tms != NUL)
158 {
159 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
160
161 rows = atoi((char *)curwin->w_p_tms);
162 cols = atoi((char *)p);
163 /* TODO: resize window if possible. */
164 }
165 else
166 {
167 rows = curwin->w_height;
168 cols = curwin->w_width;
169 }
170
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200171 if (term_init(term, rows, cols) == OK)
172 {
173 argvars[0].v_type = VAR_STRING;
174 argvars[0].vval.v_string = eap->arg;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200175
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200176 clear_job_options(&opt);
177 opt.jo_mode = MODE_RAW;
178 opt.jo_out_mode = MODE_RAW;
179 opt.jo_err_mode = MODE_RAW;
180 opt.jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
181 opt.jo_io[PART_OUT] = JIO_BUFFER;
182 opt.jo_io[PART_ERR] = JIO_BUFFER;
183 opt.jo_set |= JO_OUT_IO + (JO_OUT_IO << (PART_ERR - PART_OUT));
184 opt.jo_io_buf[PART_OUT] = curbuf->b_fnum;
185 opt.jo_io_buf[PART_ERR] = curbuf->b_fnum;
186 opt.jo_set |= JO_OUT_BUF + (JO_OUT_BUF << (PART_ERR - PART_OUT));
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200187
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200188 term->tl_job = job_start(argvars, &opt);
189 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200190
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200191 if (term->tl_job == NULL)
192 /* Wiping out the buffer will also close the window. */
193 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
194
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200195 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200196}
197
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200198/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200199 * Free a terminal and everything it refers to.
200 * Kills the job if there is one.
201 * Called when wiping out a buffer.
202 */
203 void
204free_terminal(term_T *term)
205{
206 term_T *tp;
207
208 if (term == NULL)
209 return;
210 if (first_term == term)
211 first_term = term->tl_next;
212 else
213 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
214 if (tp->tl_next == term)
215 {
216 tp->tl_next = term->tl_next;
217 break;
218 }
219
220 if (term->tl_job != NULL)
221 {
222 if (term->tl_job->jv_status != JOB_ENDED)
223 job_stop(term->tl_job, NULL, "kill");
224 job_unref(term->tl_job);
225 }
226
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200227 term_free(term);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200228 vim_free(term);
229}
230
231/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200232 * Invoked when "msg" output from a job was received. Write it to the terminal
233 * of "buffer".
234 */
235 void
236write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
237{
238 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200239 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200240
241 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200242 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200243
244 /* TODO: only update once in a while. */
245 update_screen(0);
246 setcursor();
247 out_flush();
248}
249
250/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200251 * Wait for input and send it to the job.
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200252 * Return when the start of a CTRL-W command is typed or anything else that
253 * should be handled as a Normal mode command.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200254 */
255 void
256terminal_loop(void)
257{
258 char buf[KEY_BUF_LEN];
259 int c;
260 size_t len;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200261 static int mouse_was_outside = FALSE;
262 int dragging_outside = FALSE;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200263
264 for (;;)
265 {
266 /* TODO: skip screen update when handling a sequence of keys. */
267 update_screen(0);
268 setcursor();
269 out_flush();
270 c = vgetc();
271
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200272 /* Catch keys that need to be handled as in Normal mode. */
273 switch (c)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200274 {
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200275 case Ctrl_W:
276 case NUL:
277 case K_ZERO:
278 stuffcharReadbuff(c);
279 return;
280
281 case K_IGNORE: continue;
282
283 case K_LEFTDRAG:
284 case K_MIDDLEDRAG:
285 case K_RIGHTDRAG:
286 case K_X1DRAG:
287 case K_X2DRAG:
288 dragging_outside = mouse_was_outside;
289 /* FALLTHROUGH */
290 case K_LEFTMOUSE:
291 case K_LEFTMOUSE_NM:
292 case K_LEFTRELEASE:
293 case K_LEFTRELEASE_NM:
294 case K_MIDDLEMOUSE:
295 case K_MIDDLERELEASE:
296 case K_RIGHTMOUSE:
297 case K_RIGHTRELEASE:
298 case K_X1MOUSE:
299 case K_X1RELEASE:
300 case K_X2MOUSE:
301 case K_X2RELEASE:
302 if (mouse_row < W_WINROW(curwin)
303 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
304 || mouse_col < W_WINCOL(curwin)
305 || mouse_col >= W_ENDCOL(curwin)
306 || dragging_outside)
307 {
308 /* click outside the current window */
309 stuffcharReadbuff(c);
310 mouse_was_outside = TRUE;
311 return;
312 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200313 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200314 mouse_was_outside = FALSE;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200315
316 /* Convert the typed key to a sequence of bytes for the job. */
317 len = term_convert_key(c, buf);
318 if (len > 0)
319 /* TODO: if FAIL is returned, stop? */
320 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
321 (char_u *)buf, len, NULL);
322 }
323}
324
325/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200326 * Called to update the window that contains the terminal.
327 */
328 void
329term_update_window(win_T *wp)
330{
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200331 term_update_lines(wp);
332}
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200333
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200334#ifdef WIN3264
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200335
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200336/**************************************
337 * 2. MS-Windows implementation.
338 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200339
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200340/*
341 * Create a new terminal of "rows" by "cols" cells.
342 * Store a reference in "term".
343 * Return OK or FAIL.
344 */
345 static int
346term_init(term_T *term, int rows, int cols)
347{
348 /* TODO: Create a hidden console */
349 return FAIL;
350}
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200351
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200352/*
353 * Free the terminal emulator part of "term".
354 */
355 static void
356term_free(term_T *term)
357{
358 /* TODO */
359}
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200360
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200361/*
362 * Write job output "msg[len]" to the terminal.
363 */
364 static void
365term_write_job_output(term_T *term, char_u *msg, size_t len)
366{
367 /* TODO */
368}
369
370/*
371 * Convert typed key "c" into bytes to send to the job.
372 * Return the number of bytes in "buf".
373 */
374 static int
375term_convert_key(int c, char *buf)
376{
377 /* TODO */
378 return 0;
379}
380
381/*
382 * Called to update the window that contains the terminal.
383 */
384 static void
385term_update_lines(win_T *wp)
386{
387 /* TODO */
388}
389
390#else
391
392/**************************************
393 * 3. Unix-like implementation.
394 *
395 * For a terminal one VTerm is constructed. This uses libvterm. A copy of
396 * that library is in the libvterm directory.
397 */
398
399static int handle_damage(VTermRect rect, void *user);
400static int handle_moverect(VTermRect dest, VTermRect src, void *user);
401static int handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user);
402static int handle_resize(int rows, int cols, void *user);
403
404static VTermScreenCallbacks screen_callbacks = {
405 handle_damage, /* damage */
406 handle_moverect, /* moverect */
407 handle_movecursor, /* movecursor */
408 NULL, /* settermprop */
409 NULL, /* bell */
410 handle_resize, /* resize */
411 NULL, /* sb_pushline */
412 NULL /* sb_popline */
413};
414
415/*
416 * Create a new terminal of "rows" by "cols" cells.
417 * Store a reference in "term".
418 * Return OK or FAIL.
419 */
420 static int
421term_init(term_T *term, int rows, int cols)
422{
423 VTerm *vterm = vterm_new(rows, cols);
424 VTermScreen *screen;
425
426 term->tl_vterm = vterm;
427 screen = vterm_obtain_screen(vterm);
428 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
429 /* TODO: depends on 'encoding'. */
430 vterm_set_utf8(vterm, 1);
431 /* Required to initialize most things. */
432 vterm_screen_reset(screen, 1 /* hard */);
433
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200434 return OK;
435}
436
437/*
438 * Free the terminal emulator part of "term".
439 */
440 static void
441term_free(term_T *term)
442{
443 vterm_free(term->tl_vterm);
444}
445
446/*
447 * Write job output "msg[len]" to the terminal.
448 */
449 static void
450term_write_job_output(term_T *term, char_u *msg, size_t len)
451{
452 VTerm *vterm = term->tl_vterm;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200453 char_u *p;
454 size_t done;
455 size_t len_now;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200456
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200457 for (done = 0; done < len; done += len_now)
458 {
459 for (p = msg + done; p < msg + len; )
460 {
461 if (*p == NL)
462 break;
463 p += mb_ptr2len_len(p, len - (p - msg));
464 }
465 len_now = p - msg - done;
466 vterm_input_write(vterm, (char *)msg + done, len_now);
467 if (p < msg + len && *p == NL)
468 {
469 /* Convert NL to CR-NL, that appears to work best. */
470 vterm_input_write(vterm, "\r\n", 2);
471 ++len_now;
472 }
473 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200474 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200475}
476
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200477 static int
478handle_damage(VTermRect rect, void *user)
479{
480 term_T *term = (term_T *)user;
481
482 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
483 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200484 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200485 return 1;
486}
487
488 static int
489handle_moverect(VTermRect dest, VTermRect src, void *user)
490{
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200491 term_T *term = (term_T *)user;
492
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200493 /* TODO */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200494 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200495 return 1;
496}
497
498 static int
499handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user)
500{
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200501 term_T *term = (term_T *)user;
502 win_T *wp;
503 int is_current = FALSE;
504
505 FOR_ALL_WINDOWS(wp)
506 {
507 if (wp->w_buffer == term->tl_buffer)
508 {
509 /* TODO: limit to window size? */
510 wp->w_wrow = pos.row;
511 wp->w_wcol = pos.col;
512 if (wp == curwin)
513 is_current = TRUE;
514 }
515 }
516
517 if (is_current)
518 {
519 setcursor();
520 out_flush();
521 }
522
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200523 return 1;
524}
525
526 static int
527handle_resize(int rows, int cols, void *user)
528{
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200529 term_T *term = (term_T *)user;
530
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200531 /* TODO: handle terminal resize. */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200532 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200533 return 1;
534}
535
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200536/*
537 * Convert typed key "c" into bytes to send to the job.
538 * Return the number of bytes in "buf".
539 */
540 static int
541term_convert_key(int c, char *buf)
542{
543 VTerm *vterm = curbuf->b_term->tl_vterm;
544 VTermKey key = VTERM_KEY_NONE;
545 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200546
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200547 switch (c)
548 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200549 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200550 case ESC: key = VTERM_KEY_ESCAPE; break;
551 case K_BS: key = VTERM_KEY_BACKSPACE; break;
552 case K_DEL: key = VTERM_KEY_DEL; break;
553 case K_DOWN: key = VTERM_KEY_DOWN; break;
554 case K_END: key = VTERM_KEY_END; break;
555 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
556 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
557 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
558 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
559 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
560 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
561 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
562 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
563 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
564 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
565 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
566 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
567 case K_HOME: key = VTERM_KEY_HOME; break;
568 case K_INS: key = VTERM_KEY_INS; break;
569 case K_K0: key = VTERM_KEY_KP_0; break;
570 case K_K1: key = VTERM_KEY_KP_1; break;
571 case K_K2: key = VTERM_KEY_KP_2; break;
572 case K_K3: key = VTERM_KEY_KP_3; break;
573 case K_K4: key = VTERM_KEY_KP_4; break;
574 case K_K5: key = VTERM_KEY_KP_5; break;
575 case K_K6: key = VTERM_KEY_KP_6; break;
576 case K_K7: key = VTERM_KEY_KP_7; break;
577 case K_K8: key = VTERM_KEY_KP_8; break;
578 case K_K9: key = VTERM_KEY_KP_9; break;
579 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
580 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
581 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
582 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
583 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
584 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
585 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
586 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
587 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
588 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
589 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
590 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
591 case K_LEFT: key = VTERM_KEY_LEFT; break;
592 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
593 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
594 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
595 case K_UP: key = VTERM_KEY_UP; break;
596 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200597
598 case K_MOUSEUP: /* TODO */ break;
599 case K_MOUSEDOWN: /* TODO */ break;
600 case K_MOUSELEFT: /* TODO */ break;
601 case K_MOUSERIGHT: /* TODO */ break;
602
603 case K_LEFTMOUSE: /* TODO */ break;
604 case K_LEFTMOUSE_NM: /* TODO */ break;
605 case K_LEFTDRAG: /* TODO */ break;
606 case K_LEFTRELEASE: /* TODO */ break;
607 case K_LEFTRELEASE_NM: /* TODO */ break;
608 case K_MIDDLEMOUSE: /* TODO */ break;
609 case K_MIDDLEDRAG: /* TODO */ break;
610 case K_MIDDLERELEASE: /* TODO */ break;
611 case K_RIGHTMOUSE: /* TODO */ break;
612 case K_RIGHTDRAG: /* TODO */ break;
613 case K_RIGHTRELEASE: /* TODO */ break;
614 case K_X1MOUSE: /* TODO */ break;
615 case K_X1DRAG: /* TODO */ break;
616 case K_X1RELEASE: /* TODO */ break;
617 case K_X2MOUSE: /* TODO */ break;
618 case K_X2DRAG: /* TODO */ break;
619 case K_X2RELEASE: /* TODO */ break;
620
621 /* TODO: handle all special keys and modifiers that terminal_loop()
622 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200623 }
624
625 /*
626 * Convert special keys to vterm keys:
627 * - Write keys to vterm: vterm_keyboard_key()
628 * - Write output to channel.
629 */
630 if (key != VTERM_KEY_NONE)
631 /* Special key, let vterm convert it. */
632 vterm_keyboard_key(vterm, key, mod);
633 else
634 /* Normal character, let vterm convert it. */
635 vterm_keyboard_unichar(vterm, c, mod);
636
637 /* Read back the converted escape sequence. */
638 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
639}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200640
Bram Moolenaar938783d2017-07-16 20:13:26 +0200641/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200642 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200643 */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200644 static void
645term_update_lines(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200646{
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200647 int vterm_rows;
648 int vterm_cols;
649 VTerm *vterm = wp->w_buffer->b_term->tl_vterm;
650 VTermScreen *screen = vterm_obtain_screen(vterm);
651 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200652
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200653 vterm_get_size(vterm, &vterm_rows, &vterm_cols);
654
655 /* TODO: Only redraw what changed. */
656 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200657 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200658 int off = screen_get_current_line_off();
Bram Moolenaar938783d2017-07-16 20:13:26 +0200659
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200660 if (pos.row < vterm_rows)
661 for (pos.col = 0; pos.col < wp->w_width && pos.col < vterm_cols;
662 ++pos.col)
663 {
664 VTermScreenCell cell;
665 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200666
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200667 vterm_screen_get_cell(screen, pos, &cell);
668 /* TODO: use cell.attrs and colors */
669 /* TODO: use cell.width */
670 /* TODO: multi-byte chars */
671 c = cell.chars[0];
672 ScreenLines[off] = c == NUL ? ' ' : c;
673 ScreenAttrs[off] = 0;
674 ++off;
675 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200676 else
677 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200678
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200679 screen_line(wp->w_winrow + pos.row, wp->w_wincol, pos.col, wp->w_width,
680 FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200681 }
682}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200683
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200684#endif
685
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200686#endif /* FEAT_TERMINAL */