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