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