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 | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 41 | * - Add a way to set the 16 ANSI colors, to be used for 'termguicolors' and in |
Bram Moolenaar | 756ef11 | 2018-04-10 12:04:27 +0200 | [diff] [blame] | 42 | * the GUI. #2747 |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 43 | * - Win32: Make terminal used for :!cmd in the GUI work better. Allow for |
Bram Moolenaar | 4a69634 | 2018-04-05 18:45:26 +0200 | [diff] [blame] | 44 | * redirection. Probably in call to channel_set_pipes(). |
Bram Moolenaar | b852c3e | 2018-03-11 16:55:36 +0100 | [diff] [blame] | 45 | * - implement term_setsize() |
| 46 | * - Copy text in the vterm to the Vim buffer once in a while, so that |
| 47 | * completion works. |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 48 | * - in GUI vertical split causes problems. Cursor is flickering. (Hirohito |
| 49 | * Higashi, 2017 Sep 19) |
Bram Moolenaar | 3a497e1 | 2017-09-30 20:40:27 +0200 | [diff] [blame] | 50 | * - after resizing windows overlap. (Boris Staletic, #2164) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 51 | * - Redirecting output does not work on MS-Windows, Test_terminal_redir_file() |
| 52 | * is disabled. |
Bram Moolenaar | a8fc0d3 | 2017-09-26 13:59:47 +0200 | [diff] [blame] | 53 | * - cursor blinks in terminal on widows with a timer. (xtal8, #2142) |
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 | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 56 | * - MS-Windows GUI: still need to type a key after shell exits? #1924 |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 57 | * - After executing a shell command the status line isn't redraw. |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 58 | * - add test for giving error for invalid 'termsize' value. |
| 59 | * - support minimal size when 'termsize' is "rows*cols". |
| 60 | * - support minimal size when 'termsize' is empty? |
| 61 | * - GUI: when using tabs, focus in terminal, click on tab does not work. |
Bram Moolenaar | 65e4c4f | 2017-10-14 23:24:25 +0200 | [diff] [blame] | 62 | * - Redrawing is slow with Athena and Motif. Also other GUI? (Ramel Eshed) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 63 | * - For the GUI fill termios with default values, perhaps like pangoterm: |
| 64 | * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134 |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 65 | * - when 'encoding' is not utf-8, or the job is using another encoding, setup |
| 66 | * conversions. |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 67 | * - add an optional limit for the scrollback size. When reaching it remove |
| 68 | * 10% at the start. |
| 69 | */ |
| 70 | |
| 71 | #include "vim.h" |
| 72 | |
| 73 | #if defined(FEAT_TERMINAL) || defined(PROTO) |
| 74 | |
| 75 | #ifndef MIN |
| 76 | # define MIN(x,y) ((x) < (y) ? (x) : (y)) |
| 77 | #endif |
| 78 | #ifndef MAX |
| 79 | # define MAX(x,y) ((x) > (y) ? (x) : (y)) |
| 80 | #endif |
| 81 | |
| 82 | #include "libvterm/include/vterm.h" |
| 83 | |
| 84 | /* This is VTermScreenCell without the characters, thus much smaller. */ |
| 85 | typedef struct { |
| 86 | VTermScreenCellAttrs attrs; |
| 87 | char width; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 88 | VTermColor fg; |
| 89 | VTermColor bg; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 90 | } cellattr_T; |
| 91 | |
| 92 | typedef struct sb_line_S { |
| 93 | int sb_cols; /* can differ per line */ |
| 94 | cellattr_T *sb_cells; /* allocated */ |
| 95 | cellattr_T sb_fill_attr; /* for short line */ |
| 96 | } sb_line_T; |
| 97 | |
| 98 | /* typedef term_T in structs.h */ |
| 99 | struct terminal_S { |
| 100 | term_T *tl_next; |
| 101 | |
| 102 | VTerm *tl_vterm; |
| 103 | job_T *tl_job; |
| 104 | buf_T *tl_buffer; |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 105 | #if defined(FEAT_GUI) |
| 106 | int tl_system; /* when non-zero used for :!cmd output */ |
| 107 | int tl_toprow; /* row with first line of system terminal */ |
| 108 | #endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 109 | |
| 110 | /* Set when setting the size of a vterm, reset after redrawing. */ |
| 111 | int tl_vterm_size_changed; |
| 112 | |
| 113 | /* used when tl_job is NULL and only a pty was created */ |
| 114 | int tl_tty_fd; |
| 115 | char_u *tl_tty_in; |
| 116 | char_u *tl_tty_out; |
| 117 | |
| 118 | int tl_normal_mode; /* TRUE: Terminal-Normal mode */ |
| 119 | int tl_channel_closed; |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 120 | int tl_finish; |
| 121 | #define TL_FINISH_UNSET NUL |
| 122 | #define TL_FINISH_CLOSE 'c' /* ++close or :terminal without argument */ |
| 123 | #define TL_FINISH_NOCLOSE 'n' /* ++noclose */ |
| 124 | #define TL_FINISH_OPEN 'o' /* ++open */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 125 | char_u *tl_opencmd; |
| 126 | char_u *tl_eof_chars; |
| 127 | |
| 128 | #ifdef WIN3264 |
| 129 | void *tl_winpty_config; |
| 130 | void *tl_winpty; |
| 131 | #endif |
Bram Moolenaar | 4d8bac8 | 2018-03-09 21:33:34 +0100 | [diff] [blame] | 132 | #if defined(FEAT_SESSION) |
| 133 | char_u *tl_command; |
| 134 | #endif |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 135 | char_u *tl_kill; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 136 | |
| 137 | /* last known vterm size */ |
| 138 | int tl_rows; |
| 139 | int tl_cols; |
| 140 | /* vterm size does not follow window size */ |
| 141 | int tl_rows_fixed; |
| 142 | int tl_cols_fixed; |
| 143 | |
| 144 | char_u *tl_title; /* NULL or allocated */ |
| 145 | char_u *tl_status_text; /* NULL or allocated */ |
| 146 | |
| 147 | /* Range of screen rows to update. Zero based. */ |
Bram Moolenaar | 3a497e1 | 2017-09-30 20:40:27 +0200 | [diff] [blame] | 148 | int tl_dirty_row_start; /* MAX_ROW if nothing dirty */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 149 | int tl_dirty_row_end; /* row below last one to update */ |
| 150 | |
| 151 | garray_T tl_scrollback; |
| 152 | int tl_scrollback_scrolled; |
| 153 | cellattr_T tl_default_color; |
| 154 | |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 155 | linenr_T tl_top_diff_rows; /* rows of top diff file or zero */ |
| 156 | linenr_T tl_bot_diff_rows; /* rows of bottom diff file */ |
| 157 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 158 | VTermPos tl_cursor_pos; |
| 159 | int tl_cursor_visible; |
| 160 | int tl_cursor_blink; |
| 161 | int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */ |
| 162 | char_u *tl_cursor_color; /* NULL or allocated */ |
| 163 | |
| 164 | int tl_using_altscreen; |
| 165 | }; |
| 166 | |
| 167 | #define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */ |
| 168 | #define TMODE_LOOP 2 /* CTRL-W N used */ |
| 169 | |
| 170 | /* |
| 171 | * List of all active terminals. |
| 172 | */ |
| 173 | static term_T *first_term = NULL; |
| 174 | |
| 175 | /* Terminal active in terminal_loop(). */ |
| 176 | static term_T *in_terminal_loop = NULL; |
| 177 | |
| 178 | #define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */ |
| 179 | #define KEY_BUF_LEN 200 |
| 180 | |
| 181 | /* |
| 182 | * Functions with separate implementation for MS-Windows and Unix-like systems. |
| 183 | */ |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 184 | static int term_and_job_init(term_T *term, typval_T *argvar, char **argv, jobopt_T *opt); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 185 | static int create_pty_only(term_T *term, jobopt_T *opt); |
| 186 | static void term_report_winsize(term_T *term, int rows, int cols); |
| 187 | static void term_free_vterm(term_T *term); |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 188 | #ifdef FEAT_GUI |
| 189 | static void update_system_term(term_T *term); |
| 190 | #endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 191 | |
Bram Moolenaar | 26d205d | 2017-11-09 17:33:11 +0100 | [diff] [blame] | 192 | /* The character that we know (or assume) that the terminal expects for the |
| 193 | * backspace key. */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 194 | static int term_backspace_char = BS; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 195 | |
Bram Moolenaar | a7c54cf | 2017-12-01 21:07:20 +0100 | [diff] [blame] | 196 | /* "Terminal" highlight group colors. */ |
| 197 | static int term_default_cterm_fg = -1; |
| 198 | static int term_default_cterm_bg = -1; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 199 | |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 200 | /* Store the last set and the desired cursor properties, so that we only update |
| 201 | * them when needed. Doing it unnecessary may result in flicker. */ |
| 202 | static char_u *last_set_cursor_color = (char_u *)""; |
| 203 | static char_u *desired_cursor_color = (char_u *)""; |
| 204 | static int last_set_cursor_shape = -1; |
| 205 | static int desired_cursor_shape = -1; |
| 206 | static int last_set_cursor_blink = -1; |
| 207 | static int desired_cursor_blink = -1; |
| 208 | |
| 209 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 210 | /************************************** |
| 211 | * 1. Generic code for all systems. |
| 212 | */ |
| 213 | |
| 214 | /* |
| 215 | * Determine the terminal size from 'termsize' and the current window. |
| 216 | * Assumes term->tl_rows and term->tl_cols are zero. |
| 217 | */ |
| 218 | static void |
| 219 | set_term_and_win_size(term_T *term) |
| 220 | { |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 221 | #ifdef FEAT_GUI |
| 222 | if (term->tl_system) |
| 223 | { |
| 224 | /* Use the whole screen for the system command. However, it will start |
| 225 | * at the command line and scroll up as needed, using tl_toprow. */ |
| 226 | term->tl_rows = Rows; |
| 227 | term->tl_cols = Columns; |
| 228 | } |
| 229 | else |
| 230 | #endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 231 | if (*curwin->w_p_tms != NUL) |
| 232 | { |
| 233 | char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1; |
| 234 | |
| 235 | term->tl_rows = atoi((char *)curwin->w_p_tms); |
| 236 | term->tl_cols = atoi((char *)p); |
| 237 | } |
| 238 | if (term->tl_rows == 0) |
| 239 | term->tl_rows = curwin->w_height; |
| 240 | else |
| 241 | { |
| 242 | win_setheight_win(term->tl_rows, curwin); |
| 243 | term->tl_rows_fixed = TRUE; |
| 244 | } |
| 245 | if (term->tl_cols == 0) |
| 246 | term->tl_cols = curwin->w_width; |
| 247 | else |
| 248 | { |
| 249 | win_setwidth_win(term->tl_cols, curwin); |
| 250 | term->tl_cols_fixed = TRUE; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Initialize job options for a terminal job. |
| 256 | * Caller may overrule some of them. |
| 257 | */ |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 258 | void |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 259 | init_job_options(jobopt_T *opt) |
| 260 | { |
| 261 | clear_job_options(opt); |
| 262 | |
| 263 | opt->jo_mode = MODE_RAW; |
| 264 | opt->jo_out_mode = MODE_RAW; |
| 265 | opt->jo_err_mode = MODE_RAW; |
| 266 | opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Set job options mandatory for a terminal job. |
| 271 | */ |
| 272 | static void |
| 273 | setup_job_options(jobopt_T *opt, int rows, int cols) |
| 274 | { |
| 275 | if (!(opt->jo_set & JO_OUT_IO)) |
| 276 | { |
| 277 | /* Connect stdout to the terminal. */ |
| 278 | opt->jo_io[PART_OUT] = JIO_BUFFER; |
| 279 | opt->jo_io_buf[PART_OUT] = curbuf->b_fnum; |
| 280 | opt->jo_modifiable[PART_OUT] = 0; |
| 281 | opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE; |
| 282 | } |
| 283 | |
| 284 | if (!(opt->jo_set & JO_ERR_IO)) |
| 285 | { |
| 286 | /* Connect stderr to the terminal. */ |
| 287 | opt->jo_io[PART_ERR] = JIO_BUFFER; |
| 288 | opt->jo_io_buf[PART_ERR] = curbuf->b_fnum; |
| 289 | opt->jo_modifiable[PART_ERR] = 0; |
| 290 | opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE; |
| 291 | } |
| 292 | |
| 293 | opt->jo_pty = TRUE; |
| 294 | if ((opt->jo_set2 & JO2_TERM_ROWS) == 0) |
| 295 | opt->jo_term_rows = rows; |
| 296 | if ((opt->jo_set2 & JO2_TERM_COLS) == 0) |
| 297 | opt->jo_term_cols = cols; |
| 298 | } |
| 299 | |
| 300 | /* |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 301 | * Close a terminal buffer (and its window). Used when creating the terminal |
| 302 | * fails. |
| 303 | */ |
| 304 | static void |
| 305 | term_close_buffer(buf_T *buf, buf_T *old_curbuf) |
| 306 | { |
| 307 | free_terminal(buf); |
| 308 | if (old_curbuf != NULL) |
| 309 | { |
| 310 | --curbuf->b_nwindows; |
| 311 | curbuf = old_curbuf; |
| 312 | curwin->w_buffer = curbuf; |
| 313 | ++curbuf->b_nwindows; |
| 314 | } |
| 315 | |
| 316 | /* Wiping out the buffer will also close the window and call |
| 317 | * free_terminal(). */ |
| 318 | do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE); |
| 319 | } |
| 320 | |
| 321 | /* |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 322 | * Start a terminal window and return its buffer. |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 323 | * Use either "argvar" or "argv", the other must be NULL. |
| 324 | * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open |
| 325 | * the window. |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 326 | * Returns NULL when failed. |
| 327 | */ |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 328 | buf_T * |
| 329 | term_start( |
| 330 | typval_T *argvar, |
| 331 | char **argv, |
| 332 | jobopt_T *opt, |
| 333 | int flags) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 334 | { |
| 335 | exarg_T split_ea; |
| 336 | win_T *old_curwin = curwin; |
| 337 | term_T *term; |
| 338 | buf_T *old_curbuf = NULL; |
| 339 | int res; |
| 340 | buf_T *newbuf; |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 341 | int vertical = opt->jo_vertical || (cmdmod.split & WSP_VERT); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 342 | |
| 343 | if (check_restricted() || check_secure()) |
| 344 | return NULL; |
| 345 | |
| 346 | if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)) |
| 347 | == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO) |
| 348 | || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF)) |
| 349 | || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))) |
| 350 | { |
| 351 | EMSG(_(e_invarg)); |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | term = (term_T *)alloc_clear(sizeof(term_T)); |
| 356 | if (term == NULL) |
| 357 | return NULL; |
| 358 | term->tl_dirty_row_end = MAX_ROW; |
| 359 | term->tl_cursor_visible = TRUE; |
| 360 | term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK; |
| 361 | term->tl_finish = opt->jo_term_finish; |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 362 | #ifdef FEAT_GUI |
| 363 | term->tl_system = (flags & TERM_START_SYSTEM); |
| 364 | #endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 365 | ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300); |
| 366 | |
| 367 | vim_memset(&split_ea, 0, sizeof(split_ea)); |
| 368 | if (opt->jo_curwin) |
| 369 | { |
| 370 | /* Create a new buffer in the current window. */ |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 371 | if (!can_abandon(curbuf, flags & TERM_START_FORCEIT)) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 372 | { |
| 373 | no_write_message(); |
| 374 | vim_free(term); |
| 375 | return NULL; |
| 376 | } |
| 377 | if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE, |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 378 | ECMD_HIDE |
| 379 | + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0), |
| 380 | curwin) == FAIL) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 381 | { |
| 382 | vim_free(term); |
| 383 | return NULL; |
| 384 | } |
| 385 | } |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 386 | else if (opt->jo_hidden || (flags & TERM_START_SYSTEM)) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 387 | { |
| 388 | buf_T *buf; |
| 389 | |
| 390 | /* Create a new buffer without a window. Make it the current buffer for |
| 391 | * a moment to be able to do the initialisations. */ |
| 392 | buf = buflist_new((char_u *)"", NULL, (linenr_T)0, |
| 393 | BLN_NEW | BLN_LISTED); |
| 394 | if (buf == NULL || ml_open(buf) == FAIL) |
| 395 | { |
| 396 | vim_free(term); |
| 397 | return NULL; |
| 398 | } |
| 399 | old_curbuf = curbuf; |
| 400 | --curbuf->b_nwindows; |
| 401 | curbuf = buf; |
| 402 | curwin->w_buffer = buf; |
| 403 | ++curbuf->b_nwindows; |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | /* Open a new window or tab. */ |
| 408 | split_ea.cmdidx = CMD_new; |
| 409 | split_ea.cmd = (char_u *)"new"; |
| 410 | split_ea.arg = (char_u *)""; |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 411 | if (opt->jo_term_rows > 0 && !vertical) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 412 | { |
| 413 | split_ea.line2 = opt->jo_term_rows; |
| 414 | split_ea.addr_count = 1; |
| 415 | } |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 416 | if (opt->jo_term_cols > 0 && vertical) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 417 | { |
| 418 | split_ea.line2 = opt->jo_term_cols; |
| 419 | split_ea.addr_count = 1; |
| 420 | } |
| 421 | |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 422 | if (vertical) |
| 423 | cmdmod.split |= WSP_VERT; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 424 | ex_splitview(&split_ea); |
| 425 | if (curwin == old_curwin) |
| 426 | { |
| 427 | /* split failed */ |
| 428 | vim_free(term); |
| 429 | return NULL; |
| 430 | } |
| 431 | } |
| 432 | term->tl_buffer = curbuf; |
| 433 | curbuf->b_term = term; |
| 434 | |
| 435 | if (!opt->jo_hidden) |
| 436 | { |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 437 | /* Only one size was taken care of with :new, do the other one. With |
| 438 | * "curwin" both need to be done. */ |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 439 | if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical)) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 440 | win_setheight(opt->jo_term_rows); |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 441 | if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical)) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 442 | win_setwidth(opt->jo_term_cols); |
| 443 | } |
| 444 | |
| 445 | /* Link the new terminal in the list of active terminals. */ |
| 446 | term->tl_next = first_term; |
| 447 | first_term = term; |
| 448 | |
| 449 | if (opt->jo_term_name != NULL) |
| 450 | curbuf->b_ffname = vim_strsave(opt->jo_term_name); |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 451 | else if (argv != NULL) |
| 452 | curbuf->b_ffname = vim_strsave((char_u *)"!system"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 453 | else |
| 454 | { |
| 455 | int i; |
| 456 | size_t len; |
| 457 | char_u *cmd, *p; |
| 458 | |
| 459 | if (argvar->v_type == VAR_STRING) |
| 460 | { |
| 461 | cmd = argvar->vval.v_string; |
| 462 | if (cmd == NULL) |
| 463 | cmd = (char_u *)""; |
| 464 | else if (STRCMP(cmd, "NONE") == 0) |
| 465 | cmd = (char_u *)"pty"; |
| 466 | } |
| 467 | else if (argvar->v_type != VAR_LIST |
| 468 | || argvar->vval.v_list == NULL |
| 469 | || argvar->vval.v_list->lv_len < 1 |
| 470 | || (cmd = get_tv_string_chk( |
| 471 | &argvar->vval.v_list->lv_first->li_tv)) == NULL) |
| 472 | cmd = (char_u*)""; |
| 473 | |
| 474 | len = STRLEN(cmd) + 10; |
| 475 | p = alloc((int)len); |
| 476 | |
| 477 | for (i = 0; p != NULL; ++i) |
| 478 | { |
| 479 | /* Prepend a ! to the command name to avoid the buffer name equals |
| 480 | * the executable, otherwise ":w!" would overwrite it. */ |
| 481 | if (i == 0) |
| 482 | vim_snprintf((char *)p, len, "!%s", cmd); |
| 483 | else |
| 484 | vim_snprintf((char *)p, len, "!%s (%d)", cmd, i); |
| 485 | if (buflist_findname(p) == NULL) |
| 486 | { |
| 487 | vim_free(curbuf->b_ffname); |
| 488 | curbuf->b_ffname = p; |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | curbuf->b_fname = curbuf->b_ffname; |
| 494 | |
| 495 | if (opt->jo_term_opencmd != NULL) |
| 496 | term->tl_opencmd = vim_strsave(opt->jo_term_opencmd); |
| 497 | |
| 498 | if (opt->jo_eof_chars != NULL) |
| 499 | term->tl_eof_chars = vim_strsave(opt->jo_eof_chars); |
| 500 | |
| 501 | set_string_option_direct((char_u *)"buftype", -1, |
| 502 | (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0); |
| 503 | |
| 504 | /* Mark the buffer as not modifiable. It can only be made modifiable after |
| 505 | * the job finished. */ |
| 506 | curbuf->b_p_ma = FALSE; |
| 507 | |
| 508 | set_term_and_win_size(term); |
| 509 | setup_job_options(opt, term->tl_rows, term->tl_cols); |
| 510 | |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 511 | if (flags & TERM_START_NOJOB) |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 512 | return curbuf; |
| 513 | |
Bram Moolenaar | 4d8bac8 | 2018-03-09 21:33:34 +0100 | [diff] [blame] | 514 | #if defined(FEAT_SESSION) |
| 515 | /* Remember the command for the session file. */ |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 516 | if (opt->jo_term_norestore || argv != NULL) |
Bram Moolenaar | 4d8bac8 | 2018-03-09 21:33:34 +0100 | [diff] [blame] | 517 | { |
| 518 | term->tl_command = vim_strsave((char_u *)"NONE"); |
| 519 | } |
| 520 | else if (argvar->v_type == VAR_STRING) |
| 521 | { |
| 522 | char_u *cmd = argvar->vval.v_string; |
| 523 | |
| 524 | if (cmd != NULL && STRCMP(cmd, p_sh) != 0) |
| 525 | term->tl_command = vim_strsave(cmd); |
| 526 | } |
| 527 | else if (argvar->v_type == VAR_LIST |
| 528 | && argvar->vval.v_list != NULL |
| 529 | && argvar->vval.v_list->lv_len > 0) |
| 530 | { |
| 531 | garray_T ga; |
| 532 | listitem_T *item; |
| 533 | |
| 534 | ga_init2(&ga, 1, 100); |
| 535 | for (item = argvar->vval.v_list->lv_first; |
| 536 | item != NULL; item = item->li_next) |
| 537 | { |
| 538 | char_u *s = get_tv_string_chk(&item->li_tv); |
| 539 | char_u *p; |
| 540 | |
| 541 | if (s == NULL) |
| 542 | break; |
| 543 | p = vim_strsave_fnameescape(s, FALSE); |
| 544 | if (p == NULL) |
| 545 | break; |
| 546 | ga_concat(&ga, p); |
| 547 | vim_free(p); |
| 548 | ga_append(&ga, ' '); |
| 549 | } |
| 550 | if (item == NULL) |
| 551 | { |
| 552 | ga_append(&ga, NUL); |
| 553 | term->tl_command = ga.ga_data; |
| 554 | } |
| 555 | else |
| 556 | ga_clear(&ga); |
| 557 | } |
| 558 | #endif |
| 559 | |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 560 | if (opt->jo_term_kill != NULL) |
| 561 | { |
| 562 | char_u *p = skiptowhite(opt->jo_term_kill); |
| 563 | |
| 564 | term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill); |
| 565 | } |
| 566 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 567 | /* System dependent: setup the vterm and maybe start the job in it. */ |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 568 | if (argv == NULL |
| 569 | && argvar->v_type == VAR_STRING |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 570 | && argvar->vval.v_string != NULL |
| 571 | && STRCMP(argvar->vval.v_string, "NONE") == 0) |
| 572 | res = create_pty_only(term, opt); |
| 573 | else |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 574 | res = term_and_job_init(term, argvar, argv, opt); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 575 | |
| 576 | newbuf = curbuf; |
| 577 | if (res == OK) |
| 578 | { |
| 579 | /* Get and remember the size we ended up with. Update the pty. */ |
| 580 | vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols); |
| 581 | term_report_winsize(term, term->tl_rows, term->tl_cols); |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 582 | #ifdef FEAT_GUI |
| 583 | if (term->tl_system) |
| 584 | { |
| 585 | /* display first line below typed command */ |
| 586 | term->tl_toprow = msg_row + 1; |
| 587 | term->tl_dirty_row_end = 0; |
| 588 | } |
| 589 | #endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 590 | |
| 591 | /* Make sure we don't get stuck on sending keys to the job, it leads to |
| 592 | * a deadlock if the job is waiting for Vim to read. */ |
| 593 | channel_set_nonblock(term->tl_job->jv_channel, PART_IN); |
| 594 | |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 595 | if (old_curbuf == NULL) |
Bram Moolenaar | ab5e7c3 | 2018-02-13 14:07:18 +0100 | [diff] [blame] | 596 | { |
| 597 | ++curbuf->b_locked; |
| 598 | apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf); |
| 599 | --curbuf->b_locked; |
| 600 | } |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 601 | else |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 602 | { |
| 603 | --curbuf->b_nwindows; |
| 604 | curbuf = old_curbuf; |
| 605 | curwin->w_buffer = curbuf; |
| 606 | ++curbuf->b_nwindows; |
| 607 | } |
| 608 | } |
| 609 | else |
| 610 | { |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 611 | term_close_buffer(curbuf, old_curbuf); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 612 | return NULL; |
| 613 | } |
Bram Moolenaar | b852c3e | 2018-03-11 16:55:36 +0100 | [diff] [blame] | 614 | |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 615 | apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 616 | return newbuf; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * ":terminal": open a terminal window and execute a job in it. |
| 621 | */ |
| 622 | void |
| 623 | ex_terminal(exarg_T *eap) |
| 624 | { |
| 625 | typval_T argvar[2]; |
| 626 | jobopt_T opt; |
| 627 | char_u *cmd; |
| 628 | char_u *tofree = NULL; |
| 629 | |
| 630 | init_job_options(&opt); |
| 631 | |
| 632 | cmd = eap->arg; |
Bram Moolenaar | a15ef45 | 2018-02-09 16:46:00 +0100 | [diff] [blame] | 633 | while (*cmd == '+' && *(cmd + 1) == '+') |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 634 | { |
| 635 | char_u *p, *ep; |
| 636 | |
| 637 | cmd += 2; |
| 638 | p = skiptowhite(cmd); |
| 639 | ep = vim_strchr(cmd, '='); |
| 640 | if (ep != NULL && ep < p) |
| 641 | p = ep; |
| 642 | |
| 643 | if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0) |
| 644 | opt.jo_term_finish = 'c'; |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 645 | else if ((int)(p - cmd) == 7 && STRNICMP(cmd, "noclose", 7) == 0) |
| 646 | opt.jo_term_finish = 'n'; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 647 | else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0) |
| 648 | opt.jo_term_finish = 'o'; |
| 649 | else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0) |
| 650 | opt.jo_curwin = 1; |
| 651 | else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0) |
| 652 | opt.jo_hidden = 1; |
Bram Moolenaar | 4d8bac8 | 2018-03-09 21:33:34 +0100 | [diff] [blame] | 653 | else if ((int)(p - cmd) == 9 && STRNICMP(cmd, "norestore", 9) == 0) |
| 654 | opt.jo_term_norestore = 1; |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 655 | else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "kill", 4) == 0 |
| 656 | && ep != NULL) |
| 657 | { |
| 658 | opt.jo_set2 |= JO2_TERM_KILL; |
| 659 | opt.jo_term_kill = ep + 1; |
| 660 | p = skiptowhite(cmd); |
| 661 | } |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 662 | else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0 |
| 663 | && ep != NULL && isdigit(ep[1])) |
| 664 | { |
| 665 | opt.jo_set2 |= JO2_TERM_ROWS; |
| 666 | opt.jo_term_rows = atoi((char *)ep + 1); |
| 667 | p = skiptowhite(cmd); |
| 668 | } |
| 669 | else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0 |
| 670 | && ep != NULL && isdigit(ep[1])) |
| 671 | { |
| 672 | opt.jo_set2 |= JO2_TERM_COLS; |
| 673 | opt.jo_term_cols = atoi((char *)ep + 1); |
| 674 | p = skiptowhite(cmd); |
| 675 | } |
| 676 | else if ((int)(p - cmd) == 3 && STRNICMP(cmd, "eof", 3) == 0 |
| 677 | && ep != NULL) |
| 678 | { |
| 679 | char_u *buf = NULL; |
| 680 | char_u *keys; |
| 681 | |
| 682 | p = skiptowhite(cmd); |
| 683 | *p = NUL; |
| 684 | keys = replace_termcodes(ep + 1, &buf, TRUE, TRUE, TRUE); |
| 685 | opt.jo_set2 |= JO2_EOF_CHARS; |
| 686 | opt.jo_eof_chars = vim_strsave(keys); |
| 687 | vim_free(buf); |
| 688 | *p = ' '; |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | if (*p) |
| 693 | *p = NUL; |
| 694 | EMSG2(_("E181: Invalid attribute: %s"), cmd); |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 695 | goto theend; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 696 | } |
| 697 | cmd = skipwhite(p); |
| 698 | } |
| 699 | if (*cmd == NUL) |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 700 | { |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 701 | /* Make a copy of 'shell', an autocommand may change the option. */ |
| 702 | tofree = cmd = vim_strsave(p_sh); |
| 703 | |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 704 | /* default to close when the shell exits */ |
| 705 | if (opt.jo_term_finish == NUL) |
| 706 | opt.jo_term_finish = 'c'; |
| 707 | } |
| 708 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 709 | if (eap->addr_count > 0) |
| 710 | { |
| 711 | /* Write lines from current buffer to the job. */ |
| 712 | opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT; |
| 713 | opt.jo_io[PART_IN] = JIO_BUFFER; |
| 714 | opt.jo_io_buf[PART_IN] = curbuf->b_fnum; |
| 715 | opt.jo_in_top = eap->line1; |
| 716 | opt.jo_in_bot = eap->line2; |
| 717 | } |
| 718 | |
| 719 | argvar[0].v_type = VAR_STRING; |
| 720 | argvar[0].vval.v_string = cmd; |
| 721 | argvar[1].v_type = VAR_UNKNOWN; |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 722 | term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 723 | vim_free(tofree); |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 724 | |
| 725 | theend: |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 726 | vim_free(opt.jo_eof_chars); |
| 727 | } |
| 728 | |
Bram Moolenaar | 4d8bac8 | 2018-03-09 21:33:34 +0100 | [diff] [blame] | 729 | #if defined(FEAT_SESSION) || defined(PROTO) |
| 730 | /* |
| 731 | * Write a :terminal command to the session file to restore the terminal in |
| 732 | * window "wp". |
| 733 | * Return FAIL if writing fails. |
| 734 | */ |
| 735 | int |
| 736 | term_write_session(FILE *fd, win_T *wp) |
| 737 | { |
| 738 | term_T *term = wp->w_buffer->b_term; |
| 739 | |
| 740 | /* Create the terminal and run the command. This is not without |
| 741 | * risk, but let's assume the user only creates a session when this |
| 742 | * will be OK. */ |
| 743 | if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ", |
| 744 | term->tl_cols, term->tl_rows) < 0) |
| 745 | return FAIL; |
| 746 | if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0) |
| 747 | return FAIL; |
| 748 | |
| 749 | return put_eol(fd); |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Return TRUE if "buf" has a terminal that should be restored. |
| 754 | */ |
| 755 | int |
| 756 | term_should_restore(buf_T *buf) |
| 757 | { |
| 758 | term_T *term = buf->b_term; |
| 759 | |
| 760 | return term != NULL && (term->tl_command == NULL |
| 761 | || STRCMP(term->tl_command, "NONE") != 0); |
| 762 | } |
| 763 | #endif |
| 764 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 765 | /* |
| 766 | * Free the scrollback buffer for "term". |
| 767 | */ |
| 768 | static void |
| 769 | free_scrollback(term_T *term) |
| 770 | { |
| 771 | int i; |
| 772 | |
| 773 | for (i = 0; i < term->tl_scrollback.ga_len; ++i) |
| 774 | vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells); |
| 775 | ga_clear(&term->tl_scrollback); |
| 776 | } |
| 777 | |
| 778 | /* |
| 779 | * Free a terminal and everything it refers to. |
| 780 | * Kills the job if there is one. |
| 781 | * Called when wiping out a buffer. |
| 782 | */ |
| 783 | void |
| 784 | free_terminal(buf_T *buf) |
| 785 | { |
| 786 | term_T *term = buf->b_term; |
| 787 | term_T *tp; |
| 788 | |
| 789 | if (term == NULL) |
| 790 | return; |
| 791 | if (first_term == term) |
| 792 | first_term = term->tl_next; |
| 793 | else |
| 794 | for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next) |
| 795 | if (tp->tl_next == term) |
| 796 | { |
| 797 | tp->tl_next = term->tl_next; |
| 798 | break; |
| 799 | } |
| 800 | |
| 801 | if (term->tl_job != NULL) |
| 802 | { |
| 803 | if (term->tl_job->jv_status != JOB_ENDED |
| 804 | && term->tl_job->jv_status != JOB_FINISHED |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 805 | && term->tl_job->jv_status != JOB_FAILED) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 806 | job_stop(term->tl_job, NULL, "kill"); |
| 807 | job_unref(term->tl_job); |
| 808 | } |
| 809 | |
| 810 | free_scrollback(term); |
| 811 | |
| 812 | term_free_vterm(term); |
| 813 | vim_free(term->tl_title); |
Bram Moolenaar | 4d8bac8 | 2018-03-09 21:33:34 +0100 | [diff] [blame] | 814 | #ifdef FEAT_SESSION |
| 815 | vim_free(term->tl_command); |
| 816 | #endif |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 817 | vim_free(term->tl_kill); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 818 | vim_free(term->tl_status_text); |
| 819 | vim_free(term->tl_opencmd); |
| 820 | vim_free(term->tl_eof_chars); |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 821 | if (desired_cursor_color == term->tl_cursor_color) |
| 822 | desired_cursor_color = (char_u *)""; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 823 | vim_free(term->tl_cursor_color); |
| 824 | vim_free(term); |
| 825 | buf->b_term = NULL; |
| 826 | if (in_terminal_loop == term) |
| 827 | in_terminal_loop = NULL; |
| 828 | } |
| 829 | |
| 830 | /* |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 831 | * Get the part that is connected to the tty. Normally this is PART_IN, but |
| 832 | * when writing buffer lines to the job it can be another. This makes it |
| 833 | * possible to do "1,5term vim -". |
| 834 | */ |
| 835 | static ch_part_T |
| 836 | get_tty_part(term_T *term) |
| 837 | { |
| 838 | #ifdef UNIX |
| 839 | ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR}; |
| 840 | int i; |
| 841 | |
| 842 | for (i = 0; i < 3; ++i) |
| 843 | { |
| 844 | int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd; |
| 845 | |
| 846 | if (isatty(fd)) |
| 847 | return parts[i]; |
| 848 | } |
| 849 | #endif |
| 850 | return PART_IN; |
| 851 | } |
| 852 | |
| 853 | /* |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 854 | * Write job output "msg[len]" to the vterm. |
| 855 | */ |
| 856 | static void |
| 857 | term_write_job_output(term_T *term, char_u *msg, size_t len) |
| 858 | { |
| 859 | VTerm *vterm = term->tl_vterm; |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 860 | size_t prevlen = vterm_output_get_buffer_current(vterm); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 861 | |
Bram Moolenaar | 26d205d | 2017-11-09 17:33:11 +0100 | [diff] [blame] | 862 | vterm_input_write(vterm, (char *)msg, len); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 863 | |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 864 | /* flush vterm buffer when vterm responded to control sequence */ |
| 865 | if (prevlen != vterm_output_get_buffer_current(vterm)) |
| 866 | { |
| 867 | char buf[KEY_BUF_LEN]; |
| 868 | size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN); |
| 869 | |
| 870 | if (curlen > 0) |
| 871 | channel_send(term->tl_job->jv_channel, get_tty_part(term), |
| 872 | (char_u *)buf, (int)curlen, NULL); |
| 873 | } |
| 874 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 875 | /* this invokes the damage callbacks */ |
| 876 | vterm_screen_flush_damage(vterm_obtain_screen(vterm)); |
| 877 | } |
| 878 | |
| 879 | static void |
| 880 | update_cursor(term_T *term, int redraw) |
| 881 | { |
| 882 | if (term->tl_normal_mode) |
| 883 | return; |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 884 | #ifdef FEAT_GUI |
| 885 | if (term->tl_system) |
| 886 | windgoto(term->tl_cursor_pos.row + term->tl_toprow, |
| 887 | term->tl_cursor_pos.col); |
| 888 | else |
| 889 | #endif |
| 890 | setcursor(); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 891 | if (redraw) |
| 892 | { |
| 893 | if (term->tl_buffer == curbuf && term->tl_cursor_visible) |
| 894 | cursor_on(); |
| 895 | out_flush(); |
| 896 | #ifdef FEAT_GUI |
| 897 | if (gui.in_use) |
Bram Moolenaar | 23c1b2b | 2017-12-05 21:32:33 +0100 | [diff] [blame] | 898 | { |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 899 | gui_update_cursor(FALSE, FALSE); |
Bram Moolenaar | 23c1b2b | 2017-12-05 21:32:33 +0100 | [diff] [blame] | 900 | gui_mch_flush(); |
| 901 | } |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 902 | #endif |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Invoked when "msg" output from a job was received. Write it to the terminal |
| 908 | * of "buffer". |
| 909 | */ |
| 910 | void |
| 911 | write_to_term(buf_T *buffer, char_u *msg, channel_T *channel) |
| 912 | { |
| 913 | size_t len = STRLEN(msg); |
| 914 | term_T *term = buffer->b_term; |
| 915 | |
| 916 | if (term->tl_vterm == NULL) |
| 917 | { |
| 918 | ch_log(channel, "NOT writing %d bytes to terminal", (int)len); |
| 919 | return; |
| 920 | } |
| 921 | ch_log(channel, "writing %d bytes to terminal", (int)len); |
| 922 | term_write_job_output(term, msg, len); |
| 923 | |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 924 | #ifdef FEAT_GUI |
| 925 | if (term->tl_system) |
| 926 | { |
| 927 | /* show system output, scrolling up the screen as needed */ |
| 928 | update_system_term(term); |
| 929 | update_cursor(term, TRUE); |
| 930 | } |
| 931 | else |
| 932 | #endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 933 | /* In Terminal-Normal mode we are displaying the buffer, not the terminal |
| 934 | * contents, thus no screen update is needed. */ |
| 935 | if (!term->tl_normal_mode) |
| 936 | { |
| 937 | /* TODO: only update once in a while. */ |
| 938 | ch_log(term->tl_job->jv_channel, "updating screen"); |
| 939 | if (buffer == curbuf) |
| 940 | { |
| 941 | update_screen(0); |
| 942 | update_cursor(term, TRUE); |
| 943 | } |
| 944 | else |
| 945 | redraw_after_callback(TRUE); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Send a mouse position and click to the vterm |
| 951 | */ |
| 952 | static int |
| 953 | term_send_mouse(VTerm *vterm, int button, int pressed) |
| 954 | { |
| 955 | VTermModifier mod = VTERM_MOD_NONE; |
| 956 | |
| 957 | vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin), |
Bram Moolenaar | 53f8174 | 2017-09-22 14:35:51 +0200 | [diff] [blame] | 958 | mouse_col - curwin->w_wincol, mod); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 959 | if (button != 0) |
| 960 | vterm_mouse_button(vterm, button, pressed, mod); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 961 | return TRUE; |
| 962 | } |
| 963 | |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 964 | static int enter_mouse_col = -1; |
| 965 | static int enter_mouse_row = -1; |
| 966 | |
| 967 | /* |
| 968 | * Handle a mouse click, drag or release. |
| 969 | * Return TRUE when a mouse event is sent to the terminal. |
| 970 | */ |
| 971 | static int |
| 972 | term_mouse_click(VTerm *vterm, int key) |
| 973 | { |
| 974 | #if defined(FEAT_CLIPBOARD) |
| 975 | /* For modeless selection mouse drag and release events are ignored, unless |
| 976 | * they are preceded with a mouse down event */ |
| 977 | static int ignore_drag_release = TRUE; |
| 978 | VTermMouseState mouse_state; |
| 979 | |
| 980 | vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state); |
| 981 | if (mouse_state.flags == 0) |
| 982 | { |
| 983 | /* Terminal is not using the mouse, use modeless selection. */ |
| 984 | switch (key) |
| 985 | { |
| 986 | case K_LEFTDRAG: |
| 987 | case K_LEFTRELEASE: |
| 988 | case K_RIGHTDRAG: |
| 989 | case K_RIGHTRELEASE: |
| 990 | /* Ignore drag and release events when the button-down wasn't |
| 991 | * seen before. */ |
| 992 | if (ignore_drag_release) |
| 993 | { |
| 994 | int save_mouse_col, save_mouse_row; |
| 995 | |
| 996 | if (enter_mouse_col < 0) |
| 997 | break; |
| 998 | |
| 999 | /* mouse click in the window gave us focus, handle that |
| 1000 | * click now */ |
| 1001 | save_mouse_col = mouse_col; |
| 1002 | save_mouse_row = mouse_row; |
| 1003 | mouse_col = enter_mouse_col; |
| 1004 | mouse_row = enter_mouse_row; |
| 1005 | clip_modeless(MOUSE_LEFT, TRUE, FALSE); |
| 1006 | mouse_col = save_mouse_col; |
| 1007 | mouse_row = save_mouse_row; |
| 1008 | } |
| 1009 | /* FALLTHROUGH */ |
| 1010 | case K_LEFTMOUSE: |
| 1011 | case K_RIGHTMOUSE: |
| 1012 | if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE) |
| 1013 | ignore_drag_release = TRUE; |
| 1014 | else |
| 1015 | ignore_drag_release = FALSE; |
| 1016 | /* Should we call mouse_has() here? */ |
| 1017 | if (clip_star.available) |
| 1018 | { |
| 1019 | int button, is_click, is_drag; |
| 1020 | |
| 1021 | button = get_mouse_button(KEY2TERMCAP1(key), |
| 1022 | &is_click, &is_drag); |
| 1023 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1024 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1025 | { |
| 1026 | /* Translate shift-left to right button. */ |
| 1027 | button = MOUSE_RIGHT; |
| 1028 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1029 | } |
| 1030 | clip_modeless(button, is_click, is_drag); |
| 1031 | } |
| 1032 | break; |
| 1033 | |
| 1034 | case K_MIDDLEMOUSE: |
| 1035 | if (clip_star.available) |
| 1036 | insert_reg('*', TRUE); |
| 1037 | break; |
| 1038 | } |
| 1039 | enter_mouse_col = -1; |
| 1040 | return FALSE; |
| 1041 | } |
| 1042 | #endif |
| 1043 | enter_mouse_col = -1; |
| 1044 | |
| 1045 | switch (key) |
| 1046 | { |
| 1047 | case K_LEFTMOUSE: |
| 1048 | case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break; |
| 1049 | case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break; |
| 1050 | case K_LEFTRELEASE: |
| 1051 | case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break; |
| 1052 | case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break; |
| 1053 | case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break; |
| 1054 | case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break; |
| 1055 | case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break; |
| 1056 | case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break; |
| 1057 | case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break; |
| 1058 | case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break; |
| 1059 | } |
| 1060 | return TRUE; |
| 1061 | } |
| 1062 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1063 | /* |
| 1064 | * Convert typed key "c" into bytes to send to the job. |
| 1065 | * Return the number of bytes in "buf". |
| 1066 | */ |
| 1067 | static int |
| 1068 | term_convert_key(term_T *term, int c, char *buf) |
| 1069 | { |
| 1070 | VTerm *vterm = term->tl_vterm; |
| 1071 | VTermKey key = VTERM_KEY_NONE; |
| 1072 | VTermModifier mod = VTERM_MOD_NONE; |
Bram Moolenaar | a42ad57 | 2017-11-16 13:08:04 +0100 | [diff] [blame] | 1073 | int other = FALSE; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1074 | |
| 1075 | switch (c) |
| 1076 | { |
Bram Moolenaar | 26d205d | 2017-11-09 17:33:11 +0100 | [diff] [blame] | 1077 | /* don't use VTERM_KEY_ENTER, it may do an unwanted conversion */ |
| 1078 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1079 | /* don't use VTERM_KEY_BACKSPACE, it always |
| 1080 | * becomes 0x7f DEL */ |
| 1081 | case K_BS: c = term_backspace_char; break; |
| 1082 | |
| 1083 | case ESC: key = VTERM_KEY_ESCAPE; break; |
| 1084 | case K_DEL: key = VTERM_KEY_DEL; break; |
| 1085 | case K_DOWN: key = VTERM_KEY_DOWN; break; |
| 1086 | case K_S_DOWN: mod = VTERM_MOD_SHIFT; |
| 1087 | key = VTERM_KEY_DOWN; break; |
| 1088 | case K_END: key = VTERM_KEY_END; break; |
| 1089 | case K_S_END: mod = VTERM_MOD_SHIFT; |
| 1090 | key = VTERM_KEY_END; break; |
| 1091 | case K_C_END: mod = VTERM_MOD_CTRL; |
| 1092 | key = VTERM_KEY_END; break; |
| 1093 | case K_F10: key = VTERM_KEY_FUNCTION(10); break; |
| 1094 | case K_F11: key = VTERM_KEY_FUNCTION(11); break; |
| 1095 | case K_F12: key = VTERM_KEY_FUNCTION(12); break; |
| 1096 | case K_F1: key = VTERM_KEY_FUNCTION(1); break; |
| 1097 | case K_F2: key = VTERM_KEY_FUNCTION(2); break; |
| 1098 | case K_F3: key = VTERM_KEY_FUNCTION(3); break; |
| 1099 | case K_F4: key = VTERM_KEY_FUNCTION(4); break; |
| 1100 | case K_F5: key = VTERM_KEY_FUNCTION(5); break; |
| 1101 | case K_F6: key = VTERM_KEY_FUNCTION(6); break; |
| 1102 | case K_F7: key = VTERM_KEY_FUNCTION(7); break; |
| 1103 | case K_F8: key = VTERM_KEY_FUNCTION(8); break; |
| 1104 | case K_F9: key = VTERM_KEY_FUNCTION(9); break; |
| 1105 | case K_HOME: key = VTERM_KEY_HOME; break; |
| 1106 | case K_S_HOME: mod = VTERM_MOD_SHIFT; |
| 1107 | key = VTERM_KEY_HOME; break; |
| 1108 | case K_C_HOME: mod = VTERM_MOD_CTRL; |
| 1109 | key = VTERM_KEY_HOME; break; |
| 1110 | case K_INS: key = VTERM_KEY_INS; break; |
| 1111 | case K_K0: key = VTERM_KEY_KP_0; break; |
| 1112 | case K_K1: key = VTERM_KEY_KP_1; break; |
| 1113 | case K_K2: key = VTERM_KEY_KP_2; break; |
| 1114 | case K_K3: key = VTERM_KEY_KP_3; break; |
| 1115 | case K_K4: key = VTERM_KEY_KP_4; break; |
| 1116 | case K_K5: key = VTERM_KEY_KP_5; break; |
| 1117 | case K_K6: key = VTERM_KEY_KP_6; break; |
| 1118 | case K_K7: key = VTERM_KEY_KP_7; break; |
| 1119 | case K_K8: key = VTERM_KEY_KP_8; break; |
| 1120 | case K_K9: key = VTERM_KEY_KP_9; break; |
| 1121 | case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */ |
| 1122 | case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break; |
| 1123 | case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */ |
| 1124 | case K_KENTER: key = VTERM_KEY_KP_ENTER; break; |
| 1125 | case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */ |
| 1126 | case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */ |
| 1127 | case K_KMINUS: key = VTERM_KEY_KP_MINUS; break; |
| 1128 | case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break; |
| 1129 | case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */ |
| 1130 | case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */ |
| 1131 | case K_KPLUS: key = VTERM_KEY_KP_PLUS; break; |
| 1132 | case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break; |
| 1133 | case K_LEFT: key = VTERM_KEY_LEFT; break; |
| 1134 | case K_S_LEFT: mod = VTERM_MOD_SHIFT; |
| 1135 | key = VTERM_KEY_LEFT; break; |
| 1136 | case K_C_LEFT: mod = VTERM_MOD_CTRL; |
| 1137 | key = VTERM_KEY_LEFT; break; |
| 1138 | case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break; |
| 1139 | case K_PAGEUP: key = VTERM_KEY_PAGEUP; break; |
| 1140 | case K_RIGHT: key = VTERM_KEY_RIGHT; break; |
| 1141 | case K_S_RIGHT: mod = VTERM_MOD_SHIFT; |
| 1142 | key = VTERM_KEY_RIGHT; break; |
| 1143 | case K_C_RIGHT: mod = VTERM_MOD_CTRL; |
| 1144 | key = VTERM_KEY_RIGHT; break; |
| 1145 | case K_UP: key = VTERM_KEY_UP; break; |
| 1146 | case K_S_UP: mod = VTERM_MOD_SHIFT; |
| 1147 | key = VTERM_KEY_UP; break; |
| 1148 | case TAB: key = VTERM_KEY_TAB; break; |
Bram Moolenaar | 73cddfd | 2018-02-16 20:01:04 +0100 | [diff] [blame] | 1149 | case K_S_TAB: mod = VTERM_MOD_SHIFT; |
| 1150 | key = VTERM_KEY_TAB; break; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1151 | |
Bram Moolenaar | a42ad57 | 2017-11-16 13:08:04 +0100 | [diff] [blame] | 1152 | case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break; |
| 1153 | case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1154 | case K_MOUSELEFT: /* TODO */ return 0; |
| 1155 | case K_MOUSERIGHT: /* TODO */ return 0; |
| 1156 | |
| 1157 | case K_LEFTMOUSE: |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 1158 | case K_LEFTMOUSE_NM: |
| 1159 | case K_LEFTDRAG: |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1160 | case K_LEFTRELEASE: |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 1161 | case K_LEFTRELEASE_NM: |
| 1162 | case K_MOUSEMOVE: |
| 1163 | case K_MIDDLEMOUSE: |
| 1164 | case K_MIDDLEDRAG: |
| 1165 | case K_MIDDLERELEASE: |
| 1166 | case K_RIGHTMOUSE: |
| 1167 | case K_RIGHTDRAG: |
| 1168 | case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c)) |
| 1169 | return 0; |
| 1170 | other = TRUE; |
| 1171 | break; |
| 1172 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1173 | case K_X1MOUSE: /* TODO */ return 0; |
| 1174 | case K_X1DRAG: /* TODO */ return 0; |
| 1175 | case K_X1RELEASE: /* TODO */ return 0; |
| 1176 | case K_X2MOUSE: /* TODO */ return 0; |
| 1177 | case K_X2DRAG: /* TODO */ return 0; |
| 1178 | case K_X2RELEASE: /* TODO */ return 0; |
| 1179 | |
| 1180 | case K_IGNORE: return 0; |
| 1181 | case K_NOP: return 0; |
| 1182 | case K_UNDO: return 0; |
| 1183 | case K_HELP: return 0; |
| 1184 | case K_XF1: key = VTERM_KEY_FUNCTION(1); break; |
| 1185 | case K_XF2: key = VTERM_KEY_FUNCTION(2); break; |
| 1186 | case K_XF3: key = VTERM_KEY_FUNCTION(3); break; |
| 1187 | case K_XF4: key = VTERM_KEY_FUNCTION(4); break; |
| 1188 | case K_SELECT: return 0; |
| 1189 | #ifdef FEAT_GUI |
| 1190 | case K_VER_SCROLLBAR: return 0; |
| 1191 | case K_HOR_SCROLLBAR: return 0; |
| 1192 | #endif |
| 1193 | #ifdef FEAT_GUI_TABLINE |
| 1194 | case K_TABLINE: return 0; |
| 1195 | case K_TABMENU: return 0; |
| 1196 | #endif |
| 1197 | #ifdef FEAT_NETBEANS_INTG |
| 1198 | case K_F21: key = VTERM_KEY_FUNCTION(21); break; |
| 1199 | #endif |
| 1200 | #ifdef FEAT_DND |
| 1201 | case K_DROP: return 0; |
| 1202 | #endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1203 | case K_CURSORHOLD: return 0; |
Bram Moolenaar | a42ad57 | 2017-11-16 13:08:04 +0100 | [diff] [blame] | 1204 | case K_PS: vterm_keyboard_start_paste(vterm); |
| 1205 | other = TRUE; |
| 1206 | break; |
| 1207 | case K_PE: vterm_keyboard_end_paste(vterm); |
| 1208 | other = TRUE; |
| 1209 | break; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | * Convert special keys to vterm keys: |
| 1214 | * - Write keys to vterm: vterm_keyboard_key() |
| 1215 | * - Write output to channel. |
| 1216 | * TODO: use mod_mask |
| 1217 | */ |
| 1218 | if (key != VTERM_KEY_NONE) |
| 1219 | /* Special key, let vterm convert it. */ |
| 1220 | vterm_keyboard_key(vterm, key, mod); |
Bram Moolenaar | a42ad57 | 2017-11-16 13:08:04 +0100 | [diff] [blame] | 1221 | else if (!other) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1222 | /* Normal character, let vterm convert it. */ |
| 1223 | vterm_keyboard_unichar(vterm, c, mod); |
| 1224 | |
| 1225 | /* Read back the converted escape sequence. */ |
| 1226 | return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN); |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * Return TRUE if the job for "term" is still running. |
| 1231 | */ |
| 1232 | int |
| 1233 | term_job_running(term_T *term) |
| 1234 | { |
| 1235 | /* Also consider the job finished when the channel is closed, to avoid a |
| 1236 | * race condition when updating the title. */ |
| 1237 | return term != NULL |
| 1238 | && term->tl_job != NULL |
| 1239 | && channel_is_open(term->tl_job->jv_channel) |
| 1240 | && (term->tl_job->jv_status == JOB_STARTED |
| 1241 | || term->tl_job->jv_channel->ch_keep_open); |
| 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | * Return TRUE if "term" has an active channel and used ":term NONE". |
| 1246 | */ |
| 1247 | int |
| 1248 | term_none_open(term_T *term) |
| 1249 | { |
| 1250 | /* Also consider the job finished when the channel is closed, to avoid a |
| 1251 | * race condition when updating the title. */ |
| 1252 | return term != NULL |
| 1253 | && term->tl_job != NULL |
| 1254 | && channel_is_open(term->tl_job->jv_channel) |
| 1255 | && term->tl_job->jv_channel->ch_keep_open; |
| 1256 | } |
| 1257 | |
| 1258 | /* |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 1259 | * Used when exiting: kill the job in "buf" if so desired. |
| 1260 | * Return OK when the job finished. |
| 1261 | * Return FAIL when the job is still running. |
| 1262 | */ |
| 1263 | int |
| 1264 | term_try_stop_job(buf_T *buf) |
| 1265 | { |
| 1266 | int count; |
| 1267 | char *how = (char *)buf->b_term->tl_kill; |
| 1268 | |
| 1269 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 1270 | if ((how == NULL || *how == NUL) && (p_confirm || cmdmod.confirm)) |
| 1271 | { |
| 1272 | char_u buff[DIALOG_MSG_SIZE]; |
| 1273 | int ret; |
| 1274 | |
| 1275 | dialog_msg(buff, _("Kill job in \"%s\"?"), buf->b_fname); |
| 1276 | ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1); |
| 1277 | if (ret == VIM_YES) |
| 1278 | how = "kill"; |
| 1279 | else if (ret == VIM_CANCEL) |
| 1280 | return FAIL; |
| 1281 | } |
| 1282 | #endif |
| 1283 | if (how == NULL || *how == NUL) |
| 1284 | return FAIL; |
| 1285 | |
| 1286 | job_stop(buf->b_term->tl_job, NULL, how); |
| 1287 | |
| 1288 | /* wait for up to a second for the job to die */ |
| 1289 | for (count = 0; count < 100; ++count) |
| 1290 | { |
| 1291 | /* buffer, terminal and job may be cleaned up while waiting */ |
| 1292 | if (!buf_valid(buf) |
| 1293 | || buf->b_term == NULL |
| 1294 | || buf->b_term->tl_job == NULL) |
| 1295 | return OK; |
| 1296 | |
| 1297 | /* call job_status() to update jv_status */ |
| 1298 | job_status(buf->b_term->tl_job); |
| 1299 | if (buf->b_term->tl_job->jv_status >= JOB_ENDED) |
| 1300 | return OK; |
| 1301 | ui_delay(10L, FALSE); |
| 1302 | mch_check_messages(); |
| 1303 | parse_queued_messages(); |
| 1304 | } |
| 1305 | return FAIL; |
| 1306 | } |
| 1307 | |
| 1308 | /* |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1309 | * Add the last line of the scrollback buffer to the buffer in the window. |
| 1310 | */ |
| 1311 | static void |
| 1312 | add_scrollback_line_to_buffer(term_T *term, char_u *text, int len) |
| 1313 | { |
| 1314 | buf_T *buf = term->tl_buffer; |
| 1315 | int empty = (buf->b_ml.ml_flags & ML_EMPTY); |
| 1316 | linenr_T lnum = buf->b_ml.ml_line_count; |
| 1317 | |
| 1318 | #ifdef WIN3264 |
| 1319 | if (!enc_utf8 && enc_codepage > 0) |
| 1320 | { |
| 1321 | WCHAR *ret = NULL; |
| 1322 | int length = 0; |
| 1323 | |
| 1324 | MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1, |
| 1325 | &ret, &length); |
| 1326 | if (ret != NULL) |
| 1327 | { |
| 1328 | WideCharToMultiByte_alloc(enc_codepage, 0, |
| 1329 | ret, length, (char **)&text, &len, 0, 0); |
| 1330 | vim_free(ret); |
| 1331 | ml_append_buf(term->tl_buffer, lnum, text, len, FALSE); |
| 1332 | vim_free(text); |
| 1333 | } |
| 1334 | } |
| 1335 | else |
| 1336 | #endif |
| 1337 | ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE); |
| 1338 | if (empty) |
| 1339 | { |
| 1340 | /* Delete the empty line that was in the empty buffer. */ |
| 1341 | curbuf = buf; |
| 1342 | ml_delete(1, FALSE); |
| 1343 | curbuf = curwin->w_buffer; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | static void |
| 1348 | cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr) |
| 1349 | { |
| 1350 | attr->width = cell->width; |
| 1351 | attr->attrs = cell->attrs; |
| 1352 | attr->fg = cell->fg; |
| 1353 | attr->bg = cell->bg; |
| 1354 | } |
| 1355 | |
| 1356 | static int |
| 1357 | equal_celattr(cellattr_T *a, cellattr_T *b) |
| 1358 | { |
| 1359 | /* Comparing the colors should be sufficient. */ |
| 1360 | return a->fg.red == b->fg.red |
| 1361 | && a->fg.green == b->fg.green |
| 1362 | && a->fg.blue == b->fg.blue |
| 1363 | && a->bg.red == b->bg.red |
| 1364 | && a->bg.green == b->bg.green |
| 1365 | && a->bg.blue == b->bg.blue; |
| 1366 | } |
| 1367 | |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 1368 | /* |
| 1369 | * Add an empty scrollback line to "term". When "lnum" is not zero, add the |
| 1370 | * line at this position. Otherwise at the end. |
| 1371 | */ |
| 1372 | static int |
| 1373 | add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum) |
| 1374 | { |
| 1375 | if (ga_grow(&term->tl_scrollback, 1) == OK) |
| 1376 | { |
| 1377 | sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data |
| 1378 | + term->tl_scrollback.ga_len; |
| 1379 | |
| 1380 | if (lnum > 0) |
| 1381 | { |
| 1382 | int i; |
| 1383 | |
| 1384 | for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i) |
| 1385 | { |
| 1386 | *line = *(line - 1); |
| 1387 | --line; |
| 1388 | } |
| 1389 | } |
| 1390 | line->sb_cols = 0; |
| 1391 | line->sb_cells = NULL; |
| 1392 | line->sb_fill_attr = *fill_attr; |
| 1393 | ++term->tl_scrollback.ga_len; |
| 1394 | return OK; |
| 1395 | } |
| 1396 | return FALSE; |
| 1397 | } |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1398 | |
| 1399 | /* |
| 1400 | * Add the current lines of the terminal to scrollback and to the buffer. |
| 1401 | * Called after the job has ended and when switching to Terminal-Normal mode. |
| 1402 | */ |
| 1403 | static void |
| 1404 | move_terminal_to_buffer(term_T *term) |
| 1405 | { |
| 1406 | win_T *wp; |
| 1407 | int len; |
| 1408 | int lines_skipped = 0; |
| 1409 | VTermPos pos; |
| 1410 | VTermScreenCell cell; |
| 1411 | cellattr_T fill_attr, new_fill_attr; |
| 1412 | cellattr_T *p; |
| 1413 | VTermScreen *screen; |
| 1414 | |
| 1415 | if (term->tl_vterm == NULL) |
| 1416 | return; |
| 1417 | screen = vterm_obtain_screen(term->tl_vterm); |
| 1418 | fill_attr = new_fill_attr = term->tl_default_color; |
| 1419 | |
| 1420 | for (pos.row = 0; pos.row < term->tl_rows; ++pos.row) |
| 1421 | { |
| 1422 | len = 0; |
| 1423 | for (pos.col = 0; pos.col < term->tl_cols; ++pos.col) |
| 1424 | if (vterm_screen_get_cell(screen, pos, &cell) != 0 |
| 1425 | && cell.chars[0] != NUL) |
| 1426 | { |
| 1427 | len = pos.col + 1; |
| 1428 | new_fill_attr = term->tl_default_color; |
| 1429 | } |
| 1430 | else |
| 1431 | /* Assume the last attr is the filler attr. */ |
| 1432 | cell2cellattr(&cell, &new_fill_attr); |
| 1433 | |
| 1434 | if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr)) |
| 1435 | ++lines_skipped; |
| 1436 | else |
| 1437 | { |
| 1438 | while (lines_skipped > 0) |
| 1439 | { |
| 1440 | /* Line was skipped, add an empty line. */ |
| 1441 | --lines_skipped; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 1442 | if (add_empty_scrollback(term, &fill_attr, 0) == OK) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1443 | add_scrollback_line_to_buffer(term, (char_u *)"", 0); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | if (len == 0) |
| 1447 | p = NULL; |
| 1448 | else |
| 1449 | p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len); |
| 1450 | if ((p != NULL || len == 0) |
| 1451 | && ga_grow(&term->tl_scrollback, 1) == OK) |
| 1452 | { |
| 1453 | garray_T ga; |
| 1454 | int width; |
| 1455 | sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data |
| 1456 | + term->tl_scrollback.ga_len; |
| 1457 | |
| 1458 | ga_init2(&ga, 1, 100); |
| 1459 | for (pos.col = 0; pos.col < len; pos.col += width) |
| 1460 | { |
| 1461 | if (vterm_screen_get_cell(screen, pos, &cell) == 0) |
| 1462 | { |
| 1463 | width = 1; |
| 1464 | vim_memset(p + pos.col, 0, sizeof(cellattr_T)); |
| 1465 | if (ga_grow(&ga, 1) == OK) |
| 1466 | ga.ga_len += utf_char2bytes(' ', |
| 1467 | (char_u *)ga.ga_data + ga.ga_len); |
| 1468 | } |
| 1469 | else |
| 1470 | { |
| 1471 | width = cell.width; |
| 1472 | |
| 1473 | cell2cellattr(&cell, &p[pos.col]); |
| 1474 | |
| 1475 | if (ga_grow(&ga, MB_MAXBYTES) == OK) |
| 1476 | { |
| 1477 | int i; |
| 1478 | int c; |
| 1479 | |
| 1480 | for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i) |
| 1481 | ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c, |
| 1482 | (char_u *)ga.ga_data + ga.ga_len); |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | line->sb_cols = len; |
| 1487 | line->sb_cells = p; |
| 1488 | line->sb_fill_attr = new_fill_attr; |
| 1489 | fill_attr = new_fill_attr; |
| 1490 | ++term->tl_scrollback.ga_len; |
| 1491 | |
| 1492 | if (ga_grow(&ga, 1) == FAIL) |
| 1493 | add_scrollback_line_to_buffer(term, (char_u *)"", 0); |
| 1494 | else |
| 1495 | { |
| 1496 | *((char_u *)ga.ga_data + ga.ga_len) = NUL; |
| 1497 | add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len); |
| 1498 | } |
| 1499 | ga_clear(&ga); |
| 1500 | } |
| 1501 | else |
| 1502 | vim_free(p); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | /* Obtain the current background color. */ |
| 1507 | vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm), |
| 1508 | &term->tl_default_color.fg, &term->tl_default_color.bg); |
| 1509 | |
| 1510 | FOR_ALL_WINDOWS(wp) |
| 1511 | { |
| 1512 | if (wp->w_buffer == term->tl_buffer) |
| 1513 | { |
| 1514 | wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count; |
| 1515 | wp->w_cursor.col = 0; |
| 1516 | wp->w_valid = 0; |
| 1517 | if (wp->w_cursor.lnum >= wp->w_height) |
| 1518 | { |
| 1519 | linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1; |
| 1520 | |
| 1521 | if (wp->w_topline < min_topline) |
| 1522 | wp->w_topline = min_topline; |
| 1523 | } |
| 1524 | redraw_win_later(wp, NOT_VALID); |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | static void |
| 1530 | set_terminal_mode(term_T *term, int normal_mode) |
| 1531 | { |
| 1532 | term->tl_normal_mode = normal_mode; |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1533 | VIM_CLEAR(term->tl_status_text); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1534 | if (term->tl_buffer == curbuf) |
| 1535 | maketitle(); |
| 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | * Called after the job if finished and Terminal mode is not active: |
| 1540 | * Move the vterm contents into the scrollback buffer and free the vterm. |
| 1541 | */ |
| 1542 | static void |
| 1543 | cleanup_vterm(term_T *term) |
| 1544 | { |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 1545 | if (term->tl_finish != TL_FINISH_CLOSE) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1546 | move_terminal_to_buffer(term); |
| 1547 | term_free_vterm(term); |
| 1548 | set_terminal_mode(term, FALSE); |
| 1549 | } |
| 1550 | |
| 1551 | /* |
| 1552 | * Switch from Terminal-Job mode to Terminal-Normal mode. |
| 1553 | * Suspends updating the terminal window. |
| 1554 | */ |
| 1555 | static void |
| 1556 | term_enter_normal_mode(void) |
| 1557 | { |
| 1558 | term_T *term = curbuf->b_term; |
| 1559 | |
| 1560 | /* Append the current terminal contents to the buffer. */ |
| 1561 | move_terminal_to_buffer(term); |
| 1562 | |
| 1563 | set_terminal_mode(term, TRUE); |
| 1564 | |
| 1565 | /* Move the window cursor to the position of the cursor in the |
| 1566 | * terminal. */ |
| 1567 | curwin->w_cursor.lnum = term->tl_scrollback_scrolled |
| 1568 | + term->tl_cursor_pos.row + 1; |
| 1569 | check_cursor(); |
| 1570 | coladvance(term->tl_cursor_pos.col); |
| 1571 | |
| 1572 | /* Display the same lines as in the terminal. */ |
| 1573 | curwin->w_topline = term->tl_scrollback_scrolled + 1; |
| 1574 | } |
| 1575 | |
| 1576 | /* |
| 1577 | * Returns TRUE if the current window contains a terminal and we are in |
| 1578 | * Terminal-Normal mode. |
| 1579 | */ |
| 1580 | int |
| 1581 | term_in_normal_mode(void) |
| 1582 | { |
| 1583 | term_T *term = curbuf->b_term; |
| 1584 | |
| 1585 | return term != NULL && term->tl_normal_mode; |
| 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * Switch from Terminal-Normal mode to Terminal-Job mode. |
| 1590 | * Restores updating the terminal window. |
| 1591 | */ |
| 1592 | void |
| 1593 | term_enter_job_mode() |
| 1594 | { |
| 1595 | term_T *term = curbuf->b_term; |
| 1596 | sb_line_T *line; |
| 1597 | garray_T *gap; |
| 1598 | |
| 1599 | /* Remove the terminal contents from the scrollback and the buffer. */ |
| 1600 | gap = &term->tl_scrollback; |
| 1601 | while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled |
| 1602 | && gap->ga_len > 0) |
| 1603 | { |
| 1604 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 1605 | line = (sb_line_T *)gap->ga_data + gap->ga_len - 1; |
| 1606 | vim_free(line->sb_cells); |
| 1607 | --gap->ga_len; |
| 1608 | } |
| 1609 | check_cursor(); |
| 1610 | |
| 1611 | set_terminal_mode(term, FALSE); |
| 1612 | |
| 1613 | if (term->tl_channel_closed) |
| 1614 | cleanup_vterm(term); |
| 1615 | redraw_buf_and_status_later(curbuf, NOT_VALID); |
| 1616 | } |
| 1617 | |
| 1618 | /* |
Bram Moolenaar | c8bcfe7 | 2018-02-27 16:29:28 +0100 | [diff] [blame] | 1619 | * Get a key from the user with terminal mode mappings. |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1620 | * Note: while waiting a terminal may be closed and freed if the channel is |
| 1621 | * closed and ++close was used. |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1622 | */ |
| 1623 | static int |
| 1624 | term_vgetc() |
| 1625 | { |
| 1626 | int c; |
| 1627 | int save_State = State; |
| 1628 | |
| 1629 | State = TERMINAL; |
| 1630 | got_int = FALSE; |
| 1631 | #ifdef WIN3264 |
| 1632 | ctrl_break_was_pressed = FALSE; |
| 1633 | #endif |
| 1634 | c = vgetc(); |
| 1635 | got_int = FALSE; |
| 1636 | State = save_State; |
| 1637 | return c; |
| 1638 | } |
| 1639 | |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 1640 | static int mouse_was_outside = FALSE; |
| 1641 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1642 | /* |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1643 | * Send keys to terminal. |
| 1644 | * Return FAIL when the key needs to be handled in Normal mode. |
| 1645 | * Return OK when the key was dropped or sent to the terminal. |
| 1646 | */ |
| 1647 | int |
| 1648 | send_keys_to_term(term_T *term, int c, int typed) |
| 1649 | { |
| 1650 | char msg[KEY_BUF_LEN]; |
| 1651 | size_t len; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1652 | int dragging_outside = FALSE; |
| 1653 | |
| 1654 | /* Catch keys that need to be handled as in Normal mode. */ |
| 1655 | switch (c) |
| 1656 | { |
| 1657 | case NUL: |
| 1658 | case K_ZERO: |
| 1659 | if (typed) |
| 1660 | stuffcharReadbuff(c); |
| 1661 | return FAIL; |
| 1662 | |
| 1663 | case K_IGNORE: |
| 1664 | return FAIL; |
| 1665 | |
| 1666 | case K_LEFTDRAG: |
| 1667 | case K_MIDDLEDRAG: |
| 1668 | case K_RIGHTDRAG: |
| 1669 | case K_X1DRAG: |
| 1670 | case K_X2DRAG: |
| 1671 | dragging_outside = mouse_was_outside; |
| 1672 | /* FALLTHROUGH */ |
| 1673 | case K_LEFTMOUSE: |
| 1674 | case K_LEFTMOUSE_NM: |
| 1675 | case K_LEFTRELEASE: |
| 1676 | case K_LEFTRELEASE_NM: |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1677 | case K_MOUSEMOVE: |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1678 | case K_MIDDLEMOUSE: |
| 1679 | case K_MIDDLERELEASE: |
| 1680 | case K_RIGHTMOUSE: |
| 1681 | case K_RIGHTRELEASE: |
| 1682 | case K_X1MOUSE: |
| 1683 | case K_X1RELEASE: |
| 1684 | case K_X2MOUSE: |
| 1685 | case K_X2RELEASE: |
| 1686 | |
| 1687 | case K_MOUSEUP: |
| 1688 | case K_MOUSEDOWN: |
| 1689 | case K_MOUSELEFT: |
| 1690 | case K_MOUSERIGHT: |
| 1691 | if (mouse_row < W_WINROW(curwin) |
Bram Moolenaar | ce6179c | 2017-12-05 13:06:16 +0100 | [diff] [blame] | 1692 | || mouse_row >= (W_WINROW(curwin) + curwin->w_height) |
Bram Moolenaar | 53f8174 | 2017-09-22 14:35:51 +0200 | [diff] [blame] | 1693 | || mouse_col < curwin->w_wincol |
Bram Moolenaar | ce6179c | 2017-12-05 13:06:16 +0100 | [diff] [blame] | 1694 | || mouse_col >= W_ENDCOL(curwin) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1695 | || dragging_outside) |
| 1696 | { |
Bram Moolenaar | ce6179c | 2017-12-05 13:06:16 +0100 | [diff] [blame] | 1697 | /* click or scroll outside the current window or on status line |
| 1698 | * or vertical separator */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1699 | if (typed) |
| 1700 | { |
| 1701 | stuffcharReadbuff(c); |
| 1702 | mouse_was_outside = TRUE; |
| 1703 | } |
| 1704 | return FAIL; |
| 1705 | } |
| 1706 | } |
| 1707 | if (typed) |
| 1708 | mouse_was_outside = FALSE; |
| 1709 | |
| 1710 | /* Convert the typed key to a sequence of bytes for the job. */ |
| 1711 | len = term_convert_key(term, c, msg); |
| 1712 | if (len > 0) |
| 1713 | /* TODO: if FAIL is returned, stop? */ |
| 1714 | channel_send(term->tl_job->jv_channel, get_tty_part(term), |
| 1715 | (char_u *)msg, (int)len, NULL); |
| 1716 | |
| 1717 | return OK; |
| 1718 | } |
| 1719 | |
| 1720 | static void |
| 1721 | position_cursor(win_T *wp, VTermPos *pos) |
| 1722 | { |
| 1723 | wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1)); |
| 1724 | wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1)); |
| 1725 | wp->w_valid |= (VALID_WCOL|VALID_WROW); |
| 1726 | } |
| 1727 | |
| 1728 | /* |
| 1729 | * Handle CTRL-W "": send register contents to the job. |
| 1730 | */ |
| 1731 | static void |
| 1732 | term_paste_register(int prev_c UNUSED) |
| 1733 | { |
| 1734 | int c; |
| 1735 | list_T *l; |
| 1736 | listitem_T *item; |
| 1737 | long reglen = 0; |
| 1738 | int type; |
| 1739 | |
| 1740 | #ifdef FEAT_CMDL_INFO |
| 1741 | if (add_to_showcmd(prev_c)) |
| 1742 | if (add_to_showcmd('"')) |
| 1743 | out_flush(); |
| 1744 | #endif |
| 1745 | c = term_vgetc(); |
| 1746 | #ifdef FEAT_CMDL_INFO |
| 1747 | clear_showcmd(); |
| 1748 | #endif |
| 1749 | if (!term_use_loop()) |
| 1750 | /* job finished while waiting for a character */ |
| 1751 | return; |
| 1752 | |
| 1753 | /* CTRL-W "= prompt for expression to evaluate. */ |
| 1754 | if (c == '=' && get_expr_register() != '=') |
| 1755 | return; |
| 1756 | if (!term_use_loop()) |
| 1757 | /* job finished while waiting for a character */ |
| 1758 | return; |
| 1759 | |
| 1760 | l = (list_T *)get_reg_contents(c, GREG_LIST); |
| 1761 | if (l != NULL) |
| 1762 | { |
| 1763 | type = get_reg_type(c, ®len); |
| 1764 | for (item = l->lv_first; item != NULL; item = item->li_next) |
| 1765 | { |
| 1766 | char_u *s = get_tv_string(&item->li_tv); |
| 1767 | #ifdef WIN3264 |
| 1768 | char_u *tmp = s; |
| 1769 | |
| 1770 | if (!enc_utf8 && enc_codepage > 0) |
| 1771 | { |
| 1772 | WCHAR *ret = NULL; |
| 1773 | int length = 0; |
| 1774 | |
| 1775 | MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s, |
| 1776 | (int)STRLEN(s), &ret, &length); |
| 1777 | if (ret != NULL) |
| 1778 | { |
| 1779 | WideCharToMultiByte_alloc(CP_UTF8, 0, |
| 1780 | ret, length, (char **)&s, &length, 0, 0); |
| 1781 | vim_free(ret); |
| 1782 | } |
| 1783 | } |
| 1784 | #endif |
| 1785 | channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN, |
| 1786 | s, (int)STRLEN(s), NULL); |
| 1787 | #ifdef WIN3264 |
| 1788 | if (tmp != s) |
| 1789 | vim_free(s); |
| 1790 | #endif |
| 1791 | |
| 1792 | if (item->li_next != NULL || type == MLINE) |
| 1793 | channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN, |
| 1794 | (char_u *)"\r", 1, NULL); |
| 1795 | } |
| 1796 | list_free(l); |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | #if defined(FEAT_GUI) || defined(PROTO) |
| 1801 | /* |
| 1802 | * Return TRUE when the cursor of the terminal should be displayed. |
| 1803 | */ |
| 1804 | int |
| 1805 | terminal_is_active() |
| 1806 | { |
| 1807 | return in_terminal_loop != NULL; |
| 1808 | } |
| 1809 | |
| 1810 | cursorentry_T * |
| 1811 | term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg) |
| 1812 | { |
| 1813 | term_T *term = in_terminal_loop; |
| 1814 | static cursorentry_T entry; |
| 1815 | |
| 1816 | vim_memset(&entry, 0, sizeof(entry)); |
| 1817 | entry.shape = entry.mshape = |
| 1818 | term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR : |
| 1819 | term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER : |
| 1820 | SHAPE_BLOCK; |
| 1821 | entry.percentage = 20; |
| 1822 | if (term->tl_cursor_blink) |
| 1823 | { |
| 1824 | entry.blinkwait = 700; |
| 1825 | entry.blinkon = 400; |
| 1826 | entry.blinkoff = 250; |
| 1827 | } |
| 1828 | *fg = gui.back_pixel; |
| 1829 | if (term->tl_cursor_color == NULL) |
| 1830 | *bg = gui.norm_pixel; |
| 1831 | else |
| 1832 | *bg = color_name2handle(term->tl_cursor_color); |
| 1833 | entry.name = "n"; |
| 1834 | entry.used_for = SHAPE_CURSOR; |
| 1835 | |
| 1836 | return &entry; |
| 1837 | } |
| 1838 | #endif |
| 1839 | |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 1840 | static void |
| 1841 | may_output_cursor_props(void) |
| 1842 | { |
| 1843 | if (STRCMP(last_set_cursor_color, desired_cursor_color) != 0 |
| 1844 | || last_set_cursor_shape != desired_cursor_shape |
| 1845 | || last_set_cursor_blink != desired_cursor_blink) |
| 1846 | { |
| 1847 | last_set_cursor_color = desired_cursor_color; |
| 1848 | last_set_cursor_shape = desired_cursor_shape; |
| 1849 | last_set_cursor_blink = desired_cursor_blink; |
| 1850 | term_cursor_color(desired_cursor_color); |
| 1851 | if (desired_cursor_shape == -1 || desired_cursor_blink == -1) |
| 1852 | /* this will restore the initial cursor style, if possible */ |
| 1853 | ui_cursor_shape_forced(TRUE); |
| 1854 | else |
| 1855 | term_cursor_shape(desired_cursor_shape, desired_cursor_blink); |
| 1856 | } |
| 1857 | } |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1858 | |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 1859 | /* |
| 1860 | * Set the cursor color and shape, if not last set to these. |
| 1861 | */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1862 | static void |
| 1863 | may_set_cursor_props(term_T *term) |
| 1864 | { |
| 1865 | #ifdef FEAT_GUI |
| 1866 | /* For the GUI the cursor properties are obtained with |
| 1867 | * term_get_cursor_shape(). */ |
| 1868 | if (gui.in_use) |
| 1869 | return; |
| 1870 | #endif |
| 1871 | if (in_terminal_loop == term) |
| 1872 | { |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1873 | if (term->tl_cursor_color != NULL) |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 1874 | desired_cursor_color = term->tl_cursor_color; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1875 | else |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 1876 | desired_cursor_color = (char_u *)""; |
| 1877 | desired_cursor_shape = term->tl_cursor_shape; |
| 1878 | desired_cursor_blink = term->tl_cursor_blink; |
| 1879 | may_output_cursor_props(); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 1883 | /* |
| 1884 | * Reset the desired cursor properties and restore them when needed. |
| 1885 | */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1886 | static void |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 1887 | prepare_restore_cursor_props(void) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1888 | { |
| 1889 | #ifdef FEAT_GUI |
| 1890 | if (gui.in_use) |
| 1891 | return; |
| 1892 | #endif |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 1893 | desired_cursor_color = (char_u *)""; |
| 1894 | desired_cursor_shape = -1; |
| 1895 | desired_cursor_blink = -1; |
| 1896 | may_output_cursor_props(); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | /* |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 1900 | * Called when entering a window with the mouse. If this is a terminal window |
| 1901 | * we may want to change state. |
| 1902 | */ |
| 1903 | void |
| 1904 | term_win_entered() |
| 1905 | { |
| 1906 | term_T *term = curbuf->b_term; |
| 1907 | |
| 1908 | if (term != NULL) |
| 1909 | { |
| 1910 | if (term_use_loop()) |
| 1911 | { |
| 1912 | reset_VIsual_and_resel(); |
| 1913 | if (State & INSERT) |
| 1914 | stop_insert_mode = TRUE; |
| 1915 | } |
| 1916 | mouse_was_outside = FALSE; |
| 1917 | enter_mouse_col = mouse_col; |
| 1918 | enter_mouse_row = mouse_row; |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | /* |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1923 | * Returns TRUE if the current window contains a terminal and we are sending |
| 1924 | * keys to the job. |
| 1925 | */ |
| 1926 | int |
| 1927 | term_use_loop(void) |
| 1928 | { |
| 1929 | term_T *term = curbuf->b_term; |
| 1930 | |
| 1931 | return term != NULL |
| 1932 | && !term->tl_normal_mode |
| 1933 | && term->tl_vterm != NULL |
| 1934 | && term_job_running(term); |
| 1935 | } |
| 1936 | |
| 1937 | /* |
| 1938 | * Wait for input and send it to the job. |
| 1939 | * When "blocking" is TRUE wait for a character to be typed. Otherwise return |
| 1940 | * when there is no more typahead. |
| 1941 | * Return when the start of a CTRL-W command is typed or anything else that |
| 1942 | * should be handled as a Normal mode command. |
| 1943 | * Returns OK if a typed character is to be handled in Normal mode, FAIL if |
| 1944 | * the terminal was closed. |
| 1945 | */ |
| 1946 | int |
| 1947 | terminal_loop(int blocking) |
| 1948 | { |
| 1949 | int c; |
| 1950 | int termkey = 0; |
| 1951 | int ret; |
Bram Moolenaar | 1232624 | 2017-11-04 20:12:14 +0100 | [diff] [blame] | 1952 | #ifdef UNIX |
Bram Moolenaar | 26d205d | 2017-11-09 17:33:11 +0100 | [diff] [blame] | 1953 | int tty_fd = curbuf->b_term->tl_job->jv_channel |
| 1954 | ->ch_part[get_tty_part(curbuf->b_term)].ch_fd; |
Bram Moolenaar | 1232624 | 2017-11-04 20:12:14 +0100 | [diff] [blame] | 1955 | #endif |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 1956 | int restore_cursor; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1957 | |
| 1958 | /* Remember the terminal we are sending keys to. However, the terminal |
| 1959 | * might be closed while waiting for a character, e.g. typing "exit" in a |
| 1960 | * shell and ++close was used. Therefore use curbuf->b_term instead of a |
| 1961 | * stored reference. */ |
| 1962 | in_terminal_loop = curbuf->b_term; |
| 1963 | |
| 1964 | if (*curwin->w_p_tk != NUL) |
| 1965 | termkey = string_to_key(curwin->w_p_tk, TRUE); |
| 1966 | position_cursor(curwin, &curbuf->b_term->tl_cursor_pos); |
| 1967 | may_set_cursor_props(curbuf->b_term); |
| 1968 | |
Bram Moolenaar | c8bcfe7 | 2018-02-27 16:29:28 +0100 | [diff] [blame] | 1969 | while (blocking || vpeekc_nomap() != NUL) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1970 | { |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 1971 | #ifdef FEAT_GUI |
| 1972 | if (!curbuf->b_term->tl_system) |
| 1973 | #endif |
| 1974 | /* TODO: skip screen update when handling a sequence of keys. */ |
| 1975 | /* Repeat redrawing in case a message is received while redrawing. |
| 1976 | */ |
| 1977 | while (must_redraw != 0) |
| 1978 | if (update_screen(0) == FAIL) |
| 1979 | break; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1980 | update_cursor(curbuf->b_term, FALSE); |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 1981 | restore_cursor = TRUE; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1982 | |
| 1983 | c = term_vgetc(); |
| 1984 | if (!term_use_loop()) |
Bram Moolenaar | a3f7e58 | 2017-11-09 13:21:58 +0100 | [diff] [blame] | 1985 | { |
Bram Moolenaar | 26d205d | 2017-11-09 17:33:11 +0100 | [diff] [blame] | 1986 | /* Job finished while waiting for a character. Push back the |
| 1987 | * received character. */ |
Bram Moolenaar | a3f7e58 | 2017-11-09 13:21:58 +0100 | [diff] [blame] | 1988 | if (c != K_IGNORE) |
| 1989 | vungetc(c); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1990 | break; |
Bram Moolenaar | a3f7e58 | 2017-11-09 13:21:58 +0100 | [diff] [blame] | 1991 | } |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 1992 | if (c == K_IGNORE) |
| 1993 | continue; |
| 1994 | |
Bram Moolenaar | 26d205d | 2017-11-09 17:33:11 +0100 | [diff] [blame] | 1995 | #ifdef UNIX |
| 1996 | /* |
| 1997 | * The shell or another program may change the tty settings. Getting |
| 1998 | * them for every typed character is a bit of overhead, but it's needed |
| 1999 | * for the first character typed, e.g. when Vim starts in a shell. |
| 2000 | */ |
| 2001 | if (isatty(tty_fd)) |
| 2002 | { |
| 2003 | ttyinfo_T info; |
| 2004 | |
| 2005 | /* Get the current backspace character of the pty. */ |
| 2006 | if (get_tty_info(tty_fd, &info) == OK) |
| 2007 | term_backspace_char = info.backspace; |
| 2008 | } |
| 2009 | #endif |
| 2010 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2011 | #ifdef WIN3264 |
| 2012 | /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT. |
| 2013 | * Use CTRL-BREAK to kill the job. */ |
| 2014 | if (ctrl_break_was_pressed) |
| 2015 | mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill"); |
| 2016 | #endif |
Bram Moolenaar | af23bad | 2018-03-16 22:20:49 +0100 | [diff] [blame] | 2017 | /* Was either CTRL-W (termkey) or CTRL-\ pressed? |
| 2018 | * Not in a system terminal. */ |
| 2019 | if ((c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL) |
| 2020 | #ifdef FEAT_GUI |
| 2021 | && !curbuf->b_term->tl_system |
| 2022 | #endif |
| 2023 | ) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2024 | { |
| 2025 | int prev_c = c; |
| 2026 | |
| 2027 | #ifdef FEAT_CMDL_INFO |
| 2028 | if (add_to_showcmd(c)) |
| 2029 | out_flush(); |
| 2030 | #endif |
| 2031 | c = term_vgetc(); |
| 2032 | #ifdef FEAT_CMDL_INFO |
| 2033 | clear_showcmd(); |
| 2034 | #endif |
| 2035 | if (!term_use_loop()) |
| 2036 | /* job finished while waiting for a character */ |
| 2037 | break; |
| 2038 | |
| 2039 | if (prev_c == Ctrl_BSL) |
| 2040 | { |
| 2041 | if (c == Ctrl_N) |
| 2042 | { |
| 2043 | /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */ |
| 2044 | term_enter_normal_mode(); |
| 2045 | ret = FAIL; |
| 2046 | goto theend; |
| 2047 | } |
| 2048 | /* Send both keys to the terminal. */ |
| 2049 | send_keys_to_term(curbuf->b_term, prev_c, TRUE); |
| 2050 | } |
| 2051 | else if (c == Ctrl_C) |
| 2052 | { |
| 2053 | /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */ |
| 2054 | mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill"); |
| 2055 | } |
| 2056 | else if (termkey == 0 && c == '.') |
| 2057 | { |
| 2058 | /* "CTRL-W .": send CTRL-W to the job */ |
| 2059 | c = Ctrl_W; |
| 2060 | } |
| 2061 | else if (c == 'N') |
| 2062 | { |
| 2063 | /* CTRL-W N : go to Terminal-Normal mode. */ |
| 2064 | term_enter_normal_mode(); |
| 2065 | ret = FAIL; |
| 2066 | goto theend; |
| 2067 | } |
| 2068 | else if (c == '"') |
| 2069 | { |
| 2070 | term_paste_register(prev_c); |
| 2071 | continue; |
| 2072 | } |
| 2073 | else if (termkey == 0 || c != termkey) |
| 2074 | { |
| 2075 | stuffcharReadbuff(Ctrl_W); |
| 2076 | stuffcharReadbuff(c); |
| 2077 | ret = OK; |
| 2078 | goto theend; |
| 2079 | } |
| 2080 | } |
| 2081 | # ifdef WIN3264 |
| 2082 | if (!enc_utf8 && has_mbyte && c >= 0x80) |
| 2083 | { |
| 2084 | WCHAR wc; |
| 2085 | char_u mb[3]; |
| 2086 | |
| 2087 | mb[0] = (unsigned)c >> 8; |
| 2088 | mb[1] = c; |
| 2089 | if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0) |
| 2090 | c = wc; |
| 2091 | } |
| 2092 | # endif |
| 2093 | if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK) |
| 2094 | { |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 2095 | if (c == K_MOUSEMOVE) |
| 2096 | /* We are sure to come back here, don't reset the cursor color |
| 2097 | * and shape to avoid flickering. */ |
| 2098 | restore_cursor = FALSE; |
| 2099 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2100 | ret = OK; |
| 2101 | goto theend; |
| 2102 | } |
| 2103 | } |
| 2104 | ret = FAIL; |
| 2105 | |
| 2106 | theend: |
| 2107 | in_terminal_loop = NULL; |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 2108 | if (restore_cursor) |
| 2109 | prepare_restore_cursor_props(); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2110 | return ret; |
| 2111 | } |
| 2112 | |
| 2113 | /* |
| 2114 | * Called when a job has finished. |
| 2115 | * This updates the title and status, but does not close the vterm, because |
| 2116 | * there might still be pending output in the channel. |
| 2117 | */ |
| 2118 | void |
| 2119 | term_job_ended(job_T *job) |
| 2120 | { |
| 2121 | term_T *term; |
| 2122 | int did_one = FALSE; |
| 2123 | |
| 2124 | for (term = first_term; term != NULL; term = term->tl_next) |
| 2125 | if (term->tl_job == job) |
| 2126 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 2127 | VIM_CLEAR(term->tl_title); |
| 2128 | VIM_CLEAR(term->tl_status_text); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2129 | redraw_buf_and_status_later(term->tl_buffer, VALID); |
| 2130 | did_one = TRUE; |
| 2131 | } |
| 2132 | if (did_one) |
| 2133 | redraw_statuslines(); |
| 2134 | if (curbuf->b_term != NULL) |
| 2135 | { |
| 2136 | if (curbuf->b_term->tl_job == job) |
| 2137 | maketitle(); |
| 2138 | update_cursor(curbuf->b_term, TRUE); |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | static void |
| 2143 | may_toggle_cursor(term_T *term) |
| 2144 | { |
| 2145 | if (in_terminal_loop == term) |
| 2146 | { |
| 2147 | if (term->tl_cursor_visible) |
| 2148 | cursor_on(); |
| 2149 | else |
| 2150 | cursor_off(); |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | /* |
| 2155 | * Reverse engineer the RGB value into a cterm color index. |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 2156 | * First color is 1. Return 0 if no match found (default color). |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2157 | */ |
| 2158 | static int |
| 2159 | color2index(VTermColor *color, int fg, int *boldp) |
| 2160 | { |
| 2161 | int red = color->red; |
| 2162 | int blue = color->blue; |
| 2163 | int green = color->green; |
| 2164 | |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 2165 | if (color->ansi_index != VTERM_ANSI_INDEX_NONE) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2166 | { |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 2167 | /* First 16 colors and default: use the ANSI index, because these |
| 2168 | * colors can be redefined. */ |
| 2169 | if (t_colors >= 16) |
| 2170 | return color->ansi_index; |
| 2171 | switch (color->ansi_index) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2172 | { |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 2173 | case 0: return 0; |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 2174 | case 1: return lookup_color( 0, fg, boldp) + 1; /* black */ |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 2175 | case 2: return lookup_color( 4, fg, boldp) + 1; /* dark red */ |
| 2176 | case 3: return lookup_color( 2, fg, boldp) + 1; /* dark green */ |
| 2177 | case 4: return lookup_color( 6, fg, boldp) + 1; /* brown */ |
| 2178 | case 5: return lookup_color( 1, fg, boldp) + 1; /* dark blue*/ |
| 2179 | case 6: return lookup_color( 5, fg, boldp) + 1; /* dark magenta */ |
| 2180 | case 7: return lookup_color( 3, fg, boldp) + 1; /* dark cyan */ |
| 2181 | case 8: return lookup_color( 8, fg, boldp) + 1; /* light grey */ |
| 2182 | case 9: return lookup_color(12, fg, boldp) + 1; /* dark grey */ |
| 2183 | case 10: return lookup_color(20, fg, boldp) + 1; /* red */ |
| 2184 | case 11: return lookup_color(16, fg, boldp) + 1; /* green */ |
| 2185 | case 12: return lookup_color(24, fg, boldp) + 1; /* yellow */ |
| 2186 | case 13: return lookup_color(14, fg, boldp) + 1; /* blue */ |
| 2187 | case 14: return lookup_color(22, fg, boldp) + 1; /* magenta */ |
| 2188 | case 15: return lookup_color(18, fg, boldp) + 1; /* cyan */ |
| 2189 | case 16: return lookup_color(26, fg, boldp) + 1; /* white */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2190 | } |
| 2191 | } |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 2192 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2193 | if (t_colors >= 256) |
| 2194 | { |
| 2195 | if (red == blue && red == green) |
| 2196 | { |
Bram Moolenaar | a8fc0d3 | 2017-09-26 13:59:47 +0200 | [diff] [blame] | 2197 | /* 24-color greyscale plus white and black */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2198 | static int cutoff[23] = { |
Bram Moolenaar | a8fc0d3 | 2017-09-26 13:59:47 +0200 | [diff] [blame] | 2199 | 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67, |
| 2200 | 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB, |
| 2201 | 0xD5, 0xDF, 0xE9}; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2202 | int i; |
| 2203 | |
Bram Moolenaar | a8fc0d3 | 2017-09-26 13:59:47 +0200 | [diff] [blame] | 2204 | if (red < 5) |
| 2205 | return 17; /* 00/00/00 */ |
| 2206 | if (red > 245) /* ff/ff/ff */ |
| 2207 | return 232; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2208 | for (i = 0; i < 23; ++i) |
| 2209 | if (red < cutoff[i]) |
| 2210 | return i + 233; |
| 2211 | return 256; |
| 2212 | } |
Bram Moolenaar | a8fc0d3 | 2017-09-26 13:59:47 +0200 | [diff] [blame] | 2213 | { |
| 2214 | static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB}; |
| 2215 | int ri, gi, bi; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2216 | |
Bram Moolenaar | a8fc0d3 | 2017-09-26 13:59:47 +0200 | [diff] [blame] | 2217 | /* 216-color cube */ |
| 2218 | for (ri = 0; ri < 5; ++ri) |
| 2219 | if (red < cutoff[ri]) |
| 2220 | break; |
| 2221 | for (gi = 0; gi < 5; ++gi) |
| 2222 | if (green < cutoff[gi]) |
| 2223 | break; |
| 2224 | for (bi = 0; bi < 5; ++bi) |
| 2225 | if (blue < cutoff[bi]) |
| 2226 | break; |
| 2227 | return 17 + ri * 36 + gi * 6 + bi; |
| 2228 | } |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2229 | } |
| 2230 | return 0; |
| 2231 | } |
| 2232 | |
| 2233 | /* |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 2234 | * Convert Vterm attributes to highlight flags. |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2235 | */ |
| 2236 | static int |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 2237 | vtermAttr2hl(VTermScreenCellAttrs cellattrs) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2238 | { |
| 2239 | int attr = 0; |
| 2240 | |
| 2241 | if (cellattrs.bold) |
| 2242 | attr |= HL_BOLD; |
| 2243 | if (cellattrs.underline) |
| 2244 | attr |= HL_UNDERLINE; |
| 2245 | if (cellattrs.italic) |
| 2246 | attr |= HL_ITALIC; |
| 2247 | if (cellattrs.strike) |
| 2248 | attr |= HL_STRIKETHROUGH; |
| 2249 | if (cellattrs.reverse) |
| 2250 | attr |= HL_INVERSE; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 2251 | return attr; |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | * Store Vterm attributes in "cell" from highlight flags. |
| 2256 | */ |
| 2257 | static void |
| 2258 | hl2vtermAttr(int attr, cellattr_T *cell) |
| 2259 | { |
| 2260 | vim_memset(&cell->attrs, 0, sizeof(VTermScreenCellAttrs)); |
| 2261 | if (attr & HL_BOLD) |
| 2262 | cell->attrs.bold = 1; |
| 2263 | if (attr & HL_UNDERLINE) |
| 2264 | cell->attrs.underline = 1; |
| 2265 | if (attr & HL_ITALIC) |
| 2266 | cell->attrs.italic = 1; |
| 2267 | if (attr & HL_STRIKETHROUGH) |
| 2268 | cell->attrs.strike = 1; |
| 2269 | if (attr & HL_INVERSE) |
| 2270 | cell->attrs.reverse = 1; |
| 2271 | } |
| 2272 | |
| 2273 | /* |
| 2274 | * Convert the attributes of a vterm cell into an attribute index. |
| 2275 | */ |
| 2276 | static int |
| 2277 | cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg) |
| 2278 | { |
| 2279 | int attr = vtermAttr2hl(cellattrs); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2280 | |
| 2281 | #ifdef FEAT_GUI |
| 2282 | if (gui.in_use) |
| 2283 | { |
| 2284 | guicolor_T fg, bg; |
| 2285 | |
| 2286 | fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue); |
| 2287 | bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue); |
| 2288 | return get_gui_attr_idx(attr, fg, bg); |
| 2289 | } |
| 2290 | else |
| 2291 | #endif |
| 2292 | #ifdef FEAT_TERMGUICOLORS |
| 2293 | if (p_tgc) |
| 2294 | { |
| 2295 | guicolor_T fg, bg; |
| 2296 | |
| 2297 | fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue); |
| 2298 | bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue); |
| 2299 | |
| 2300 | return get_tgc_attr_idx(attr, fg, bg); |
| 2301 | } |
| 2302 | else |
| 2303 | #endif |
| 2304 | { |
| 2305 | int bold = MAYBE; |
| 2306 | int fg = color2index(&cellfg, TRUE, &bold); |
| 2307 | int bg = color2index(&cellbg, FALSE, &bold); |
| 2308 | |
Bram Moolenaar | 76bb719 | 2017-11-30 22:07:07 +0100 | [diff] [blame] | 2309 | /* Use the "Terminal" highlighting for the default colors. */ |
Bram Moolenaar | a7c54cf | 2017-12-01 21:07:20 +0100 | [diff] [blame] | 2310 | if ((fg == 0 || bg == 0) && t_colors >= 16) |
Bram Moolenaar | 76bb719 | 2017-11-30 22:07:07 +0100 | [diff] [blame] | 2311 | { |
Bram Moolenaar | a7c54cf | 2017-12-01 21:07:20 +0100 | [diff] [blame] | 2312 | if (fg == 0 && term_default_cterm_fg >= 0) |
| 2313 | fg = term_default_cterm_fg + 1; |
| 2314 | if (bg == 0 && term_default_cterm_bg >= 0) |
| 2315 | bg = term_default_cterm_bg + 1; |
Bram Moolenaar | 76bb719 | 2017-11-30 22:07:07 +0100 | [diff] [blame] | 2316 | } |
| 2317 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2318 | /* with 8 colors set the bold attribute to get a bright foreground */ |
| 2319 | if (bold == TRUE) |
| 2320 | attr |= HL_BOLD; |
| 2321 | return get_cterm_attr_idx(attr, fg, bg); |
| 2322 | } |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | static int |
| 2327 | handle_damage(VTermRect rect, void *user) |
| 2328 | { |
| 2329 | term_T *term = (term_T *)user; |
| 2330 | |
| 2331 | term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row); |
| 2332 | term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row); |
| 2333 | redraw_buf_later(term->tl_buffer, NOT_VALID); |
| 2334 | return 1; |
| 2335 | } |
| 2336 | |
| 2337 | static int |
| 2338 | handle_moverect(VTermRect dest, VTermRect src, void *user) |
| 2339 | { |
| 2340 | term_T *term = (term_T *)user; |
| 2341 | |
| 2342 | /* Scrolling up is done much more efficiently by deleting lines instead of |
| 2343 | * redrawing the text. */ |
| 2344 | if (dest.start_col == src.start_col |
| 2345 | && dest.end_col == src.end_col |
| 2346 | && dest.start_row < src.start_row) |
| 2347 | { |
| 2348 | win_T *wp; |
| 2349 | VTermColor fg, bg; |
| 2350 | VTermScreenCellAttrs attr; |
| 2351 | int clear_attr; |
| 2352 | |
| 2353 | /* Set the color to clear lines with. */ |
| 2354 | vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm), |
| 2355 | &fg, &bg); |
| 2356 | vim_memset(&attr, 0, sizeof(attr)); |
| 2357 | clear_attr = cell2attr(attr, fg, bg); |
| 2358 | |
| 2359 | FOR_ALL_WINDOWS(wp) |
| 2360 | { |
| 2361 | if (wp->w_buffer == term->tl_buffer) |
| 2362 | win_del_lines(wp, dest.start_row, |
| 2363 | src.start_row - dest.start_row, FALSE, FALSE, |
| 2364 | clear_attr); |
| 2365 | } |
| 2366 | } |
Bram Moolenaar | 3a497e1 | 2017-09-30 20:40:27 +0200 | [diff] [blame] | 2367 | |
| 2368 | term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row); |
| 2369 | term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row); |
| 2370 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2371 | redraw_buf_later(term->tl_buffer, NOT_VALID); |
| 2372 | return 1; |
| 2373 | } |
| 2374 | |
| 2375 | static int |
| 2376 | handle_movecursor( |
| 2377 | VTermPos pos, |
| 2378 | VTermPos oldpos UNUSED, |
| 2379 | int visible, |
| 2380 | void *user) |
| 2381 | { |
| 2382 | term_T *term = (term_T *)user; |
| 2383 | win_T *wp; |
| 2384 | |
| 2385 | term->tl_cursor_pos = pos; |
| 2386 | term->tl_cursor_visible = visible; |
| 2387 | |
| 2388 | FOR_ALL_WINDOWS(wp) |
| 2389 | { |
| 2390 | if (wp->w_buffer == term->tl_buffer) |
| 2391 | position_cursor(wp, &pos); |
| 2392 | } |
| 2393 | if (term->tl_buffer == curbuf && !term->tl_normal_mode) |
| 2394 | { |
| 2395 | may_toggle_cursor(term); |
| 2396 | update_cursor(term, term->tl_cursor_visible); |
| 2397 | } |
| 2398 | |
| 2399 | return 1; |
| 2400 | } |
| 2401 | |
| 2402 | static int |
| 2403 | handle_settermprop( |
| 2404 | VTermProp prop, |
| 2405 | VTermValue *value, |
| 2406 | void *user) |
| 2407 | { |
| 2408 | term_T *term = (term_T *)user; |
| 2409 | |
| 2410 | switch (prop) |
| 2411 | { |
| 2412 | case VTERM_PROP_TITLE: |
| 2413 | vim_free(term->tl_title); |
| 2414 | /* a blank title isn't useful, make it empty, so that "running" is |
| 2415 | * displayed */ |
| 2416 | if (*skipwhite((char_u *)value->string) == NUL) |
| 2417 | term->tl_title = NULL; |
| 2418 | #ifdef WIN3264 |
| 2419 | else if (!enc_utf8 && enc_codepage > 0) |
| 2420 | { |
| 2421 | WCHAR *ret = NULL; |
| 2422 | int length = 0; |
| 2423 | |
| 2424 | MultiByteToWideChar_alloc(CP_UTF8, 0, |
| 2425 | (char*)value->string, (int)STRLEN(value->string), |
| 2426 | &ret, &length); |
| 2427 | if (ret != NULL) |
| 2428 | { |
| 2429 | WideCharToMultiByte_alloc(enc_codepage, 0, |
| 2430 | ret, length, (char**)&term->tl_title, |
| 2431 | &length, 0, 0); |
| 2432 | vim_free(ret); |
| 2433 | } |
| 2434 | } |
| 2435 | #endif |
| 2436 | else |
| 2437 | term->tl_title = vim_strsave((char_u *)value->string); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 2438 | VIM_CLEAR(term->tl_status_text); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2439 | if (term == curbuf->b_term) |
| 2440 | maketitle(); |
| 2441 | break; |
| 2442 | |
| 2443 | case VTERM_PROP_CURSORVISIBLE: |
| 2444 | term->tl_cursor_visible = value->boolean; |
| 2445 | may_toggle_cursor(term); |
| 2446 | out_flush(); |
| 2447 | break; |
| 2448 | |
| 2449 | case VTERM_PROP_CURSORBLINK: |
| 2450 | term->tl_cursor_blink = value->boolean; |
| 2451 | may_set_cursor_props(term); |
| 2452 | break; |
| 2453 | |
| 2454 | case VTERM_PROP_CURSORSHAPE: |
| 2455 | term->tl_cursor_shape = value->number; |
| 2456 | may_set_cursor_props(term); |
| 2457 | break; |
| 2458 | |
| 2459 | case VTERM_PROP_CURSORCOLOR: |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 2460 | if (desired_cursor_color == term->tl_cursor_color) |
| 2461 | desired_cursor_color = (char_u *)""; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2462 | vim_free(term->tl_cursor_color); |
| 2463 | if (*value->string == NUL) |
| 2464 | term->tl_cursor_color = NULL; |
| 2465 | else |
| 2466 | term->tl_cursor_color = vim_strsave((char_u *)value->string); |
| 2467 | may_set_cursor_props(term); |
| 2468 | break; |
| 2469 | |
| 2470 | case VTERM_PROP_ALTSCREEN: |
| 2471 | /* TODO: do anything else? */ |
| 2472 | term->tl_using_altscreen = value->boolean; |
| 2473 | break; |
| 2474 | |
| 2475 | default: |
| 2476 | break; |
| 2477 | } |
| 2478 | /* Always return 1, otherwise vterm doesn't store the value internally. */ |
| 2479 | return 1; |
| 2480 | } |
| 2481 | |
| 2482 | /* |
| 2483 | * The job running in the terminal resized the terminal. |
| 2484 | */ |
| 2485 | static int |
| 2486 | handle_resize(int rows, int cols, void *user) |
| 2487 | { |
| 2488 | term_T *term = (term_T *)user; |
| 2489 | win_T *wp; |
| 2490 | |
| 2491 | term->tl_rows = rows; |
| 2492 | term->tl_cols = cols; |
| 2493 | if (term->tl_vterm_size_changed) |
| 2494 | /* Size was set by vterm_set_size(), don't set the window size. */ |
| 2495 | term->tl_vterm_size_changed = FALSE; |
| 2496 | else |
| 2497 | { |
| 2498 | FOR_ALL_WINDOWS(wp) |
| 2499 | { |
| 2500 | if (wp->w_buffer == term->tl_buffer) |
| 2501 | { |
| 2502 | win_setheight_win(rows, wp); |
| 2503 | win_setwidth_win(cols, wp); |
| 2504 | } |
| 2505 | } |
| 2506 | redraw_buf_later(term->tl_buffer, NOT_VALID); |
| 2507 | } |
| 2508 | return 1; |
| 2509 | } |
| 2510 | |
| 2511 | /* |
| 2512 | * Handle a line that is pushed off the top of the screen. |
| 2513 | */ |
| 2514 | static int |
| 2515 | handle_pushline(int cols, const VTermScreenCell *cells, void *user) |
| 2516 | { |
| 2517 | term_T *term = (term_T *)user; |
| 2518 | |
| 2519 | /* TODO: Limit the number of lines that are stored. */ |
| 2520 | if (ga_grow(&term->tl_scrollback, 1) == OK) |
| 2521 | { |
| 2522 | cellattr_T *p = NULL; |
| 2523 | int len = 0; |
| 2524 | int i; |
| 2525 | int c; |
| 2526 | int col; |
| 2527 | sb_line_T *line; |
| 2528 | garray_T ga; |
| 2529 | cellattr_T fill_attr = term->tl_default_color; |
| 2530 | |
| 2531 | /* do not store empty cells at the end */ |
| 2532 | for (i = 0; i < cols; ++i) |
| 2533 | if (cells[i].chars[0] != 0) |
| 2534 | len = i + 1; |
| 2535 | else |
| 2536 | cell2cellattr(&cells[i], &fill_attr); |
| 2537 | |
| 2538 | ga_init2(&ga, 1, 100); |
| 2539 | if (len > 0) |
| 2540 | p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len); |
| 2541 | if (p != NULL) |
| 2542 | { |
| 2543 | for (col = 0; col < len; col += cells[col].width) |
| 2544 | { |
| 2545 | if (ga_grow(&ga, MB_MAXBYTES) == FAIL) |
| 2546 | { |
| 2547 | ga.ga_len = 0; |
| 2548 | break; |
| 2549 | } |
| 2550 | for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i) |
| 2551 | ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c, |
| 2552 | (char_u *)ga.ga_data + ga.ga_len); |
| 2553 | cell2cellattr(&cells[col], &p[col]); |
| 2554 | } |
| 2555 | } |
| 2556 | if (ga_grow(&ga, 1) == FAIL) |
| 2557 | add_scrollback_line_to_buffer(term, (char_u *)"", 0); |
| 2558 | else |
| 2559 | { |
| 2560 | *((char_u *)ga.ga_data + ga.ga_len) = NUL; |
| 2561 | add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len); |
| 2562 | } |
| 2563 | ga_clear(&ga); |
| 2564 | |
| 2565 | line = (sb_line_T *)term->tl_scrollback.ga_data |
| 2566 | + term->tl_scrollback.ga_len; |
| 2567 | line->sb_cols = len; |
| 2568 | line->sb_cells = p; |
| 2569 | line->sb_fill_attr = fill_attr; |
| 2570 | ++term->tl_scrollback.ga_len; |
| 2571 | ++term->tl_scrollback_scrolled; |
| 2572 | } |
| 2573 | return 0; /* ignored */ |
| 2574 | } |
| 2575 | |
| 2576 | static VTermScreenCallbacks screen_callbacks = { |
| 2577 | handle_damage, /* damage */ |
| 2578 | handle_moverect, /* moverect */ |
| 2579 | handle_movecursor, /* movecursor */ |
| 2580 | handle_settermprop, /* settermprop */ |
| 2581 | NULL, /* bell */ |
| 2582 | handle_resize, /* resize */ |
| 2583 | handle_pushline, /* sb_pushline */ |
| 2584 | NULL /* sb_popline */ |
| 2585 | }; |
| 2586 | |
| 2587 | /* |
| 2588 | * Called when a channel has been closed. |
| 2589 | * If this was a channel for a terminal window then finish it up. |
| 2590 | */ |
| 2591 | void |
| 2592 | term_channel_closed(channel_T *ch) |
| 2593 | { |
| 2594 | term_T *term; |
| 2595 | int did_one = FALSE; |
| 2596 | |
| 2597 | for (term = first_term; term != NULL; term = term->tl_next) |
| 2598 | if (term->tl_job == ch->ch_job) |
| 2599 | { |
| 2600 | term->tl_channel_closed = TRUE; |
| 2601 | did_one = TRUE; |
| 2602 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 2603 | VIM_CLEAR(term->tl_title); |
| 2604 | VIM_CLEAR(term->tl_status_text); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2605 | |
| 2606 | /* Unless in Terminal-Normal mode: clear the vterm. */ |
| 2607 | if (!term->tl_normal_mode) |
| 2608 | { |
| 2609 | int fnum = term->tl_buffer->b_fnum; |
| 2610 | |
| 2611 | cleanup_vterm(term); |
| 2612 | |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 2613 | if (term->tl_finish == TL_FINISH_CLOSE) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2614 | { |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 2615 | aco_save_T aco; |
| 2616 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2617 | /* ++close or term_finish == "close" */ |
| 2618 | ch_log(NULL, "terminal job finished, closing window"); |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 2619 | aucmd_prepbuf(&aco, term->tl_buffer); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2620 | do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE); |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 2621 | aucmd_restbuf(&aco); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2622 | break; |
| 2623 | } |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 2624 | if (term->tl_finish == TL_FINISH_OPEN |
| 2625 | && term->tl_buffer->b_nwindows == 0) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2626 | { |
| 2627 | char buf[50]; |
| 2628 | |
| 2629 | /* TODO: use term_opencmd */ |
| 2630 | ch_log(NULL, "terminal job finished, opening window"); |
| 2631 | vim_snprintf(buf, sizeof(buf), |
| 2632 | term->tl_opencmd == NULL |
| 2633 | ? "botright sbuf %d" |
| 2634 | : (char *)term->tl_opencmd, fnum); |
| 2635 | do_cmdline_cmd((char_u *)buf); |
| 2636 | } |
| 2637 | else |
| 2638 | ch_log(NULL, "terminal job finished"); |
| 2639 | } |
| 2640 | |
| 2641 | redraw_buf_and_status_later(term->tl_buffer, NOT_VALID); |
| 2642 | } |
| 2643 | if (did_one) |
| 2644 | { |
| 2645 | redraw_statuslines(); |
| 2646 | |
| 2647 | /* Need to break out of vgetc(). */ |
| 2648 | ins_char_typebuf(K_IGNORE); |
| 2649 | typebuf_was_filled = TRUE; |
| 2650 | |
| 2651 | term = curbuf->b_term; |
| 2652 | if (term != NULL) |
| 2653 | { |
| 2654 | if (term->tl_job == ch->ch_job) |
| 2655 | maketitle(); |
| 2656 | update_cursor(term, term->tl_cursor_visible); |
| 2657 | } |
| 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | /* |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 2662 | * Fill one screen line from a line of the terminal. |
| 2663 | * Advances "pos" to past the last column. |
| 2664 | */ |
| 2665 | static void |
| 2666 | term_line2screenline(VTermScreen *screen, VTermPos *pos, int max_col) |
| 2667 | { |
| 2668 | int off = screen_get_current_line_off(); |
| 2669 | |
| 2670 | for (pos->col = 0; pos->col < max_col; ) |
| 2671 | { |
| 2672 | VTermScreenCell cell; |
| 2673 | int c; |
| 2674 | |
| 2675 | if (vterm_screen_get_cell(screen, *pos, &cell) == 0) |
| 2676 | vim_memset(&cell, 0, sizeof(cell)); |
| 2677 | |
| 2678 | c = cell.chars[0]; |
| 2679 | if (c == NUL) |
| 2680 | { |
| 2681 | ScreenLines[off] = ' '; |
| 2682 | if (enc_utf8) |
| 2683 | ScreenLinesUC[off] = NUL; |
| 2684 | } |
| 2685 | else |
| 2686 | { |
| 2687 | if (enc_utf8) |
| 2688 | { |
| 2689 | int i; |
| 2690 | |
| 2691 | /* composing chars */ |
| 2692 | for (i = 0; i < Screen_mco |
| 2693 | && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i) |
| 2694 | { |
| 2695 | ScreenLinesC[i][off] = cell.chars[i + 1]; |
| 2696 | if (cell.chars[i + 1] == 0) |
| 2697 | break; |
| 2698 | } |
| 2699 | if (c >= 0x80 || (Screen_mco > 0 |
| 2700 | && ScreenLinesC[0][off] != 0)) |
| 2701 | { |
| 2702 | ScreenLines[off] = ' '; |
| 2703 | ScreenLinesUC[off] = c; |
| 2704 | } |
| 2705 | else |
| 2706 | { |
| 2707 | ScreenLines[off] = c; |
| 2708 | ScreenLinesUC[off] = NUL; |
| 2709 | } |
| 2710 | } |
| 2711 | #ifdef WIN3264 |
| 2712 | else if (has_mbyte && c >= 0x80) |
| 2713 | { |
| 2714 | char_u mb[MB_MAXBYTES+1]; |
| 2715 | WCHAR wc = c; |
| 2716 | |
| 2717 | if (WideCharToMultiByte(GetACP(), 0, &wc, 1, |
| 2718 | (char*)mb, 2, 0, 0) > 1) |
| 2719 | { |
| 2720 | ScreenLines[off] = mb[0]; |
| 2721 | ScreenLines[off + 1] = mb[1]; |
| 2722 | cell.width = mb_ptr2cells(mb); |
| 2723 | } |
| 2724 | else |
| 2725 | ScreenLines[off] = c; |
| 2726 | } |
| 2727 | #endif |
| 2728 | else |
| 2729 | ScreenLines[off] = c; |
| 2730 | } |
| 2731 | ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg); |
| 2732 | |
| 2733 | ++pos->col; |
| 2734 | ++off; |
| 2735 | if (cell.width == 2) |
| 2736 | { |
| 2737 | if (enc_utf8) |
| 2738 | ScreenLinesUC[off] = NUL; |
| 2739 | |
| 2740 | /* don't set the second byte to NUL for a DBCS encoding, it |
| 2741 | * has been set above */ |
| 2742 | if (enc_utf8 || !has_mbyte) |
| 2743 | ScreenLines[off] = NUL; |
| 2744 | |
| 2745 | ++pos->col; |
| 2746 | ++off; |
| 2747 | } |
| 2748 | } |
| 2749 | } |
| 2750 | |
Bram Moolenaar | 4ac31ee | 2018-03-16 21:34:25 +0100 | [diff] [blame] | 2751 | #if defined(FEAT_GUI) |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 2752 | static void |
| 2753 | update_system_term(term_T *term) |
| 2754 | { |
| 2755 | VTermPos pos; |
| 2756 | VTermScreen *screen; |
| 2757 | |
| 2758 | if (term->tl_vterm == NULL) |
| 2759 | return; |
| 2760 | screen = vterm_obtain_screen(term->tl_vterm); |
| 2761 | |
| 2762 | /* Scroll up to make more room for terminal lines if needed. */ |
| 2763 | while (term->tl_toprow > 0 |
| 2764 | && (Rows - term->tl_toprow) < term->tl_dirty_row_end) |
| 2765 | { |
| 2766 | int save_p_more = p_more; |
| 2767 | |
| 2768 | p_more = FALSE; |
| 2769 | msg_row = Rows - 1; |
| 2770 | msg_puts((char_u *)"\n"); |
| 2771 | p_more = save_p_more; |
| 2772 | --term->tl_toprow; |
| 2773 | } |
| 2774 | |
| 2775 | for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end |
| 2776 | && pos.row < Rows; ++pos.row) |
| 2777 | { |
| 2778 | if (pos.row < term->tl_rows) |
| 2779 | { |
| 2780 | int max_col = MIN(Columns, term->tl_cols); |
| 2781 | |
| 2782 | term_line2screenline(screen, &pos, max_col); |
| 2783 | } |
| 2784 | else |
| 2785 | pos.col = 0; |
| 2786 | |
| 2787 | screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, FALSE); |
| 2788 | } |
| 2789 | |
| 2790 | term->tl_dirty_row_start = MAX_ROW; |
| 2791 | term->tl_dirty_row_end = 0; |
| 2792 | update_cursor(term, TRUE); |
| 2793 | } |
Bram Moolenaar | 4ac31ee | 2018-03-16 21:34:25 +0100 | [diff] [blame] | 2794 | #endif |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 2795 | |
| 2796 | /* |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2797 | * Called to update a window that contains an active terminal. |
| 2798 | * Returns FAIL when there is no terminal running in this window or in |
| 2799 | * Terminal-Normal mode. |
| 2800 | */ |
| 2801 | int |
| 2802 | term_update_window(win_T *wp) |
| 2803 | { |
| 2804 | term_T *term = wp->w_buffer->b_term; |
| 2805 | VTerm *vterm; |
| 2806 | VTermScreen *screen; |
| 2807 | VTermState *state; |
| 2808 | VTermPos pos; |
| 2809 | |
| 2810 | if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode) |
| 2811 | return FAIL; |
| 2812 | |
| 2813 | vterm = term->tl_vterm; |
| 2814 | screen = vterm_obtain_screen(vterm); |
| 2815 | state = vterm_obtain_state(vterm); |
| 2816 | |
Bram Moolenaar | 54e5dbf | 2017-10-07 17:35:09 +0200 | [diff] [blame] | 2817 | if (wp->w_redr_type >= SOME_VALID) |
Bram Moolenaar | 19a3d68 | 2017-10-02 21:54:59 +0200 | [diff] [blame] | 2818 | { |
| 2819 | term->tl_dirty_row_start = 0; |
| 2820 | term->tl_dirty_row_end = MAX_ROW; |
| 2821 | } |
| 2822 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2823 | /* |
| 2824 | * If the window was resized a redraw will be triggered and we get here. |
| 2825 | * Adjust the size of the vterm unless 'termsize' specifies a fixed size. |
| 2826 | */ |
| 2827 | if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height) |
| 2828 | || (!term->tl_cols_fixed && term->tl_cols != wp->w_width)) |
| 2829 | { |
| 2830 | int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height; |
| 2831 | int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width; |
| 2832 | win_T *twp; |
| 2833 | |
| 2834 | FOR_ALL_WINDOWS(twp) |
| 2835 | { |
| 2836 | /* When more than one window shows the same terminal, use the |
| 2837 | * smallest size. */ |
| 2838 | if (twp->w_buffer == term->tl_buffer) |
| 2839 | { |
| 2840 | if (!term->tl_rows_fixed && rows > twp->w_height) |
| 2841 | rows = twp->w_height; |
| 2842 | if (!term->tl_cols_fixed && cols > twp->w_width) |
| 2843 | cols = twp->w_width; |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | term->tl_vterm_size_changed = TRUE; |
| 2848 | vterm_set_size(vterm, rows, cols); |
| 2849 | ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines", |
| 2850 | rows); |
| 2851 | term_report_winsize(term, rows, cols); |
| 2852 | } |
| 2853 | |
| 2854 | /* The cursor may have been moved when resizing. */ |
| 2855 | vterm_state_get_cursorpos(state, &pos); |
| 2856 | position_cursor(wp, &pos); |
| 2857 | |
Bram Moolenaar | 3a497e1 | 2017-09-30 20:40:27 +0200 | [diff] [blame] | 2858 | for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end |
| 2859 | && pos.row < wp->w_height; ++pos.row) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2860 | { |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2861 | if (pos.row < term->tl_rows) |
| 2862 | { |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 2863 | int max_col = MIN(wp->w_width, term->tl_cols); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2864 | |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 2865 | term_line2screenline(screen, &pos, max_col); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2866 | } |
| 2867 | else |
| 2868 | pos.col = 0; |
| 2869 | |
Bram Moolenaar | f118d48 | 2018-03-13 13:14:00 +0100 | [diff] [blame] | 2870 | screen_line(wp->w_winrow + pos.row |
| 2871 | #ifdef FEAT_MENU |
| 2872 | + winbar_height(wp) |
| 2873 | #endif |
| 2874 | , wp->w_wincol, pos.col, wp->w_width, FALSE); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2875 | } |
Bram Moolenaar | 3a497e1 | 2017-09-30 20:40:27 +0200 | [diff] [blame] | 2876 | term->tl_dirty_row_start = MAX_ROW; |
| 2877 | term->tl_dirty_row_end = 0; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2878 | |
| 2879 | return OK; |
| 2880 | } |
| 2881 | |
| 2882 | /* |
| 2883 | * Return TRUE if "wp" is a terminal window where the job has finished. |
| 2884 | */ |
| 2885 | int |
| 2886 | term_is_finished(buf_T *buf) |
| 2887 | { |
| 2888 | return buf->b_term != NULL && buf->b_term->tl_vterm == NULL; |
| 2889 | } |
| 2890 | |
| 2891 | /* |
| 2892 | * Return TRUE if "wp" is a terminal window where the job has finished or we |
| 2893 | * are in Terminal-Normal mode, thus we show the buffer contents. |
| 2894 | */ |
| 2895 | int |
| 2896 | term_show_buffer(buf_T *buf) |
| 2897 | { |
| 2898 | term_T *term = buf->b_term; |
| 2899 | |
| 2900 | return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode); |
| 2901 | } |
| 2902 | |
| 2903 | /* |
| 2904 | * The current buffer is going to be changed. If there is terminal |
| 2905 | * highlighting remove it now. |
| 2906 | */ |
| 2907 | void |
| 2908 | term_change_in_curbuf(void) |
| 2909 | { |
| 2910 | term_T *term = curbuf->b_term; |
| 2911 | |
| 2912 | if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0) |
| 2913 | { |
| 2914 | free_scrollback(term); |
| 2915 | redraw_buf_later(term->tl_buffer, NOT_VALID); |
| 2916 | |
| 2917 | /* The buffer is now like a normal buffer, it cannot be easily |
| 2918 | * abandoned when changed. */ |
| 2919 | set_string_option_direct((char_u *)"buftype", -1, |
| 2920 | (char_u *)"", OPT_FREE|OPT_LOCAL, 0); |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | /* |
| 2925 | * Get the screen attribute for a position in the buffer. |
| 2926 | * Use a negative "col" to get the filler background color. |
| 2927 | */ |
| 2928 | int |
| 2929 | term_get_attr(buf_T *buf, linenr_T lnum, int col) |
| 2930 | { |
| 2931 | term_T *term = buf->b_term; |
| 2932 | sb_line_T *line; |
| 2933 | cellattr_T *cellattr; |
| 2934 | |
| 2935 | if (lnum > term->tl_scrollback.ga_len) |
| 2936 | cellattr = &term->tl_default_color; |
| 2937 | else |
| 2938 | { |
| 2939 | line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1; |
| 2940 | if (col < 0 || col >= line->sb_cols) |
| 2941 | cellattr = &line->sb_fill_attr; |
| 2942 | else |
| 2943 | cellattr = line->sb_cells + col; |
| 2944 | } |
| 2945 | return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg); |
| 2946 | } |
| 2947 | |
| 2948 | static VTermColor ansi_table[16] = { |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 2949 | { 0, 0, 0, 1}, /* black */ |
| 2950 | {224, 0, 0, 2}, /* dark red */ |
| 2951 | { 0, 224, 0, 3}, /* dark green */ |
| 2952 | {224, 224, 0, 4}, /* dark yellow / brown */ |
| 2953 | { 0, 0, 224, 5}, /* dark blue */ |
| 2954 | {224, 0, 224, 6}, /* dark magenta */ |
| 2955 | { 0, 224, 224, 7}, /* dark cyan */ |
| 2956 | {224, 224, 224, 8}, /* light grey */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2957 | |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 2958 | {128, 128, 128, 9}, /* dark grey */ |
| 2959 | {255, 64, 64, 10}, /* light red */ |
| 2960 | { 64, 255, 64, 11}, /* light green */ |
| 2961 | {255, 255, 64, 12}, /* yellow */ |
| 2962 | { 64, 64, 255, 13}, /* light blue */ |
| 2963 | {255, 64, 255, 14}, /* light magenta */ |
| 2964 | { 64, 255, 255, 15}, /* light cyan */ |
| 2965 | {255, 255, 255, 16}, /* white */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2966 | }; |
| 2967 | |
| 2968 | static int cube_value[] = { |
Bram Moolenaar | a8fc0d3 | 2017-09-26 13:59:47 +0200 | [diff] [blame] | 2969 | 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2970 | }; |
| 2971 | |
| 2972 | static int grey_ramp[] = { |
Bram Moolenaar | a8fc0d3 | 2017-09-26 13:59:47 +0200 | [diff] [blame] | 2973 | 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76, |
| 2974 | 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2975 | }; |
| 2976 | |
| 2977 | /* |
| 2978 | * Convert a cterm color number 0 - 255 to RGB. |
Bram Moolenaar | a8fc0d3 | 2017-09-26 13:59:47 +0200 | [diff] [blame] | 2979 | * This is compatible with xterm. |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2980 | */ |
| 2981 | static void |
| 2982 | cterm_color2rgb(int nr, VTermColor *rgb) |
| 2983 | { |
| 2984 | int idx; |
| 2985 | |
| 2986 | if (nr < 16) |
| 2987 | { |
| 2988 | *rgb = ansi_table[nr]; |
| 2989 | } |
| 2990 | else if (nr < 232) |
| 2991 | { |
| 2992 | /* 216 color cube */ |
| 2993 | idx = nr - 16; |
| 2994 | rgb->blue = cube_value[idx % 6]; |
| 2995 | rgb->green = cube_value[idx / 6 % 6]; |
| 2996 | rgb->red = cube_value[idx / 36 % 6]; |
Bram Moolenaar | a7c54cf | 2017-12-01 21:07:20 +0100 | [diff] [blame] | 2997 | rgb->ansi_index = VTERM_ANSI_INDEX_NONE; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 2998 | } |
| 2999 | else if (nr < 256) |
| 3000 | { |
| 3001 | /* 24 grey scale ramp */ |
| 3002 | idx = nr - 232; |
| 3003 | rgb->blue = grey_ramp[idx]; |
| 3004 | rgb->green = grey_ramp[idx]; |
| 3005 | rgb->red = grey_ramp[idx]; |
Bram Moolenaar | a7c54cf | 2017-12-01 21:07:20 +0100 | [diff] [blame] | 3006 | rgb->ansi_index = VTERM_ANSI_INDEX_NONE; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3007 | } |
| 3008 | } |
| 3009 | |
| 3010 | /* |
Bram Moolenaar | 52acb11 | 2018-03-18 19:20:22 +0100 | [diff] [blame] | 3011 | * Initialize term->tl_default_color from the environment. |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3012 | */ |
| 3013 | static void |
Bram Moolenaar | 52acb11 | 2018-03-18 19:20:22 +0100 | [diff] [blame] | 3014 | init_default_colors(term_T *term) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3015 | { |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3016 | VTermColor *fg, *bg; |
| 3017 | int fgval, bgval; |
| 3018 | int id; |
| 3019 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3020 | vim_memset(&term->tl_default_color.attrs, 0, sizeof(VTermScreenCellAttrs)); |
| 3021 | term->tl_default_color.width = 1; |
| 3022 | fg = &term->tl_default_color.fg; |
| 3023 | bg = &term->tl_default_color.bg; |
| 3024 | |
| 3025 | /* Vterm uses a default black background. Set it to white when |
| 3026 | * 'background' is "light". */ |
| 3027 | if (*p_bg == 'l') |
| 3028 | { |
| 3029 | fgval = 0; |
| 3030 | bgval = 255; |
| 3031 | } |
| 3032 | else |
| 3033 | { |
| 3034 | fgval = 255; |
| 3035 | bgval = 0; |
| 3036 | } |
| 3037 | fg->red = fg->green = fg->blue = fgval; |
| 3038 | bg->red = bg->green = bg->blue = bgval; |
Bram Moolenaar | a7c54cf | 2017-12-01 21:07:20 +0100 | [diff] [blame] | 3039 | fg->ansi_index = bg->ansi_index = VTERM_ANSI_INDEX_DEFAULT; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3040 | |
| 3041 | /* The "Terminal" highlight group overrules the defaults. */ |
| 3042 | id = syn_name2id((char_u *)"Terminal"); |
| 3043 | |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 3044 | /* Use the actual color for the GUI and when 'termguicolors' is set. */ |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3045 | #if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) |
| 3046 | if (0 |
| 3047 | # ifdef FEAT_GUI |
| 3048 | || gui.in_use |
| 3049 | # endif |
| 3050 | # ifdef FEAT_TERMGUICOLORS |
| 3051 | || p_tgc |
| 3052 | # endif |
| 3053 | ) |
| 3054 | { |
| 3055 | guicolor_T fg_rgb = INVALCOLOR; |
| 3056 | guicolor_T bg_rgb = INVALCOLOR; |
| 3057 | |
| 3058 | if (id != 0) |
| 3059 | syn_id2colors(id, &fg_rgb, &bg_rgb); |
| 3060 | |
| 3061 | # ifdef FEAT_GUI |
| 3062 | if (gui.in_use) |
| 3063 | { |
| 3064 | if (fg_rgb == INVALCOLOR) |
| 3065 | fg_rgb = gui.norm_pixel; |
| 3066 | if (bg_rgb == INVALCOLOR) |
| 3067 | bg_rgb = gui.back_pixel; |
| 3068 | } |
| 3069 | # ifdef FEAT_TERMGUICOLORS |
| 3070 | else |
| 3071 | # endif |
| 3072 | # endif |
| 3073 | # ifdef FEAT_TERMGUICOLORS |
| 3074 | { |
| 3075 | if (fg_rgb == INVALCOLOR) |
| 3076 | fg_rgb = cterm_normal_fg_gui_color; |
| 3077 | if (bg_rgb == INVALCOLOR) |
| 3078 | bg_rgb = cterm_normal_bg_gui_color; |
| 3079 | } |
| 3080 | # endif |
| 3081 | if (fg_rgb != INVALCOLOR) |
| 3082 | { |
| 3083 | long_u rgb = GUI_MCH_GET_RGB(fg_rgb); |
| 3084 | |
| 3085 | fg->red = (unsigned)(rgb >> 16); |
| 3086 | fg->green = (unsigned)(rgb >> 8) & 255; |
| 3087 | fg->blue = (unsigned)rgb & 255; |
| 3088 | } |
| 3089 | if (bg_rgb != INVALCOLOR) |
| 3090 | { |
| 3091 | long_u rgb = GUI_MCH_GET_RGB(bg_rgb); |
| 3092 | |
| 3093 | bg->red = (unsigned)(rgb >> 16); |
| 3094 | bg->green = (unsigned)(rgb >> 8) & 255; |
| 3095 | bg->blue = (unsigned)rgb & 255; |
| 3096 | } |
| 3097 | } |
| 3098 | else |
| 3099 | #endif |
| 3100 | if (id != 0 && t_colors >= 16) |
| 3101 | { |
Bram Moolenaar | a7c54cf | 2017-12-01 21:07:20 +0100 | [diff] [blame] | 3102 | if (term_default_cterm_fg >= 0) |
| 3103 | cterm_color2rgb(term_default_cterm_fg, fg); |
| 3104 | if (term_default_cterm_bg >= 0) |
| 3105 | cterm_color2rgb(term_default_cterm_bg, bg); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3106 | } |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3107 | else |
| 3108 | { |
Bram Moolenaar | 65e4c4f | 2017-10-14 23:24:25 +0200 | [diff] [blame] | 3109 | #if defined(WIN3264) && !defined(FEAT_GUI_W32) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3110 | int tmp; |
Bram Moolenaar | 65e4c4f | 2017-10-14 23:24:25 +0200 | [diff] [blame] | 3111 | #endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3112 | |
| 3113 | /* In an MS-Windows console we know the normal colors. */ |
| 3114 | if (cterm_normal_fg_color > 0) |
| 3115 | { |
| 3116 | cterm_color2rgb(cterm_normal_fg_color - 1, fg); |
Bram Moolenaar | 65e4c4f | 2017-10-14 23:24:25 +0200 | [diff] [blame] | 3117 | # if defined(WIN3264) && !defined(FEAT_GUI_W32) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3118 | tmp = fg->red; |
| 3119 | fg->red = fg->blue; |
| 3120 | fg->blue = tmp; |
Bram Moolenaar | 65e4c4f | 2017-10-14 23:24:25 +0200 | [diff] [blame] | 3121 | # endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3122 | } |
Bram Moolenaar | 9377df3 | 2017-10-15 13:22:01 +0200 | [diff] [blame] | 3123 | # ifdef FEAT_TERMRESPONSE |
Bram Moolenaar | 65e4c4f | 2017-10-14 23:24:25 +0200 | [diff] [blame] | 3124 | else |
| 3125 | term_get_fg_color(&fg->red, &fg->green, &fg->blue); |
Bram Moolenaar | 9377df3 | 2017-10-15 13:22:01 +0200 | [diff] [blame] | 3126 | # endif |
Bram Moolenaar | 65e4c4f | 2017-10-14 23:24:25 +0200 | [diff] [blame] | 3127 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3128 | if (cterm_normal_bg_color > 0) |
| 3129 | { |
| 3130 | cterm_color2rgb(cterm_normal_bg_color - 1, bg); |
Bram Moolenaar | 65e4c4f | 2017-10-14 23:24:25 +0200 | [diff] [blame] | 3131 | # if defined(WIN3264) && !defined(FEAT_GUI_W32) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3132 | tmp = bg->red; |
| 3133 | bg->red = bg->blue; |
| 3134 | bg->blue = tmp; |
Bram Moolenaar | 65e4c4f | 2017-10-14 23:24:25 +0200 | [diff] [blame] | 3135 | # endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3136 | } |
Bram Moolenaar | 9377df3 | 2017-10-15 13:22:01 +0200 | [diff] [blame] | 3137 | # ifdef FEAT_TERMRESPONSE |
Bram Moolenaar | 65e4c4f | 2017-10-14 23:24:25 +0200 | [diff] [blame] | 3138 | else |
| 3139 | term_get_bg_color(&bg->red, &bg->green, &bg->blue); |
Bram Moolenaar | 9377df3 | 2017-10-15 13:22:01 +0200 | [diff] [blame] | 3140 | # endif |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3141 | } |
Bram Moolenaar | 52acb11 | 2018-03-18 19:20:22 +0100 | [diff] [blame] | 3142 | } |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3143 | |
Bram Moolenaar | 52acb11 | 2018-03-18 19:20:22 +0100 | [diff] [blame] | 3144 | /* |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 3145 | * Handles a "drop" command from the job in the terminal. |
| 3146 | * "item" is the file name, "item->li_next" may have options. |
| 3147 | */ |
| 3148 | static void |
| 3149 | handle_drop_command(listitem_T *item) |
| 3150 | { |
| 3151 | char_u *fname = get_tv_string(&item->li_tv); |
Bram Moolenaar | 333b80a | 2018-04-04 22:57:29 +0200 | [diff] [blame] | 3152 | listitem_T *opt_item = item->li_next; |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 3153 | int bufnr; |
| 3154 | win_T *wp; |
| 3155 | tabpage_T *tp; |
| 3156 | exarg_T ea; |
Bram Moolenaar | 333b80a | 2018-04-04 22:57:29 +0200 | [diff] [blame] | 3157 | char_u *tofree = NULL; |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 3158 | |
| 3159 | bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT); |
| 3160 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 3161 | { |
| 3162 | if (wp->w_buffer->b_fnum == bufnr) |
| 3163 | { |
| 3164 | /* buffer is in a window already, go there */ |
| 3165 | goto_tabpage_win(tp, wp); |
| 3166 | return; |
| 3167 | } |
| 3168 | } |
| 3169 | |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 3170 | vim_memset(&ea, 0, sizeof(ea)); |
Bram Moolenaar | 333b80a | 2018-04-04 22:57:29 +0200 | [diff] [blame] | 3171 | |
| 3172 | if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT |
| 3173 | && opt_item->li_tv.vval.v_dict != NULL) |
| 3174 | { |
| 3175 | dict_T *dict = opt_item->li_tv.vval.v_dict; |
| 3176 | char_u *p; |
| 3177 | |
| 3178 | p = get_dict_string(dict, (char_u *)"ff", FALSE); |
| 3179 | if (p == NULL) |
| 3180 | p = get_dict_string(dict, (char_u *)"fileformat", FALSE); |
| 3181 | if (p != NULL) |
| 3182 | { |
| 3183 | if (check_ff_value(p) == FAIL) |
| 3184 | ch_log(NULL, "Invalid ff argument to drop: %s", p); |
| 3185 | else |
| 3186 | ea.force_ff = *p; |
| 3187 | } |
| 3188 | p = get_dict_string(dict, (char_u *)"enc", FALSE); |
| 3189 | if (p == NULL) |
| 3190 | p = get_dict_string(dict, (char_u *)"encoding", FALSE); |
| 3191 | if (p != NULL) |
| 3192 | { |
Bram Moolenaar | 3aa67fb | 2018-04-05 21:04:15 +0200 | [diff] [blame] | 3193 | ea.cmd = alloc((int)STRLEN(p) + 12); |
Bram Moolenaar | 333b80a | 2018-04-04 22:57:29 +0200 | [diff] [blame] | 3194 | if (ea.cmd != NULL) |
| 3195 | { |
| 3196 | sprintf((char *)ea.cmd, "sbuf ++enc=%s", p); |
| 3197 | ea.force_enc = 11; |
| 3198 | tofree = ea.cmd; |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | p = get_dict_string(dict, (char_u *)"bad", FALSE); |
| 3203 | if (p != NULL) |
| 3204 | get_bad_opt(p, &ea); |
| 3205 | |
| 3206 | if (dict_find(dict, (char_u *)"bin", -1) != NULL) |
| 3207 | ea.force_bin = FORCE_BIN; |
| 3208 | if (dict_find(dict, (char_u *)"binary", -1) != NULL) |
| 3209 | ea.force_bin = FORCE_BIN; |
| 3210 | if (dict_find(dict, (char_u *)"nobin", -1) != NULL) |
| 3211 | ea.force_bin = FORCE_NOBIN; |
| 3212 | if (dict_find(dict, (char_u *)"nobinary", -1) != NULL) |
| 3213 | ea.force_bin = FORCE_NOBIN; |
| 3214 | } |
| 3215 | |
| 3216 | /* open in new window, like ":split fname" */ |
| 3217 | if (ea.cmd == NULL) |
| 3218 | ea.cmd = (char_u *)"split"; |
| 3219 | ea.arg = fname; |
| 3220 | ea.cmdidx = CMD_split; |
| 3221 | ex_splitview(&ea); |
| 3222 | |
| 3223 | vim_free(tofree); |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | /* |
| 3227 | * Handles a function call from the job running in a terminal. |
| 3228 | * "item" is the function name, "item->li_next" has the arguments. |
| 3229 | */ |
| 3230 | static void |
| 3231 | handle_call_command(term_T *term, channel_T *channel, listitem_T *item) |
| 3232 | { |
| 3233 | char_u *func; |
| 3234 | typval_T argvars[2]; |
| 3235 | typval_T rettv; |
| 3236 | int doesrange; |
| 3237 | |
| 3238 | if (item->li_next == NULL) |
| 3239 | { |
| 3240 | ch_log(channel, "Missing function arguments for call"); |
| 3241 | return; |
| 3242 | } |
| 3243 | func = get_tv_string(&item->li_tv); |
| 3244 | |
Bram Moolenaar | 2a77d21 | 2018-03-26 21:38:52 +0200 | [diff] [blame] | 3245 | if (STRNCMP(func, "Tapi_", 5) != 0) |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 3246 | { |
| 3247 | ch_log(channel, "Invalid function name: %s", func); |
| 3248 | return; |
| 3249 | } |
| 3250 | |
| 3251 | argvars[0].v_type = VAR_NUMBER; |
| 3252 | argvars[0].vval.v_number = term->tl_buffer->b_fnum; |
| 3253 | argvars[1] = item->li_next->li_tv; |
Bram Moolenaar | 878c96d | 2018-04-04 23:00:06 +0200 | [diff] [blame] | 3254 | if (call_func(func, (int)STRLEN(func), &rettv, |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 3255 | 2, argvars, /* argv_func */ NULL, |
| 3256 | /* firstline */ 1, /* lastline */ 1, |
| 3257 | &doesrange, /* evaluate */ TRUE, |
| 3258 | /* partial */ NULL, /* selfdict */ NULL) == OK) |
| 3259 | { |
| 3260 | clear_tv(&rettv); |
| 3261 | ch_log(channel, "Function %s called", func); |
| 3262 | } |
| 3263 | else |
| 3264 | ch_log(channel, "Calling function %s failed", func); |
| 3265 | } |
| 3266 | |
| 3267 | /* |
| 3268 | * Called by libvterm when it cannot recognize an OSC sequence. |
| 3269 | * We recognize a terminal API command. |
| 3270 | */ |
| 3271 | static int |
| 3272 | parse_osc(const char *command, size_t cmdlen, void *user) |
| 3273 | { |
| 3274 | term_T *term = (term_T *)user; |
| 3275 | js_read_T reader; |
| 3276 | typval_T tv; |
| 3277 | channel_T *channel = term->tl_job == NULL ? NULL |
| 3278 | : term->tl_job->jv_channel; |
| 3279 | |
| 3280 | /* We recognize only OSC 5 1 ; {command} */ |
| 3281 | if (cmdlen < 3 || STRNCMP(command, "51;", 3) != 0) |
| 3282 | return 0; /* not handled */ |
| 3283 | |
Bram Moolenaar | 878c96d | 2018-04-04 23:00:06 +0200 | [diff] [blame] | 3284 | reader.js_buf = vim_strnsave((char_u *)command + 3, (int)(cmdlen - 3)); |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 3285 | if (reader.js_buf == NULL) |
| 3286 | return 1; |
| 3287 | reader.js_fill = NULL; |
| 3288 | reader.js_used = 0; |
| 3289 | if (json_decode(&reader, &tv, 0) == OK |
| 3290 | && tv.v_type == VAR_LIST |
| 3291 | && tv.vval.v_list != NULL) |
| 3292 | { |
| 3293 | listitem_T *item = tv.vval.v_list->lv_first; |
| 3294 | |
| 3295 | if (item == NULL) |
| 3296 | ch_log(channel, "Missing command"); |
| 3297 | else |
| 3298 | { |
| 3299 | char_u *cmd = get_tv_string(&item->li_tv); |
| 3300 | |
| 3301 | item = item->li_next; |
| 3302 | if (item == NULL) |
| 3303 | ch_log(channel, "Missing argument for %s", cmd); |
| 3304 | else if (STRCMP(cmd, "drop") == 0) |
| 3305 | handle_drop_command(item); |
| 3306 | else if (STRCMP(cmd, "call") == 0) |
| 3307 | handle_call_command(term, channel, item); |
| 3308 | else |
| 3309 | ch_log(channel, "Invalid command received: %s", cmd); |
| 3310 | } |
| 3311 | } |
| 3312 | else |
| 3313 | ch_log(channel, "Invalid JSON received"); |
| 3314 | |
| 3315 | vim_free(reader.js_buf); |
| 3316 | clear_tv(&tv); |
| 3317 | return 1; |
| 3318 | } |
| 3319 | |
| 3320 | static VTermParserCallbacks parser_fallbacks = { |
| 3321 | NULL, /* text */ |
| 3322 | NULL, /* control */ |
| 3323 | NULL, /* escape */ |
| 3324 | NULL, /* csi */ |
| 3325 | parse_osc, /* osc */ |
| 3326 | NULL, /* dcs */ |
| 3327 | NULL /* resize */ |
| 3328 | }; |
| 3329 | |
| 3330 | /* |
Bram Moolenaar | 756ef11 | 2018-04-10 12:04:27 +0200 | [diff] [blame] | 3331 | * Use Vim's allocation functions for vterm so profiling works. |
| 3332 | */ |
| 3333 | static void * |
| 3334 | vterm_malloc(size_t size, void *data UNUSED) |
| 3335 | { |
| 3336 | return alloc_clear(size); |
| 3337 | } |
| 3338 | |
| 3339 | static void |
| 3340 | vterm_memfree(void *ptr, void *data UNUSED) |
| 3341 | { |
| 3342 | vim_free(ptr); |
| 3343 | } |
| 3344 | |
| 3345 | static VTermAllocatorFunctions vterm_allocator = { |
| 3346 | &vterm_malloc, |
| 3347 | &vterm_memfree |
| 3348 | }; |
| 3349 | |
| 3350 | /* |
Bram Moolenaar | 52acb11 | 2018-03-18 19:20:22 +0100 | [diff] [blame] | 3351 | * Create a new vterm and initialize it. |
| 3352 | */ |
| 3353 | static void |
| 3354 | create_vterm(term_T *term, int rows, int cols) |
| 3355 | { |
| 3356 | VTerm *vterm; |
| 3357 | VTermScreen *screen; |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 3358 | VTermState *state; |
Bram Moolenaar | 52acb11 | 2018-03-18 19:20:22 +0100 | [diff] [blame] | 3359 | VTermValue value; |
| 3360 | |
Bram Moolenaar | 756ef11 | 2018-04-10 12:04:27 +0200 | [diff] [blame] | 3361 | vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL); |
Bram Moolenaar | 52acb11 | 2018-03-18 19:20:22 +0100 | [diff] [blame] | 3362 | term->tl_vterm = vterm; |
| 3363 | screen = vterm_obtain_screen(vterm); |
| 3364 | vterm_screen_set_callbacks(screen, &screen_callbacks, term); |
| 3365 | /* TODO: depends on 'encoding'. */ |
| 3366 | vterm_set_utf8(vterm, 1); |
| 3367 | |
| 3368 | init_default_colors(term); |
| 3369 | |
| 3370 | vterm_state_set_default_colors( |
| 3371 | vterm_obtain_state(vterm), |
| 3372 | &term->tl_default_color.fg, |
| 3373 | &term->tl_default_color.bg); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3374 | |
| 3375 | /* Required to initialize most things. */ |
| 3376 | vterm_screen_reset(screen, 1 /* hard */); |
| 3377 | |
| 3378 | /* Allow using alternate screen. */ |
| 3379 | vterm_screen_enable_altscreen(screen, 1); |
| 3380 | |
| 3381 | /* For unix do not use a blinking cursor. In an xterm this causes the |
| 3382 | * cursor to blink if it's blinking in the xterm. |
| 3383 | * For Windows we respect the system wide setting. */ |
| 3384 | #ifdef WIN3264 |
| 3385 | if (GetCaretBlinkTime() == INFINITE) |
| 3386 | value.boolean = 0; |
| 3387 | else |
| 3388 | value.boolean = 1; |
| 3389 | #else |
| 3390 | value.boolean = 0; |
| 3391 | #endif |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 3392 | state = vterm_obtain_state(vterm); |
| 3393 | vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value); |
| 3394 | vterm_state_set_unrecognised_fallbacks(state, &parser_fallbacks, term); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | /* |
| 3398 | * Return the text to show for the buffer name and status. |
| 3399 | */ |
| 3400 | char_u * |
| 3401 | term_get_status_text(term_T *term) |
| 3402 | { |
| 3403 | if (term->tl_status_text == NULL) |
| 3404 | { |
| 3405 | char_u *txt; |
| 3406 | size_t len; |
| 3407 | |
| 3408 | if (term->tl_normal_mode) |
| 3409 | { |
| 3410 | if (term_job_running(term)) |
| 3411 | txt = (char_u *)_("Terminal"); |
| 3412 | else |
| 3413 | txt = (char_u *)_("Terminal-finished"); |
| 3414 | } |
| 3415 | else if (term->tl_title != NULL) |
| 3416 | txt = term->tl_title; |
| 3417 | else if (term_none_open(term)) |
| 3418 | txt = (char_u *)_("active"); |
| 3419 | else if (term_job_running(term)) |
| 3420 | txt = (char_u *)_("running"); |
| 3421 | else |
| 3422 | txt = (char_u *)_("finished"); |
| 3423 | len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt); |
| 3424 | term->tl_status_text = alloc((int)len); |
| 3425 | if (term->tl_status_text != NULL) |
| 3426 | vim_snprintf((char *)term->tl_status_text, len, "%s [%s]", |
| 3427 | term->tl_buffer->b_fname, txt); |
| 3428 | } |
| 3429 | return term->tl_status_text; |
| 3430 | } |
| 3431 | |
| 3432 | /* |
| 3433 | * Mark references in jobs of terminals. |
| 3434 | */ |
| 3435 | int |
| 3436 | set_ref_in_term(int copyID) |
| 3437 | { |
| 3438 | int abort = FALSE; |
| 3439 | term_T *term; |
| 3440 | typval_T tv; |
| 3441 | |
| 3442 | for (term = first_term; term != NULL; term = term->tl_next) |
| 3443 | if (term->tl_job != NULL) |
| 3444 | { |
| 3445 | tv.v_type = VAR_JOB; |
| 3446 | tv.vval.v_job = term->tl_job; |
| 3447 | abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL); |
| 3448 | } |
| 3449 | return abort; |
| 3450 | } |
| 3451 | |
| 3452 | /* |
Bram Moolenaar | a7c54cf | 2017-12-01 21:07:20 +0100 | [diff] [blame] | 3453 | * Cache "Terminal" highlight group colors. |
| 3454 | */ |
| 3455 | void |
| 3456 | set_terminal_default_colors(int cterm_fg, int cterm_bg) |
| 3457 | { |
| 3458 | term_default_cterm_fg = cterm_fg - 1; |
| 3459 | term_default_cterm_bg = cterm_bg - 1; |
| 3460 | } |
| 3461 | |
| 3462 | /* |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3463 | * Get the buffer from the first argument in "argvars". |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 3464 | * Returns NULL when the buffer is not for a terminal window and logs a message |
| 3465 | * with "where". |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3466 | */ |
| 3467 | static buf_T * |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 3468 | term_get_buf(typval_T *argvars, char *where) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3469 | { |
| 3470 | buf_T *buf; |
| 3471 | |
| 3472 | (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ |
| 3473 | ++emsg_off; |
| 3474 | buf = get_buf_tv(&argvars[0], FALSE); |
| 3475 | --emsg_off; |
| 3476 | if (buf == NULL || buf->b_term == NULL) |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 3477 | { |
| 3478 | ch_log(NULL, "%s: invalid buffer argument", where); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3479 | return NULL; |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 3480 | } |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 3481 | return buf; |
| 3482 | } |
| 3483 | |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3484 | static int |
| 3485 | same_color(VTermColor *a, VTermColor *b) |
| 3486 | { |
| 3487 | return a->red == b->red |
| 3488 | && a->green == b->green |
| 3489 | && a->blue == b->blue |
| 3490 | && a->ansi_index == b->ansi_index; |
| 3491 | } |
| 3492 | |
| 3493 | static void |
| 3494 | dump_term_color(FILE *fd, VTermColor *color) |
| 3495 | { |
| 3496 | fprintf(fd, "%02x%02x%02x%d", |
| 3497 | (int)color->red, (int)color->green, (int)color->blue, |
| 3498 | (int)color->ansi_index); |
| 3499 | } |
| 3500 | |
| 3501 | /* |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 3502 | * "term_dumpwrite(buf, filename, options)" function |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3503 | * |
| 3504 | * Each screen cell in full is: |
| 3505 | * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx} |
| 3506 | * {characters} is a space for an empty cell |
| 3507 | * For a double-width character "+" is changed to "*" and the next cell is |
| 3508 | * skipped. |
| 3509 | * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc. |
| 3510 | * when "&" use the same as the previous cell. |
| 3511 | * {fg-color} is hex RGB, when "&" use the same as the previous cell. |
| 3512 | * {bg-color} is hex RGB, when "&" use the same as the previous cell. |
| 3513 | * {color-idx} is a number from 0 to 255 |
| 3514 | * |
| 3515 | * Screen cell with same width, attributes and color as the previous one: |
| 3516 | * |{characters} |
| 3517 | * |
| 3518 | * To use the color of the previous cell, use "&" instead of {color}-{idx}. |
| 3519 | * |
| 3520 | * Repeating the previous screen cell: |
| 3521 | * @{count} |
| 3522 | */ |
| 3523 | void |
| 3524 | f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED) |
| 3525 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 3526 | buf_T *buf = term_get_buf(argvars, "term_dumpwrite()"); |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3527 | term_T *term; |
| 3528 | char_u *fname; |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 3529 | int max_height = 0; |
| 3530 | int max_width = 0; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3531 | stat_T st; |
| 3532 | FILE *fd; |
| 3533 | VTermPos pos; |
| 3534 | VTermScreen *screen; |
| 3535 | VTermScreenCell prev_cell; |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3536 | VTermState *state; |
| 3537 | VTermPos cursor_pos; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3538 | |
| 3539 | if (check_restricted() || check_secure()) |
| 3540 | return; |
| 3541 | if (buf == NULL) |
| 3542 | return; |
| 3543 | term = buf->b_term; |
| 3544 | |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 3545 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 3546 | { |
| 3547 | dict_T *d; |
| 3548 | |
| 3549 | if (argvars[2].v_type != VAR_DICT) |
| 3550 | { |
| 3551 | EMSG(_(e_dictreq)); |
| 3552 | return; |
| 3553 | } |
| 3554 | d = argvars[2].vval.v_dict; |
| 3555 | if (d != NULL) |
| 3556 | { |
| 3557 | max_height = get_dict_number(d, (char_u *)"rows"); |
| 3558 | max_width = get_dict_number(d, (char_u *)"columns"); |
| 3559 | } |
| 3560 | } |
| 3561 | |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3562 | fname = get_tv_string_chk(&argvars[1]); |
| 3563 | if (fname == NULL) |
| 3564 | return; |
| 3565 | if (mch_stat((char *)fname, &st) >= 0) |
| 3566 | { |
| 3567 | EMSG2(_("E953: File exists: %s"), fname); |
| 3568 | return; |
| 3569 | } |
| 3570 | |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3571 | if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL) |
| 3572 | { |
| 3573 | EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname); |
| 3574 | return; |
| 3575 | } |
| 3576 | |
| 3577 | vim_memset(&prev_cell, 0, sizeof(prev_cell)); |
| 3578 | |
| 3579 | screen = vterm_obtain_screen(term->tl_vterm); |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3580 | state = vterm_obtain_state(term->tl_vterm); |
| 3581 | vterm_state_get_cursorpos(state, &cursor_pos); |
| 3582 | |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 3583 | for (pos.row = 0; (max_height == 0 || pos.row < max_height) |
| 3584 | && pos.row < term->tl_rows; ++pos.row) |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3585 | { |
| 3586 | int repeat = 0; |
| 3587 | |
Bram Moolenaar | 6bb2cdf | 2018-02-24 19:53:53 +0100 | [diff] [blame] | 3588 | for (pos.col = 0; (max_width == 0 || pos.col < max_width) |
| 3589 | && pos.col < term->tl_cols; ++pos.col) |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3590 | { |
| 3591 | VTermScreenCell cell; |
| 3592 | int same_attr; |
| 3593 | int same_chars = TRUE; |
| 3594 | int i; |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3595 | int is_cursor_pos = (pos.col == cursor_pos.col |
| 3596 | && pos.row == cursor_pos.row); |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3597 | |
| 3598 | if (vterm_screen_get_cell(screen, pos, &cell) == 0) |
| 3599 | vim_memset(&cell, 0, sizeof(cell)); |
| 3600 | |
| 3601 | for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i) |
| 3602 | { |
Bram Moolenaar | 47015b8 | 2018-03-23 22:10:34 +0100 | [diff] [blame] | 3603 | int c = cell.chars[i]; |
| 3604 | int pc = prev_cell.chars[i]; |
| 3605 | |
| 3606 | /* For the first character NUL is the same as space. */ |
| 3607 | if (i == 0) |
| 3608 | { |
| 3609 | c = (c == NUL) ? ' ' : c; |
| 3610 | pc = (pc == NUL) ? ' ' : pc; |
| 3611 | } |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3612 | if (cell.chars[i] != prev_cell.chars[i]) |
| 3613 | same_chars = FALSE; |
| 3614 | if (cell.chars[i] == NUL || prev_cell.chars[i] == NUL) |
| 3615 | break; |
| 3616 | } |
| 3617 | same_attr = vtermAttr2hl(cell.attrs) |
| 3618 | == vtermAttr2hl(prev_cell.attrs) |
| 3619 | && same_color(&cell.fg, &prev_cell.fg) |
| 3620 | && same_color(&cell.bg, &prev_cell.bg); |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3621 | if (same_chars && cell.width == prev_cell.width && same_attr |
| 3622 | && !is_cursor_pos) |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3623 | { |
| 3624 | ++repeat; |
| 3625 | } |
| 3626 | else |
| 3627 | { |
| 3628 | if (repeat > 0) |
| 3629 | { |
| 3630 | fprintf(fd, "@%d", repeat); |
| 3631 | repeat = 0; |
| 3632 | } |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3633 | fputs(is_cursor_pos ? ">" : "|", fd); |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3634 | |
| 3635 | if (cell.chars[0] == NUL) |
| 3636 | fputs(" ", fd); |
| 3637 | else |
| 3638 | { |
| 3639 | char_u charbuf[10]; |
| 3640 | int len; |
| 3641 | |
| 3642 | for (i = 0; i < VTERM_MAX_CHARS_PER_CELL |
| 3643 | && cell.chars[i] != NUL; ++i) |
| 3644 | { |
Bram Moolenaar | f06b0b6 | 2018-03-29 17:22:24 +0200 | [diff] [blame] | 3645 | len = utf_char2bytes(cell.chars[i], charbuf); |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3646 | fwrite(charbuf, len, 1, fd); |
| 3647 | } |
| 3648 | } |
| 3649 | |
| 3650 | /* When only the characters differ we don't write anything, the |
| 3651 | * following "|", "@" or NL will indicate using the same |
| 3652 | * attributes. */ |
| 3653 | if (cell.width != prev_cell.width || !same_attr) |
| 3654 | { |
| 3655 | if (cell.width == 2) |
| 3656 | { |
| 3657 | fputs("*", fd); |
| 3658 | ++pos.col; |
| 3659 | } |
| 3660 | else |
| 3661 | fputs("+", fd); |
| 3662 | |
| 3663 | if (same_attr) |
| 3664 | { |
| 3665 | fputs("&", fd); |
| 3666 | } |
| 3667 | else |
| 3668 | { |
| 3669 | fprintf(fd, "%d", vtermAttr2hl(cell.attrs)); |
| 3670 | if (same_color(&cell.fg, &prev_cell.fg)) |
| 3671 | fputs("&", fd); |
| 3672 | else |
| 3673 | { |
| 3674 | fputs("#", fd); |
| 3675 | dump_term_color(fd, &cell.fg); |
| 3676 | } |
| 3677 | if (same_color(&cell.bg, &prev_cell.bg)) |
| 3678 | fputs("&", fd); |
| 3679 | else |
| 3680 | { |
| 3681 | fputs("#", fd); |
| 3682 | dump_term_color(fd, &cell.bg); |
| 3683 | } |
| 3684 | } |
| 3685 | } |
| 3686 | |
| 3687 | prev_cell = cell; |
| 3688 | } |
| 3689 | } |
| 3690 | if (repeat > 0) |
| 3691 | fprintf(fd, "@%d", repeat); |
| 3692 | fputs("\n", fd); |
| 3693 | } |
| 3694 | |
| 3695 | fclose(fd); |
| 3696 | } |
| 3697 | |
| 3698 | /* |
| 3699 | * Called when a dump is corrupted. Put a breakpoint here when debugging. |
| 3700 | */ |
| 3701 | static void |
| 3702 | dump_is_corrupt(garray_T *gap) |
| 3703 | { |
| 3704 | ga_concat(gap, (char_u *)"CORRUPT"); |
| 3705 | } |
| 3706 | |
| 3707 | static void |
| 3708 | append_cell(garray_T *gap, cellattr_T *cell) |
| 3709 | { |
| 3710 | if (ga_grow(gap, 1) == OK) |
| 3711 | { |
| 3712 | *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell; |
| 3713 | ++gap->ga_len; |
| 3714 | } |
| 3715 | } |
| 3716 | |
| 3717 | /* |
| 3718 | * Read the dump file from "fd" and append lines to the current buffer. |
| 3719 | * Return the cell width of the longest line. |
| 3720 | */ |
| 3721 | static int |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3722 | read_dump_file(FILE *fd, VTermPos *cursor_pos) |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3723 | { |
| 3724 | int c; |
| 3725 | garray_T ga_text; |
| 3726 | garray_T ga_cell; |
| 3727 | char_u *prev_char = NULL; |
| 3728 | int attr = 0; |
| 3729 | cellattr_T cell; |
| 3730 | term_T *term = curbuf->b_term; |
| 3731 | int max_cells = 0; |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3732 | int start_row = term->tl_scrollback.ga_len; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3733 | |
| 3734 | ga_init2(&ga_text, 1, 90); |
| 3735 | ga_init2(&ga_cell, sizeof(cellattr_T), 90); |
| 3736 | vim_memset(&cell, 0, sizeof(cell)); |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3737 | cursor_pos->row = -1; |
| 3738 | cursor_pos->col = -1; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3739 | |
| 3740 | c = fgetc(fd); |
| 3741 | for (;;) |
| 3742 | { |
| 3743 | if (c == EOF) |
| 3744 | break; |
| 3745 | if (c == '\n') |
| 3746 | { |
| 3747 | /* End of a line: append it to the buffer. */ |
| 3748 | if (ga_text.ga_data == NULL) |
| 3749 | dump_is_corrupt(&ga_text); |
| 3750 | if (ga_grow(&term->tl_scrollback, 1) == OK) |
| 3751 | { |
| 3752 | sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data |
| 3753 | + term->tl_scrollback.ga_len; |
| 3754 | |
| 3755 | if (max_cells < ga_cell.ga_len) |
| 3756 | max_cells = ga_cell.ga_len; |
| 3757 | line->sb_cols = ga_cell.ga_len; |
| 3758 | line->sb_cells = ga_cell.ga_data; |
| 3759 | line->sb_fill_attr = term->tl_default_color; |
| 3760 | ++term->tl_scrollback.ga_len; |
| 3761 | ga_init(&ga_cell); |
| 3762 | |
| 3763 | ga_append(&ga_text, NUL); |
| 3764 | ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data, |
| 3765 | ga_text.ga_len, FALSE); |
| 3766 | } |
| 3767 | else |
| 3768 | ga_clear(&ga_cell); |
| 3769 | ga_text.ga_len = 0; |
| 3770 | |
| 3771 | c = fgetc(fd); |
| 3772 | } |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3773 | else if (c == '|' || c == '>') |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3774 | { |
| 3775 | int prev_len = ga_text.ga_len; |
| 3776 | |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3777 | if (c == '>') |
| 3778 | { |
| 3779 | if (cursor_pos->row != -1) |
| 3780 | dump_is_corrupt(&ga_text); /* duplicate cursor */ |
| 3781 | cursor_pos->row = term->tl_scrollback.ga_len - start_row; |
| 3782 | cursor_pos->col = ga_cell.ga_len; |
| 3783 | } |
| 3784 | |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3785 | /* normal character(s) followed by "+", "*", "|", "@" or NL */ |
| 3786 | c = fgetc(fd); |
| 3787 | if (c != EOF) |
| 3788 | ga_append(&ga_text, c); |
| 3789 | for (;;) |
| 3790 | { |
| 3791 | c = fgetc(fd); |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3792 | if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@' |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3793 | || c == EOF || c == '\n') |
| 3794 | break; |
| 3795 | ga_append(&ga_text, c); |
| 3796 | } |
| 3797 | |
| 3798 | /* save the character for repeating it */ |
| 3799 | vim_free(prev_char); |
| 3800 | if (ga_text.ga_data != NULL) |
| 3801 | prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len, |
| 3802 | ga_text.ga_len - prev_len); |
| 3803 | |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 3804 | if (c == '@' || c == '|' || c == '>' || c == '\n') |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3805 | { |
| 3806 | /* use all attributes from previous cell */ |
| 3807 | } |
| 3808 | else if (c == '+' || c == '*') |
| 3809 | { |
| 3810 | int is_bg; |
| 3811 | |
| 3812 | cell.width = c == '+' ? 1 : 2; |
| 3813 | |
| 3814 | c = fgetc(fd); |
| 3815 | if (c == '&') |
| 3816 | { |
| 3817 | /* use same attr as previous cell */ |
| 3818 | c = fgetc(fd); |
| 3819 | } |
| 3820 | else if (isdigit(c)) |
| 3821 | { |
| 3822 | /* get the decimal attribute */ |
| 3823 | attr = 0; |
| 3824 | while (isdigit(c)) |
| 3825 | { |
| 3826 | attr = attr * 10 + (c - '0'); |
| 3827 | c = fgetc(fd); |
| 3828 | } |
| 3829 | hl2vtermAttr(attr, &cell); |
| 3830 | } |
| 3831 | else |
| 3832 | dump_is_corrupt(&ga_text); |
| 3833 | |
| 3834 | /* is_bg == 0: fg, is_bg == 1: bg */ |
| 3835 | for (is_bg = 0; is_bg <= 1; ++is_bg) |
| 3836 | { |
| 3837 | if (c == '&') |
| 3838 | { |
| 3839 | /* use same color as previous cell */ |
| 3840 | c = fgetc(fd); |
| 3841 | } |
| 3842 | else if (c == '#') |
| 3843 | { |
| 3844 | int red, green, blue, index = 0; |
| 3845 | |
| 3846 | c = fgetc(fd); |
| 3847 | red = hex2nr(c); |
| 3848 | c = fgetc(fd); |
| 3849 | red = (red << 4) + hex2nr(c); |
| 3850 | c = fgetc(fd); |
| 3851 | green = hex2nr(c); |
| 3852 | c = fgetc(fd); |
| 3853 | green = (green << 4) + hex2nr(c); |
| 3854 | c = fgetc(fd); |
| 3855 | blue = hex2nr(c); |
| 3856 | c = fgetc(fd); |
| 3857 | blue = (blue << 4) + hex2nr(c); |
| 3858 | c = fgetc(fd); |
| 3859 | if (!isdigit(c)) |
| 3860 | dump_is_corrupt(&ga_text); |
| 3861 | while (isdigit(c)) |
| 3862 | { |
| 3863 | index = index * 10 + (c - '0'); |
| 3864 | c = fgetc(fd); |
| 3865 | } |
| 3866 | |
| 3867 | if (is_bg) |
| 3868 | { |
| 3869 | cell.bg.red = red; |
| 3870 | cell.bg.green = green; |
| 3871 | cell.bg.blue = blue; |
| 3872 | cell.bg.ansi_index = index; |
| 3873 | } |
| 3874 | else |
| 3875 | { |
| 3876 | cell.fg.red = red; |
| 3877 | cell.fg.green = green; |
| 3878 | cell.fg.blue = blue; |
| 3879 | cell.fg.ansi_index = index; |
| 3880 | } |
| 3881 | } |
| 3882 | else |
| 3883 | dump_is_corrupt(&ga_text); |
| 3884 | } |
| 3885 | } |
| 3886 | else |
| 3887 | dump_is_corrupt(&ga_text); |
| 3888 | |
| 3889 | append_cell(&ga_cell, &cell); |
| 3890 | } |
| 3891 | else if (c == '@') |
| 3892 | { |
| 3893 | if (prev_char == NULL) |
| 3894 | dump_is_corrupt(&ga_text); |
| 3895 | else |
| 3896 | { |
| 3897 | int count = 0; |
| 3898 | |
| 3899 | /* repeat previous character, get the count */ |
| 3900 | for (;;) |
| 3901 | { |
| 3902 | c = fgetc(fd); |
| 3903 | if (!isdigit(c)) |
| 3904 | break; |
| 3905 | count = count * 10 + (c - '0'); |
| 3906 | } |
| 3907 | |
| 3908 | while (count-- > 0) |
| 3909 | { |
| 3910 | ga_concat(&ga_text, prev_char); |
| 3911 | append_cell(&ga_cell, &cell); |
| 3912 | } |
| 3913 | } |
| 3914 | } |
| 3915 | else |
| 3916 | { |
| 3917 | dump_is_corrupt(&ga_text); |
| 3918 | c = fgetc(fd); |
| 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | if (ga_text.ga_len > 0) |
| 3923 | { |
| 3924 | /* trailing characters after last NL */ |
| 3925 | dump_is_corrupt(&ga_text); |
| 3926 | ga_append(&ga_text, NUL); |
| 3927 | ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data, |
| 3928 | ga_text.ga_len, FALSE); |
| 3929 | } |
| 3930 | |
| 3931 | ga_clear(&ga_text); |
| 3932 | vim_free(prev_char); |
| 3933 | |
| 3934 | return max_cells; |
| 3935 | } |
| 3936 | |
| 3937 | /* |
Bram Moolenaar | 4a69634 | 2018-04-05 18:45:26 +0200 | [diff] [blame] | 3938 | * Return an allocated string with at least "text_width" "=" characters and |
| 3939 | * "fname" inserted in the middle. |
| 3940 | */ |
| 3941 | static char_u * |
| 3942 | get_separator(int text_width, char_u *fname) |
| 3943 | { |
| 3944 | int width = MAX(text_width, curwin->w_width); |
| 3945 | char_u *textline; |
| 3946 | int fname_size; |
| 3947 | char_u *p = fname; |
| 3948 | int i; |
| 3949 | int off; |
| 3950 | |
| 3951 | textline = alloc(width + STRLEN(fname) + 1); |
| 3952 | if (textline == NULL) |
| 3953 | return NULL; |
| 3954 | |
| 3955 | fname_size = vim_strsize(fname); |
| 3956 | if (fname_size < width - 8) |
| 3957 | { |
| 3958 | /* enough room, don't use the full window width */ |
| 3959 | width = MAX(text_width, fname_size + 8); |
| 3960 | } |
| 3961 | else if (fname_size > width - 8) |
| 3962 | { |
| 3963 | /* full name doesn't fit, use only the tail */ |
| 3964 | p = gettail(fname); |
| 3965 | fname_size = vim_strsize(p); |
| 3966 | } |
| 3967 | /* skip characters until the name fits */ |
| 3968 | while (fname_size > width - 8) |
| 3969 | { |
| 3970 | p += (*mb_ptr2len)(p); |
| 3971 | fname_size = vim_strsize(p); |
| 3972 | } |
| 3973 | |
| 3974 | for (i = 0; i < (width - fname_size) / 2 - 1; ++i) |
| 3975 | textline[i] = '='; |
| 3976 | textline[i++] = ' '; |
| 3977 | |
| 3978 | STRCPY(textline + i, p); |
| 3979 | off = STRLEN(textline); |
| 3980 | textline[off] = ' '; |
| 3981 | for (i = 1; i < (width - fname_size) / 2; ++i) |
| 3982 | textline[off + i] = '='; |
| 3983 | textline[off + i] = NUL; |
| 3984 | |
| 3985 | return textline; |
| 3986 | } |
| 3987 | |
| 3988 | /* |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 3989 | * Common for "term_dumpdiff()" and "term_dumpload()". |
| 3990 | */ |
| 3991 | static void |
| 3992 | term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff) |
| 3993 | { |
| 3994 | jobopt_T opt; |
| 3995 | buf_T *buf; |
| 3996 | char_u buf1[NUMBUFLEN]; |
| 3997 | char_u buf2[NUMBUFLEN]; |
| 3998 | char_u *fname1; |
Bram Moolenaar | 9c8816b | 2018-02-19 21:50:42 +0100 | [diff] [blame] | 3999 | char_u *fname2 = NULL; |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 4000 | char_u *fname_tofree = NULL; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4001 | FILE *fd1; |
Bram Moolenaar | 9c8816b | 2018-02-19 21:50:42 +0100 | [diff] [blame] | 4002 | FILE *fd2 = NULL; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4003 | char_u *textline = NULL; |
| 4004 | |
| 4005 | /* First open the files. If this fails bail out. */ |
| 4006 | fname1 = get_tv_string_buf_chk(&argvars[0], buf1); |
| 4007 | if (do_diff) |
| 4008 | fname2 = get_tv_string_buf_chk(&argvars[1], buf2); |
| 4009 | if (fname1 == NULL || (do_diff && fname2 == NULL)) |
| 4010 | { |
| 4011 | EMSG(_(e_invarg)); |
| 4012 | return; |
| 4013 | } |
| 4014 | fd1 = mch_fopen((char *)fname1, READBIN); |
| 4015 | if (fd1 == NULL) |
| 4016 | { |
| 4017 | EMSG2(_(e_notread), fname1); |
| 4018 | return; |
| 4019 | } |
| 4020 | if (do_diff) |
| 4021 | { |
| 4022 | fd2 = mch_fopen((char *)fname2, READBIN); |
| 4023 | if (fd2 == NULL) |
| 4024 | { |
| 4025 | fclose(fd1); |
| 4026 | EMSG2(_(e_notread), fname2); |
| 4027 | return; |
| 4028 | } |
| 4029 | } |
| 4030 | |
| 4031 | init_job_options(&opt); |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 4032 | if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN |
| 4033 | && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0, |
| 4034 | JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS |
| 4035 | + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL) |
| 4036 | goto theend; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4037 | |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 4038 | if (opt.jo_term_name == NULL) |
| 4039 | { |
Bram Moolenaar | b571c63 | 2018-03-21 22:27:59 +0100 | [diff] [blame] | 4040 | size_t len = STRLEN(fname1) + 12; |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 4041 | |
Bram Moolenaar | b571c63 | 2018-03-21 22:27:59 +0100 | [diff] [blame] | 4042 | fname_tofree = alloc((int)len); |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 4043 | if (fname_tofree != NULL) |
| 4044 | { |
| 4045 | vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1); |
| 4046 | opt.jo_term_name = fname_tofree; |
| 4047 | } |
| 4048 | } |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4049 | |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4050 | buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB); |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4051 | if (buf != NULL && buf->b_term != NULL) |
| 4052 | { |
| 4053 | int i; |
| 4054 | linenr_T bot_lnum; |
| 4055 | linenr_T lnum; |
| 4056 | term_T *term = buf->b_term; |
| 4057 | int width; |
| 4058 | int width2; |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 4059 | VTermPos cursor_pos1; |
| 4060 | VTermPos cursor_pos2; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4061 | |
Bram Moolenaar | 52acb11 | 2018-03-18 19:20:22 +0100 | [diff] [blame] | 4062 | init_default_colors(term); |
| 4063 | |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4064 | rettv->vval.v_number = buf->b_fnum; |
| 4065 | |
| 4066 | /* read the files, fill the buffer with the diff */ |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 4067 | width = read_dump_file(fd1, &cursor_pos1); |
| 4068 | |
| 4069 | /* position the cursor */ |
| 4070 | if (cursor_pos1.row >= 0) |
| 4071 | { |
| 4072 | curwin->w_cursor.lnum = cursor_pos1.row + 1; |
| 4073 | coladvance(cursor_pos1.col); |
| 4074 | } |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4075 | |
| 4076 | /* Delete the empty line that was in the empty buffer. */ |
| 4077 | ml_delete(1, FALSE); |
| 4078 | |
| 4079 | /* For term_dumpload() we are done here. */ |
| 4080 | if (!do_diff) |
| 4081 | goto theend; |
| 4082 | |
| 4083 | term->tl_top_diff_rows = curbuf->b_ml.ml_line_count; |
| 4084 | |
Bram Moolenaar | 4a69634 | 2018-04-05 18:45:26 +0200 | [diff] [blame] | 4085 | textline = get_separator(width, fname1); |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4086 | if (textline == NULL) |
| 4087 | goto theend; |
Bram Moolenaar | 4a69634 | 2018-04-05 18:45:26 +0200 | [diff] [blame] | 4088 | if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK) |
| 4089 | ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE); |
| 4090 | vim_free(textline); |
| 4091 | |
| 4092 | textline = get_separator(width, fname2); |
| 4093 | if (textline == NULL) |
| 4094 | goto theend; |
| 4095 | if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK) |
| 4096 | ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE); |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4097 | textline[width] = NUL; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4098 | |
| 4099 | bot_lnum = curbuf->b_ml.ml_line_count; |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 4100 | width2 = read_dump_file(fd2, &cursor_pos2); |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4101 | if (width2 > width) |
| 4102 | { |
| 4103 | vim_free(textline); |
| 4104 | textline = alloc(width2 + 1); |
| 4105 | if (textline == NULL) |
| 4106 | goto theend; |
| 4107 | width = width2; |
| 4108 | textline[width] = NUL; |
| 4109 | } |
| 4110 | term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum; |
| 4111 | |
| 4112 | for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum) |
| 4113 | { |
| 4114 | if (lnum + bot_lnum > curbuf->b_ml.ml_line_count) |
| 4115 | { |
| 4116 | /* bottom part has fewer rows, fill with "-" */ |
| 4117 | for (i = 0; i < width; ++i) |
| 4118 | textline[i] = '-'; |
| 4119 | } |
| 4120 | else |
| 4121 | { |
| 4122 | char_u *line1; |
| 4123 | char_u *line2; |
| 4124 | char_u *p1; |
| 4125 | char_u *p2; |
| 4126 | int col; |
| 4127 | sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data; |
| 4128 | cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells; |
| 4129 | cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1) |
| 4130 | ->sb_cells; |
| 4131 | |
| 4132 | /* Make a copy, getting the second line will invalidate it. */ |
| 4133 | line1 = vim_strsave(ml_get(lnum)); |
| 4134 | if (line1 == NULL) |
| 4135 | break; |
| 4136 | p1 = line1; |
| 4137 | |
| 4138 | line2 = ml_get(lnum + bot_lnum); |
| 4139 | p2 = line2; |
| 4140 | for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col) |
| 4141 | { |
| 4142 | int len1 = utfc_ptr2len(p1); |
| 4143 | int len2 = utfc_ptr2len(p2); |
| 4144 | |
| 4145 | textline[col] = ' '; |
| 4146 | if (len1 != len2 || STRNCMP(p1, p2, len1) != 0) |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 4147 | /* text differs */ |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4148 | textline[col] = 'X'; |
Bram Moolenaar | 9271d05 | 2018-02-25 21:39:46 +0100 | [diff] [blame] | 4149 | else if (lnum == cursor_pos1.row + 1 |
| 4150 | && col == cursor_pos1.col |
| 4151 | && (cursor_pos1.row != cursor_pos2.row |
| 4152 | || cursor_pos1.col != cursor_pos2.col)) |
| 4153 | /* cursor in first but not in second */ |
| 4154 | textline[col] = '>'; |
| 4155 | else if (lnum == cursor_pos2.row + 1 |
| 4156 | && col == cursor_pos2.col |
| 4157 | && (cursor_pos1.row != cursor_pos2.row |
| 4158 | || cursor_pos1.col != cursor_pos2.col)) |
| 4159 | /* cursor in second but not in first */ |
| 4160 | textline[col] = '<'; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4161 | else if (cellattr1 != NULL && cellattr2 != NULL) |
| 4162 | { |
| 4163 | if ((cellattr1 + col)->width |
| 4164 | != (cellattr2 + col)->width) |
| 4165 | textline[col] = 'w'; |
| 4166 | else if (!same_color(&(cellattr1 + col)->fg, |
| 4167 | &(cellattr2 + col)->fg)) |
| 4168 | textline[col] = 'f'; |
| 4169 | else if (!same_color(&(cellattr1 + col)->bg, |
| 4170 | &(cellattr2 + col)->bg)) |
| 4171 | textline[col] = 'b'; |
| 4172 | else if (vtermAttr2hl((cellattr1 + col)->attrs) |
| 4173 | != vtermAttr2hl(((cellattr2 + col)->attrs))) |
| 4174 | textline[col] = 'a'; |
| 4175 | } |
| 4176 | p1 += len1; |
| 4177 | p2 += len2; |
| 4178 | /* TODO: handle different width */ |
| 4179 | } |
| 4180 | vim_free(line1); |
| 4181 | |
| 4182 | while (col < width) |
| 4183 | { |
| 4184 | if (*p1 == NUL && *p2 == NUL) |
| 4185 | textline[col] = '?'; |
| 4186 | else if (*p1 == NUL) |
| 4187 | { |
| 4188 | textline[col] = '+'; |
| 4189 | p2 += utfc_ptr2len(p2); |
| 4190 | } |
| 4191 | else |
| 4192 | { |
| 4193 | textline[col] = '-'; |
| 4194 | p1 += utfc_ptr2len(p1); |
| 4195 | } |
| 4196 | ++col; |
| 4197 | } |
| 4198 | } |
| 4199 | if (add_empty_scrollback(term, &term->tl_default_color, |
| 4200 | term->tl_top_diff_rows) == OK) |
| 4201 | ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE); |
| 4202 | ++bot_lnum; |
| 4203 | } |
| 4204 | |
| 4205 | while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count) |
| 4206 | { |
| 4207 | /* bottom part has more rows, fill with "+" */ |
| 4208 | for (i = 0; i < width; ++i) |
| 4209 | textline[i] = '+'; |
| 4210 | if (add_empty_scrollback(term, &term->tl_default_color, |
| 4211 | term->tl_top_diff_rows) == OK) |
| 4212 | ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE); |
| 4213 | ++lnum; |
| 4214 | ++bot_lnum; |
| 4215 | } |
| 4216 | |
| 4217 | term->tl_cols = width; |
Bram Moolenaar | 4a69634 | 2018-04-05 18:45:26 +0200 | [diff] [blame] | 4218 | |
| 4219 | /* looks better without wrapping */ |
| 4220 | curwin->w_p_wrap = 0; |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | theend: |
| 4224 | vim_free(textline); |
Bram Moolenaar | 5a3a49e | 2018-03-20 18:35:53 +0100 | [diff] [blame] | 4225 | vim_free(fname_tofree); |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4226 | fclose(fd1); |
Bram Moolenaar | 9c8816b | 2018-02-19 21:50:42 +0100 | [diff] [blame] | 4227 | if (fd2 != NULL) |
Bram Moolenaar | d96ff16 | 2018-02-18 22:13:29 +0100 | [diff] [blame] | 4228 | fclose(fd2); |
| 4229 | } |
| 4230 | |
| 4231 | /* |
| 4232 | * If the current buffer shows the output of term_dumpdiff(), swap the top and |
| 4233 | * bottom files. |
| 4234 | * Return FAIL when this is not possible. |
| 4235 | */ |
| 4236 | int |
| 4237 | term_swap_diff() |
| 4238 | { |
| 4239 | term_T *term = curbuf->b_term; |
| 4240 | linenr_T line_count; |
| 4241 | linenr_T top_rows; |
| 4242 | linenr_T bot_rows; |
| 4243 | linenr_T bot_start; |
| 4244 | linenr_T lnum; |
| 4245 | char_u *p; |
| 4246 | sb_line_T *sb_line; |
| 4247 | |
| 4248 | if (term == NULL |
| 4249 | || !term_is_finished(curbuf) |
| 4250 | || term->tl_top_diff_rows == 0 |
| 4251 | || term->tl_scrollback.ga_len == 0) |
| 4252 | return FAIL; |
| 4253 | |
| 4254 | line_count = curbuf->b_ml.ml_line_count; |
| 4255 | top_rows = term->tl_top_diff_rows; |
| 4256 | bot_rows = term->tl_bot_diff_rows; |
| 4257 | bot_start = line_count - bot_rows; |
| 4258 | sb_line = (sb_line_T *)term->tl_scrollback.ga_data; |
| 4259 | |
| 4260 | /* move lines from top to above the bottom part */ |
| 4261 | for (lnum = 1; lnum <= top_rows; ++lnum) |
| 4262 | { |
| 4263 | p = vim_strsave(ml_get(1)); |
| 4264 | if (p == NULL) |
| 4265 | return OK; |
| 4266 | ml_append(bot_start, p, 0, FALSE); |
| 4267 | ml_delete(1, FALSE); |
| 4268 | vim_free(p); |
| 4269 | } |
| 4270 | |
| 4271 | /* move lines from bottom to the top */ |
| 4272 | for (lnum = 1; lnum <= bot_rows; ++lnum) |
| 4273 | { |
| 4274 | p = vim_strsave(ml_get(bot_start + lnum)); |
| 4275 | if (p == NULL) |
| 4276 | return OK; |
| 4277 | ml_delete(bot_start + lnum, FALSE); |
| 4278 | ml_append(lnum - 1, p, 0, FALSE); |
| 4279 | vim_free(p); |
| 4280 | } |
| 4281 | |
| 4282 | if (top_rows == bot_rows) |
| 4283 | { |
| 4284 | /* rows counts are equal, can swap cell properties */ |
| 4285 | for (lnum = 0; lnum < top_rows; ++lnum) |
| 4286 | { |
| 4287 | sb_line_T temp; |
| 4288 | |
| 4289 | temp = *(sb_line + lnum); |
| 4290 | *(sb_line + lnum) = *(sb_line + bot_start + lnum); |
| 4291 | *(sb_line + bot_start + lnum) = temp; |
| 4292 | } |
| 4293 | } |
| 4294 | else |
| 4295 | { |
| 4296 | size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len; |
| 4297 | sb_line_T *temp = (sb_line_T *)alloc((int)size); |
| 4298 | |
| 4299 | /* need to copy cell properties into temp memory */ |
| 4300 | if (temp != NULL) |
| 4301 | { |
| 4302 | mch_memmove(temp, term->tl_scrollback.ga_data, size); |
| 4303 | mch_memmove(term->tl_scrollback.ga_data, |
| 4304 | temp + bot_start, |
| 4305 | sizeof(sb_line_T) * bot_rows); |
| 4306 | mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows, |
| 4307 | temp + top_rows, |
| 4308 | sizeof(sb_line_T) * (line_count - top_rows - bot_rows)); |
| 4309 | mch_memmove((sb_line_T *)term->tl_scrollback.ga_data |
| 4310 | + line_count - top_rows, |
| 4311 | temp, |
| 4312 | sizeof(sb_line_T) * top_rows); |
| 4313 | vim_free(temp); |
| 4314 | } |
| 4315 | } |
| 4316 | |
| 4317 | term->tl_top_diff_rows = bot_rows; |
| 4318 | term->tl_bot_diff_rows = top_rows; |
| 4319 | |
| 4320 | update_screen(NOT_VALID); |
| 4321 | return OK; |
| 4322 | } |
| 4323 | |
| 4324 | /* |
| 4325 | * "term_dumpdiff(filename, filename, options)" function |
| 4326 | */ |
| 4327 | void |
| 4328 | f_term_dumpdiff(typval_T *argvars, typval_T *rettv) |
| 4329 | { |
| 4330 | term_load_dump(argvars, rettv, TRUE); |
| 4331 | } |
| 4332 | |
| 4333 | /* |
| 4334 | * "term_dumpload(filename, options)" function |
| 4335 | */ |
| 4336 | void |
| 4337 | f_term_dumpload(typval_T *argvars, typval_T *rettv) |
| 4338 | { |
| 4339 | term_load_dump(argvars, rettv, FALSE); |
| 4340 | } |
| 4341 | |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4342 | /* |
| 4343 | * "term_getaltscreen(buf)" function |
| 4344 | */ |
| 4345 | void |
| 4346 | f_term_getaltscreen(typval_T *argvars, typval_T *rettv) |
| 4347 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4348 | buf_T *buf = term_get_buf(argvars, "term_getaltscreen()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4349 | |
| 4350 | if (buf == NULL) |
| 4351 | return; |
| 4352 | rettv->vval.v_number = buf->b_term->tl_using_altscreen; |
| 4353 | } |
| 4354 | |
| 4355 | /* |
| 4356 | * "term_getattr(attr, name)" function |
| 4357 | */ |
| 4358 | void |
| 4359 | f_term_getattr(typval_T *argvars, typval_T *rettv) |
| 4360 | { |
| 4361 | int attr; |
| 4362 | size_t i; |
| 4363 | char_u *name; |
| 4364 | |
| 4365 | static struct { |
| 4366 | char *name; |
| 4367 | int attr; |
| 4368 | } attrs[] = { |
| 4369 | {"bold", HL_BOLD}, |
| 4370 | {"italic", HL_ITALIC}, |
| 4371 | {"underline", HL_UNDERLINE}, |
| 4372 | {"strike", HL_STRIKETHROUGH}, |
| 4373 | {"reverse", HL_INVERSE}, |
| 4374 | }; |
| 4375 | |
| 4376 | attr = get_tv_number(&argvars[0]); |
| 4377 | name = get_tv_string_chk(&argvars[1]); |
| 4378 | if (name == NULL) |
| 4379 | return; |
| 4380 | |
| 4381 | for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i) |
| 4382 | if (STRCMP(name, attrs[i].name) == 0) |
| 4383 | { |
| 4384 | rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0; |
| 4385 | break; |
| 4386 | } |
| 4387 | } |
| 4388 | |
| 4389 | /* |
| 4390 | * "term_getcursor(buf)" function |
| 4391 | */ |
| 4392 | void |
| 4393 | f_term_getcursor(typval_T *argvars, typval_T *rettv) |
| 4394 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4395 | buf_T *buf = term_get_buf(argvars, "term_getcursor()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4396 | term_T *term; |
| 4397 | list_T *l; |
| 4398 | dict_T *d; |
| 4399 | |
| 4400 | if (rettv_list_alloc(rettv) == FAIL) |
| 4401 | return; |
| 4402 | if (buf == NULL) |
| 4403 | return; |
| 4404 | term = buf->b_term; |
| 4405 | |
| 4406 | l = rettv->vval.v_list; |
| 4407 | list_append_number(l, term->tl_cursor_pos.row + 1); |
| 4408 | list_append_number(l, term->tl_cursor_pos.col + 1); |
| 4409 | |
| 4410 | d = dict_alloc(); |
| 4411 | if (d != NULL) |
| 4412 | { |
| 4413 | dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL); |
| 4414 | dict_add_nr_str(d, "blink", blink_state_is_inverted() |
| 4415 | ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL); |
| 4416 | dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL); |
| 4417 | dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL |
| 4418 | ? (char_u *)"" : term->tl_cursor_color); |
| 4419 | list_append_dict(l, d); |
| 4420 | } |
| 4421 | } |
| 4422 | |
| 4423 | /* |
| 4424 | * "term_getjob(buf)" function |
| 4425 | */ |
| 4426 | void |
| 4427 | f_term_getjob(typval_T *argvars, typval_T *rettv) |
| 4428 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4429 | buf_T *buf = term_get_buf(argvars, "term_getjob()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4430 | |
| 4431 | rettv->v_type = VAR_JOB; |
| 4432 | rettv->vval.v_job = NULL; |
| 4433 | if (buf == NULL) |
| 4434 | return; |
| 4435 | |
| 4436 | rettv->vval.v_job = buf->b_term->tl_job; |
| 4437 | if (rettv->vval.v_job != NULL) |
| 4438 | ++rettv->vval.v_job->jv_refcount; |
| 4439 | } |
| 4440 | |
| 4441 | static int |
| 4442 | get_row_number(typval_T *tv, term_T *term) |
| 4443 | { |
| 4444 | if (tv->v_type == VAR_STRING |
| 4445 | && tv->vval.v_string != NULL |
| 4446 | && STRCMP(tv->vval.v_string, ".") == 0) |
| 4447 | return term->tl_cursor_pos.row; |
| 4448 | return (int)get_tv_number(tv) - 1; |
| 4449 | } |
| 4450 | |
| 4451 | /* |
| 4452 | * "term_getline(buf, row)" function |
| 4453 | */ |
| 4454 | void |
| 4455 | f_term_getline(typval_T *argvars, typval_T *rettv) |
| 4456 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4457 | buf_T *buf = term_get_buf(argvars, "term_getline()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4458 | term_T *term; |
| 4459 | int row; |
| 4460 | |
| 4461 | rettv->v_type = VAR_STRING; |
| 4462 | if (buf == NULL) |
| 4463 | return; |
| 4464 | term = buf->b_term; |
| 4465 | row = get_row_number(&argvars[1], term); |
| 4466 | |
| 4467 | if (term->tl_vterm == NULL) |
| 4468 | { |
| 4469 | linenr_T lnum = row + term->tl_scrollback_scrolled + 1; |
| 4470 | |
| 4471 | /* vterm is finished, get the text from the buffer */ |
| 4472 | if (lnum > 0 && lnum <= buf->b_ml.ml_line_count) |
| 4473 | rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE)); |
| 4474 | } |
| 4475 | else |
| 4476 | { |
| 4477 | VTermScreen *screen = vterm_obtain_screen(term->tl_vterm); |
| 4478 | VTermRect rect; |
| 4479 | int len; |
| 4480 | char_u *p; |
| 4481 | |
| 4482 | if (row < 0 || row >= term->tl_rows) |
| 4483 | return; |
| 4484 | len = term->tl_cols * MB_MAXBYTES + 1; |
| 4485 | p = alloc(len); |
| 4486 | if (p == NULL) |
| 4487 | return; |
| 4488 | rettv->vval.v_string = p; |
| 4489 | |
| 4490 | rect.start_col = 0; |
| 4491 | rect.end_col = term->tl_cols; |
| 4492 | rect.start_row = row; |
| 4493 | rect.end_row = row + 1; |
| 4494 | p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL; |
| 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | /* |
| 4499 | * "term_getscrolled(buf)" function |
| 4500 | */ |
| 4501 | void |
| 4502 | f_term_getscrolled(typval_T *argvars, typval_T *rettv) |
| 4503 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4504 | buf_T *buf = term_get_buf(argvars, "term_getscrolled()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4505 | |
| 4506 | if (buf == NULL) |
| 4507 | return; |
| 4508 | rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled; |
| 4509 | } |
| 4510 | |
| 4511 | /* |
| 4512 | * "term_getsize(buf)" function |
| 4513 | */ |
| 4514 | void |
| 4515 | f_term_getsize(typval_T *argvars, typval_T *rettv) |
| 4516 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4517 | buf_T *buf = term_get_buf(argvars, "term_getsize()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4518 | list_T *l; |
| 4519 | |
| 4520 | if (rettv_list_alloc(rettv) == FAIL) |
| 4521 | return; |
| 4522 | if (buf == NULL) |
| 4523 | return; |
| 4524 | |
| 4525 | l = rettv->vval.v_list; |
| 4526 | list_append_number(l, buf->b_term->tl_rows); |
| 4527 | list_append_number(l, buf->b_term->tl_cols); |
| 4528 | } |
| 4529 | |
| 4530 | /* |
| 4531 | * "term_getstatus(buf)" function |
| 4532 | */ |
| 4533 | void |
| 4534 | f_term_getstatus(typval_T *argvars, typval_T *rettv) |
| 4535 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4536 | buf_T *buf = term_get_buf(argvars, "term_getstatus()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4537 | term_T *term; |
| 4538 | char_u val[100]; |
| 4539 | |
| 4540 | rettv->v_type = VAR_STRING; |
| 4541 | if (buf == NULL) |
| 4542 | return; |
| 4543 | term = buf->b_term; |
| 4544 | |
| 4545 | if (term_job_running(term)) |
| 4546 | STRCPY(val, "running"); |
| 4547 | else |
| 4548 | STRCPY(val, "finished"); |
| 4549 | if (term->tl_normal_mode) |
| 4550 | STRCAT(val, ",normal"); |
| 4551 | rettv->vval.v_string = vim_strsave(val); |
| 4552 | } |
| 4553 | |
| 4554 | /* |
| 4555 | * "term_gettitle(buf)" function |
| 4556 | */ |
| 4557 | void |
| 4558 | f_term_gettitle(typval_T *argvars, typval_T *rettv) |
| 4559 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4560 | buf_T *buf = term_get_buf(argvars, "term_gettitle()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4561 | |
| 4562 | rettv->v_type = VAR_STRING; |
| 4563 | if (buf == NULL) |
| 4564 | return; |
| 4565 | |
| 4566 | if (buf->b_term->tl_title != NULL) |
| 4567 | rettv->vval.v_string = vim_strsave(buf->b_term->tl_title); |
| 4568 | } |
| 4569 | |
| 4570 | /* |
| 4571 | * "term_gettty(buf)" function |
| 4572 | */ |
| 4573 | void |
| 4574 | f_term_gettty(typval_T *argvars, typval_T *rettv) |
| 4575 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4576 | buf_T *buf = term_get_buf(argvars, "term_gettty()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4577 | char_u *p; |
| 4578 | int num = 0; |
| 4579 | |
| 4580 | rettv->v_type = VAR_STRING; |
| 4581 | if (buf == NULL) |
| 4582 | return; |
| 4583 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 4584 | num = get_tv_number(&argvars[1]); |
| 4585 | |
| 4586 | switch (num) |
| 4587 | { |
| 4588 | case 0: |
| 4589 | if (buf->b_term->tl_job != NULL) |
| 4590 | p = buf->b_term->tl_job->jv_tty_out; |
| 4591 | else |
| 4592 | p = buf->b_term->tl_tty_out; |
| 4593 | break; |
| 4594 | case 1: |
| 4595 | if (buf->b_term->tl_job != NULL) |
| 4596 | p = buf->b_term->tl_job->jv_tty_in; |
| 4597 | else |
| 4598 | p = buf->b_term->tl_tty_in; |
| 4599 | break; |
| 4600 | default: |
| 4601 | EMSG2(_(e_invarg2), get_tv_string(&argvars[1])); |
| 4602 | return; |
| 4603 | } |
| 4604 | if (p != NULL) |
| 4605 | rettv->vval.v_string = vim_strsave(p); |
| 4606 | } |
| 4607 | |
| 4608 | /* |
| 4609 | * "term_list()" function |
| 4610 | */ |
| 4611 | void |
| 4612 | f_term_list(typval_T *argvars UNUSED, typval_T *rettv) |
| 4613 | { |
| 4614 | term_T *tp; |
| 4615 | list_T *l; |
| 4616 | |
| 4617 | if (rettv_list_alloc(rettv) == FAIL || first_term == NULL) |
| 4618 | return; |
| 4619 | |
| 4620 | l = rettv->vval.v_list; |
| 4621 | for (tp = first_term; tp != NULL; tp = tp->tl_next) |
| 4622 | if (tp != NULL && tp->tl_buffer != NULL) |
| 4623 | if (list_append_number(l, |
| 4624 | (varnumber_T)tp->tl_buffer->b_fnum) == FAIL) |
| 4625 | return; |
| 4626 | } |
| 4627 | |
| 4628 | /* |
| 4629 | * "term_scrape(buf, row)" function |
| 4630 | */ |
| 4631 | void |
| 4632 | f_term_scrape(typval_T *argvars, typval_T *rettv) |
| 4633 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4634 | buf_T *buf = term_get_buf(argvars, "term_scrape()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4635 | VTermScreen *screen = NULL; |
| 4636 | VTermPos pos; |
| 4637 | list_T *l; |
| 4638 | term_T *term; |
| 4639 | char_u *p; |
| 4640 | sb_line_T *line; |
| 4641 | |
| 4642 | if (rettv_list_alloc(rettv) == FAIL) |
| 4643 | return; |
| 4644 | if (buf == NULL) |
| 4645 | return; |
| 4646 | term = buf->b_term; |
| 4647 | |
| 4648 | l = rettv->vval.v_list; |
| 4649 | pos.row = get_row_number(&argvars[1], term); |
| 4650 | |
| 4651 | if (term->tl_vterm != NULL) |
| 4652 | { |
| 4653 | screen = vterm_obtain_screen(term->tl_vterm); |
| 4654 | p = NULL; |
| 4655 | line = NULL; |
| 4656 | } |
| 4657 | else |
| 4658 | { |
| 4659 | linenr_T lnum = pos.row + term->tl_scrollback_scrolled; |
| 4660 | |
| 4661 | if (lnum < 0 || lnum >= term->tl_scrollback.ga_len) |
| 4662 | return; |
| 4663 | p = ml_get_buf(buf, lnum + 1, FALSE); |
| 4664 | line = (sb_line_T *)term->tl_scrollback.ga_data + lnum; |
| 4665 | } |
| 4666 | |
| 4667 | for (pos.col = 0; pos.col < term->tl_cols; ) |
| 4668 | { |
| 4669 | dict_T *dcell; |
| 4670 | int width; |
| 4671 | VTermScreenCellAttrs attrs; |
| 4672 | VTermColor fg, bg; |
| 4673 | char_u rgb[8]; |
| 4674 | char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1]; |
| 4675 | int off = 0; |
| 4676 | int i; |
| 4677 | |
| 4678 | if (screen == NULL) |
| 4679 | { |
| 4680 | cellattr_T *cellattr; |
| 4681 | int len; |
| 4682 | |
| 4683 | /* vterm has finished, get the cell from scrollback */ |
| 4684 | if (pos.col >= line->sb_cols) |
| 4685 | break; |
| 4686 | cellattr = line->sb_cells + pos.col; |
| 4687 | width = cellattr->width; |
| 4688 | attrs = cellattr->attrs; |
| 4689 | fg = cellattr->fg; |
| 4690 | bg = cellattr->bg; |
| 4691 | len = MB_PTR2LEN(p); |
| 4692 | mch_memmove(mbs, p, len); |
| 4693 | mbs[len] = NUL; |
| 4694 | p += len; |
| 4695 | } |
| 4696 | else |
| 4697 | { |
| 4698 | VTermScreenCell cell; |
| 4699 | if (vterm_screen_get_cell(screen, pos, &cell) == 0) |
| 4700 | break; |
| 4701 | for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i) |
| 4702 | { |
| 4703 | if (cell.chars[i] == 0) |
| 4704 | break; |
| 4705 | off += (*utf_char2bytes)((int)cell.chars[i], mbs + off); |
| 4706 | } |
| 4707 | mbs[off] = NUL; |
| 4708 | width = cell.width; |
| 4709 | attrs = cell.attrs; |
| 4710 | fg = cell.fg; |
| 4711 | bg = cell.bg; |
| 4712 | } |
| 4713 | dcell = dict_alloc(); |
Bram Moolenaar | 4b7e7be | 2018-02-11 14:53:30 +0100 | [diff] [blame] | 4714 | if (dcell == NULL) |
| 4715 | break; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4716 | list_append_dict(l, dcell); |
| 4717 | |
| 4718 | dict_add_nr_str(dcell, "chars", 0, mbs); |
| 4719 | |
| 4720 | vim_snprintf((char *)rgb, 8, "#%02x%02x%02x", |
| 4721 | fg.red, fg.green, fg.blue); |
| 4722 | dict_add_nr_str(dcell, "fg", 0, rgb); |
| 4723 | vim_snprintf((char *)rgb, 8, "#%02x%02x%02x", |
| 4724 | bg.red, bg.green, bg.blue); |
| 4725 | dict_add_nr_str(dcell, "bg", 0, rgb); |
| 4726 | |
| 4727 | dict_add_nr_str(dcell, "attr", |
| 4728 | cell2attr(attrs, fg, bg), NULL); |
| 4729 | dict_add_nr_str(dcell, "width", width, NULL); |
| 4730 | |
| 4731 | ++pos.col; |
| 4732 | if (width == 2) |
| 4733 | ++pos.col; |
| 4734 | } |
| 4735 | } |
| 4736 | |
| 4737 | /* |
| 4738 | * "term_sendkeys(buf, keys)" function |
| 4739 | */ |
| 4740 | void |
| 4741 | f_term_sendkeys(typval_T *argvars, typval_T *rettv) |
| 4742 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4743 | buf_T *buf = term_get_buf(argvars, "term_sendkeys()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4744 | char_u *msg; |
| 4745 | term_T *term; |
| 4746 | |
| 4747 | rettv->v_type = VAR_UNKNOWN; |
| 4748 | if (buf == NULL) |
| 4749 | return; |
| 4750 | |
| 4751 | msg = get_tv_string_chk(&argvars[1]); |
| 4752 | if (msg == NULL) |
| 4753 | return; |
| 4754 | term = buf->b_term; |
| 4755 | if (term->tl_vterm == NULL) |
| 4756 | return; |
| 4757 | |
| 4758 | while (*msg != NUL) |
| 4759 | { |
| 4760 | send_keys_to_term(term, PTR2CHAR(msg), FALSE); |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 4761 | msg += MB_CPTR2LEN(msg); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4762 | } |
| 4763 | } |
| 4764 | |
| 4765 | /* |
Bram Moolenaar | 4d8bac8 | 2018-03-09 21:33:34 +0100 | [diff] [blame] | 4766 | * "term_setrestore(buf, command)" function |
| 4767 | */ |
| 4768 | void |
| 4769 | f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 4770 | { |
| 4771 | #if defined(FEAT_SESSION) |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4772 | buf_T *buf = term_get_buf(argvars, "term_setrestore()"); |
Bram Moolenaar | 4d8bac8 | 2018-03-09 21:33:34 +0100 | [diff] [blame] | 4773 | term_T *term; |
| 4774 | char_u *cmd; |
| 4775 | |
| 4776 | if (buf == NULL) |
| 4777 | return; |
| 4778 | term = buf->b_term; |
| 4779 | vim_free(term->tl_command); |
| 4780 | cmd = get_tv_string_chk(&argvars[1]); |
| 4781 | if (cmd != NULL) |
| 4782 | term->tl_command = vim_strsave(cmd); |
| 4783 | else |
| 4784 | term->tl_command = NULL; |
| 4785 | #endif |
| 4786 | } |
| 4787 | |
| 4788 | /* |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4789 | * "term_setkill(buf, how)" function |
| 4790 | */ |
| 4791 | void |
| 4792 | f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 4793 | { |
| 4794 | buf_T *buf = term_get_buf(argvars, "term_setkill()"); |
| 4795 | term_T *term; |
| 4796 | char_u *how; |
| 4797 | |
| 4798 | if (buf == NULL) |
| 4799 | return; |
| 4800 | term = buf->b_term; |
| 4801 | vim_free(term->tl_kill); |
| 4802 | how = get_tv_string_chk(&argvars[1]); |
| 4803 | if (how != NULL) |
| 4804 | term->tl_kill = vim_strsave(how); |
| 4805 | else |
| 4806 | term->tl_kill = NULL; |
| 4807 | } |
| 4808 | |
| 4809 | /* |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4810 | * "term_start(command, options)" function |
| 4811 | */ |
| 4812 | void |
| 4813 | f_term_start(typval_T *argvars, typval_T *rettv) |
| 4814 | { |
| 4815 | jobopt_T opt; |
| 4816 | buf_T *buf; |
| 4817 | |
| 4818 | init_job_options(&opt); |
| 4819 | if (argvars[1].v_type != VAR_UNKNOWN |
| 4820 | && get_job_options(&argvars[1], &opt, |
| 4821 | JO_TIMEOUT_ALL + JO_STOPONEXIT |
| 4822 | + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK |
| 4823 | + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO, |
| 4824 | JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD |
| 4825 | + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN |
Bram Moolenaar | 4d8bac8 | 2018-03-09 21:33:34 +0100 | [diff] [blame] | 4826 | + JO2_CWD + JO2_ENV + JO2_EOF_CHARS |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4827 | + JO2_NORESTORE + JO2_TERM_KILL) == FAIL) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4828 | return; |
| 4829 | |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 4830 | buf = term_start(&argvars[0], NULL, &opt, 0); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4831 | |
| 4832 | if (buf != NULL && buf->b_term != NULL) |
| 4833 | rettv->vval.v_number = buf->b_fnum; |
| 4834 | } |
| 4835 | |
| 4836 | /* |
| 4837 | * "term_wait" function |
| 4838 | */ |
| 4839 | void |
| 4840 | f_term_wait(typval_T *argvars, typval_T *rettv UNUSED) |
| 4841 | { |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 4842 | buf_T *buf = term_get_buf(argvars, "term_wait()"); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4843 | |
| 4844 | if (buf == NULL) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4845 | return; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4846 | if (buf->b_term->tl_job == NULL) |
| 4847 | { |
| 4848 | ch_log(NULL, "term_wait(): no job to wait for"); |
| 4849 | return; |
| 4850 | } |
| 4851 | if (buf->b_term->tl_job->jv_channel == NULL) |
| 4852 | /* channel is closed, nothing to do */ |
| 4853 | return; |
| 4854 | |
| 4855 | /* Get the job status, this will detect a job that finished. */ |
Bram Moolenaar | a15ef45 | 2018-02-09 16:46:00 +0100 | [diff] [blame] | 4856 | if (!buf->b_term->tl_job->jv_channel->ch_keep_open |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4857 | && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0) |
| 4858 | { |
| 4859 | /* The job is dead, keep reading channel I/O until the channel is |
| 4860 | * closed. buf->b_term may become NULL if the terminal was closed while |
| 4861 | * waiting. */ |
| 4862 | ch_log(NULL, "term_wait(): waiting for channel to close"); |
| 4863 | while (buf->b_term != NULL && !buf->b_term->tl_channel_closed) |
| 4864 | { |
| 4865 | mch_check_messages(); |
| 4866 | parse_queued_messages(); |
Bram Moolenaar | e518226 | 2017-11-19 15:05:44 +0100 | [diff] [blame] | 4867 | if (!buf_valid(buf)) |
| 4868 | /* If the terminal is closed when the channel is closed the |
| 4869 | * buffer disappears. */ |
| 4870 | break; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4871 | ui_delay(10L, FALSE); |
| 4872 | } |
| 4873 | mch_check_messages(); |
| 4874 | parse_queued_messages(); |
| 4875 | } |
| 4876 | else |
| 4877 | { |
| 4878 | long wait = 10L; |
| 4879 | |
| 4880 | mch_check_messages(); |
| 4881 | parse_queued_messages(); |
| 4882 | |
| 4883 | /* Wait for some time for any channel I/O. */ |
| 4884 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 4885 | wait = get_tv_number(&argvars[1]); |
| 4886 | ui_delay(wait, TRUE); |
| 4887 | mch_check_messages(); |
| 4888 | |
| 4889 | /* Flushing messages on channels is hopefully sufficient. |
| 4890 | * TODO: is there a better way? */ |
| 4891 | parse_queued_messages(); |
| 4892 | } |
| 4893 | } |
| 4894 | |
| 4895 | /* |
| 4896 | * Called when a channel has sent all the lines to a terminal. |
| 4897 | * Send a CTRL-D to mark the end of the text. |
| 4898 | */ |
| 4899 | void |
| 4900 | term_send_eof(channel_T *ch) |
| 4901 | { |
| 4902 | term_T *term; |
| 4903 | |
| 4904 | for (term = first_term; term != NULL; term = term->tl_next) |
| 4905 | if (term->tl_job == ch->ch_job) |
| 4906 | { |
| 4907 | if (term->tl_eof_chars != NULL) |
| 4908 | { |
| 4909 | channel_send(ch, PART_IN, term->tl_eof_chars, |
| 4910 | (int)STRLEN(term->tl_eof_chars), NULL); |
| 4911 | channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL); |
| 4912 | } |
| 4913 | # ifdef WIN3264 |
| 4914 | else |
| 4915 | /* Default: CTRL-D */ |
| 4916 | channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL); |
| 4917 | # endif |
| 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | # if defined(WIN3264) || defined(PROTO) |
| 4922 | |
| 4923 | /************************************** |
| 4924 | * 2. MS-Windows implementation. |
| 4925 | */ |
| 4926 | |
| 4927 | # ifndef PROTO |
| 4928 | |
| 4929 | #define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul |
| 4930 | #define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull |
Bram Moolenaar | d317b38 | 2018-02-08 22:33:31 +0100 | [diff] [blame] | 4931 | #define WINPTY_MOUSE_MODE_FORCE 2 |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 4932 | |
| 4933 | void* (*winpty_config_new)(UINT64, void*); |
| 4934 | void* (*winpty_open)(void*, void*); |
| 4935 | void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*); |
| 4936 | BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*); |
| 4937 | void (*winpty_config_set_mouse_mode)(void*, int); |
| 4938 | void (*winpty_config_set_initial_size)(void*, int, int); |
| 4939 | LPCWSTR (*winpty_conin_name)(void*); |
| 4940 | LPCWSTR (*winpty_conout_name)(void*); |
| 4941 | LPCWSTR (*winpty_conerr_name)(void*); |
| 4942 | void (*winpty_free)(void*); |
| 4943 | void (*winpty_config_free)(void*); |
| 4944 | void (*winpty_spawn_config_free)(void*); |
| 4945 | void (*winpty_error_free)(void*); |
| 4946 | LPCWSTR (*winpty_error_msg)(void*); |
| 4947 | BOOL (*winpty_set_size)(void*, int, int, void*); |
| 4948 | HANDLE (*winpty_agent_process)(void*); |
| 4949 | |
| 4950 | #define WINPTY_DLL "winpty.dll" |
| 4951 | |
| 4952 | static HINSTANCE hWinPtyDLL = NULL; |
| 4953 | # endif |
| 4954 | |
| 4955 | static int |
| 4956 | dyn_winpty_init(int verbose) |
| 4957 | { |
| 4958 | int i; |
| 4959 | static struct |
| 4960 | { |
| 4961 | char *name; |
| 4962 | FARPROC *ptr; |
| 4963 | } winpty_entry[] = |
| 4964 | { |
| 4965 | {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name}, |
| 4966 | {"winpty_config_free", (FARPROC*)&winpty_config_free}, |
| 4967 | {"winpty_config_new", (FARPROC*)&winpty_config_new}, |
| 4968 | {"winpty_config_set_mouse_mode", |
| 4969 | (FARPROC*)&winpty_config_set_mouse_mode}, |
| 4970 | {"winpty_config_set_initial_size", |
| 4971 | (FARPROC*)&winpty_config_set_initial_size}, |
| 4972 | {"winpty_conin_name", (FARPROC*)&winpty_conin_name}, |
| 4973 | {"winpty_conout_name", (FARPROC*)&winpty_conout_name}, |
| 4974 | {"winpty_error_free", (FARPROC*)&winpty_error_free}, |
| 4975 | {"winpty_free", (FARPROC*)&winpty_free}, |
| 4976 | {"winpty_open", (FARPROC*)&winpty_open}, |
| 4977 | {"winpty_spawn", (FARPROC*)&winpty_spawn}, |
| 4978 | {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free}, |
| 4979 | {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new}, |
| 4980 | {"winpty_error_msg", (FARPROC*)&winpty_error_msg}, |
| 4981 | {"winpty_set_size", (FARPROC*)&winpty_set_size}, |
| 4982 | {"winpty_agent_process", (FARPROC*)&winpty_agent_process}, |
| 4983 | {NULL, NULL} |
| 4984 | }; |
| 4985 | |
| 4986 | /* No need to initialize twice. */ |
| 4987 | if (hWinPtyDLL) |
| 4988 | return OK; |
| 4989 | /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just |
| 4990 | * winpty.dll. */ |
| 4991 | if (*p_winptydll != NUL) |
| 4992 | hWinPtyDLL = vimLoadLib((char *)p_winptydll); |
| 4993 | if (!hWinPtyDLL) |
| 4994 | hWinPtyDLL = vimLoadLib(WINPTY_DLL); |
| 4995 | if (!hWinPtyDLL) |
| 4996 | { |
| 4997 | if (verbose) |
| 4998 | EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll |
| 4999 | : (char_u *)WINPTY_DLL); |
| 5000 | return FAIL; |
| 5001 | } |
| 5002 | for (i = 0; winpty_entry[i].name != NULL |
| 5003 | && winpty_entry[i].ptr != NULL; ++i) |
| 5004 | { |
| 5005 | if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL, |
| 5006 | winpty_entry[i].name)) == NULL) |
| 5007 | { |
| 5008 | if (verbose) |
| 5009 | EMSG2(_(e_loadfunc), winpty_entry[i].name); |
| 5010 | return FAIL; |
| 5011 | } |
| 5012 | } |
| 5013 | |
| 5014 | return OK; |
| 5015 | } |
| 5016 | |
| 5017 | /* |
| 5018 | * Create a new terminal of "rows" by "cols" cells. |
| 5019 | * Store a reference in "term". |
| 5020 | * Return OK or FAIL. |
| 5021 | */ |
| 5022 | static int |
| 5023 | term_and_job_init( |
| 5024 | term_T *term, |
| 5025 | typval_T *argvar, |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 5026 | char **argv UNUSED, |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5027 | jobopt_T *opt) |
| 5028 | { |
| 5029 | WCHAR *cmd_wchar = NULL; |
| 5030 | WCHAR *cwd_wchar = NULL; |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 5031 | WCHAR *env_wchar = NULL; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5032 | channel_T *channel = NULL; |
| 5033 | job_T *job = NULL; |
| 5034 | DWORD error; |
| 5035 | HANDLE jo = NULL; |
| 5036 | HANDLE child_process_handle; |
| 5037 | HANDLE child_thread_handle; |
Bram Moolenaar | 4aad53c | 2018-01-26 21:11:03 +0100 | [diff] [blame] | 5038 | void *winpty_err = NULL; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5039 | void *spawn_config = NULL; |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 5040 | garray_T ga_cmd, ga_env; |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 5041 | char_u *cmd = NULL; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5042 | |
| 5043 | if (dyn_winpty_init(TRUE) == FAIL) |
| 5044 | return FAIL; |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 5045 | ga_init2(&ga_cmd, (int)sizeof(char*), 20); |
| 5046 | ga_init2(&ga_env, (int)sizeof(char*), 20); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5047 | |
| 5048 | if (argvar->v_type == VAR_STRING) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5049 | { |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 5050 | cmd = argvar->vval.v_string; |
| 5051 | } |
| 5052 | else if (argvar->v_type == VAR_LIST) |
| 5053 | { |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 5054 | if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL) |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5055 | goto failed; |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 5056 | cmd = ga_cmd.ga_data; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5057 | } |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 5058 | if (cmd == NULL || *cmd == NUL) |
| 5059 | { |
| 5060 | EMSG(_(e_invarg)); |
| 5061 | goto failed; |
| 5062 | } |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5063 | |
| 5064 | cmd_wchar = enc_to_utf16(cmd, NULL); |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 5065 | ga_clear(&ga_cmd); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5066 | if (cmd_wchar == NULL) |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 5067 | goto failed; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5068 | if (opt->jo_cwd != NULL) |
| 5069 | cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL); |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 5070 | |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 5071 | win32_build_env(opt->jo_env, &ga_env, TRUE); |
| 5072 | env_wchar = ga_env.ga_data; |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5073 | |
| 5074 | job = job_alloc(); |
| 5075 | if (job == NULL) |
| 5076 | goto failed; |
| 5077 | |
| 5078 | channel = add_channel(); |
| 5079 | if (channel == NULL) |
| 5080 | goto failed; |
| 5081 | |
| 5082 | term->tl_winpty_config = winpty_config_new(0, &winpty_err); |
| 5083 | if (term->tl_winpty_config == NULL) |
| 5084 | goto failed; |
| 5085 | |
| 5086 | winpty_config_set_mouse_mode(term->tl_winpty_config, |
| 5087 | WINPTY_MOUSE_MODE_FORCE); |
| 5088 | winpty_config_set_initial_size(term->tl_winpty_config, |
| 5089 | term->tl_cols, term->tl_rows); |
| 5090 | term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err); |
| 5091 | if (term->tl_winpty == NULL) |
| 5092 | goto failed; |
| 5093 | |
| 5094 | spawn_config = winpty_spawn_config_new( |
| 5095 | WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN | |
| 5096 | WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN, |
| 5097 | NULL, |
| 5098 | cmd_wchar, |
| 5099 | cwd_wchar, |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 5100 | env_wchar, |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5101 | &winpty_err); |
| 5102 | if (spawn_config == NULL) |
| 5103 | goto failed; |
| 5104 | |
| 5105 | channel = add_channel(); |
| 5106 | if (channel == NULL) |
| 5107 | goto failed; |
| 5108 | |
| 5109 | job = job_alloc(); |
| 5110 | if (job == NULL) |
| 5111 | goto failed; |
| 5112 | |
| 5113 | if (opt->jo_set & JO_IN_BUF) |
| 5114 | job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]); |
| 5115 | |
| 5116 | if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle, |
| 5117 | &child_thread_handle, &error, &winpty_err)) |
| 5118 | goto failed; |
| 5119 | |
| 5120 | channel_set_pipes(channel, |
| 5121 | (sock_T)CreateFileW( |
| 5122 | winpty_conin_name(term->tl_winpty), |
| 5123 | GENERIC_WRITE, 0, NULL, |
| 5124 | OPEN_EXISTING, 0, NULL), |
| 5125 | (sock_T)CreateFileW( |
| 5126 | winpty_conout_name(term->tl_winpty), |
| 5127 | GENERIC_READ, 0, NULL, |
| 5128 | OPEN_EXISTING, 0, NULL), |
| 5129 | (sock_T)CreateFileW( |
| 5130 | winpty_conerr_name(term->tl_winpty), |
| 5131 | GENERIC_READ, 0, NULL, |
| 5132 | OPEN_EXISTING, 0, NULL)); |
| 5133 | |
| 5134 | /* Write lines with CR instead of NL. */ |
| 5135 | channel->ch_write_text_mode = TRUE; |
| 5136 | |
| 5137 | jo = CreateJobObject(NULL, NULL); |
| 5138 | if (jo == NULL) |
| 5139 | goto failed; |
| 5140 | |
| 5141 | if (!AssignProcessToJobObject(jo, child_process_handle)) |
| 5142 | { |
| 5143 | /* Failed, switch the way to terminate process with TerminateProcess. */ |
| 5144 | CloseHandle(jo); |
| 5145 | jo = NULL; |
| 5146 | } |
| 5147 | |
| 5148 | winpty_spawn_config_free(spawn_config); |
| 5149 | vim_free(cmd_wchar); |
| 5150 | vim_free(cwd_wchar); |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 5151 | vim_free(env_wchar); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5152 | |
| 5153 | create_vterm(term, term->tl_rows, term->tl_cols); |
| 5154 | |
| 5155 | channel_set_job(channel, job, opt); |
| 5156 | job_set_options(job, opt); |
| 5157 | |
| 5158 | job->jv_channel = channel; |
| 5159 | job->jv_proc_info.hProcess = child_process_handle; |
| 5160 | job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle); |
| 5161 | job->jv_job_object = jo; |
| 5162 | job->jv_status = JOB_STARTED; |
| 5163 | job->jv_tty_in = utf16_to_enc( |
| 5164 | (short_u*)winpty_conin_name(term->tl_winpty), NULL); |
| 5165 | job->jv_tty_out = utf16_to_enc( |
| 5166 | (short_u*)winpty_conout_name(term->tl_winpty), NULL); |
| 5167 | ++job->jv_refcount; |
| 5168 | term->tl_job = job; |
| 5169 | |
| 5170 | return OK; |
| 5171 | |
| 5172 | failed: |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 5173 | ga_clear(&ga_cmd); |
| 5174 | ga_clear(&ga_env); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5175 | vim_free(cmd_wchar); |
| 5176 | vim_free(cwd_wchar); |
| 5177 | if (spawn_config != NULL) |
| 5178 | winpty_spawn_config_free(spawn_config); |
| 5179 | if (channel != NULL) |
| 5180 | channel_clear(channel); |
| 5181 | if (job != NULL) |
| 5182 | { |
| 5183 | job->jv_channel = NULL; |
| 5184 | job_cleanup(job); |
| 5185 | } |
| 5186 | term->tl_job = NULL; |
| 5187 | if (jo != NULL) |
| 5188 | CloseHandle(jo); |
| 5189 | if (term->tl_winpty != NULL) |
| 5190 | winpty_free(term->tl_winpty); |
| 5191 | term->tl_winpty = NULL; |
| 5192 | if (term->tl_winpty_config != NULL) |
| 5193 | winpty_config_free(term->tl_winpty_config); |
| 5194 | term->tl_winpty_config = NULL; |
| 5195 | if (winpty_err != NULL) |
| 5196 | { |
| 5197 | char_u *msg = utf16_to_enc( |
| 5198 | (short_u *)winpty_error_msg(winpty_err), NULL); |
| 5199 | |
| 5200 | EMSG(msg); |
| 5201 | winpty_error_free(winpty_err); |
| 5202 | } |
| 5203 | return FAIL; |
| 5204 | } |
| 5205 | |
| 5206 | static int |
| 5207 | create_pty_only(term_T *term, jobopt_T *options) |
| 5208 | { |
| 5209 | HANDLE hPipeIn = INVALID_HANDLE_VALUE; |
| 5210 | HANDLE hPipeOut = INVALID_HANDLE_VALUE; |
| 5211 | char in_name[80], out_name[80]; |
| 5212 | channel_T *channel = NULL; |
| 5213 | |
| 5214 | create_vterm(term, term->tl_rows, term->tl_cols); |
| 5215 | |
| 5216 | vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d", |
| 5217 | GetCurrentProcessId(), |
| 5218 | curbuf->b_fnum); |
| 5219 | hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND, |
| 5220 | PIPE_TYPE_MESSAGE | PIPE_NOWAIT, |
| 5221 | PIPE_UNLIMITED_INSTANCES, |
| 5222 | 0, 0, NMPWAIT_NOWAIT, NULL); |
| 5223 | if (hPipeIn == INVALID_HANDLE_VALUE) |
| 5224 | goto failed; |
| 5225 | |
| 5226 | vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d", |
| 5227 | GetCurrentProcessId(), |
| 5228 | curbuf->b_fnum); |
| 5229 | hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND, |
| 5230 | PIPE_TYPE_MESSAGE | PIPE_NOWAIT, |
| 5231 | PIPE_UNLIMITED_INSTANCES, |
| 5232 | 0, 0, 0, NULL); |
| 5233 | if (hPipeOut == INVALID_HANDLE_VALUE) |
| 5234 | goto failed; |
| 5235 | |
| 5236 | ConnectNamedPipe(hPipeIn, NULL); |
| 5237 | ConnectNamedPipe(hPipeOut, NULL); |
| 5238 | |
| 5239 | term->tl_job = job_alloc(); |
| 5240 | if (term->tl_job == NULL) |
| 5241 | goto failed; |
| 5242 | ++term->tl_job->jv_refcount; |
| 5243 | |
| 5244 | /* behave like the job is already finished */ |
| 5245 | term->tl_job->jv_status = JOB_FINISHED; |
| 5246 | |
| 5247 | channel = add_channel(); |
| 5248 | if (channel == NULL) |
| 5249 | goto failed; |
| 5250 | term->tl_job->jv_channel = channel; |
| 5251 | channel->ch_keep_open = TRUE; |
| 5252 | channel->ch_named_pipe = TRUE; |
| 5253 | |
| 5254 | channel_set_pipes(channel, |
| 5255 | (sock_T)hPipeIn, |
| 5256 | (sock_T)hPipeOut, |
| 5257 | (sock_T)hPipeOut); |
| 5258 | channel_set_job(channel, term->tl_job, options); |
| 5259 | term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name); |
| 5260 | term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name); |
| 5261 | |
| 5262 | return OK; |
| 5263 | |
| 5264 | failed: |
| 5265 | if (hPipeIn != NULL) |
| 5266 | CloseHandle(hPipeIn); |
| 5267 | if (hPipeOut != NULL) |
| 5268 | CloseHandle(hPipeOut); |
| 5269 | return FAIL; |
| 5270 | } |
| 5271 | |
| 5272 | /* |
| 5273 | * Free the terminal emulator part of "term". |
| 5274 | */ |
| 5275 | static void |
| 5276 | term_free_vterm(term_T *term) |
| 5277 | { |
| 5278 | if (term->tl_winpty != NULL) |
| 5279 | winpty_free(term->tl_winpty); |
| 5280 | term->tl_winpty = NULL; |
| 5281 | if (term->tl_winpty_config != NULL) |
| 5282 | winpty_config_free(term->tl_winpty_config); |
| 5283 | term->tl_winpty_config = NULL; |
| 5284 | if (term->tl_vterm != NULL) |
| 5285 | vterm_free(term->tl_vterm); |
| 5286 | term->tl_vterm = NULL; |
| 5287 | } |
| 5288 | |
| 5289 | /* |
| 5290 | * Request size to terminal. |
| 5291 | */ |
| 5292 | static void |
| 5293 | term_report_winsize(term_T *term, int rows, int cols) |
| 5294 | { |
| 5295 | if (term->tl_winpty) |
| 5296 | winpty_set_size(term->tl_winpty, cols, rows, NULL); |
| 5297 | } |
| 5298 | |
| 5299 | int |
| 5300 | terminal_enabled(void) |
| 5301 | { |
| 5302 | return dyn_winpty_init(FALSE) == OK; |
| 5303 | } |
| 5304 | |
| 5305 | # else |
| 5306 | |
| 5307 | /************************************** |
| 5308 | * 3. Unix-like implementation. |
| 5309 | */ |
| 5310 | |
| 5311 | /* |
| 5312 | * Create a new terminal of "rows" by "cols" cells. |
| 5313 | * Start job for "cmd". |
| 5314 | * Store the pointers in "term". |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 5315 | * When "argv" is not NULL then "argvar" is not used. |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5316 | * Return OK or FAIL. |
| 5317 | */ |
| 5318 | static int |
| 5319 | term_and_job_init( |
| 5320 | term_T *term, |
| 5321 | typval_T *argvar, |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 5322 | char **argv, |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5323 | jobopt_T *opt) |
| 5324 | { |
| 5325 | create_vterm(term, term->tl_rows, term->tl_cols); |
| 5326 | |
Bram Moolenaar | 1356825 | 2018-03-16 20:46:58 +0100 | [diff] [blame] | 5327 | /* This may change a string in "argvar". */ |
| 5328 | term->tl_job = job_start(argvar, argv, opt); |
Bram Moolenaar | 2e6ab18 | 2017-09-20 10:03:07 +0200 | [diff] [blame] | 5329 | if (term->tl_job != NULL) |
| 5330 | ++term->tl_job->jv_refcount; |
| 5331 | |
| 5332 | return term->tl_job != NULL |
| 5333 | && term->tl_job->jv_channel != NULL |
| 5334 | && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL; |
| 5335 | } |
| 5336 | |
| 5337 | static int |
| 5338 | create_pty_only(term_T *term, jobopt_T *opt) |
| 5339 | { |
| 5340 | create_vterm(term, term->tl_rows, term->tl_cols); |
| 5341 | |
| 5342 | term->tl_job = job_alloc(); |
| 5343 | if (term->tl_job == NULL) |
| 5344 | return FAIL; |
| 5345 | ++term->tl_job->jv_refcount; |
| 5346 | |
| 5347 | /* behave like the job is already finished */ |
| 5348 | term->tl_job->jv_status = JOB_FINISHED; |
| 5349 | |
| 5350 | return mch_create_pty_channel(term->tl_job, opt); |
| 5351 | } |
| 5352 | |
| 5353 | /* |
| 5354 | * Free the terminal emulator part of "term". |
| 5355 | */ |
| 5356 | static void |
| 5357 | term_free_vterm(term_T *term) |
| 5358 | { |
| 5359 | if (term->tl_vterm != NULL) |
| 5360 | vterm_free(term->tl_vterm); |
| 5361 | term->tl_vterm = NULL; |
| 5362 | } |
| 5363 | |
| 5364 | /* |
| 5365 | * Request size to terminal. |
| 5366 | */ |
| 5367 | static void |
| 5368 | term_report_winsize(term_T *term, int rows, int cols) |
| 5369 | { |
| 5370 | /* Use an ioctl() to report the new window size to the job. */ |
| 5371 | if (term->tl_job != NULL && term->tl_job->jv_channel != NULL) |
| 5372 | { |
| 5373 | int fd = -1; |
| 5374 | int part; |
| 5375 | |
| 5376 | for (part = PART_OUT; part < PART_COUNT; ++part) |
| 5377 | { |
| 5378 | fd = term->tl_job->jv_channel->ch_part[part].ch_fd; |
| 5379 | if (isatty(fd)) |
| 5380 | break; |
| 5381 | } |
| 5382 | if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK) |
| 5383 | mch_signal_job(term->tl_job, (char_u *)"winch"); |
| 5384 | } |
| 5385 | } |
| 5386 | |
| 5387 | # endif |
| 5388 | |
| 5389 | #endif /* FEAT_TERMINAL */ |