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