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