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