blob: b7765cd5e3b3ae1eef712949a135f31784978d95 [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 *
13 * For a terminal one VTerm is constructed. This uses libvterm. A copy of
14 * that library is in the libvterm directory.
15 *
16 * The VTerm invokes callbacks when its screen contents changes. The line
17 * range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
18 * while, if the window is visible, the screen contents is drawn.
19 *
20 * If the terminal window has keyboard focus, typed keys are converted to the
21 * terminal encoding and writting to the job over a channel.
22 *
23 * If the job produces output, it is written to the VTerm.
24 * This will result in screen updates.
25 *
26 * TODO:
Bram Moolenaare4f25e42017-07-07 11:54:15 +020027 * - free b_term when closing terminal.
28 * - remove term from first_term list when closing terminal.
29 * - set buffer options to be scratch, hidden, nomodifiable, etc.
30 * - set buffer name to command, add (1) to avoid duplicates.
31 * - command line completion (command name)
32 * - support fixed size when 'termsize' is "rowsXcols".
33 * - support minimal size when 'termsize' is "rows*cols".
34 * - support minimal size when 'termsize' is empty.
35 * - implement ":buf {term-buf-name}"
36 * - implement term_getsize()
37 * - implement term_setsize()
38 * - implement term_sendkeys() send keystrokes to a terminal
39 * - implement term_wait() wait for screen to be updated
40 * - implement term_scrape() inspect terminal screen
41 * - implement term_open() open terminal window
42 * - implement term_getjob()
43 */
44
45#include "vim.h"
46
47#ifdef FEAT_TERMINAL
48
49#include "libvterm/include/vterm.h"
50
51/* typedef term_T in structs.h */
52struct terminal_S {
53 term_T *tl_next;
54
55 VTerm *tl_vterm;
56 job_T *tl_job;
57
58 /* Range of screen rows to update. Zero based. */
59 int tl_dirty_row_start; /* -1 if nothing dirty */
60 int tl_dirty_row_end; /* row below last one to update */
61
62 pos_T tl_cursor;
63};
64
65#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
66
67/*
68 * List of all active terminals.
69 */
70static term_T *first_term = NULL;
71
72static int handle_damage(VTermRect rect, void *user);
73static int handle_moverect(VTermRect dest, VTermRect src, void *user);
74static int handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user);
75static int handle_resize(int rows, int cols, void *user);
76
77static VTermScreenCallbacks screen_callbacks = {
78 handle_damage, /* damage */
79 handle_moverect, /* moverect */
80 handle_movecursor, /* movecursor */
81 NULL, /* settermprop */
82 NULL, /* bell */
83 handle_resize, /* resize */
84 NULL, /* sb_pushline */
85 NULL /* sb_popline */
86};
87
88/*
89 * ":terminal": open a terminal window and execute a job in it.
90 */
91 void
92ex_terminal(exarg_T *eap)
93{
94 int rows;
95 int cols;
96 exarg_T split_ea;
97 win_T *old_curwin = curwin;
98 typval_T argvars[2];
99 term_T *term;
100 VTerm *vterm;
101 VTermScreen *screen;
102
103 if (check_restricted() || check_secure())
104 return;
105
106 term = (term_T *)alloc_clear(sizeof(term_T));
107 if (term == NULL)
108 return;
109 term->tl_dirty_row_end = MAX_ROW;
110
111 /* Open a new window or tab. */
112 vim_memset(&split_ea, 0, sizeof(split_ea));
113 split_ea.cmdidx = CMD_new;
114 split_ea.cmd = (char_u *)"new";
115 split_ea.arg = (char_u *)"";
116 ex_splitview(&split_ea);
117 if (curwin == old_curwin)
118 {
119 /* split failed */
120 vim_free(term);
121 return;
122 }
123
124 curbuf->b_term = term;
125 term->tl_next = first_term;
126 first_term = term;
127
128 /* TODO: set buffer type, hidden, etc. */
129
130 if (*curwin->w_p_tms != NUL)
131 {
132 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
133
134 rows = atoi((char *)curwin->w_p_tms);
135 cols = atoi((char *)p);
136 /* TODO: resize window if possible. */
137 }
138 else
139 {
140 rows = curwin->w_height;
141 cols = curwin->w_width;
142 }
143
144 vterm = vterm_new(rows, cols);
145 term->tl_vterm = vterm;
146 screen = vterm_obtain_screen(vterm);
147 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
148
149 argvars[0].v_type = VAR_STRING;
150 argvars[0].vval.v_string = eap->arg;
151 argvars[1].v_type = VAR_UNKNOWN;
152 term->tl_job = job_start(argvars);
153
154 /* TODO: setup channels to/from job */
155 /* Setup pty, see mch_call_shell(). */
156}
157
158 static int
159handle_damage(VTermRect rect, void *user)
160{
161 term_T *term = (term_T *)user;
162
163 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
164 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
165 return 1;
166}
167
168 static int
169handle_moverect(VTermRect dest, VTermRect src, void *user)
170{
171 /* TODO */
172 return 1;
173}
174
175 static int
176handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user)
177{
178 /* TODO: handle moving the cursor. */
179 return 1;
180}
181
182 static int
183handle_resize(int rows, int cols, void *user)
184{
185 /* TODO: handle terminal resize. */
186 return 1;
187}
188
189/* TODO: Use win_del_lines() to make scroll up efficient. */
190
191/* TODO: function to update the window.
192 * Get the screen contents from vterm with vterm_screen_get_cell().
193 * put in current_ScreenLine and call screen_line().
194 */
195
196/* TODO: function to wait for input and send it to the job.
197 * Return when a CTRL-W command is typed that moves to another window.
198 * Convert special keys to vterm keys:
199 * - Write keys to vterm: vterm_keyboard_key()
200 * - read the output (xterm escape sequences): vterm_output_read()
201 * - Write output to channel.
202 */
203
204/* TODO: function to read job output from the channel.
205 * write to vterm: vterm_input_write()
206 * This will invoke screen callbacks.
207 * call vterm_screen_flush_damage()
208 */
209
210#endif /* FEAT_TERMINAL */