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