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