Mercurial > vim
annotate src/terminal.c @ 12590:3c46dcf3b335 v8.0.1173
patch 8.0.1173: terminal window is not redrawn after CTRL-L
commit https://github.com/vim/vim/commit/19a3d68b2cd4beb1ee1d97a84ad4e860ffe0cbb2
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Oct 2 21:54:59 2017 +0200
patch 8.0.1173: terminal window is not redrawn after CTRL-L
Problem: Terminal window is not redrawn after CTRL-L. (Marcin Szamotulski)
Solution: Redraw the whole terminal when w_redr_type is NOT_VALID.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Mon, 02 Oct 2017 22:00:05 +0200 |
parents | f8beecfea2c4 |
children | 97a2f34962ee |
rev | line source |
---|---|
12502 | 1 /* 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 * There are three parts: | |
14 * 1. Generic code for all systems. | |
15 * Uses libvterm for the terminal emulator. | |
16 * 2. The MS-Windows implementation. | |
17 * Uses winpty. | |
18 * 3. The Unix-like implementation. | |
19 * Uses pseudo-tty's (pty's). | |
20 * | |
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of | |
22 * this library is in the libvterm directory. | |
23 * | |
24 * When a terminal window is opened, a job is started that will be connected to | |
25 * the terminal emulator. | |
26 * | |
27 * If the terminal window has keyboard focus, typed keys are converted to the | |
28 * terminal encoding and writing to the job over a channel. | |
29 * | |
30 * 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. | |
34 * | |
35 * 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 * When the buffer is changed it is turned into a normal buffer, the attributes | |
38 * in tl_scrollback are no longer used. | |
39 * | |
40 * TODO: | |
41 * - in GUI vertical split causes problems. Cursor is flickering. (Hirohito | |
42 * Higashi, 2017 Sep 19) | |
43 * - Shift-Tab does not work. | |
12578
f8beecfea2c4
patch 8.0.1167: Motif: typing in terminal window is slow
Christian Brabandt <cb@256bit.org>
parents:
12541
diff
changeset
|
44 * - after resizing windows overlap. (Boris Staletic, #2164) |
12541
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
45 * - double click in Window toolbar starts Visual mode. |
12502 | 46 * - Redirecting output does not work on MS-Windows, Test_terminal_redir_file() |
47 * is disabled. | |
12541
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
48 * - cursor blinks in terminal on widows with a timer. (xtal8, #2142) |
12502 | 49 * - implement term_setsize() |
12541
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
50 * - MS-Windows GUI: WinBar has tearoff item |
12502 | 51 * - MS-Windows GUI: still need to type a key after shell exits? #1924 |
52 * - add test for giving error for invalid 'termsize' value. | |
53 * - support minimal size when 'termsize' is "rows*cols". | |
54 * - support minimal size when 'termsize' is empty? | |
55 * - GUI: when using tabs, focus in terminal, click on tab does not work. | |
56 * - GUI: when 'confirm' is set and trying to exit Vim, dialog offers to save | |
57 * changes to "!shell". | |
58 * (justrajdeep, 2017 Aug 22) | |
12590
3c46dcf3b335
patch 8.0.1173: terminal window is not redrawn after CTRL-L
Christian Brabandt <cb@256bit.org>
parents:
12578
diff
changeset
|
59 * - Redrawing is slow with Athena and Motif. |
12502 | 60 * - For the GUI fill termios with default values, perhaps like pangoterm: |
61 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134 | |
62 * - if the job in the terminal does not support the mouse, we can use the | |
63 * mouse in the Terminal window for copy/paste. | |
64 * - when 'encoding' is not utf-8, or the job is using another encoding, setup | |
65 * conversions. | |
66 * - In the GUI use a terminal emulator for :!cmd. Make the height the same as | |
67 * the window and position it higher up when it gets filled, so it looks like | |
68 * the text scrolls up. | |
69 * - Copy text in the vterm to the Vim buffer once in a while, so that | |
70 * completion works. | |
71 * - add an optional limit for the scrollback size. When reaching it remove | |
72 * 10% at the start. | |
73 */ | |
74 | |
75 #include "vim.h" | |
76 | |
77 #if defined(FEAT_TERMINAL) || defined(PROTO) | |
78 | |
79 #ifndef MIN | |
80 # define MIN(x,y) ((x) < (y) ? (x) : (y)) | |
81 #endif | |
82 #ifndef MAX | |
83 # define MAX(x,y) ((x) > (y) ? (x) : (y)) | |
84 #endif | |
85 | |
86 #include "libvterm/include/vterm.h" | |
87 | |
88 /* This is VTermScreenCell without the characters, thus much smaller. */ | |
89 typedef struct { | |
90 VTermScreenCellAttrs attrs; | |
91 char width; | |
92 VTermColor fg, bg; | |
93 } cellattr_T; | |
94 | |
95 typedef struct sb_line_S { | |
96 int sb_cols; /* can differ per line */ | |
97 cellattr_T *sb_cells; /* allocated */ | |
98 cellattr_T sb_fill_attr; /* for short line */ | |
99 } sb_line_T; | |
100 | |
101 /* typedef term_T in structs.h */ | |
102 struct terminal_S { | |
103 term_T *tl_next; | |
104 | |
105 VTerm *tl_vterm; | |
106 job_T *tl_job; | |
107 buf_T *tl_buffer; | |
108 | |
109 /* Set when setting the size of a vterm, reset after redrawing. */ | |
110 int tl_vterm_size_changed; | |
111 | |
112 /* used when tl_job is NULL and only a pty was created */ | |
113 int tl_tty_fd; | |
114 char_u *tl_tty_in; | |
115 char_u *tl_tty_out; | |
116 | |
117 int tl_normal_mode; /* TRUE: Terminal-Normal mode */ | |
118 int tl_channel_closed; | |
119 int tl_finish; /* 'c' for ++close, 'o' for ++open */ | |
120 char_u *tl_opencmd; | |
121 char_u *tl_eof_chars; | |
122 | |
123 #ifdef WIN3264 | |
124 void *tl_winpty_config; | |
125 void *tl_winpty; | |
126 #endif | |
127 | |
128 /* last known vterm size */ | |
129 int tl_rows; | |
130 int tl_cols; | |
131 /* vterm size does not follow window size */ | |
132 int tl_rows_fixed; | |
133 int tl_cols_fixed; | |
134 | |
135 char_u *tl_title; /* NULL or allocated */ | |
136 char_u *tl_status_text; /* NULL or allocated */ | |
137 | |
138 /* Range of screen rows to update. Zero based. */ | |
12578
f8beecfea2c4
patch 8.0.1167: Motif: typing in terminal window is slow
Christian Brabandt <cb@256bit.org>
parents:
12541
diff
changeset
|
139 int tl_dirty_row_start; /* MAX_ROW if nothing dirty */ |
12502 | 140 int tl_dirty_row_end; /* row below last one to update */ |
141 | |
142 garray_T tl_scrollback; | |
143 int tl_scrollback_scrolled; | |
144 cellattr_T tl_default_color; | |
145 | |
146 VTermPos tl_cursor_pos; | |
147 int tl_cursor_visible; | |
148 int tl_cursor_blink; | |
149 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */ | |
150 char_u *tl_cursor_color; /* NULL or allocated */ | |
151 | |
152 int tl_using_altscreen; | |
153 }; | |
154 | |
155 #define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */ | |
156 #define TMODE_LOOP 2 /* CTRL-W N used */ | |
157 | |
158 /* | |
159 * List of all active terminals. | |
160 */ | |
161 static term_T *first_term = NULL; | |
162 | |
163 /* Terminal active in terminal_loop(). */ | |
164 static term_T *in_terminal_loop = NULL; | |
165 | |
166 #define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */ | |
167 #define KEY_BUF_LEN 200 | |
168 | |
169 /* | |
170 * Functions with separate implementation for MS-Windows and Unix-like systems. | |
171 */ | |
172 static int term_and_job_init(term_T *term, typval_T *argvar, jobopt_T *opt); | |
173 static int create_pty_only(term_T *term, jobopt_T *opt); | |
174 static void term_report_winsize(term_T *term, int rows, int cols); | |
175 static void term_free_vterm(term_T *term); | |
176 | |
177 /* The characters that we know (or assume) that the terminal expects for the | |
178 * backspace and enter keys. */ | |
179 static int term_backspace_char = BS; | |
180 static int term_enter_char = CAR; | |
181 static int term_nl_does_cr = FALSE; | |
182 | |
183 | |
184 /************************************** | |
185 * 1. Generic code for all systems. | |
186 */ | |
187 | |
188 /* | |
189 * Determine the terminal size from 'termsize' and the current window. | |
190 * Assumes term->tl_rows and term->tl_cols are zero. | |
191 */ | |
192 static void | |
193 set_term_and_win_size(term_T *term) | |
194 { | |
195 if (*curwin->w_p_tms != NUL) | |
196 { | |
197 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1; | |
198 | |
199 term->tl_rows = atoi((char *)curwin->w_p_tms); | |
200 term->tl_cols = atoi((char *)p); | |
201 } | |
202 if (term->tl_rows == 0) | |
203 term->tl_rows = curwin->w_height; | |
204 else | |
205 { | |
206 win_setheight_win(term->tl_rows, curwin); | |
207 term->tl_rows_fixed = TRUE; | |
208 } | |
209 if (term->tl_cols == 0) | |
210 term->tl_cols = curwin->w_width; | |
211 else | |
212 { | |
213 win_setwidth_win(term->tl_cols, curwin); | |
214 term->tl_cols_fixed = TRUE; | |
215 } | |
216 } | |
217 | |
218 /* | |
219 * Initialize job options for a terminal job. | |
220 * Caller may overrule some of them. | |
221 */ | |
222 static void | |
223 init_job_options(jobopt_T *opt) | |
224 { | |
225 clear_job_options(opt); | |
226 | |
227 opt->jo_mode = MODE_RAW; | |
228 opt->jo_out_mode = MODE_RAW; | |
229 opt->jo_err_mode = MODE_RAW; | |
230 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE; | |
231 } | |
232 | |
233 /* | |
234 * Set job options mandatory for a terminal job. | |
235 */ | |
236 static void | |
237 setup_job_options(jobopt_T *opt, int rows, int cols) | |
238 { | |
239 if (!(opt->jo_set & JO_OUT_IO)) | |
240 { | |
241 /* Connect stdout to the terminal. */ | |
242 opt->jo_io[PART_OUT] = JIO_BUFFER; | |
243 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum; | |
244 opt->jo_modifiable[PART_OUT] = 0; | |
245 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE; | |
246 } | |
247 | |
248 if (!(opt->jo_set & JO_ERR_IO)) | |
249 { | |
250 /* Connect stderr to the terminal. */ | |
251 opt->jo_io[PART_ERR] = JIO_BUFFER; | |
252 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum; | |
253 opt->jo_modifiable[PART_ERR] = 0; | |
254 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE; | |
255 } | |
256 | |
257 opt->jo_pty = TRUE; | |
258 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0) | |
259 opt->jo_term_rows = rows; | |
260 if ((opt->jo_set2 & JO2_TERM_COLS) == 0) | |
261 opt->jo_term_cols = cols; | |
262 } | |
263 | |
264 /* | |
265 * Start a terminal window and return its buffer. | |
266 * Returns NULL when failed. | |
267 */ | |
268 static buf_T * | |
269 term_start(typval_T *argvar, jobopt_T *opt, int forceit) | |
270 { | |
271 exarg_T split_ea; | |
272 win_T *old_curwin = curwin; | |
273 term_T *term; | |
274 buf_T *old_curbuf = NULL; | |
275 int res; | |
276 buf_T *newbuf; | |
277 | |
278 if (check_restricted() || check_secure()) | |
279 return NULL; | |
280 | |
281 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)) | |
282 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO) | |
283 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF)) | |
284 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))) | |
285 { | |
286 EMSG(_(e_invarg)); | |
287 return NULL; | |
288 } | |
289 | |
290 term = (term_T *)alloc_clear(sizeof(term_T)); | |
291 if (term == NULL) | |
292 return NULL; | |
293 term->tl_dirty_row_end = MAX_ROW; | |
294 term->tl_cursor_visible = TRUE; | |
295 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK; | |
296 term->tl_finish = opt->jo_term_finish; | |
297 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300); | |
298 | |
299 vim_memset(&split_ea, 0, sizeof(split_ea)); | |
300 if (opt->jo_curwin) | |
301 { | |
302 /* Create a new buffer in the current window. */ | |
303 if (!can_abandon(curbuf, forceit)) | |
304 { | |
305 no_write_message(); | |
306 vim_free(term); | |
307 return NULL; | |
308 } | |
309 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE, | |
310 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL) | |
311 { | |
312 vim_free(term); | |
313 return NULL; | |
314 } | |
315 } | |
316 else if (opt->jo_hidden) | |
317 { | |
318 buf_T *buf; | |
319 | |
320 /* Create a new buffer without a window. Make it the current buffer for | |
321 * a moment to be able to do the initialisations. */ | |
322 buf = buflist_new((char_u *)"", NULL, (linenr_T)0, | |
323 BLN_NEW | BLN_LISTED); | |
324 if (buf == NULL || ml_open(buf) == FAIL) | |
325 { | |
326 vim_free(term); | |
327 return NULL; | |
328 } | |
329 old_curbuf = curbuf; | |
330 --curbuf->b_nwindows; | |
331 curbuf = buf; | |
332 curwin->w_buffer = buf; | |
333 ++curbuf->b_nwindows; | |
334 } | |
335 else | |
336 { | |
337 /* Open a new window or tab. */ | |
338 split_ea.cmdidx = CMD_new; | |
339 split_ea.cmd = (char_u *)"new"; | |
340 split_ea.arg = (char_u *)""; | |
341 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT)) | |
342 { | |
343 split_ea.line2 = opt->jo_term_rows; | |
344 split_ea.addr_count = 1; | |
345 } | |
346 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT)) | |
347 { | |
348 split_ea.line2 = opt->jo_term_cols; | |
349 split_ea.addr_count = 1; | |
350 } | |
351 | |
352 ex_splitview(&split_ea); | |
353 if (curwin == old_curwin) | |
354 { | |
355 /* split failed */ | |
356 vim_free(term); | |
357 return NULL; | |
358 } | |
359 } | |
360 term->tl_buffer = curbuf; | |
361 curbuf->b_term = term; | |
362 | |
363 if (!opt->jo_hidden) | |
364 { | |
365 /* only one size was taken care of with :new, do the other one */ | |
366 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT)) | |
367 win_setheight(opt->jo_term_rows); | |
368 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT)) | |
369 win_setwidth(opt->jo_term_cols); | |
370 } | |
371 | |
372 /* Link the new terminal in the list of active terminals. */ | |
373 term->tl_next = first_term; | |
374 first_term = term; | |
375 | |
376 if (opt->jo_term_name != NULL) | |
377 curbuf->b_ffname = vim_strsave(opt->jo_term_name); | |
378 else | |
379 { | |
380 int i; | |
381 size_t len; | |
382 char_u *cmd, *p; | |
383 | |
384 if (argvar->v_type == VAR_STRING) | |
385 { | |
386 cmd = argvar->vval.v_string; | |
387 if (cmd == NULL) | |
388 cmd = (char_u *)""; | |
389 else if (STRCMP(cmd, "NONE") == 0) | |
390 cmd = (char_u *)"pty"; | |
391 } | |
392 else if (argvar->v_type != VAR_LIST | |
393 || argvar->vval.v_list == NULL | |
394 || argvar->vval.v_list->lv_len < 1 | |
395 || (cmd = get_tv_string_chk( | |
396 &argvar->vval.v_list->lv_first->li_tv)) == NULL) | |
397 cmd = (char_u*)""; | |
398 | |
399 len = STRLEN(cmd) + 10; | |
400 p = alloc((int)len); | |
401 | |
402 for (i = 0; p != NULL; ++i) | |
403 { | |
404 /* Prepend a ! to the command name to avoid the buffer name equals | |
405 * the executable, otherwise ":w!" would overwrite it. */ | |
406 if (i == 0) | |
407 vim_snprintf((char *)p, len, "!%s", cmd); | |
408 else | |
409 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i); | |
410 if (buflist_findname(p) == NULL) | |
411 { | |
412 vim_free(curbuf->b_ffname); | |
413 curbuf->b_ffname = p; | |
414 break; | |
415 } | |
416 } | |
417 } | |
418 curbuf->b_fname = curbuf->b_ffname; | |
419 | |
420 if (opt->jo_term_opencmd != NULL) | |
421 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd); | |
422 | |
423 if (opt->jo_eof_chars != NULL) | |
424 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars); | |
425 | |
426 set_string_option_direct((char_u *)"buftype", -1, | |
427 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0); | |
428 | |
429 /* Mark the buffer as not modifiable. It can only be made modifiable after | |
430 * the job finished. */ | |
431 curbuf->b_p_ma = FALSE; | |
432 | |
433 set_term_and_win_size(term); | |
434 setup_job_options(opt, term->tl_rows, term->tl_cols); | |
435 | |
436 /* System dependent: setup the vterm and maybe start the job in it. */ | |
437 if (argvar->v_type == VAR_STRING | |
438 && argvar->vval.v_string != NULL | |
439 && STRCMP(argvar->vval.v_string, "NONE") == 0) | |
440 res = create_pty_only(term, opt); | |
441 else | |
442 res = term_and_job_init(term, argvar, opt); | |
443 | |
444 newbuf = curbuf; | |
445 if (res == OK) | |
446 { | |
447 /* Get and remember the size we ended up with. Update the pty. */ | |
448 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols); | |
449 term_report_winsize(term, term->tl_rows, term->tl_cols); | |
450 | |
451 /* Make sure we don't get stuck on sending keys to the job, it leads to | |
452 * a deadlock if the job is waiting for Vim to read. */ | |
453 channel_set_nonblock(term->tl_job->jv_channel, PART_IN); | |
454 | |
12509
1fd4594eb74a
Missing part of 8.0.1131.
Christian Brabandt <cb@256bit.org>
parents:
12502
diff
changeset
|
455 #ifdef FEAT_AUTOCMD |
1fd4594eb74a
Missing part of 8.0.1131.
Christian Brabandt <cb@256bit.org>
parents:
12502
diff
changeset
|
456 ++curbuf->b_locked; |
1fd4594eb74a
Missing part of 8.0.1131.
Christian Brabandt <cb@256bit.org>
parents:
12502
diff
changeset
|
457 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf); |
1fd4594eb74a
Missing part of 8.0.1131.
Christian Brabandt <cb@256bit.org>
parents:
12502
diff
changeset
|
458 --curbuf->b_locked; |
1fd4594eb74a
Missing part of 8.0.1131.
Christian Brabandt <cb@256bit.org>
parents:
12502
diff
changeset
|
459 #endif |
1fd4594eb74a
Missing part of 8.0.1131.
Christian Brabandt <cb@256bit.org>
parents:
12502
diff
changeset
|
460 |
12502 | 461 if (old_curbuf != NULL) |
462 { | |
463 --curbuf->b_nwindows; | |
464 curbuf = old_curbuf; | |
465 curwin->w_buffer = curbuf; | |
466 ++curbuf->b_nwindows; | |
467 } | |
468 } | |
469 else | |
470 { | |
471 buf_T *buf = curbuf; | |
472 | |
473 free_terminal(curbuf); | |
474 if (old_curbuf != NULL) | |
475 { | |
476 --curbuf->b_nwindows; | |
477 curbuf = old_curbuf; | |
478 curwin->w_buffer = curbuf; | |
479 ++curbuf->b_nwindows; | |
480 } | |
481 | |
482 /* Wiping out the buffer will also close the window and call | |
483 * free_terminal(). */ | |
484 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE); | |
485 return NULL; | |
486 } | |
487 return newbuf; | |
488 } | |
489 | |
490 /* | |
491 * ":terminal": open a terminal window and execute a job in it. | |
492 */ | |
493 void | |
494 ex_terminal(exarg_T *eap) | |
495 { | |
496 typval_T argvar[2]; | |
497 jobopt_T opt; | |
498 char_u *cmd; | |
499 char_u *tofree = NULL; | |
500 | |
501 init_job_options(&opt); | |
502 | |
503 cmd = eap->arg; | |
504 while (*cmd && *cmd == '+' && *(cmd + 1) == '+') | |
505 { | |
506 char_u *p, *ep; | |
507 | |
508 cmd += 2; | |
509 p = skiptowhite(cmd); | |
510 ep = vim_strchr(cmd, '='); | |
511 if (ep != NULL && ep < p) | |
512 p = ep; | |
513 | |
514 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0) | |
515 opt.jo_term_finish = 'c'; | |
516 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0) | |
517 opt.jo_term_finish = 'o'; | |
518 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0) | |
519 opt.jo_curwin = 1; | |
520 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0) | |
521 opt.jo_hidden = 1; | |
522 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0 | |
523 && ep != NULL && isdigit(ep[1])) | |
524 { | |
525 opt.jo_set2 |= JO2_TERM_ROWS; | |
526 opt.jo_term_rows = atoi((char *)ep + 1); | |
527 p = skiptowhite(cmd); | |
528 } | |
529 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0 | |
530 && ep != NULL && isdigit(ep[1])) | |
531 { | |
532 opt.jo_set2 |= JO2_TERM_COLS; | |
533 opt.jo_term_cols = atoi((char *)ep + 1); | |
534 p = skiptowhite(cmd); | |
535 } | |
536 else if ((int)(p - cmd) == 3 && STRNICMP(cmd, "eof", 3) == 0 | |
537 && ep != NULL) | |
538 { | |
539 char_u *buf = NULL; | |
540 char_u *keys; | |
541 | |
542 p = skiptowhite(cmd); | |
543 *p = NUL; | |
544 keys = replace_termcodes(ep + 1, &buf, TRUE, TRUE, TRUE); | |
545 opt.jo_set2 |= JO2_EOF_CHARS; | |
546 opt.jo_eof_chars = vim_strsave(keys); | |
547 vim_free(buf); | |
548 *p = ' '; | |
549 } | |
550 else | |
551 { | |
552 if (*p) | |
553 *p = NUL; | |
554 EMSG2(_("E181: Invalid attribute: %s"), cmd); | |
555 return; | |
556 } | |
557 cmd = skipwhite(p); | |
558 } | |
559 if (*cmd == NUL) | |
560 /* Make a copy of 'shell', an autocommand may change the option. */ | |
561 tofree = cmd = vim_strsave(p_sh); | |
562 | |
563 if (eap->addr_count > 0) | |
564 { | |
565 /* Write lines from current buffer to the job. */ | |
566 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT; | |
567 opt.jo_io[PART_IN] = JIO_BUFFER; | |
568 opt.jo_io_buf[PART_IN] = curbuf->b_fnum; | |
569 opt.jo_in_top = eap->line1; | |
570 opt.jo_in_bot = eap->line2; | |
571 } | |
572 | |
573 argvar[0].v_type = VAR_STRING; | |
574 argvar[0].vval.v_string = cmd; | |
575 argvar[1].v_type = VAR_UNKNOWN; | |
576 term_start(argvar, &opt, eap->forceit); | |
577 vim_free(tofree); | |
578 vim_free(opt.jo_eof_chars); | |
579 } | |
580 | |
581 /* | |
582 * Free the scrollback buffer for "term". | |
583 */ | |
584 static void | |
585 free_scrollback(term_T *term) | |
586 { | |
587 int i; | |
588 | |
589 for (i = 0; i < term->tl_scrollback.ga_len; ++i) | |
590 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells); | |
591 ga_clear(&term->tl_scrollback); | |
592 } | |
593 | |
594 /* | |
595 * Free a terminal and everything it refers to. | |
596 * Kills the job if there is one. | |
597 * Called when wiping out a buffer. | |
598 */ | |
599 void | |
600 free_terminal(buf_T *buf) | |
601 { | |
602 term_T *term = buf->b_term; | |
603 term_T *tp; | |
604 | |
605 if (term == NULL) | |
606 return; | |
607 if (first_term == term) | |
608 first_term = term->tl_next; | |
609 else | |
610 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next) | |
611 if (tp->tl_next == term) | |
612 { | |
613 tp->tl_next = term->tl_next; | |
614 break; | |
615 } | |
616 | |
617 if (term->tl_job != NULL) | |
618 { | |
619 if (term->tl_job->jv_status != JOB_ENDED | |
620 && term->tl_job->jv_status != JOB_FINISHED | |
621 && term->tl_job->jv_status != JOB_FAILED) | |
622 job_stop(term->tl_job, NULL, "kill"); | |
623 job_unref(term->tl_job); | |
624 } | |
625 | |
626 free_scrollback(term); | |
627 | |
628 term_free_vterm(term); | |
629 vim_free(term->tl_title); | |
630 vim_free(term->tl_status_text); | |
631 vim_free(term->tl_opencmd); | |
632 vim_free(term->tl_eof_chars); | |
633 vim_free(term->tl_cursor_color); | |
634 vim_free(term); | |
635 buf->b_term = NULL; | |
636 if (in_terminal_loop == term) | |
637 in_terminal_loop = NULL; | |
638 } | |
639 | |
640 /* | |
641 * Write job output "msg[len]" to the vterm. | |
642 */ | |
643 static void | |
644 term_write_job_output(term_T *term, char_u *msg, size_t len) | |
645 { | |
646 VTerm *vterm = term->tl_vterm; | |
647 char_u *p; | |
648 size_t done; | |
649 size_t len_now; | |
650 | |
651 if (term_nl_does_cr) | |
652 vterm_input_write(vterm, (char *)msg, len); | |
653 else | |
654 /* need to convert NL to CR-NL */ | |
655 for (done = 0; done < len; done += len_now) | |
656 { | |
657 for (p = msg + done; p < msg + len; ) | |
658 { | |
659 if (*p == NL) | |
660 break; | |
661 p += utf_ptr2len_len(p, (int)(len - (p - msg))); | |
662 } | |
663 len_now = p - msg - done; | |
664 vterm_input_write(vterm, (char *)msg + done, len_now); | |
665 if (p < msg + len && *p == NL) | |
666 { | |
667 vterm_input_write(vterm, "\r\n", 2); | |
668 ++len_now; | |
669 } | |
670 } | |
671 | |
672 /* this invokes the damage callbacks */ | |
673 vterm_screen_flush_damage(vterm_obtain_screen(vterm)); | |
674 } | |
675 | |
676 static void | |
677 update_cursor(term_T *term, int redraw) | |
678 { | |
679 if (term->tl_normal_mode) | |
680 return; | |
681 setcursor(); | |
682 if (redraw) | |
683 { | |
684 if (term->tl_buffer == curbuf && term->tl_cursor_visible) | |
685 cursor_on(); | |
686 out_flush(); | |
687 #ifdef FEAT_GUI | |
688 if (gui.in_use) | |
689 gui_update_cursor(FALSE, FALSE); | |
690 #endif | |
691 } | |
692 } | |
693 | |
694 /* | |
695 * Invoked when "msg" output from a job was received. Write it to the terminal | |
696 * of "buffer". | |
697 */ | |
698 void | |
699 write_to_term(buf_T *buffer, char_u *msg, channel_T *channel) | |
700 { | |
701 size_t len = STRLEN(msg); | |
702 term_T *term = buffer->b_term; | |
703 | |
704 if (term->tl_vterm == NULL) | |
705 { | |
706 ch_log(channel, "NOT writing %d bytes to terminal", (int)len); | |
707 return; | |
708 } | |
709 ch_log(channel, "writing %d bytes to terminal", (int)len); | |
710 term_write_job_output(term, msg, len); | |
711 | |
712 /* In Terminal-Normal mode we are displaying the buffer, not the terminal | |
713 * contents, thus no screen update is needed. */ | |
714 if (!term->tl_normal_mode) | |
715 { | |
716 /* TODO: only update once in a while. */ | |
717 ch_log(term->tl_job->jv_channel, "updating screen"); | |
718 if (buffer == curbuf) | |
719 { | |
720 update_screen(0); | |
721 update_cursor(term, TRUE); | |
722 } | |
723 else | |
724 redraw_after_callback(TRUE); | |
725 } | |
726 } | |
727 | |
728 /* | |
729 * Send a mouse position and click to the vterm | |
730 */ | |
731 static int | |
732 term_send_mouse(VTerm *vterm, int button, int pressed) | |
733 { | |
734 VTermModifier mod = VTERM_MOD_NONE; | |
735 | |
736 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin), | |
12513
3ca08bf99396
patch 8.0.1135: W_WINCOL() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12509
diff
changeset
|
737 mouse_col - curwin->w_wincol, mod); |
12502 | 738 vterm_mouse_button(vterm, button, pressed, mod); |
739 return TRUE; | |
740 } | |
741 | |
742 /* | |
743 * Convert typed key "c" into bytes to send to the job. | |
744 * Return the number of bytes in "buf". | |
745 */ | |
746 static int | |
747 term_convert_key(term_T *term, int c, char *buf) | |
748 { | |
749 VTerm *vterm = term->tl_vterm; | |
750 VTermKey key = VTERM_KEY_NONE; | |
751 VTermModifier mod = VTERM_MOD_NONE; | |
752 int mouse = FALSE; | |
753 | |
754 switch (c) | |
755 { | |
756 case CAR: c = term_enter_char; break; | |
757 /* don't use VTERM_KEY_BACKSPACE, it always | |
758 * becomes 0x7f DEL */ | |
759 case K_BS: c = term_backspace_char; break; | |
760 | |
761 case ESC: key = VTERM_KEY_ESCAPE; break; | |
762 case K_DEL: key = VTERM_KEY_DEL; break; | |
763 case K_DOWN: key = VTERM_KEY_DOWN; break; | |
764 case K_S_DOWN: mod = VTERM_MOD_SHIFT; | |
765 key = VTERM_KEY_DOWN; break; | |
766 case K_END: key = VTERM_KEY_END; break; | |
767 case K_S_END: mod = VTERM_MOD_SHIFT; | |
768 key = VTERM_KEY_END; break; | |
769 case K_C_END: mod = VTERM_MOD_CTRL; | |
770 key = VTERM_KEY_END; break; | |
771 case K_F10: key = VTERM_KEY_FUNCTION(10); break; | |
772 case K_F11: key = VTERM_KEY_FUNCTION(11); break; | |
773 case K_F12: key = VTERM_KEY_FUNCTION(12); break; | |
774 case K_F1: key = VTERM_KEY_FUNCTION(1); break; | |
775 case K_F2: key = VTERM_KEY_FUNCTION(2); break; | |
776 case K_F3: key = VTERM_KEY_FUNCTION(3); break; | |
777 case K_F4: key = VTERM_KEY_FUNCTION(4); break; | |
778 case K_F5: key = VTERM_KEY_FUNCTION(5); break; | |
779 case K_F6: key = VTERM_KEY_FUNCTION(6); break; | |
780 case K_F7: key = VTERM_KEY_FUNCTION(7); break; | |
781 case K_F8: key = VTERM_KEY_FUNCTION(8); break; | |
782 case K_F9: key = VTERM_KEY_FUNCTION(9); break; | |
783 case K_HOME: key = VTERM_KEY_HOME; break; | |
784 case K_S_HOME: mod = VTERM_MOD_SHIFT; | |
785 key = VTERM_KEY_HOME; break; | |
786 case K_C_HOME: mod = VTERM_MOD_CTRL; | |
787 key = VTERM_KEY_HOME; break; | |
788 case K_INS: key = VTERM_KEY_INS; break; | |
789 case K_K0: key = VTERM_KEY_KP_0; break; | |
790 case K_K1: key = VTERM_KEY_KP_1; break; | |
791 case K_K2: key = VTERM_KEY_KP_2; break; | |
792 case K_K3: key = VTERM_KEY_KP_3; break; | |
793 case K_K4: key = VTERM_KEY_KP_4; break; | |
794 case K_K5: key = VTERM_KEY_KP_5; break; | |
795 case K_K6: key = VTERM_KEY_KP_6; break; | |
796 case K_K7: key = VTERM_KEY_KP_7; break; | |
797 case K_K8: key = VTERM_KEY_KP_8; break; | |
798 case K_K9: key = VTERM_KEY_KP_9; break; | |
799 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */ | |
800 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break; | |
801 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */ | |
802 case K_KENTER: key = VTERM_KEY_KP_ENTER; break; | |
803 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */ | |
804 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */ | |
805 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break; | |
806 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break; | |
807 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */ | |
808 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */ | |
809 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break; | |
810 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break; | |
811 case K_LEFT: key = VTERM_KEY_LEFT; break; | |
812 case K_S_LEFT: mod = VTERM_MOD_SHIFT; | |
813 key = VTERM_KEY_LEFT; break; | |
814 case K_C_LEFT: mod = VTERM_MOD_CTRL; | |
815 key = VTERM_KEY_LEFT; break; | |
816 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break; | |
817 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break; | |
818 case K_RIGHT: key = VTERM_KEY_RIGHT; break; | |
819 case K_S_RIGHT: mod = VTERM_MOD_SHIFT; | |
820 key = VTERM_KEY_RIGHT; break; | |
821 case K_C_RIGHT: mod = VTERM_MOD_CTRL; | |
822 key = VTERM_KEY_RIGHT; break; | |
823 case K_UP: key = VTERM_KEY_UP; break; | |
824 case K_S_UP: mod = VTERM_MOD_SHIFT; | |
825 key = VTERM_KEY_UP; break; | |
826 case TAB: key = VTERM_KEY_TAB; break; | |
827 | |
828 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break; | |
829 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break; | |
830 case K_MOUSELEFT: /* TODO */ return 0; | |
831 case K_MOUSERIGHT: /* TODO */ return 0; | |
832 | |
833 case K_LEFTMOUSE: | |
834 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break; | |
835 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break; | |
836 case K_LEFTRELEASE: | |
837 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break; | |
838 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break; | |
839 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break; | |
840 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break; | |
841 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break; | |
842 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break; | |
843 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break; | |
844 case K_X1MOUSE: /* TODO */ return 0; | |
845 case K_X1DRAG: /* TODO */ return 0; | |
846 case K_X1RELEASE: /* TODO */ return 0; | |
847 case K_X2MOUSE: /* TODO */ return 0; | |
848 case K_X2DRAG: /* TODO */ return 0; | |
849 case K_X2RELEASE: /* TODO */ return 0; | |
850 | |
851 case K_IGNORE: return 0; | |
852 case K_NOP: return 0; | |
853 case K_UNDO: return 0; | |
854 case K_HELP: return 0; | |
855 case K_XF1: key = VTERM_KEY_FUNCTION(1); break; | |
856 case K_XF2: key = VTERM_KEY_FUNCTION(2); break; | |
857 case K_XF3: key = VTERM_KEY_FUNCTION(3); break; | |
858 case K_XF4: key = VTERM_KEY_FUNCTION(4); break; | |
859 case K_SELECT: return 0; | |
860 #ifdef FEAT_GUI | |
861 case K_VER_SCROLLBAR: return 0; | |
862 case K_HOR_SCROLLBAR: return 0; | |
863 #endif | |
864 #ifdef FEAT_GUI_TABLINE | |
865 case K_TABLINE: return 0; | |
866 case K_TABMENU: return 0; | |
867 #endif | |
868 #ifdef FEAT_NETBEANS_INTG | |
869 case K_F21: key = VTERM_KEY_FUNCTION(21); break; | |
870 #endif | |
871 #ifdef FEAT_DND | |
872 case K_DROP: return 0; | |
873 #endif | |
874 #ifdef FEAT_AUTOCMD | |
875 case K_CURSORHOLD: return 0; | |
876 #endif | |
877 case K_PS: vterm_keyboard_start_paste(vterm); return 0; | |
878 case K_PE: vterm_keyboard_end_paste(vterm); return 0; | |
879 } | |
880 | |
881 /* | |
882 * Convert special keys to vterm keys: | |
883 * - Write keys to vterm: vterm_keyboard_key() | |
884 * - Write output to channel. | |
885 * TODO: use mod_mask | |
886 */ | |
887 if (key != VTERM_KEY_NONE) | |
888 /* Special key, let vterm convert it. */ | |
889 vterm_keyboard_key(vterm, key, mod); | |
890 else if (!mouse) | |
891 /* Normal character, let vterm convert it. */ | |
892 vterm_keyboard_unichar(vterm, c, mod); | |
893 | |
894 /* Read back the converted escape sequence. */ | |
895 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN); | |
896 } | |
897 | |
898 /* | |
899 * Return TRUE if the job for "term" is still running. | |
900 */ | |
901 int | |
902 term_job_running(term_T *term) | |
903 { | |
904 /* Also consider the job finished when the channel is closed, to avoid a | |
905 * race condition when updating the title. */ | |
906 return term != NULL | |
907 && term->tl_job != NULL | |
908 && channel_is_open(term->tl_job->jv_channel) | |
909 && (term->tl_job->jv_status == JOB_STARTED | |
910 || term->tl_job->jv_channel->ch_keep_open); | |
911 } | |
912 | |
913 /* | |
914 * Return TRUE if "term" has an active channel and used ":term NONE". | |
915 */ | |
916 int | |
917 term_none_open(term_T *term) | |
918 { | |
919 /* Also consider the job finished when the channel is closed, to avoid a | |
920 * race condition when updating the title. */ | |
921 return term != NULL | |
922 && term->tl_job != NULL | |
923 && channel_is_open(term->tl_job->jv_channel) | |
924 && term->tl_job->jv_channel->ch_keep_open; | |
925 } | |
926 | |
927 /* | |
928 * Add the last line of the scrollback buffer to the buffer in the window. | |
929 */ | |
930 static void | |
931 add_scrollback_line_to_buffer(term_T *term, char_u *text, int len) | |
932 { | |
933 buf_T *buf = term->tl_buffer; | |
934 int empty = (buf->b_ml.ml_flags & ML_EMPTY); | |
935 linenr_T lnum = buf->b_ml.ml_line_count; | |
936 | |
937 #ifdef WIN3264 | |
938 if (!enc_utf8 && enc_codepage > 0) | |
939 { | |
940 WCHAR *ret = NULL; | |
941 int length = 0; | |
942 | |
943 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1, | |
944 &ret, &length); | |
945 if (ret != NULL) | |
946 { | |
947 WideCharToMultiByte_alloc(enc_codepage, 0, | |
948 ret, length, (char **)&text, &len, 0, 0); | |
949 vim_free(ret); | |
950 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE); | |
951 vim_free(text); | |
952 } | |
953 } | |
954 else | |
955 #endif | |
956 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE); | |
957 if (empty) | |
958 { | |
959 /* Delete the empty line that was in the empty buffer. */ | |
960 curbuf = buf; | |
961 ml_delete(1, FALSE); | |
962 curbuf = curwin->w_buffer; | |
963 } | |
964 } | |
965 | |
966 static void | |
967 cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr) | |
968 { | |
969 attr->width = cell->width; | |
970 attr->attrs = cell->attrs; | |
971 attr->fg = cell->fg; | |
972 attr->bg = cell->bg; | |
973 } | |
974 | |
975 static int | |
976 equal_celattr(cellattr_T *a, cellattr_T *b) | |
977 { | |
978 /* Comparing the colors should be sufficient. */ | |
979 return a->fg.red == b->fg.red | |
980 && a->fg.green == b->fg.green | |
981 && a->fg.blue == b->fg.blue | |
982 && a->bg.red == b->bg.red | |
983 && a->bg.green == b->bg.green | |
984 && a->bg.blue == b->bg.blue; | |
985 } | |
986 | |
987 | |
988 /* | |
989 * Add the current lines of the terminal to scrollback and to the buffer. | |
990 * Called after the job has ended and when switching to Terminal-Normal mode. | |
991 */ | |
992 static void | |
993 move_terminal_to_buffer(term_T *term) | |
994 { | |
995 win_T *wp; | |
996 int len; | |
997 int lines_skipped = 0; | |
998 VTermPos pos; | |
999 VTermScreenCell cell; | |
1000 cellattr_T fill_attr, new_fill_attr; | |
1001 cellattr_T *p; | |
1002 VTermScreen *screen; | |
1003 | |
1004 if (term->tl_vterm == NULL) | |
1005 return; | |
1006 screen = vterm_obtain_screen(term->tl_vterm); | |
1007 fill_attr = new_fill_attr = term->tl_default_color; | |
1008 | |
1009 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row) | |
1010 { | |
1011 len = 0; | |
1012 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col) | |
1013 if (vterm_screen_get_cell(screen, pos, &cell) != 0 | |
1014 && cell.chars[0] != NUL) | |
1015 { | |
1016 len = pos.col + 1; | |
1017 new_fill_attr = term->tl_default_color; | |
1018 } | |
1019 else | |
1020 /* Assume the last attr is the filler attr. */ | |
1021 cell2cellattr(&cell, &new_fill_attr); | |
1022 | |
1023 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr)) | |
1024 ++lines_skipped; | |
1025 else | |
1026 { | |
1027 while (lines_skipped > 0) | |
1028 { | |
1029 /* Line was skipped, add an empty line. */ | |
1030 --lines_skipped; | |
1031 if (ga_grow(&term->tl_scrollback, 1) == OK) | |
1032 { | |
1033 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data | |
1034 + term->tl_scrollback.ga_len; | |
1035 | |
1036 line->sb_cols = 0; | |
1037 line->sb_cells = NULL; | |
1038 line->sb_fill_attr = fill_attr; | |
1039 ++term->tl_scrollback.ga_len; | |
1040 | |
1041 add_scrollback_line_to_buffer(term, (char_u *)"", 0); | |
1042 } | |
1043 } | |
1044 | |
1045 if (len == 0) | |
1046 p = NULL; | |
1047 else | |
1048 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len); | |
1049 if ((p != NULL || len == 0) | |
1050 && ga_grow(&term->tl_scrollback, 1) == OK) | |
1051 { | |
1052 garray_T ga; | |
1053 int width; | |
1054 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data | |
1055 + term->tl_scrollback.ga_len; | |
1056 | |
1057 ga_init2(&ga, 1, 100); | |
1058 for (pos.col = 0; pos.col < len; pos.col += width) | |
1059 { | |
1060 if (vterm_screen_get_cell(screen, pos, &cell) == 0) | |
1061 { | |
1062 width = 1; | |
1063 vim_memset(p + pos.col, 0, sizeof(cellattr_T)); | |
1064 if (ga_grow(&ga, 1) == OK) | |
1065 ga.ga_len += utf_char2bytes(' ', | |
1066 (char_u *)ga.ga_data + ga.ga_len); | |
1067 } | |
1068 else | |
1069 { | |
1070 width = cell.width; | |
1071 | |
1072 cell2cellattr(&cell, &p[pos.col]); | |
1073 | |
1074 if (ga_grow(&ga, MB_MAXBYTES) == OK) | |
1075 { | |
1076 int i; | |
1077 int c; | |
1078 | |
1079 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i) | |
1080 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c, | |
1081 (char_u *)ga.ga_data + ga.ga_len); | |
1082 } | |
1083 } | |
1084 } | |
1085 line->sb_cols = len; | |
1086 line->sb_cells = p; | |
1087 line->sb_fill_attr = new_fill_attr; | |
1088 fill_attr = new_fill_attr; | |
1089 ++term->tl_scrollback.ga_len; | |
1090 | |
1091 if (ga_grow(&ga, 1) == FAIL) | |
1092 add_scrollback_line_to_buffer(term, (char_u *)"", 0); | |
1093 else | |
1094 { | |
1095 *((char_u *)ga.ga_data + ga.ga_len) = NUL; | |
1096 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len); | |
1097 } | |
1098 ga_clear(&ga); | |
1099 } | |
1100 else | |
1101 vim_free(p); | |
1102 } | |
1103 } | |
1104 | |
1105 /* Obtain the current background color. */ | |
1106 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm), | |
1107 &term->tl_default_color.fg, &term->tl_default_color.bg); | |
1108 | |
1109 FOR_ALL_WINDOWS(wp) | |
1110 { | |
1111 if (wp->w_buffer == term->tl_buffer) | |
1112 { | |
1113 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count; | |
1114 wp->w_cursor.col = 0; | |
1115 wp->w_valid = 0; | |
1116 if (wp->w_cursor.lnum >= wp->w_height) | |
1117 { | |
1118 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1; | |
1119 | |
1120 if (wp->w_topline < min_topline) | |
1121 wp->w_topline = min_topline; | |
1122 } | |
1123 redraw_win_later(wp, NOT_VALID); | |
1124 } | |
1125 } | |
1126 } | |
1127 | |
1128 static void | |
1129 set_terminal_mode(term_T *term, int normal_mode) | |
1130 { | |
1131 term->tl_normal_mode = normal_mode; | |
1132 vim_free(term->tl_status_text); | |
1133 term->tl_status_text = NULL; | |
1134 if (term->tl_buffer == curbuf) | |
1135 maketitle(); | |
1136 } | |
1137 | |
1138 /* | |
1139 * Called after the job if finished and Terminal mode is not active: | |
1140 * Move the vterm contents into the scrollback buffer and free the vterm. | |
1141 */ | |
1142 static void | |
1143 cleanup_vterm(term_T *term) | |
1144 { | |
1145 if (term->tl_finish != 'c') | |
1146 move_terminal_to_buffer(term); | |
1147 term_free_vterm(term); | |
1148 set_terminal_mode(term, FALSE); | |
1149 } | |
1150 | |
1151 /* | |
1152 * Switch from Terminal-Job mode to Terminal-Normal mode. | |
1153 * Suspends updating the terminal window. | |
1154 */ | |
1155 static void | |
1156 term_enter_normal_mode(void) | |
1157 { | |
1158 term_T *term = curbuf->b_term; | |
1159 | |
1160 /* Append the current terminal contents to the buffer. */ | |
1161 move_terminal_to_buffer(term); | |
1162 | |
1163 set_terminal_mode(term, TRUE); | |
1164 | |
1165 /* Move the window cursor to the position of the cursor in the | |
1166 * terminal. */ | |
1167 curwin->w_cursor.lnum = term->tl_scrollback_scrolled | |
1168 + term->tl_cursor_pos.row + 1; | |
1169 check_cursor(); | |
1170 coladvance(term->tl_cursor_pos.col); | |
1171 | |
1172 /* Display the same lines as in the terminal. */ | |
1173 curwin->w_topline = term->tl_scrollback_scrolled + 1; | |
1174 } | |
1175 | |
1176 /* | |
1177 * Returns TRUE if the current window contains a terminal and we are in | |
1178 * Terminal-Normal mode. | |
1179 */ | |
1180 int | |
1181 term_in_normal_mode(void) | |
1182 { | |
1183 term_T *term = curbuf->b_term; | |
1184 | |
1185 return term != NULL && term->tl_normal_mode; | |
1186 } | |
1187 | |
1188 /* | |
1189 * Switch from Terminal-Normal mode to Terminal-Job mode. | |
1190 * Restores updating the terminal window. | |
1191 */ | |
1192 void | |
1193 term_enter_job_mode() | |
1194 { | |
1195 term_T *term = curbuf->b_term; | |
1196 sb_line_T *line; | |
1197 garray_T *gap; | |
1198 | |
1199 /* Remove the terminal contents from the scrollback and the buffer. */ | |
1200 gap = &term->tl_scrollback; | |
1201 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled | |
1202 && gap->ga_len > 0) | |
1203 { | |
1204 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
1205 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1; | |
1206 vim_free(line->sb_cells); | |
1207 --gap->ga_len; | |
1208 } | |
1209 check_cursor(); | |
1210 | |
1211 set_terminal_mode(term, FALSE); | |
1212 | |
1213 if (term->tl_channel_closed) | |
1214 cleanup_vterm(term); | |
1215 redraw_buf_and_status_later(curbuf, NOT_VALID); | |
1216 } | |
1217 | |
1218 /* | |
1219 * Get a key from the user without mapping. | |
1220 * Note: while waiting a terminal may be closed and freed if the channel is | |
1221 * closed and ++close was used. | |
1222 * Uses terminal mode mappings. | |
1223 */ | |
1224 static int | |
1225 term_vgetc() | |
1226 { | |
1227 int c; | |
1228 int save_State = State; | |
1229 | |
1230 State = TERMINAL; | |
1231 got_int = FALSE; | |
1232 #ifdef WIN3264 | |
1233 ctrl_break_was_pressed = FALSE; | |
1234 #endif | |
1235 c = vgetc(); | |
1236 got_int = FALSE; | |
1237 State = save_State; | |
1238 return c; | |
1239 } | |
1240 | |
1241 /* | |
1242 * Get the part that is connected to the tty. Normally this is PART_IN, but | |
1243 * when writing buffer lines to the job it can be another. This makes it | |
1244 * possible to do "1,5term vim -". | |
1245 */ | |
1246 static ch_part_T | |
1247 get_tty_part(term_T *term) | |
1248 { | |
1249 #ifdef UNIX | |
1250 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR}; | |
1251 int i; | |
1252 | |
1253 for (i = 0; i < 3; ++i) | |
1254 { | |
1255 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd; | |
1256 | |
1257 if (isatty(fd)) | |
1258 return parts[i]; | |
1259 } | |
1260 #endif | |
1261 return PART_IN; | |
1262 } | |
1263 | |
1264 /* | |
1265 * Send keys to terminal. | |
1266 * Return FAIL when the key needs to be handled in Normal mode. | |
1267 * Return OK when the key was dropped or sent to the terminal. | |
1268 */ | |
1269 int | |
1270 send_keys_to_term(term_T *term, int c, int typed) | |
1271 { | |
1272 char msg[KEY_BUF_LEN]; | |
1273 size_t len; | |
1274 static int mouse_was_outside = FALSE; | |
1275 int dragging_outside = FALSE; | |
1276 | |
1277 /* Catch keys that need to be handled as in Normal mode. */ | |
1278 switch (c) | |
1279 { | |
1280 case NUL: | |
1281 case K_ZERO: | |
1282 if (typed) | |
1283 stuffcharReadbuff(c); | |
1284 return FAIL; | |
1285 | |
1286 case K_IGNORE: | |
1287 return FAIL; | |
1288 | |
1289 case K_LEFTDRAG: | |
1290 case K_MIDDLEDRAG: | |
1291 case K_RIGHTDRAG: | |
1292 case K_X1DRAG: | |
1293 case K_X2DRAG: | |
1294 dragging_outside = mouse_was_outside; | |
1295 /* FALLTHROUGH */ | |
1296 case K_LEFTMOUSE: | |
1297 case K_LEFTMOUSE_NM: | |
1298 case K_LEFTRELEASE: | |
1299 case K_LEFTRELEASE_NM: | |
1300 case K_MIDDLEMOUSE: | |
1301 case K_MIDDLERELEASE: | |
1302 case K_RIGHTMOUSE: | |
1303 case K_RIGHTRELEASE: | |
1304 case K_X1MOUSE: | |
1305 case K_X1RELEASE: | |
1306 case K_X2MOUSE: | |
1307 case K_X2RELEASE: | |
1308 | |
1309 case K_MOUSEUP: | |
1310 case K_MOUSEDOWN: | |
1311 case K_MOUSELEFT: | |
1312 case K_MOUSERIGHT: | |
1313 if (mouse_row < W_WINROW(curwin) | |
1314 || mouse_row >= (W_WINROW(curwin) + curwin->w_height) | |
12513
3ca08bf99396
patch 8.0.1135: W_WINCOL() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12509
diff
changeset
|
1315 || mouse_col < curwin->w_wincol |
12502 | 1316 || mouse_col >= W_ENDCOL(curwin) |
1317 || dragging_outside) | |
1318 { | |
1319 /* click or scroll outside the current window */ | |
1320 if (typed) | |
1321 { | |
1322 stuffcharReadbuff(c); | |
1323 mouse_was_outside = TRUE; | |
1324 } | |
1325 return FAIL; | |
1326 } | |
1327 } | |
1328 if (typed) | |
1329 mouse_was_outside = FALSE; | |
1330 | |
1331 /* Convert the typed key to a sequence of bytes for the job. */ | |
1332 len = term_convert_key(term, c, msg); | |
1333 if (len > 0) | |
1334 /* TODO: if FAIL is returned, stop? */ | |
1335 channel_send(term->tl_job->jv_channel, get_tty_part(term), | |
1336 (char_u *)msg, (int)len, NULL); | |
1337 | |
1338 return OK; | |
1339 } | |
1340 | |
1341 static void | |
1342 position_cursor(win_T *wp, VTermPos *pos) | |
1343 { | |
1344 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1)); | |
1345 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1)); | |
1346 wp->w_valid |= (VALID_WCOL|VALID_WROW); | |
1347 } | |
1348 | |
1349 /* | |
1350 * Handle CTRL-W "": send register contents to the job. | |
1351 */ | |
1352 static void | |
1353 term_paste_register(int prev_c UNUSED) | |
1354 { | |
1355 int c; | |
1356 list_T *l; | |
1357 listitem_T *item; | |
1358 long reglen = 0; | |
1359 int type; | |
1360 | |
1361 #ifdef FEAT_CMDL_INFO | |
1362 if (add_to_showcmd(prev_c)) | |
1363 if (add_to_showcmd('"')) | |
1364 out_flush(); | |
1365 #endif | |
1366 c = term_vgetc(); | |
1367 #ifdef FEAT_CMDL_INFO | |
1368 clear_showcmd(); | |
1369 #endif | |
1370 if (!term_use_loop()) | |
1371 /* job finished while waiting for a character */ | |
1372 return; | |
1373 | |
1374 /* CTRL-W "= prompt for expression to evaluate. */ | |
1375 if (c == '=' && get_expr_register() != '=') | |
1376 return; | |
1377 if (!term_use_loop()) | |
1378 /* job finished while waiting for a character */ | |
1379 return; | |
1380 | |
1381 l = (list_T *)get_reg_contents(c, GREG_LIST); | |
1382 if (l != NULL) | |
1383 { | |
1384 type = get_reg_type(c, ®len); | |
1385 for (item = l->lv_first; item != NULL; item = item->li_next) | |
1386 { | |
1387 char_u *s = get_tv_string(&item->li_tv); | |
1388 #ifdef WIN3264 | |
1389 char_u *tmp = s; | |
1390 | |
1391 if (!enc_utf8 && enc_codepage > 0) | |
1392 { | |
1393 WCHAR *ret = NULL; | |
1394 int length = 0; | |
1395 | |
1396 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s, | |
1397 (int)STRLEN(s), &ret, &length); | |
1398 if (ret != NULL) | |
1399 { | |
1400 WideCharToMultiByte_alloc(CP_UTF8, 0, | |
1401 ret, length, (char **)&s, &length, 0, 0); | |
1402 vim_free(ret); | |
1403 } | |
1404 } | |
1405 #endif | |
1406 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN, | |
1407 s, (int)STRLEN(s), NULL); | |
1408 #ifdef WIN3264 | |
1409 if (tmp != s) | |
1410 vim_free(s); | |
1411 #endif | |
1412 | |
1413 if (item->li_next != NULL || type == MLINE) | |
1414 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN, | |
1415 (char_u *)"\r", 1, NULL); | |
1416 } | |
1417 list_free(l); | |
1418 } | |
1419 } | |
1420 | |
1421 #if defined(FEAT_GUI) || defined(PROTO) | |
1422 /* | |
1423 * Return TRUE when the cursor of the terminal should be displayed. | |
1424 */ | |
1425 int | |
1426 terminal_is_active() | |
1427 { | |
1428 return in_terminal_loop != NULL; | |
1429 } | |
1430 | |
1431 cursorentry_T * | |
1432 term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg) | |
1433 { | |
1434 term_T *term = in_terminal_loop; | |
1435 static cursorentry_T entry; | |
1436 | |
1437 vim_memset(&entry, 0, sizeof(entry)); | |
1438 entry.shape = entry.mshape = | |
1439 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR : | |
1440 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER : | |
1441 SHAPE_BLOCK; | |
1442 entry.percentage = 20; | |
1443 if (term->tl_cursor_blink) | |
1444 { | |
1445 entry.blinkwait = 700; | |
1446 entry.blinkon = 400; | |
1447 entry.blinkoff = 250; | |
1448 } | |
1449 *fg = gui.back_pixel; | |
1450 if (term->tl_cursor_color == NULL) | |
1451 *bg = gui.norm_pixel; | |
1452 else | |
1453 *bg = color_name2handle(term->tl_cursor_color); | |
1454 entry.name = "n"; | |
1455 entry.used_for = SHAPE_CURSOR; | |
1456 | |
1457 return &entry; | |
1458 } | |
1459 #endif | |
1460 | |
1461 static int did_change_cursor = FALSE; | |
1462 | |
1463 static void | |
1464 may_set_cursor_props(term_T *term) | |
1465 { | |
1466 #ifdef FEAT_GUI | |
1467 /* For the GUI the cursor properties are obtained with | |
1468 * term_get_cursor_shape(). */ | |
1469 if (gui.in_use) | |
1470 return; | |
1471 #endif | |
1472 if (in_terminal_loop == term) | |
1473 { | |
1474 did_change_cursor = TRUE; | |
1475 if (term->tl_cursor_color != NULL) | |
1476 term_cursor_color(term->tl_cursor_color); | |
1477 else | |
1478 term_cursor_color((char_u *)""); | |
1479 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink); | |
1480 } | |
1481 } | |
1482 | |
1483 static void | |
1484 may_restore_cursor_props(void) | |
1485 { | |
1486 #ifdef FEAT_GUI | |
1487 if (gui.in_use) | |
1488 return; | |
1489 #endif | |
1490 if (did_change_cursor) | |
1491 { | |
1492 did_change_cursor = FALSE; | |
1493 term_cursor_color((char_u *)""); | |
1494 /* this will restore the initial cursor style, if possible */ | |
1495 ui_cursor_shape_forced(TRUE); | |
1496 } | |
1497 } | |
1498 | |
1499 /* | |
1500 * Returns TRUE if the current window contains a terminal and we are sending | |
1501 * keys to the job. | |
1502 */ | |
1503 int | |
1504 term_use_loop(void) | |
1505 { | |
1506 term_T *term = curbuf->b_term; | |
1507 | |
1508 return term != NULL | |
1509 && !term->tl_normal_mode | |
1510 && term->tl_vterm != NULL | |
1511 && term_job_running(term); | |
1512 } | |
1513 | |
1514 /* | |
1515 * Wait for input and send it to the job. | |
1516 * When "blocking" is TRUE wait for a character to be typed. Otherwise return | |
1517 * when there is no more typahead. | |
1518 * Return when the start of a CTRL-W command is typed or anything else that | |
1519 * should be handled as a Normal mode command. | |
1520 * Returns OK if a typed character is to be handled in Normal mode, FAIL if | |
1521 * the terminal was closed. | |
1522 */ | |
1523 int | |
1524 terminal_loop(int blocking) | |
1525 { | |
1526 int c; | |
1527 int termkey = 0; | |
1528 int ret; | |
1529 | |
1530 /* Remember the terminal we are sending keys to. However, the terminal | |
1531 * might be closed while waiting for a character, e.g. typing "exit" in a | |
1532 * shell and ++close was used. Therefore use curbuf->b_term instead of a | |
1533 * stored reference. */ | |
1534 in_terminal_loop = curbuf->b_term; | |
1535 | |
1536 if (*curwin->w_p_tk != NUL) | |
1537 termkey = string_to_key(curwin->w_p_tk, TRUE); | |
1538 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos); | |
1539 may_set_cursor_props(curbuf->b_term); | |
1540 | |
1541 #ifdef UNIX | |
1542 { | |
1543 int part = get_tty_part(curbuf->b_term); | |
1544 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd; | |
1545 | |
1546 if (isatty(fd)) | |
1547 { | |
1548 ttyinfo_T info; | |
1549 | |
1550 /* Get the current backspace and enter characters of the pty. */ | |
1551 if (get_tty_info(fd, &info) == OK) | |
1552 { | |
1553 term_backspace_char = info.backspace; | |
1554 term_enter_char = info.enter; | |
1555 term_nl_does_cr = info.nl_does_cr; | |
1556 } | |
1557 } | |
1558 } | |
1559 #endif | |
1560 | |
1561 while (blocking || vpeekc() != NUL) | |
1562 { | |
1563 /* TODO: skip screen update when handling a sequence of keys. */ | |
1564 /* Repeat redrawing in case a message is received while redrawing. */ | |
1565 while (must_redraw != 0) | |
1566 if (update_screen(0) == FAIL) | |
1567 break; | |
1568 update_cursor(curbuf->b_term, FALSE); | |
1569 | |
1570 c = term_vgetc(); | |
1571 if (!term_use_loop()) | |
1572 /* job finished while waiting for a character */ | |
1573 break; | |
1574 if (c == K_IGNORE) | |
1575 continue; | |
1576 | |
1577 #ifdef WIN3264 | |
1578 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT. | |
1579 * Use CTRL-BREAK to kill the job. */ | |
1580 if (ctrl_break_was_pressed) | |
1581 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill"); | |
1582 #endif | |
1583 /* Was either CTRL-W (termkey) or CTRL-\ pressed? */ | |
1584 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL) | |
1585 { | |
1586 int prev_c = c; | |
1587 | |
1588 #ifdef FEAT_CMDL_INFO | |
1589 if (add_to_showcmd(c)) | |
1590 out_flush(); | |
1591 #endif | |
1592 c = term_vgetc(); | |
1593 #ifdef FEAT_CMDL_INFO | |
1594 clear_showcmd(); | |
1595 #endif | |
1596 if (!term_use_loop()) | |
1597 /* job finished while waiting for a character */ | |
1598 break; | |
1599 | |
1600 if (prev_c == Ctrl_BSL) | |
1601 { | |
1602 if (c == Ctrl_N) | |
1603 { | |
1604 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */ | |
1605 term_enter_normal_mode(); | |
1606 ret = FAIL; | |
1607 goto theend; | |
1608 } | |
1609 /* Send both keys to the terminal. */ | |
1610 send_keys_to_term(curbuf->b_term, prev_c, TRUE); | |
1611 } | |
1612 else if (c == Ctrl_C) | |
1613 { | |
1614 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */ | |
1615 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill"); | |
1616 } | |
1617 else if (termkey == 0 && c == '.') | |
1618 { | |
1619 /* "CTRL-W .": send CTRL-W to the job */ | |
1620 c = Ctrl_W; | |
1621 } | |
1622 else if (c == 'N') | |
1623 { | |
1624 /* CTRL-W N : go to Terminal-Normal mode. */ | |
1625 term_enter_normal_mode(); | |
1626 ret = FAIL; | |
1627 goto theend; | |
1628 } | |
1629 else if (c == '"') | |
1630 { | |
1631 term_paste_register(prev_c); | |
1632 continue; | |
1633 } | |
1634 else if (termkey == 0 || c != termkey) | |
1635 { | |
1636 stuffcharReadbuff(Ctrl_W); | |
1637 stuffcharReadbuff(c); | |
1638 ret = OK; | |
1639 goto theend; | |
1640 } | |
1641 } | |
1642 # ifdef WIN3264 | |
1643 if (!enc_utf8 && has_mbyte && c >= 0x80) | |
1644 { | |
1645 WCHAR wc; | |
1646 char_u mb[3]; | |
1647 | |
1648 mb[0] = (unsigned)c >> 8; | |
1649 mb[1] = c; | |
1650 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0) | |
1651 c = wc; | |
1652 } | |
1653 # endif | |
1654 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK) | |
1655 { | |
1656 ret = OK; | |
1657 goto theend; | |
1658 } | |
1659 } | |
1660 ret = FAIL; | |
1661 | |
1662 theend: | |
1663 in_terminal_loop = NULL; | |
1664 may_restore_cursor_props(); | |
1665 return ret; | |
1666 } | |
1667 | |
1668 /* | |
1669 * Called when a job has finished. | |
1670 * This updates the title and status, but does not close the vterm, because | |
1671 * there might still be pending output in the channel. | |
1672 */ | |
1673 void | |
1674 term_job_ended(job_T *job) | |
1675 { | |
1676 term_T *term; | |
1677 int did_one = FALSE; | |
1678 | |
1679 for (term = first_term; term != NULL; term = term->tl_next) | |
1680 if (term->tl_job == job) | |
1681 { | |
1682 vim_free(term->tl_title); | |
1683 term->tl_title = NULL; | |
1684 vim_free(term->tl_status_text); | |
1685 term->tl_status_text = NULL; | |
1686 redraw_buf_and_status_later(term->tl_buffer, VALID); | |
1687 did_one = TRUE; | |
1688 } | |
1689 if (did_one) | |
1690 redraw_statuslines(); | |
1691 if (curbuf->b_term != NULL) | |
1692 { | |
1693 if (curbuf->b_term->tl_job == job) | |
1694 maketitle(); | |
1695 update_cursor(curbuf->b_term, TRUE); | |
1696 } | |
1697 } | |
1698 | |
1699 static void | |
1700 may_toggle_cursor(term_T *term) | |
1701 { | |
1702 if (in_terminal_loop == term) | |
1703 { | |
1704 if (term->tl_cursor_visible) | |
1705 cursor_on(); | |
1706 else | |
1707 cursor_off(); | |
1708 } | |
1709 } | |
1710 | |
1711 /* | |
1712 * Reverse engineer the RGB value into a cterm color index. | |
1713 * First color is 1. Return 0 if no match found. | |
1714 */ | |
1715 static int | |
1716 color2index(VTermColor *color, int fg, int *boldp) | |
1717 { | |
1718 int red = color->red; | |
1719 int blue = color->blue; | |
1720 int green = color->green; | |
1721 | |
1722 /* The argument for lookup_color() is for the color_names[] table. */ | |
1723 if (red == 0) | |
1724 { | |
1725 if (green == 0) | |
1726 { | |
1727 if (blue == 0) | |
1728 return lookup_color(0, fg, boldp) + 1; /* black */ | |
1729 if (blue == 224) | |
1730 return lookup_color(1, fg, boldp) + 1; /* dark blue */ | |
1731 } | |
1732 else if (green == 224) | |
1733 { | |
1734 if (blue == 0) | |
1735 return lookup_color(2, fg, boldp) + 1; /* dark green */ | |
1736 if (blue == 224) | |
1737 return lookup_color(3, fg, boldp) + 1; /* dark cyan */ | |
1738 } | |
1739 } | |
1740 else if (red == 224) | |
1741 { | |
1742 if (green == 0) | |
1743 { | |
1744 if (blue == 0) | |
1745 return lookup_color(4, fg, boldp) + 1; /* dark red */ | |
1746 if (blue == 224) | |
1747 return lookup_color(5, fg, boldp) + 1; /* dark magenta */ | |
1748 } | |
1749 else if (green == 224) | |
1750 { | |
1751 if (blue == 0) | |
1752 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */ | |
1753 if (blue == 224) | |
1754 return lookup_color(8, fg, boldp) + 1; /* white / light grey */ | |
1755 } | |
1756 } | |
1757 else if (red == 128) | |
1758 { | |
1759 if (green == 128 && blue == 128) | |
1760 return lookup_color(12, fg, boldp) + 1; /* dark grey */ | |
1761 } | |
1762 else if (red == 255) | |
1763 { | |
1764 if (green == 64) | |
1765 { | |
1766 if (blue == 64) | |
1767 return lookup_color(20, fg, boldp) + 1; /* light red */ | |
1768 if (blue == 255) | |
1769 return lookup_color(22, fg, boldp) + 1; /* light magenta */ | |
1770 } | |
1771 else if (green == 255) | |
1772 { | |
1773 if (blue == 64) | |
1774 return lookup_color(24, fg, boldp) + 1; /* yellow */ | |
1775 if (blue == 255) | |
1776 return lookup_color(26, fg, boldp) + 1; /* white */ | |
1777 } | |
1778 } | |
1779 else if (red == 64) | |
1780 { | |
1781 if (green == 64) | |
1782 { | |
1783 if (blue == 255) | |
1784 return lookup_color(14, fg, boldp) + 1; /* light blue */ | |
1785 } | |
1786 else if (green == 255) | |
1787 { | |
1788 if (blue == 64) | |
1789 return lookup_color(16, fg, boldp) + 1; /* light green */ | |
1790 if (blue == 255) | |
1791 return lookup_color(18, fg, boldp) + 1; /* light cyan */ | |
1792 } | |
1793 } | |
1794 if (t_colors >= 256) | |
1795 { | |
1796 if (red == blue && red == green) | |
1797 { | |
12541
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1798 /* 24-color greyscale plus white and black */ |
12502 | 1799 static int cutoff[23] = { |
12541
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1800 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67, |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1801 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB, |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1802 0xD5, 0xDF, 0xE9}; |
12502 | 1803 int i; |
1804 | |
12541
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1805 if (red < 5) |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1806 return 17; /* 00/00/00 */ |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1807 if (red > 245) /* ff/ff/ff */ |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1808 return 232; |
12502 | 1809 for (i = 0; i < 23; ++i) |
1810 if (red < cutoff[i]) | |
1811 return i + 233; | |
1812 return 256; | |
1813 } | |
12541
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1814 { |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1815 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB}; |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1816 int ri, gi, bi; |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1817 |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1818 /* 216-color cube */ |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1819 for (ri = 0; ri < 5; ++ri) |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1820 if (red < cutoff[ri]) |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1821 break; |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1822 for (gi = 0; gi < 5; ++gi) |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1823 if (green < cutoff[gi]) |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1824 break; |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1825 for (bi = 0; bi < 5; ++bi) |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1826 if (blue < cutoff[bi]) |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1827 break; |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1828 return 17 + ri * 36 + gi * 6 + bi; |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
1829 } |
12502 | 1830 } |
1831 return 0; | |
1832 } | |
1833 | |
1834 /* | |
1835 * Convert the attributes of a vterm cell into an attribute index. | |
1836 */ | |
1837 static int | |
1838 cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg) | |
1839 { | |
1840 int attr = 0; | |
1841 | |
1842 if (cellattrs.bold) | |
1843 attr |= HL_BOLD; | |
1844 if (cellattrs.underline) | |
1845 attr |= HL_UNDERLINE; | |
1846 if (cellattrs.italic) | |
1847 attr |= HL_ITALIC; | |
1848 if (cellattrs.strike) | |
1849 attr |= HL_STRIKETHROUGH; | |
1850 if (cellattrs.reverse) | |
1851 attr |= HL_INVERSE; | |
1852 | |
1853 #ifdef FEAT_GUI | |
1854 if (gui.in_use) | |
1855 { | |
1856 guicolor_T fg, bg; | |
1857 | |
1858 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue); | |
1859 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue); | |
1860 return get_gui_attr_idx(attr, fg, bg); | |
1861 } | |
1862 else | |
1863 #endif | |
1864 #ifdef FEAT_TERMGUICOLORS | |
1865 if (p_tgc) | |
1866 { | |
1867 guicolor_T fg, bg; | |
1868 | |
1869 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue); | |
1870 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue); | |
1871 | |
1872 return get_tgc_attr_idx(attr, fg, bg); | |
1873 } | |
1874 else | |
1875 #endif | |
1876 { | |
1877 int bold = MAYBE; | |
1878 int fg = color2index(&cellfg, TRUE, &bold); | |
1879 int bg = color2index(&cellbg, FALSE, &bold); | |
1880 | |
1881 /* with 8 colors set the bold attribute to get a bright foreground */ | |
1882 if (bold == TRUE) | |
1883 attr |= HL_BOLD; | |
1884 return get_cterm_attr_idx(attr, fg, bg); | |
1885 } | |
1886 return 0; | |
1887 } | |
1888 | |
1889 static int | |
1890 handle_damage(VTermRect rect, void *user) | |
1891 { | |
1892 term_T *term = (term_T *)user; | |
1893 | |
1894 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row); | |
1895 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row); | |
1896 redraw_buf_later(term->tl_buffer, NOT_VALID); | |
1897 return 1; | |
1898 } | |
1899 | |
1900 static int | |
1901 handle_moverect(VTermRect dest, VTermRect src, void *user) | |
1902 { | |
1903 term_T *term = (term_T *)user; | |
1904 | |
1905 /* Scrolling up is done much more efficiently by deleting lines instead of | |
1906 * redrawing the text. */ | |
1907 if (dest.start_col == src.start_col | |
1908 && dest.end_col == src.end_col | |
1909 && dest.start_row < src.start_row) | |
1910 { | |
1911 win_T *wp; | |
1912 VTermColor fg, bg; | |
1913 VTermScreenCellAttrs attr; | |
1914 int clear_attr; | |
1915 | |
1916 /* Set the color to clear lines with. */ | |
1917 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm), | |
1918 &fg, &bg); | |
1919 vim_memset(&attr, 0, sizeof(attr)); | |
1920 clear_attr = cell2attr(attr, fg, bg); | |
1921 | |
1922 FOR_ALL_WINDOWS(wp) | |
1923 { | |
1924 if (wp->w_buffer == term->tl_buffer) | |
1925 win_del_lines(wp, dest.start_row, | |
1926 src.start_row - dest.start_row, FALSE, FALSE, | |
1927 clear_attr); | |
1928 } | |
1929 } | |
12578
f8beecfea2c4
patch 8.0.1167: Motif: typing in terminal window is slow
Christian Brabandt <cb@256bit.org>
parents:
12541
diff
changeset
|
1930 |
f8beecfea2c4
patch 8.0.1167: Motif: typing in terminal window is slow
Christian Brabandt <cb@256bit.org>
parents:
12541
diff
changeset
|
1931 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row); |
f8beecfea2c4
patch 8.0.1167: Motif: typing in terminal window is slow
Christian Brabandt <cb@256bit.org>
parents:
12541
diff
changeset
|
1932 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row); |
f8beecfea2c4
patch 8.0.1167: Motif: typing in terminal window is slow
Christian Brabandt <cb@256bit.org>
parents:
12541
diff
changeset
|
1933 |
12502 | 1934 redraw_buf_later(term->tl_buffer, NOT_VALID); |
1935 return 1; | |
1936 } | |
1937 | |
1938 static int | |
1939 handle_movecursor( | |
1940 VTermPos pos, | |
1941 VTermPos oldpos UNUSED, | |
1942 int visible, | |
1943 void *user) | |
1944 { | |
1945 term_T *term = (term_T *)user; | |
1946 win_T *wp; | |
1947 | |
1948 term->tl_cursor_pos = pos; | |
1949 term->tl_cursor_visible = visible; | |
1950 | |
1951 FOR_ALL_WINDOWS(wp) | |
1952 { | |
1953 if (wp->w_buffer == term->tl_buffer) | |
1954 position_cursor(wp, &pos); | |
1955 } | |
1956 if (term->tl_buffer == curbuf && !term->tl_normal_mode) | |
1957 { | |
1958 may_toggle_cursor(term); | |
1959 update_cursor(term, term->tl_cursor_visible); | |
1960 } | |
1961 | |
1962 return 1; | |
1963 } | |
1964 | |
1965 static int | |
1966 handle_settermprop( | |
1967 VTermProp prop, | |
1968 VTermValue *value, | |
1969 void *user) | |
1970 { | |
1971 term_T *term = (term_T *)user; | |
1972 | |
1973 switch (prop) | |
1974 { | |
1975 case VTERM_PROP_TITLE: | |
1976 vim_free(term->tl_title); | |
1977 /* a blank title isn't useful, make it empty, so that "running" is | |
1978 * displayed */ | |
1979 if (*skipwhite((char_u *)value->string) == NUL) | |
1980 term->tl_title = NULL; | |
1981 #ifdef WIN3264 | |
1982 else if (!enc_utf8 && enc_codepage > 0) | |
1983 { | |
1984 WCHAR *ret = NULL; | |
1985 int length = 0; | |
1986 | |
1987 MultiByteToWideChar_alloc(CP_UTF8, 0, | |
1988 (char*)value->string, (int)STRLEN(value->string), | |
1989 &ret, &length); | |
1990 if (ret != NULL) | |
1991 { | |
1992 WideCharToMultiByte_alloc(enc_codepage, 0, | |
1993 ret, length, (char**)&term->tl_title, | |
1994 &length, 0, 0); | |
1995 vim_free(ret); | |
1996 } | |
1997 } | |
1998 #endif | |
1999 else | |
2000 term->tl_title = vim_strsave((char_u *)value->string); | |
2001 vim_free(term->tl_status_text); | |
2002 term->tl_status_text = NULL; | |
2003 if (term == curbuf->b_term) | |
2004 maketitle(); | |
2005 break; | |
2006 | |
2007 case VTERM_PROP_CURSORVISIBLE: | |
2008 term->tl_cursor_visible = value->boolean; | |
2009 may_toggle_cursor(term); | |
2010 out_flush(); | |
2011 break; | |
2012 | |
2013 case VTERM_PROP_CURSORBLINK: | |
2014 term->tl_cursor_blink = value->boolean; | |
2015 may_set_cursor_props(term); | |
2016 break; | |
2017 | |
2018 case VTERM_PROP_CURSORSHAPE: | |
2019 term->tl_cursor_shape = value->number; | |
2020 may_set_cursor_props(term); | |
2021 break; | |
2022 | |
2023 case VTERM_PROP_CURSORCOLOR: | |
2024 vim_free(term->tl_cursor_color); | |
2025 if (*value->string == NUL) | |
2026 term->tl_cursor_color = NULL; | |
2027 else | |
2028 term->tl_cursor_color = vim_strsave((char_u *)value->string); | |
2029 may_set_cursor_props(term); | |
2030 break; | |
2031 | |
2032 case VTERM_PROP_ALTSCREEN: | |
2033 /* TODO: do anything else? */ | |
2034 term->tl_using_altscreen = value->boolean; | |
2035 break; | |
2036 | |
2037 default: | |
2038 break; | |
2039 } | |
2040 /* Always return 1, otherwise vterm doesn't store the value internally. */ | |
2041 return 1; | |
2042 } | |
2043 | |
2044 /* | |
2045 * The job running in the terminal resized the terminal. | |
2046 */ | |
2047 static int | |
2048 handle_resize(int rows, int cols, void *user) | |
2049 { | |
2050 term_T *term = (term_T *)user; | |
2051 win_T *wp; | |
2052 | |
2053 term->tl_rows = rows; | |
2054 term->tl_cols = cols; | |
2055 if (term->tl_vterm_size_changed) | |
2056 /* Size was set by vterm_set_size(), don't set the window size. */ | |
2057 term->tl_vterm_size_changed = FALSE; | |
2058 else | |
2059 { | |
2060 FOR_ALL_WINDOWS(wp) | |
2061 { | |
2062 if (wp->w_buffer == term->tl_buffer) | |
2063 { | |
2064 win_setheight_win(rows, wp); | |
2065 win_setwidth_win(cols, wp); | |
2066 } | |
2067 } | |
2068 redraw_buf_later(term->tl_buffer, NOT_VALID); | |
2069 } | |
2070 return 1; | |
2071 } | |
2072 | |
2073 /* | |
2074 * Handle a line that is pushed off the top of the screen. | |
2075 */ | |
2076 static int | |
2077 handle_pushline(int cols, const VTermScreenCell *cells, void *user) | |
2078 { | |
2079 term_T *term = (term_T *)user; | |
2080 | |
2081 /* TODO: Limit the number of lines that are stored. */ | |
2082 if (ga_grow(&term->tl_scrollback, 1) == OK) | |
2083 { | |
2084 cellattr_T *p = NULL; | |
2085 int len = 0; | |
2086 int i; | |
2087 int c; | |
2088 int col; | |
2089 sb_line_T *line; | |
2090 garray_T ga; | |
2091 cellattr_T fill_attr = term->tl_default_color; | |
2092 | |
2093 /* do not store empty cells at the end */ | |
2094 for (i = 0; i < cols; ++i) | |
2095 if (cells[i].chars[0] != 0) | |
2096 len = i + 1; | |
2097 else | |
2098 cell2cellattr(&cells[i], &fill_attr); | |
2099 | |
2100 ga_init2(&ga, 1, 100); | |
2101 if (len > 0) | |
2102 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len); | |
2103 if (p != NULL) | |
2104 { | |
2105 for (col = 0; col < len; col += cells[col].width) | |
2106 { | |
2107 if (ga_grow(&ga, MB_MAXBYTES) == FAIL) | |
2108 { | |
2109 ga.ga_len = 0; | |
2110 break; | |
2111 } | |
2112 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i) | |
2113 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c, | |
2114 (char_u *)ga.ga_data + ga.ga_len); | |
2115 cell2cellattr(&cells[col], &p[col]); | |
2116 } | |
2117 } | |
2118 if (ga_grow(&ga, 1) == FAIL) | |
2119 add_scrollback_line_to_buffer(term, (char_u *)"", 0); | |
2120 else | |
2121 { | |
2122 *((char_u *)ga.ga_data + ga.ga_len) = NUL; | |
2123 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len); | |
2124 } | |
2125 ga_clear(&ga); | |
2126 | |
2127 line = (sb_line_T *)term->tl_scrollback.ga_data | |
2128 + term->tl_scrollback.ga_len; | |
2129 line->sb_cols = len; | |
2130 line->sb_cells = p; | |
2131 line->sb_fill_attr = fill_attr; | |
2132 ++term->tl_scrollback.ga_len; | |
2133 ++term->tl_scrollback_scrolled; | |
2134 } | |
2135 return 0; /* ignored */ | |
2136 } | |
2137 | |
2138 static VTermScreenCallbacks screen_callbacks = { | |
2139 handle_damage, /* damage */ | |
2140 handle_moverect, /* moverect */ | |
2141 handle_movecursor, /* movecursor */ | |
2142 handle_settermprop, /* settermprop */ | |
2143 NULL, /* bell */ | |
2144 handle_resize, /* resize */ | |
2145 handle_pushline, /* sb_pushline */ | |
2146 NULL /* sb_popline */ | |
2147 }; | |
2148 | |
2149 /* | |
2150 * Called when a channel has been closed. | |
2151 * If this was a channel for a terminal window then finish it up. | |
2152 */ | |
2153 void | |
2154 term_channel_closed(channel_T *ch) | |
2155 { | |
2156 term_T *term; | |
2157 int did_one = FALSE; | |
2158 | |
2159 for (term = first_term; term != NULL; term = term->tl_next) | |
2160 if (term->tl_job == ch->ch_job) | |
2161 { | |
2162 term->tl_channel_closed = TRUE; | |
2163 did_one = TRUE; | |
2164 | |
2165 vim_free(term->tl_title); | |
2166 term->tl_title = NULL; | |
2167 vim_free(term->tl_status_text); | |
2168 term->tl_status_text = NULL; | |
2169 | |
2170 /* Unless in Terminal-Normal mode: clear the vterm. */ | |
2171 if (!term->tl_normal_mode) | |
2172 { | |
2173 int fnum = term->tl_buffer->b_fnum; | |
2174 | |
2175 cleanup_vterm(term); | |
2176 | |
2177 if (term->tl_finish == 'c') | |
2178 { | |
2179 /* ++close or term_finish == "close" */ | |
2180 ch_log(NULL, "terminal job finished, closing window"); | |
2181 curbuf = term->tl_buffer; | |
2182 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE); | |
2183 break; | |
2184 } | |
2185 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0) | |
2186 { | |
2187 char buf[50]; | |
2188 | |
2189 /* TODO: use term_opencmd */ | |
2190 ch_log(NULL, "terminal job finished, opening window"); | |
2191 vim_snprintf(buf, sizeof(buf), | |
2192 term->tl_opencmd == NULL | |
2193 ? "botright sbuf %d" | |
2194 : (char *)term->tl_opencmd, fnum); | |
2195 do_cmdline_cmd((char_u *)buf); | |
2196 } | |
2197 else | |
2198 ch_log(NULL, "terminal job finished"); | |
2199 } | |
2200 | |
2201 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID); | |
2202 } | |
2203 if (did_one) | |
2204 { | |
2205 redraw_statuslines(); | |
2206 | |
2207 /* Need to break out of vgetc(). */ | |
2208 ins_char_typebuf(K_IGNORE); | |
2209 typebuf_was_filled = TRUE; | |
2210 | |
2211 term = curbuf->b_term; | |
2212 if (term != NULL) | |
2213 { | |
2214 if (term->tl_job == ch->ch_job) | |
2215 maketitle(); | |
2216 update_cursor(term, term->tl_cursor_visible); | |
2217 } | |
2218 } | |
2219 } | |
2220 | |
2221 /* | |
2222 * Called to update a window that contains an active terminal. | |
2223 * Returns FAIL when there is no terminal running in this window or in | |
2224 * Terminal-Normal mode. | |
2225 */ | |
2226 int | |
2227 term_update_window(win_T *wp) | |
2228 { | |
2229 term_T *term = wp->w_buffer->b_term; | |
2230 VTerm *vterm; | |
2231 VTermScreen *screen; | |
2232 VTermState *state; | |
2233 VTermPos pos; | |
2234 | |
2235 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode) | |
2236 return FAIL; | |
2237 | |
2238 vterm = term->tl_vterm; | |
2239 screen = vterm_obtain_screen(vterm); | |
2240 state = vterm_obtain_state(vterm); | |
2241 | |
12590
3c46dcf3b335
patch 8.0.1173: terminal window is not redrawn after CTRL-L
Christian Brabandt <cb@256bit.org>
parents:
12578
diff
changeset
|
2242 if (wp->w_redr_type >= NOT_VALID) |
3c46dcf3b335
patch 8.0.1173: terminal window is not redrawn after CTRL-L
Christian Brabandt <cb@256bit.org>
parents:
12578
diff
changeset
|
2243 { |
3c46dcf3b335
patch 8.0.1173: terminal window is not redrawn after CTRL-L
Christian Brabandt <cb@256bit.org>
parents:
12578
diff
changeset
|
2244 term->tl_dirty_row_start = 0; |
3c46dcf3b335
patch 8.0.1173: terminal window is not redrawn after CTRL-L
Christian Brabandt <cb@256bit.org>
parents:
12578
diff
changeset
|
2245 term->tl_dirty_row_end = MAX_ROW; |
3c46dcf3b335
patch 8.0.1173: terminal window is not redrawn after CTRL-L
Christian Brabandt <cb@256bit.org>
parents:
12578
diff
changeset
|
2246 } |
3c46dcf3b335
patch 8.0.1173: terminal window is not redrawn after CTRL-L
Christian Brabandt <cb@256bit.org>
parents:
12578
diff
changeset
|
2247 |
12502 | 2248 /* |
2249 * If the window was resized a redraw will be triggered and we get here. | |
2250 * Adjust the size of the vterm unless 'termsize' specifies a fixed size. | |
2251 */ | |
2252 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height) | |
2253 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width)) | |
2254 { | |
2255 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height; | |
2256 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width; | |
2257 win_T *twp; | |
2258 | |
2259 FOR_ALL_WINDOWS(twp) | |
2260 { | |
2261 /* When more than one window shows the same terminal, use the | |
2262 * smallest size. */ | |
2263 if (twp->w_buffer == term->tl_buffer) | |
2264 { | |
2265 if (!term->tl_rows_fixed && rows > twp->w_height) | |
2266 rows = twp->w_height; | |
2267 if (!term->tl_cols_fixed && cols > twp->w_width) | |
2268 cols = twp->w_width; | |
2269 } | |
2270 } | |
2271 | |
2272 term->tl_vterm_size_changed = TRUE; | |
2273 vterm_set_size(vterm, rows, cols); | |
2274 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines", | |
2275 rows); | |
2276 term_report_winsize(term, rows, cols); | |
2277 } | |
2278 | |
2279 /* The cursor may have been moved when resizing. */ | |
2280 vterm_state_get_cursorpos(state, &pos); | |
2281 position_cursor(wp, &pos); | |
2282 | |
12578
f8beecfea2c4
patch 8.0.1167: Motif: typing in terminal window is slow
Christian Brabandt <cb@256bit.org>
parents:
12541
diff
changeset
|
2283 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end |
f8beecfea2c4
patch 8.0.1167: Motif: typing in terminal window is slow
Christian Brabandt <cb@256bit.org>
parents:
12541
diff
changeset
|
2284 && pos.row < wp->w_height; ++pos.row) |
12502 | 2285 { |
2286 int off = screen_get_current_line_off(); | |
2287 int max_col = MIN(wp->w_width, term->tl_cols); | |
2288 | |
2289 if (pos.row < term->tl_rows) | |
2290 { | |
2291 for (pos.col = 0; pos.col < max_col; ) | |
2292 { | |
2293 VTermScreenCell cell; | |
2294 int c; | |
2295 | |
2296 if (vterm_screen_get_cell(screen, pos, &cell) == 0) | |
2297 vim_memset(&cell, 0, sizeof(cell)); | |
2298 | |
2299 /* TODO: composing chars */ | |
2300 c = cell.chars[0]; | |
2301 if (c == NUL) | |
2302 { | |
2303 ScreenLines[off] = ' '; | |
2304 if (enc_utf8) | |
2305 ScreenLinesUC[off] = NUL; | |
2306 } | |
2307 else | |
2308 { | |
2309 if (enc_utf8) | |
2310 { | |
2311 if (c >= 0x80) | |
2312 { | |
2313 ScreenLines[off] = ' '; | |
2314 ScreenLinesUC[off] = c; | |
2315 } | |
2316 else | |
2317 { | |
2318 ScreenLines[off] = c; | |
2319 ScreenLinesUC[off] = NUL; | |
2320 } | |
2321 } | |
2322 #ifdef WIN3264 | |
2323 else if (has_mbyte && c >= 0x80) | |
2324 { | |
2325 char_u mb[MB_MAXBYTES+1]; | |
2326 WCHAR wc = c; | |
2327 | |
2328 if (WideCharToMultiByte(GetACP(), 0, &wc, 1, | |
2329 (char*)mb, 2, 0, 0) > 1) | |
2330 { | |
2331 ScreenLines[off] = mb[0]; | |
2332 ScreenLines[off + 1] = mb[1]; | |
2333 cell.width = mb_ptr2cells(mb); | |
2334 } | |
2335 else | |
2336 ScreenLines[off] = c; | |
2337 } | |
2338 #endif | |
2339 else | |
2340 ScreenLines[off] = c; | |
2341 } | |
2342 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg); | |
2343 | |
2344 ++pos.col; | |
2345 ++off; | |
2346 if (cell.width == 2) | |
2347 { | |
2348 if (enc_utf8) | |
2349 ScreenLinesUC[off] = NUL; | |
2350 | |
2351 /* don't set the second byte to NUL for a DBCS encoding, it | |
2352 * has been set above */ | |
2353 if (enc_utf8 || !has_mbyte) | |
2354 ScreenLines[off] = NUL; | |
2355 | |
2356 ++pos.col; | |
2357 ++off; | |
2358 } | |
2359 } | |
2360 } | |
2361 else | |
2362 pos.col = 0; | |
2363 | |
2364 screen_line(wp->w_winrow + pos.row, wp->w_wincol, | |
2365 pos.col, wp->w_width, FALSE); | |
2366 } | |
12578
f8beecfea2c4
patch 8.0.1167: Motif: typing in terminal window is slow
Christian Brabandt <cb@256bit.org>
parents:
12541
diff
changeset
|
2367 term->tl_dirty_row_start = MAX_ROW; |
f8beecfea2c4
patch 8.0.1167: Motif: typing in terminal window is slow
Christian Brabandt <cb@256bit.org>
parents:
12541
diff
changeset
|
2368 term->tl_dirty_row_end = 0; |
12502 | 2369 |
2370 return OK; | |
2371 } | |
2372 | |
2373 /* | |
2374 * Return TRUE if "wp" is a terminal window where the job has finished. | |
2375 */ | |
2376 int | |
2377 term_is_finished(buf_T *buf) | |
2378 { | |
2379 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL; | |
2380 } | |
2381 | |
2382 /* | |
2383 * Return TRUE if "wp" is a terminal window where the job has finished or we | |
2384 * are in Terminal-Normal mode, thus we show the buffer contents. | |
2385 */ | |
2386 int | |
2387 term_show_buffer(buf_T *buf) | |
2388 { | |
2389 term_T *term = buf->b_term; | |
2390 | |
2391 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode); | |
2392 } | |
2393 | |
2394 /* | |
2395 * The current buffer is going to be changed. If there is terminal | |
2396 * highlighting remove it now. | |
2397 */ | |
2398 void | |
2399 term_change_in_curbuf(void) | |
2400 { | |
2401 term_T *term = curbuf->b_term; | |
2402 | |
2403 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0) | |
2404 { | |
2405 free_scrollback(term); | |
2406 redraw_buf_later(term->tl_buffer, NOT_VALID); | |
2407 | |
2408 /* The buffer is now like a normal buffer, it cannot be easily | |
2409 * abandoned when changed. */ | |
2410 set_string_option_direct((char_u *)"buftype", -1, | |
2411 (char_u *)"", OPT_FREE|OPT_LOCAL, 0); | |
2412 } | |
2413 } | |
2414 | |
2415 /* | |
2416 * Get the screen attribute for a position in the buffer. | |
2417 * Use a negative "col" to get the filler background color. | |
2418 */ | |
2419 int | |
2420 term_get_attr(buf_T *buf, linenr_T lnum, int col) | |
2421 { | |
2422 term_T *term = buf->b_term; | |
2423 sb_line_T *line; | |
2424 cellattr_T *cellattr; | |
2425 | |
2426 if (lnum > term->tl_scrollback.ga_len) | |
2427 cellattr = &term->tl_default_color; | |
2428 else | |
2429 { | |
2430 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1; | |
2431 if (col < 0 || col >= line->sb_cols) | |
2432 cellattr = &line->sb_fill_attr; | |
2433 else | |
2434 cellattr = line->sb_cells + col; | |
2435 } | |
2436 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg); | |
2437 } | |
2438 | |
2439 static VTermColor ansi_table[16] = { | |
2440 { 0, 0, 0}, /* black */ | |
2441 {224, 0, 0}, /* dark red */ | |
2442 { 0, 224, 0}, /* dark green */ | |
2443 {224, 224, 0}, /* dark yellow / brown */ | |
2444 { 0, 0, 224}, /* dark blue */ | |
2445 {224, 0, 224}, /* dark magenta */ | |
2446 { 0, 224, 224}, /* dark cyan */ | |
2447 {224, 224, 224}, /* light grey */ | |
2448 | |
2449 {128, 128, 128}, /* dark grey */ | |
2450 {255, 64, 64}, /* light red */ | |
2451 { 64, 255, 64}, /* light green */ | |
2452 {255, 255, 64}, /* yellow */ | |
2453 { 64, 64, 255}, /* light blue */ | |
2454 {255, 64, 255}, /* light magenta */ | |
2455 { 64, 255, 255}, /* light cyan */ | |
2456 {255, 255, 255}, /* white */ | |
2457 }; | |
2458 | |
2459 static int cube_value[] = { | |
12541
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
2460 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF |
12502 | 2461 }; |
2462 | |
2463 static int grey_ramp[] = { | |
12541
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
2464 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76, |
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
2465 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE |
12502 | 2466 }; |
2467 | |
2468 /* | |
2469 * Convert a cterm color number 0 - 255 to RGB. | |
12541
fcb11cfca8b3
patch 8.0.1149: libvterm colors differ from xterm
Christian Brabandt <cb@256bit.org>
parents:
12513
diff
changeset
|
2470 * This is compatible with xterm. |
12502 | 2471 */ |
2472 static void | |
2473 cterm_color2rgb(int nr, VTermColor *rgb) | |
2474 { | |
2475 int idx; | |
2476 | |
2477 if (nr < 16) | |
2478 { | |
2479 *rgb = ansi_table[nr]; | |
2480 } | |
2481 else if (nr < 232) | |
2482 { | |
2483 /* 216 color cube */ | |
2484 idx = nr - 16; | |
2485 rgb->blue = cube_value[idx % 6]; | |
2486 rgb->green = cube_value[idx / 6 % 6]; | |
2487 rgb->red = cube_value[idx / 36 % 6]; | |
2488 } | |
2489 else if (nr < 256) | |
2490 { | |
2491 /* 24 grey scale ramp */ | |
2492 idx = nr - 232; | |
2493 rgb->blue = grey_ramp[idx]; | |
2494 rgb->green = grey_ramp[idx]; | |
2495 rgb->red = grey_ramp[idx]; | |
2496 } | |
2497 } | |
2498 | |
2499 /* | |
2500 * Create a new vterm and initialize it. | |
2501 */ | |
2502 static void | |
2503 create_vterm(term_T *term, int rows, int cols) | |
2504 { | |
2505 VTerm *vterm; | |
2506 VTermScreen *screen; | |
2507 VTermValue value; | |
2508 VTermColor *fg, *bg; | |
2509 int fgval, bgval; | |
2510 int id; | |
2511 | |
2512 vterm = vterm_new(rows, cols); | |
2513 term->tl_vterm = vterm; | |
2514 screen = vterm_obtain_screen(vterm); | |
2515 vterm_screen_set_callbacks(screen, &screen_callbacks, term); | |
2516 /* TODO: depends on 'encoding'. */ | |
2517 vterm_set_utf8(vterm, 1); | |
2518 | |
2519 vim_memset(&term->tl_default_color.attrs, 0, sizeof(VTermScreenCellAttrs)); | |
2520 term->tl_default_color.width = 1; | |
2521 fg = &term->tl_default_color.fg; | |
2522 bg = &term->tl_default_color.bg; | |
2523 | |
2524 /* Vterm uses a default black background. Set it to white when | |
2525 * 'background' is "light". */ | |
2526 if (*p_bg == 'l') | |
2527 { | |
2528 fgval = 0; | |
2529 bgval = 255; | |
2530 } | |
2531 else | |
2532 { | |
2533 fgval = 255; | |
2534 bgval = 0; | |
2535 } | |
2536 fg->red = fg->green = fg->blue = fgval; | |
2537 bg->red = bg->green = bg->blue = bgval; | |
2538 | |
2539 /* The "Terminal" highlight group overrules the defaults. */ | |
2540 id = syn_name2id((char_u *)"Terminal"); | |
2541 | |
2542 /* Use the actual color for the GUI and when 'guitermcolors' is set. */ | |
2543 #if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) | |
2544 if (0 | |
2545 # ifdef FEAT_GUI | |
2546 || gui.in_use | |
2547 # endif | |
2548 # ifdef FEAT_TERMGUICOLORS | |
2549 || p_tgc | |
2550 # endif | |
2551 ) | |
2552 { | |
2553 guicolor_T fg_rgb = INVALCOLOR; | |
2554 guicolor_T bg_rgb = INVALCOLOR; | |
2555 | |
2556 if (id != 0) | |
2557 syn_id2colors(id, &fg_rgb, &bg_rgb); | |
2558 | |
2559 # ifdef FEAT_GUI | |
2560 if (gui.in_use) | |
2561 { | |
2562 if (fg_rgb == INVALCOLOR) | |
2563 fg_rgb = gui.norm_pixel; | |
2564 if (bg_rgb == INVALCOLOR) | |
2565 bg_rgb = gui.back_pixel; | |
2566 } | |
2567 # ifdef FEAT_TERMGUICOLORS | |
2568 else | |
2569 # endif | |
2570 # endif | |
2571 # ifdef FEAT_TERMGUICOLORS | |
2572 { | |
2573 if (fg_rgb == INVALCOLOR) | |
2574 fg_rgb = cterm_normal_fg_gui_color; | |
2575 if (bg_rgb == INVALCOLOR) | |
2576 bg_rgb = cterm_normal_bg_gui_color; | |
2577 } | |
2578 # endif | |
2579 if (fg_rgb != INVALCOLOR) | |
2580 { | |
2581 long_u rgb = GUI_MCH_GET_RGB(fg_rgb); | |
2582 | |
2583 fg->red = (unsigned)(rgb >> 16); | |
2584 fg->green = (unsigned)(rgb >> 8) & 255; | |
2585 fg->blue = (unsigned)rgb & 255; | |
2586 } | |
2587 if (bg_rgb != INVALCOLOR) | |
2588 { | |
2589 long_u rgb = GUI_MCH_GET_RGB(bg_rgb); | |
2590 | |
2591 bg->red = (unsigned)(rgb >> 16); | |
2592 bg->green = (unsigned)(rgb >> 8) & 255; | |
2593 bg->blue = (unsigned)rgb & 255; | |
2594 } | |
2595 } | |
2596 else | |
2597 #endif | |
2598 if (id != 0 && t_colors >= 16) | |
2599 { | |
2600 int cterm_fg, cterm_bg; | |
2601 | |
2602 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg); | |
2603 if (cterm_fg >= 0) | |
2604 cterm_color2rgb(cterm_fg, fg); | |
2605 if (cterm_bg >= 0) | |
2606 cterm_color2rgb(cterm_bg, bg); | |
2607 } | |
2608 #if defined(WIN3264) && !defined(FEAT_GUI_W32) | |
2609 else | |
2610 { | |
2611 int tmp; | |
2612 | |
2613 /* In an MS-Windows console we know the normal colors. */ | |
2614 if (cterm_normal_fg_color > 0) | |
2615 { | |
2616 cterm_color2rgb(cterm_normal_fg_color - 1, fg); | |
2617 tmp = fg->red; | |
2618 fg->red = fg->blue; | |
2619 fg->blue = tmp; | |
2620 } | |
2621 if (cterm_normal_bg_color > 0) | |
2622 { | |
2623 cterm_color2rgb(cterm_normal_bg_color - 1, bg); | |
2624 tmp = bg->red; | |
2625 bg->red = bg->blue; | |
2626 bg->blue = tmp; | |
2627 } | |
2628 } | |
2629 #endif | |
2630 | |
2631 vterm_state_set_default_colors(vterm_obtain_state(vterm), fg, bg); | |
2632 | |
2633 /* Required to initialize most things. */ | |
2634 vterm_screen_reset(screen, 1 /* hard */); | |
2635 | |
2636 /* Allow using alternate screen. */ | |
2637 vterm_screen_enable_altscreen(screen, 1); | |
2638 | |
2639 /* For unix do not use a blinking cursor. In an xterm this causes the | |
2640 * cursor to blink if it's blinking in the xterm. | |
2641 * For Windows we respect the system wide setting. */ | |
2642 #ifdef WIN3264 | |
2643 if (GetCaretBlinkTime() == INFINITE) | |
2644 value.boolean = 0; | |
2645 else | |
2646 value.boolean = 1; | |
2647 #else | |
2648 value.boolean = 0; | |
2649 #endif | |
2650 vterm_state_set_termprop(vterm_obtain_state(vterm), | |
2651 VTERM_PROP_CURSORBLINK, &value); | |
2652 } | |
2653 | |
2654 /* | |
2655 * Return the text to show for the buffer name and status. | |
2656 */ | |
2657 char_u * | |
2658 term_get_status_text(term_T *term) | |
2659 { | |
2660 if (term->tl_status_text == NULL) | |
2661 { | |
2662 char_u *txt; | |
2663 size_t len; | |
2664 | |
2665 if (term->tl_normal_mode) | |
2666 { | |
2667 if (term_job_running(term)) | |
2668 txt = (char_u *)_("Terminal"); | |
2669 else | |
2670 txt = (char_u *)_("Terminal-finished"); | |
2671 } | |
2672 else if (term->tl_title != NULL) | |
2673 txt = term->tl_title; | |
2674 else if (term_none_open(term)) | |
2675 txt = (char_u *)_("active"); | |
2676 else if (term_job_running(term)) | |
2677 txt = (char_u *)_("running"); | |
2678 else | |
2679 txt = (char_u *)_("finished"); | |
2680 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt); | |
2681 term->tl_status_text = alloc((int)len); | |
2682 if (term->tl_status_text != NULL) | |
2683 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]", | |
2684 term->tl_buffer->b_fname, txt); | |
2685 } | |
2686 return term->tl_status_text; | |
2687 } | |
2688 | |
2689 /* | |
2690 * Mark references in jobs of terminals. | |
2691 */ | |
2692 int | |
2693 set_ref_in_term(int copyID) | |
2694 { | |
2695 int abort = FALSE; | |
2696 term_T *term; | |
2697 typval_T tv; | |
2698 | |
2699 for (term = first_term; term != NULL; term = term->tl_next) | |
2700 if (term->tl_job != NULL) | |
2701 { | |
2702 tv.v_type = VAR_JOB; | |
2703 tv.vval.v_job = term->tl_job; | |
2704 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL); | |
2705 } | |
2706 return abort; | |
2707 } | |
2708 | |
2709 /* | |
2710 * Get the buffer from the first argument in "argvars". | |
2711 * Returns NULL when the buffer is not for a terminal window. | |
2712 */ | |
2713 static buf_T * | |
2714 term_get_buf(typval_T *argvars) | |
2715 { | |
2716 buf_T *buf; | |
2717 | |
2718 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ | |
2719 ++emsg_off; | |
2720 buf = get_buf_tv(&argvars[0], FALSE); | |
2721 --emsg_off; | |
2722 if (buf == NULL || buf->b_term == NULL) | |
2723 return NULL; | |
2724 return buf; | |
2725 } | |
2726 | |
2727 /* | |
2728 * "term_getaltscreen(buf)" function | |
2729 */ | |
2730 void | |
2731 f_term_getaltscreen(typval_T *argvars, typval_T *rettv) | |
2732 { | |
2733 buf_T *buf = term_get_buf(argvars); | |
2734 | |
2735 if (buf == NULL) | |
2736 return; | |
2737 rettv->vval.v_number = buf->b_term->tl_using_altscreen; | |
2738 } | |
2739 | |
2740 /* | |
2741 * "term_getattr(attr, name)" function | |
2742 */ | |
2743 void | |
2744 f_term_getattr(typval_T *argvars, typval_T *rettv) | |
2745 { | |
2746 int attr; | |
2747 size_t i; | |
2748 char_u *name; | |
2749 | |
2750 static struct { | |
2751 char *name; | |
2752 int attr; | |
2753 } attrs[] = { | |
2754 {"bold", HL_BOLD}, | |
2755 {"italic", HL_ITALIC}, | |
2756 {"underline", HL_UNDERLINE}, | |
2757 {"strike", HL_STRIKETHROUGH}, | |
2758 {"reverse", HL_INVERSE}, | |
2759 }; | |
2760 | |
2761 attr = get_tv_number(&argvars[0]); | |
2762 name = get_tv_string_chk(&argvars[1]); | |
2763 if (name == NULL) | |
2764 return; | |
2765 | |
2766 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i) | |
2767 if (STRCMP(name, attrs[i].name) == 0) | |
2768 { | |
2769 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0; | |
2770 break; | |
2771 } | |
2772 } | |
2773 | |
2774 /* | |
2775 * "term_getcursor(buf)" function | |
2776 */ | |
2777 void | |
2778 f_term_getcursor(typval_T *argvars, typval_T *rettv) | |
2779 { | |
2780 buf_T *buf = term_get_buf(argvars); | |
2781 term_T *term; | |
2782 list_T *l; | |
2783 dict_T *d; | |
2784 | |
2785 if (rettv_list_alloc(rettv) == FAIL) | |
2786 return; | |
2787 if (buf == NULL) | |
2788 return; | |
2789 term = buf->b_term; | |
2790 | |
2791 l = rettv->vval.v_list; | |
2792 list_append_number(l, term->tl_cursor_pos.row + 1); | |
2793 list_append_number(l, term->tl_cursor_pos.col + 1); | |
2794 | |
2795 d = dict_alloc(); | |
2796 if (d != NULL) | |
2797 { | |
2798 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL); | |
2799 dict_add_nr_str(d, "blink", blink_state_is_inverted() | |
2800 ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL); | |
2801 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL); | |
2802 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL | |
2803 ? (char_u *)"" : term->tl_cursor_color); | |
2804 list_append_dict(l, d); | |
2805 } | |
2806 } | |
2807 | |
2808 /* | |
2809 * "term_getjob(buf)" function | |
2810 */ | |
2811 void | |
2812 f_term_getjob(typval_T *argvars, typval_T *rettv) | |
2813 { | |
2814 buf_T *buf = term_get_buf(argvars); | |
2815 | |
2816 rettv->v_type = VAR_JOB; | |
2817 rettv->vval.v_job = NULL; | |
2818 if (buf == NULL) | |
2819 return; | |
2820 | |
2821 rettv->vval.v_job = buf->b_term->tl_job; | |
2822 if (rettv->vval.v_job != NULL) | |
2823 ++rettv->vval.v_job->jv_refcount; | |
2824 } | |
2825 | |
2826 static int | |
2827 get_row_number(typval_T *tv, term_T *term) | |
2828 { | |
2829 if (tv->v_type == VAR_STRING | |
2830 && tv->vval.v_string != NULL | |
2831 && STRCMP(tv->vval.v_string, ".") == 0) | |
2832 return term->tl_cursor_pos.row; | |
2833 return (int)get_tv_number(tv) - 1; | |
2834 } | |
2835 | |
2836 /* | |
2837 * "term_getline(buf, row)" function | |
2838 */ | |
2839 void | |
2840 f_term_getline(typval_T *argvars, typval_T *rettv) | |
2841 { | |
2842 buf_T *buf = term_get_buf(argvars); | |
2843 term_T *term; | |
2844 int row; | |
2845 | |
2846 rettv->v_type = VAR_STRING; | |
2847 if (buf == NULL) | |
2848 return; | |
2849 term = buf->b_term; | |
2850 row = get_row_number(&argvars[1], term); | |
2851 | |
2852 if (term->tl_vterm == NULL) | |
2853 { | |
2854 linenr_T lnum = row + term->tl_scrollback_scrolled + 1; | |
2855 | |
2856 /* vterm is finished, get the text from the buffer */ | |
2857 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count) | |
2858 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE)); | |
2859 } | |
2860 else | |
2861 { | |
2862 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm); | |
2863 VTermRect rect; | |
2864 int len; | |
2865 char_u *p; | |
2866 | |
2867 if (row < 0 || row >= term->tl_rows) | |
2868 return; | |
2869 len = term->tl_cols * MB_MAXBYTES + 1; | |
2870 p = alloc(len); | |
2871 if (p == NULL) | |
2872 return; | |
2873 rettv->vval.v_string = p; | |
2874 | |
2875 rect.start_col = 0; | |
2876 rect.end_col = term->tl_cols; | |
2877 rect.start_row = row; | |
2878 rect.end_row = row + 1; | |
2879 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL; | |
2880 } | |
2881 } | |
2882 | |
2883 /* | |
2884 * "term_getscrolled(buf)" function | |
2885 */ | |
2886 void | |
2887 f_term_getscrolled(typval_T *argvars, typval_T *rettv) | |
2888 { | |
2889 buf_T *buf = term_get_buf(argvars); | |
2890 | |
2891 if (buf == NULL) | |
2892 return; | |
2893 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled; | |
2894 } | |
2895 | |
2896 /* | |
2897 * "term_getsize(buf)" function | |
2898 */ | |
2899 void | |
2900 f_term_getsize(typval_T *argvars, typval_T *rettv) | |
2901 { | |
2902 buf_T *buf = term_get_buf(argvars); | |
2903 list_T *l; | |
2904 | |
2905 if (rettv_list_alloc(rettv) == FAIL) | |
2906 return; | |
2907 if (buf == NULL) | |
2908 return; | |
2909 | |
2910 l = rettv->vval.v_list; | |
2911 list_append_number(l, buf->b_term->tl_rows); | |
2912 list_append_number(l, buf->b_term->tl_cols); | |
2913 } | |
2914 | |
2915 /* | |
2916 * "term_getstatus(buf)" function | |
2917 */ | |
2918 void | |
2919 f_term_getstatus(typval_T *argvars, typval_T *rettv) | |
2920 { | |
2921 buf_T *buf = term_get_buf(argvars); | |
2922 term_T *term; | |
2923 char_u val[100]; | |
2924 | |
2925 rettv->v_type = VAR_STRING; | |
2926 if (buf == NULL) | |
2927 return; | |
2928 term = buf->b_term; | |
2929 | |
2930 if (term_job_running(term)) | |
2931 STRCPY(val, "running"); | |
2932 else | |
2933 STRCPY(val, "finished"); | |
2934 if (term->tl_normal_mode) | |
2935 STRCAT(val, ",normal"); | |
2936 rettv->vval.v_string = vim_strsave(val); | |
2937 } | |
2938 | |
2939 /* | |
2940 * "term_gettitle(buf)" function | |
2941 */ | |
2942 void | |
2943 f_term_gettitle(typval_T *argvars, typval_T *rettv) | |
2944 { | |
2945 buf_T *buf = term_get_buf(argvars); | |
2946 | |
2947 rettv->v_type = VAR_STRING; | |
2948 if (buf == NULL) | |
2949 return; | |
2950 | |
2951 if (buf->b_term->tl_title != NULL) | |
2952 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title); | |
2953 } | |
2954 | |
2955 /* | |
2956 * "term_gettty(buf)" function | |
2957 */ | |
2958 void | |
2959 f_term_gettty(typval_T *argvars, typval_T *rettv) | |
2960 { | |
2961 buf_T *buf = term_get_buf(argvars); | |
2962 char_u *p; | |
2963 int num = 0; | |
2964 | |
2965 rettv->v_type = VAR_STRING; | |
2966 if (buf == NULL) | |
2967 return; | |
2968 if (argvars[1].v_type != VAR_UNKNOWN) | |
2969 num = get_tv_number(&argvars[1]); | |
2970 | |
2971 switch (num) | |
2972 { | |
2973 case 0: | |
2974 if (buf->b_term->tl_job != NULL) | |
2975 p = buf->b_term->tl_job->jv_tty_out; | |
2976 else | |
2977 p = buf->b_term->tl_tty_out; | |
2978 break; | |
2979 case 1: | |
2980 if (buf->b_term->tl_job != NULL) | |
2981 p = buf->b_term->tl_job->jv_tty_in; | |
2982 else | |
2983 p = buf->b_term->tl_tty_in; | |
2984 break; | |
2985 default: | |
2986 EMSG2(_(e_invarg2), get_tv_string(&argvars[1])); | |
2987 return; | |
2988 } | |
2989 if (p != NULL) | |
2990 rettv->vval.v_string = vim_strsave(p); | |
2991 } | |
2992 | |
2993 /* | |
2994 * "term_list()" function | |
2995 */ | |
2996 void | |
2997 f_term_list(typval_T *argvars UNUSED, typval_T *rettv) | |
2998 { | |
2999 term_T *tp; | |
3000 list_T *l; | |
3001 | |
3002 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL) | |
3003 return; | |
3004 | |
3005 l = rettv->vval.v_list; | |
3006 for (tp = first_term; tp != NULL; tp = tp->tl_next) | |
3007 if (tp != NULL && tp->tl_buffer != NULL) | |
3008 if (list_append_number(l, | |
3009 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL) | |
3010 return; | |
3011 } | |
3012 | |
3013 /* | |
3014 * "term_scrape(buf, row)" function | |
3015 */ | |
3016 void | |
3017 f_term_scrape(typval_T *argvars, typval_T *rettv) | |
3018 { | |
3019 buf_T *buf = term_get_buf(argvars); | |
3020 VTermScreen *screen = NULL; | |
3021 VTermPos pos; | |
3022 list_T *l; | |
3023 term_T *term; | |
3024 char_u *p; | |
3025 sb_line_T *line; | |
3026 | |
3027 if (rettv_list_alloc(rettv) == FAIL) | |
3028 return; | |
3029 if (buf == NULL) | |
3030 return; | |
3031 term = buf->b_term; | |
3032 | |
3033 l = rettv->vval.v_list; | |
3034 pos.row = get_row_number(&argvars[1], term); | |
3035 | |
3036 if (term->tl_vterm != NULL) | |
3037 { | |
3038 screen = vterm_obtain_screen(term->tl_vterm); | |
3039 p = NULL; | |
3040 line = NULL; | |
3041 } | |
3042 else | |
3043 { | |
3044 linenr_T lnum = pos.row + term->tl_scrollback_scrolled; | |
3045 | |
3046 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len) | |
3047 return; | |
3048 p = ml_get_buf(buf, lnum + 1, FALSE); | |
3049 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum; | |
3050 } | |
3051 | |
3052 for (pos.col = 0; pos.col < term->tl_cols; ) | |
3053 { | |
3054 dict_T *dcell; | |
3055 int width; | |
3056 VTermScreenCellAttrs attrs; | |
3057 VTermColor fg, bg; | |
3058 char_u rgb[8]; | |
3059 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1]; | |
3060 int off = 0; | |
3061 int i; | |
3062 | |
3063 if (screen == NULL) | |
3064 { | |
3065 cellattr_T *cellattr; | |
3066 int len; | |
3067 | |
3068 /* vterm has finished, get the cell from scrollback */ | |
3069 if (pos.col >= line->sb_cols) | |
3070 break; | |
3071 cellattr = line->sb_cells + pos.col; | |
3072 width = cellattr->width; | |
3073 attrs = cellattr->attrs; | |
3074 fg = cellattr->fg; | |
3075 bg = cellattr->bg; | |
3076 len = MB_PTR2LEN(p); | |
3077 mch_memmove(mbs, p, len); | |
3078 mbs[len] = NUL; | |
3079 p += len; | |
3080 } | |
3081 else | |
3082 { | |
3083 VTermScreenCell cell; | |
3084 if (vterm_screen_get_cell(screen, pos, &cell) == 0) | |
3085 break; | |
3086 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i) | |
3087 { | |
3088 if (cell.chars[i] == 0) | |
3089 break; | |
3090 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off); | |
3091 } | |
3092 mbs[off] = NUL; | |
3093 width = cell.width; | |
3094 attrs = cell.attrs; | |
3095 fg = cell.fg; | |
3096 bg = cell.bg; | |
3097 } | |
3098 dcell = dict_alloc(); | |
3099 list_append_dict(l, dcell); | |
3100 | |
3101 dict_add_nr_str(dcell, "chars", 0, mbs); | |
3102 | |
3103 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x", | |
3104 fg.red, fg.green, fg.blue); | |
3105 dict_add_nr_str(dcell, "fg", 0, rgb); | |
3106 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x", | |
3107 bg.red, bg.green, bg.blue); | |
3108 dict_add_nr_str(dcell, "bg", 0, rgb); | |
3109 | |
3110 dict_add_nr_str(dcell, "attr", | |
3111 cell2attr(attrs, fg, bg), NULL); | |
3112 dict_add_nr_str(dcell, "width", width, NULL); | |
3113 | |
3114 ++pos.col; | |
3115 if (width == 2) | |
3116 ++pos.col; | |
3117 } | |
3118 } | |
3119 | |
3120 /* | |
3121 * "term_sendkeys(buf, keys)" function | |
3122 */ | |
3123 void | |
3124 f_term_sendkeys(typval_T *argvars, typval_T *rettv) | |
3125 { | |
3126 buf_T *buf = term_get_buf(argvars); | |
3127 char_u *msg; | |
3128 term_T *term; | |
3129 | |
3130 rettv->v_type = VAR_UNKNOWN; | |
3131 if (buf == NULL) | |
3132 return; | |
3133 | |
3134 msg = get_tv_string_chk(&argvars[1]); | |
3135 if (msg == NULL) | |
3136 return; | |
3137 term = buf->b_term; | |
3138 if (term->tl_vterm == NULL) | |
3139 return; | |
3140 | |
3141 while (*msg != NUL) | |
3142 { | |
3143 send_keys_to_term(term, PTR2CHAR(msg), FALSE); | |
3144 msg += MB_PTR2LEN(msg); | |
3145 } | |
3146 } | |
3147 | |
3148 /* | |
3149 * "term_start(command, options)" function | |
3150 */ | |
3151 void | |
3152 f_term_start(typval_T *argvars, typval_T *rettv) | |
3153 { | |
3154 jobopt_T opt; | |
3155 buf_T *buf; | |
3156 | |
3157 init_job_options(&opt); | |
3158 if (argvars[1].v_type != VAR_UNKNOWN | |
3159 && get_job_options(&argvars[1], &opt, | |
3160 JO_TIMEOUT_ALL + JO_STOPONEXIT | |
3161 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK | |
3162 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO, | |
3163 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD | |
3164 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN | |
3165 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS) == FAIL) | |
3166 return; | |
3167 | |
3168 if (opt.jo_vertical) | |
3169 cmdmod.split = WSP_VERT; | |
3170 buf = term_start(&argvars[0], &opt, FALSE); | |
3171 | |
3172 if (buf != NULL && buf->b_term != NULL) | |
3173 rettv->vval.v_number = buf->b_fnum; | |
3174 } | |
3175 | |
3176 /* | |
3177 * "term_wait" function | |
3178 */ | |
3179 void | |
3180 f_term_wait(typval_T *argvars, typval_T *rettv UNUSED) | |
3181 { | |
3182 buf_T *buf = term_get_buf(argvars); | |
3183 | |
3184 if (buf == NULL) | |
3185 { | |
3186 ch_log(NULL, "term_wait(): invalid argument"); | |
3187 return; | |
3188 } | |
3189 if (buf->b_term->tl_job == NULL) | |
3190 { | |
3191 ch_log(NULL, "term_wait(): no job to wait for"); | |
3192 return; | |
3193 } | |
3194 if (buf->b_term->tl_job->jv_channel == NULL) | |
3195 /* channel is closed, nothing to do */ | |
3196 return; | |
3197 | |
3198 /* Get the job status, this will detect a job that finished. */ | |
3199 if ((buf->b_term->tl_job->jv_channel == NULL | |
3200 || !buf->b_term->tl_job->jv_channel->ch_keep_open) | |
3201 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0) | |
3202 { | |
3203 /* The job is dead, keep reading channel I/O until the channel is | |
3204 * closed. buf->b_term may become NULL if the terminal was closed while | |
3205 * waiting. */ | |
3206 ch_log(NULL, "term_wait(): waiting for channel to close"); | |
3207 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed) | |
3208 { | |
3209 mch_check_messages(); | |
3210 parse_queued_messages(); | |
3211 ui_delay(10L, FALSE); | |
3212 } | |
3213 mch_check_messages(); | |
3214 parse_queued_messages(); | |
3215 } | |
3216 else | |
3217 { | |
3218 long wait = 10L; | |
3219 | |
3220 mch_check_messages(); | |
3221 parse_queued_messages(); | |
3222 | |
3223 /* Wait for some time for any channel I/O. */ | |
3224 if (argvars[1].v_type != VAR_UNKNOWN) | |
3225 wait = get_tv_number(&argvars[1]); | |
3226 ui_delay(wait, TRUE); | |
3227 mch_check_messages(); | |
3228 | |
3229 /* Flushing messages on channels is hopefully sufficient. | |
3230 * TODO: is there a better way? */ | |
3231 parse_queued_messages(); | |
3232 } | |
3233 } | |
3234 | |
3235 /* | |
3236 * Called when a channel has sent all the lines to a terminal. | |
3237 * Send a CTRL-D to mark the end of the text. | |
3238 */ | |
3239 void | |
3240 term_send_eof(channel_T *ch) | |
3241 { | |
3242 term_T *term; | |
3243 | |
3244 for (term = first_term; term != NULL; term = term->tl_next) | |
3245 if (term->tl_job == ch->ch_job) | |
3246 { | |
3247 if (term->tl_eof_chars != NULL) | |
3248 { | |
3249 channel_send(ch, PART_IN, term->tl_eof_chars, | |
3250 (int)STRLEN(term->tl_eof_chars), NULL); | |
3251 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL); | |
3252 } | |
3253 # ifdef WIN3264 | |
3254 else | |
3255 /* Default: CTRL-D */ | |
3256 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL); | |
3257 # endif | |
3258 } | |
3259 } | |
3260 | |
3261 # if defined(WIN3264) || defined(PROTO) | |
3262 | |
3263 /************************************** | |
3264 * 2. MS-Windows implementation. | |
3265 */ | |
3266 | |
3267 # ifndef PROTO | |
3268 | |
3269 #define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul | |
3270 #define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull | |
3271 #define WINPTY_MOUSE_MODE_FORCE 2 | |
3272 | |
3273 void* (*winpty_config_new)(UINT64, void*); | |
3274 void* (*winpty_open)(void*, void*); | |
3275 void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*); | |
3276 BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*); | |
3277 void (*winpty_config_set_mouse_mode)(void*, int); | |
3278 void (*winpty_config_set_initial_size)(void*, int, int); | |
3279 LPCWSTR (*winpty_conin_name)(void*); | |
3280 LPCWSTR (*winpty_conout_name)(void*); | |
3281 LPCWSTR (*winpty_conerr_name)(void*); | |
3282 void (*winpty_free)(void*); | |
3283 void (*winpty_config_free)(void*); | |
3284 void (*winpty_spawn_config_free)(void*); | |
3285 void (*winpty_error_free)(void*); | |
3286 LPCWSTR (*winpty_error_msg)(void*); | |
3287 BOOL (*winpty_set_size)(void*, int, int, void*); | |
3288 HANDLE (*winpty_agent_process)(void*); | |
3289 | |
3290 #define WINPTY_DLL "winpty.dll" | |
3291 | |
3292 static HINSTANCE hWinPtyDLL = NULL; | |
3293 # endif | |
3294 | |
3295 static int | |
3296 dyn_winpty_init(int verbose) | |
3297 { | |
3298 int i; | |
3299 static struct | |
3300 { | |
3301 char *name; | |
3302 FARPROC *ptr; | |
3303 } winpty_entry[] = | |
3304 { | |
3305 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name}, | |
3306 {"winpty_config_free", (FARPROC*)&winpty_config_free}, | |
3307 {"winpty_config_new", (FARPROC*)&winpty_config_new}, | |
3308 {"winpty_config_set_mouse_mode", | |
3309 (FARPROC*)&winpty_config_set_mouse_mode}, | |
3310 {"winpty_config_set_initial_size", | |
3311 (FARPROC*)&winpty_config_set_initial_size}, | |
3312 {"winpty_conin_name", (FARPROC*)&winpty_conin_name}, | |
3313 {"winpty_conout_name", (FARPROC*)&winpty_conout_name}, | |
3314 {"winpty_error_free", (FARPROC*)&winpty_error_free}, | |
3315 {"winpty_free", (FARPROC*)&winpty_free}, | |
3316 {"winpty_open", (FARPROC*)&winpty_open}, | |
3317 {"winpty_spawn", (FARPROC*)&winpty_spawn}, | |
3318 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free}, | |
3319 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new}, | |
3320 {"winpty_error_msg", (FARPROC*)&winpty_error_msg}, | |
3321 {"winpty_set_size", (FARPROC*)&winpty_set_size}, | |
3322 {"winpty_agent_process", (FARPROC*)&winpty_agent_process}, | |
3323 {NULL, NULL} | |
3324 }; | |
3325 | |
3326 /* No need to initialize twice. */ | |
3327 if (hWinPtyDLL) | |
3328 return OK; | |
3329 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just | |
3330 * winpty.dll. */ | |
3331 if (*p_winptydll != NUL) | |
3332 hWinPtyDLL = vimLoadLib((char *)p_winptydll); | |
3333 if (!hWinPtyDLL) | |
3334 hWinPtyDLL = vimLoadLib(WINPTY_DLL); | |
3335 if (!hWinPtyDLL) | |
3336 { | |
3337 if (verbose) | |
3338 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll | |
3339 : (char_u *)WINPTY_DLL); | |
3340 return FAIL; | |
3341 } | |
3342 for (i = 0; winpty_entry[i].name != NULL | |
3343 && winpty_entry[i].ptr != NULL; ++i) | |
3344 { | |
3345 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL, | |
3346 winpty_entry[i].name)) == NULL) | |
3347 { | |
3348 if (verbose) | |
3349 EMSG2(_(e_loadfunc), winpty_entry[i].name); | |
3350 return FAIL; | |
3351 } | |
3352 } | |
3353 | |
3354 return OK; | |
3355 } | |
3356 | |
3357 /* | |
3358 * Create a new terminal of "rows" by "cols" cells. | |
3359 * Store a reference in "term". | |
3360 * Return OK or FAIL. | |
3361 */ | |
3362 static int | |
3363 term_and_job_init( | |
3364 term_T *term, | |
3365 typval_T *argvar, | |
3366 jobopt_T *opt) | |
3367 { | |
3368 WCHAR *cmd_wchar = NULL; | |
3369 WCHAR *cwd_wchar = NULL; | |
3370 channel_T *channel = NULL; | |
3371 job_T *job = NULL; | |
3372 DWORD error; | |
3373 HANDLE jo = NULL; | |
3374 HANDLE child_process_handle; | |
3375 HANDLE child_thread_handle; | |
3376 void *winpty_err; | |
3377 void *spawn_config = NULL; | |
3378 garray_T ga; | |
3379 char_u *cmd; | |
3380 | |
3381 if (dyn_winpty_init(TRUE) == FAIL) | |
3382 return FAIL; | |
3383 | |
3384 if (argvar->v_type == VAR_STRING) | |
3385 cmd = argvar->vval.v_string; | |
3386 else | |
3387 { | |
3388 ga_init2(&ga, (int)sizeof(char*), 20); | |
3389 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL) | |
3390 goto failed; | |
3391 cmd = ga.ga_data; | |
3392 } | |
3393 | |
3394 cmd_wchar = enc_to_utf16(cmd, NULL); | |
3395 if (cmd_wchar == NULL) | |
3396 return FAIL; | |
3397 if (opt->jo_cwd != NULL) | |
3398 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL); | |
3399 | |
3400 job = job_alloc(); | |
3401 if (job == NULL) | |
3402 goto failed; | |
3403 | |
3404 channel = add_channel(); | |
3405 if (channel == NULL) | |
3406 goto failed; | |
3407 | |
3408 term->tl_winpty_config = winpty_config_new(0, &winpty_err); | |
3409 if (term->tl_winpty_config == NULL) | |
3410 goto failed; | |
3411 | |
3412 winpty_config_set_mouse_mode(term->tl_winpty_config, | |
3413 WINPTY_MOUSE_MODE_FORCE); | |
3414 winpty_config_set_initial_size(term->tl_winpty_config, | |
3415 term->tl_cols, term->tl_rows); | |
3416 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err); | |
3417 if (term->tl_winpty == NULL) | |
3418 goto failed; | |
3419 | |
3420 spawn_config = winpty_spawn_config_new( | |
3421 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN | | |
3422 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN, | |
3423 NULL, | |
3424 cmd_wchar, | |
3425 cwd_wchar, | |
3426 NULL, | |
3427 &winpty_err); | |
3428 if (spawn_config == NULL) | |
3429 goto failed; | |
3430 | |
3431 channel = add_channel(); | |
3432 if (channel == NULL) | |
3433 goto failed; | |
3434 | |
3435 job = job_alloc(); | |
3436 if (job == NULL) | |
3437 goto failed; | |
3438 | |
3439 if (opt->jo_set & JO_IN_BUF) | |
3440 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]); | |
3441 | |
3442 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle, | |
3443 &child_thread_handle, &error, &winpty_err)) | |
3444 goto failed; | |
3445 | |
3446 channel_set_pipes(channel, | |
3447 (sock_T)CreateFileW( | |
3448 winpty_conin_name(term->tl_winpty), | |
3449 GENERIC_WRITE, 0, NULL, | |
3450 OPEN_EXISTING, 0, NULL), | |
3451 (sock_T)CreateFileW( | |
3452 winpty_conout_name(term->tl_winpty), | |
3453 GENERIC_READ, 0, NULL, | |
3454 OPEN_EXISTING, 0, NULL), | |
3455 (sock_T)CreateFileW( | |
3456 winpty_conerr_name(term->tl_winpty), | |
3457 GENERIC_READ, 0, NULL, | |
3458 OPEN_EXISTING, 0, NULL)); | |
3459 | |
3460 /* Write lines with CR instead of NL. */ | |
3461 channel->ch_write_text_mode = TRUE; | |
3462 | |
3463 jo = CreateJobObject(NULL, NULL); | |
3464 if (jo == NULL) | |
3465 goto failed; | |
3466 | |
3467 if (!AssignProcessToJobObject(jo, child_process_handle)) | |
3468 { | |
3469 /* Failed, switch the way to terminate process with TerminateProcess. */ | |
3470 CloseHandle(jo); | |
3471 jo = NULL; | |
3472 } | |
3473 | |
3474 winpty_spawn_config_free(spawn_config); | |
3475 vim_free(cmd_wchar); | |
3476 vim_free(cwd_wchar); | |
3477 | |
3478 create_vterm(term, term->tl_rows, term->tl_cols); | |
3479 | |
3480 channel_set_job(channel, job, opt); | |
3481 job_set_options(job, opt); | |
3482 | |
3483 job->jv_channel = channel; | |
3484 job->jv_proc_info.hProcess = child_process_handle; | |
3485 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle); | |
3486 job->jv_job_object = jo; | |
3487 job->jv_status = JOB_STARTED; | |
3488 job->jv_tty_in = utf16_to_enc( | |
3489 (short_u*)winpty_conin_name(term->tl_winpty), NULL); | |
3490 job->jv_tty_out = utf16_to_enc( | |
3491 (short_u*)winpty_conout_name(term->tl_winpty), NULL); | |
3492 ++job->jv_refcount; | |
3493 term->tl_job = job; | |
3494 | |
3495 return OK; | |
3496 | |
3497 failed: | |
3498 if (argvar->v_type == VAR_LIST) | |
3499 vim_free(ga.ga_data); | |
3500 vim_free(cmd_wchar); | |
3501 vim_free(cwd_wchar); | |
3502 if (spawn_config != NULL) | |
3503 winpty_spawn_config_free(spawn_config); | |
3504 if (channel != NULL) | |
3505 channel_clear(channel); | |
3506 if (job != NULL) | |
3507 { | |
3508 job->jv_channel = NULL; | |
3509 job_cleanup(job); | |
3510 } | |
3511 term->tl_job = NULL; | |
3512 if (jo != NULL) | |
3513 CloseHandle(jo); | |
3514 if (term->tl_winpty != NULL) | |
3515 winpty_free(term->tl_winpty); | |
3516 term->tl_winpty = NULL; | |
3517 if (term->tl_winpty_config != NULL) | |
3518 winpty_config_free(term->tl_winpty_config); | |
3519 term->tl_winpty_config = NULL; | |
3520 if (winpty_err != NULL) | |
3521 { | |
3522 char_u *msg = utf16_to_enc( | |
3523 (short_u *)winpty_error_msg(winpty_err), NULL); | |
3524 | |
3525 EMSG(msg); | |
3526 winpty_error_free(winpty_err); | |
3527 } | |
3528 return FAIL; | |
3529 } | |
3530 | |
3531 static int | |
3532 create_pty_only(term_T *term, jobopt_T *options) | |
3533 { | |
3534 HANDLE hPipeIn = INVALID_HANDLE_VALUE; | |
3535 HANDLE hPipeOut = INVALID_HANDLE_VALUE; | |
3536 char in_name[80], out_name[80]; | |
3537 channel_T *channel = NULL; | |
3538 | |
3539 create_vterm(term, term->tl_rows, term->tl_cols); | |
3540 | |
3541 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d", | |
3542 GetCurrentProcessId(), | |
3543 curbuf->b_fnum); | |
3544 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND, | |
3545 PIPE_TYPE_MESSAGE | PIPE_NOWAIT, | |
3546 PIPE_UNLIMITED_INSTANCES, | |
3547 0, 0, NMPWAIT_NOWAIT, NULL); | |
3548 if (hPipeIn == INVALID_HANDLE_VALUE) | |
3549 goto failed; | |
3550 | |
3551 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d", | |
3552 GetCurrentProcessId(), | |
3553 curbuf->b_fnum); | |
3554 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND, | |
3555 PIPE_TYPE_MESSAGE | PIPE_NOWAIT, | |
3556 PIPE_UNLIMITED_INSTANCES, | |
3557 0, 0, 0, NULL); | |
3558 if (hPipeOut == INVALID_HANDLE_VALUE) | |
3559 goto failed; | |
3560 | |
3561 ConnectNamedPipe(hPipeIn, NULL); | |
3562 ConnectNamedPipe(hPipeOut, NULL); | |
3563 | |
3564 term->tl_job = job_alloc(); | |
3565 if (term->tl_job == NULL) | |
3566 goto failed; | |
3567 ++term->tl_job->jv_refcount; | |
3568 | |
3569 /* behave like the job is already finished */ | |
3570 term->tl_job->jv_status = JOB_FINISHED; | |
3571 | |
3572 channel = add_channel(); | |
3573 if (channel == NULL) | |
3574 goto failed; | |
3575 term->tl_job->jv_channel = channel; | |
3576 channel->ch_keep_open = TRUE; | |
3577 channel->ch_named_pipe = TRUE; | |
3578 | |
3579 channel_set_pipes(channel, | |
3580 (sock_T)hPipeIn, | |
3581 (sock_T)hPipeOut, | |
3582 (sock_T)hPipeOut); | |
3583 channel_set_job(channel, term->tl_job, options); | |
3584 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name); | |
3585 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name); | |
3586 | |
3587 return OK; | |
3588 | |
3589 failed: | |
3590 if (hPipeIn != NULL) | |
3591 CloseHandle(hPipeIn); | |
3592 if (hPipeOut != NULL) | |
3593 CloseHandle(hPipeOut); | |
3594 return FAIL; | |
3595 } | |
3596 | |
3597 /* | |
3598 * Free the terminal emulator part of "term". | |
3599 */ | |
3600 static void | |
3601 term_free_vterm(term_T *term) | |
3602 { | |
3603 if (term->tl_winpty != NULL) | |
3604 winpty_free(term->tl_winpty); | |
3605 term->tl_winpty = NULL; | |
3606 if (term->tl_winpty_config != NULL) | |
3607 winpty_config_free(term->tl_winpty_config); | |
3608 term->tl_winpty_config = NULL; | |
3609 if (term->tl_vterm != NULL) | |
3610 vterm_free(term->tl_vterm); | |
3611 term->tl_vterm = NULL; | |
3612 } | |
3613 | |
3614 /* | |
3615 * Request size to terminal. | |
3616 */ | |
3617 static void | |
3618 term_report_winsize(term_T *term, int rows, int cols) | |
3619 { | |
3620 if (term->tl_winpty) | |
3621 winpty_set_size(term->tl_winpty, cols, rows, NULL); | |
3622 } | |
3623 | |
3624 int | |
3625 terminal_enabled(void) | |
3626 { | |
3627 return dyn_winpty_init(FALSE) == OK; | |
3628 } | |
3629 | |
3630 # else | |
3631 | |
3632 /************************************** | |
3633 * 3. Unix-like implementation. | |
3634 */ | |
3635 | |
3636 /* | |
3637 * Create a new terminal of "rows" by "cols" cells. | |
3638 * Start job for "cmd". | |
3639 * Store the pointers in "term". | |
3640 * Return OK or FAIL. | |
3641 */ | |
3642 static int | |
3643 term_and_job_init( | |
3644 term_T *term, | |
3645 typval_T *argvar, | |
3646 jobopt_T *opt) | |
3647 { | |
3648 create_vterm(term, term->tl_rows, term->tl_cols); | |
3649 | |
3650 term->tl_job = job_start(argvar, opt); | |
3651 if (term->tl_job != NULL) | |
3652 ++term->tl_job->jv_refcount; | |
3653 | |
3654 return term->tl_job != NULL | |
3655 && term->tl_job->jv_channel != NULL | |
3656 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL; | |
3657 } | |
3658 | |
3659 static int | |
3660 create_pty_only(term_T *term, jobopt_T *opt) | |
3661 { | |
3662 create_vterm(term, term->tl_rows, term->tl_cols); | |
3663 | |
3664 term->tl_job = job_alloc(); | |
3665 if (term->tl_job == NULL) | |
3666 return FAIL; | |
3667 ++term->tl_job->jv_refcount; | |
3668 | |
3669 /* behave like the job is already finished */ | |
3670 term->tl_job->jv_status = JOB_FINISHED; | |
3671 | |
3672 return mch_create_pty_channel(term->tl_job, opt); | |
3673 } | |
3674 | |
3675 /* | |
3676 * Free the terminal emulator part of "term". | |
3677 */ | |
3678 static void | |
3679 term_free_vterm(term_T *term) | |
3680 { | |
3681 if (term->tl_vterm != NULL) | |
3682 vterm_free(term->tl_vterm); | |
3683 term->tl_vterm = NULL; | |
3684 } | |
3685 | |
3686 /* | |
3687 * Request size to terminal. | |
3688 */ | |
3689 static void | |
3690 term_report_winsize(term_T *term, int rows, int cols) | |
3691 { | |
3692 /* Use an ioctl() to report the new window size to the job. */ | |
3693 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL) | |
3694 { | |
3695 int fd = -1; | |
3696 int part; | |
3697 | |
3698 for (part = PART_OUT; part < PART_COUNT; ++part) | |
3699 { | |
3700 fd = term->tl_job->jv_channel->ch_part[part].ch_fd; | |
3701 if (isatty(fd)) | |
3702 break; | |
3703 } | |
3704 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK) | |
3705 mch_signal_job(term->tl_job, (char_u *)"winch"); | |
3706 } | |
3707 } | |
3708 | |
3709 # endif | |
3710 | |
3711 #endif /* FEAT_TERMINAL */ |