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