Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +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 | * |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 13 | * There are three parts: |
| 14 | * 1. Generic code for all systems. |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 15 | * Uses libvterm for the terminal emulator. |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 16 | * 2. The MS-Windows implementation. |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 17 | * Uses winpty. |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 18 | * 3. The Unix-like implementation. |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 19 | * Uses pseudo-tty's (pty's). |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 20 | * |
| 21 | * For each terminal one VTerm is constructed. This uses libvterm. A copy of |
| 22 | * that library is in the libvterm directory. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 23 | * |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 24 | * When a terminal window is opened, a job is started that will be connected to |
| 25 | * the terminal emulator. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 26 | * |
| 27 | * If the terminal window has keyboard focus, typed keys are converted to the |
| 28 | * terminal encoding and writting to the job over a channel. |
| 29 | * |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 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. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 34 | * |
| 35 | * TODO: |
Bram Moolenaar | eeac677 | 2017-07-23 15:48:37 +0200 | [diff] [blame] | 36 | * - cursor flickers when moving the cursor |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 37 | * - set buffer options to be scratch, hidden, nomodifiable, etc. |
| 38 | * - set buffer name to command, add (1) to avoid duplicates. |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 39 | * - Add a scrollback buffer (contains lines to scroll off the top). |
| 40 | * Can use the buf_T lines, store attributes somewhere else? |
| 41 | * - When the job ends: |
| 42 | * - Write "-- JOB ENDED --" in the terminal. |
| 43 | * - Put the terminal contents in the scrollback buffer. |
| 44 | * - Free the terminal emulator. |
| 45 | * - Display the scrollback buffer (but with attributes). |
| 46 | * Make the buffer not modifiable, drop attributes when making changes. |
Bram Moolenaar | 938783d | 2017-07-16 20:13:26 +0200 | [diff] [blame] | 47 | * - when closing window and job has not ended, make terminal hidden? |
Bram Moolenaar | 9f1f49b | 2017-07-22 18:14:17 +0200 | [diff] [blame] | 48 | * - don't allow exiting Vim when a terminal is still running a job |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 49 | * - use win_del_lines() to make scroll-up efficient. |
Bram Moolenaar | 938783d | 2017-07-16 20:13:26 +0200 | [diff] [blame] | 50 | * - command line completion for :terminal |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 51 | * - add test for giving error for invalid 'termsize' value. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 52 | * - support minimal size when 'termsize' is "rows*cols". |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 53 | * - support minimal size when 'termsize' is empty? |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 54 | * - implement "term" for job_start(): more job options when starting a |
| 55 | * terminal. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 56 | * - implement ":buf {term-buf-name}" |
Bram Moolenaar | 96ca27a | 2017-07-17 23:20:24 +0200 | [diff] [blame] | 57 | * - implement term_list() list of buffers with a terminal |
| 58 | * - implement term_getsize(buf) |
| 59 | * - implement term_setsize(buf) |
| 60 | * - implement term_sendkeys(buf, keys) send keystrokes to a terminal |
| 61 | * - implement term_wait(buf) wait for screen to be updated |
| 62 | * - implement term_scrape(buf, row) inspect terminal screen |
| 63 | * - implement term_open(command, options) open terminal window |
| 64 | * - implement term_getjob(buf) |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 65 | * - implement 'termkey' |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 66 | * - when 'encoding' is not utf-8, or the job is using another encoding, setup |
| 67 | * conversions. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 68 | */ |
| 69 | |
| 70 | #include "vim.h" |
| 71 | |
| 72 | #ifdef FEAT_TERMINAL |
| 73 | |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 74 | #ifdef WIN3264 |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 75 | # define MIN(x,y) (x < y ? x : y) |
| 76 | # define MAX(x,y) (x > y ? x : y) |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 77 | #endif |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 78 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 79 | #include "libvterm/include/vterm.h" |
| 80 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 81 | /* typedef term_T in structs.h */ |
| 82 | struct terminal_S { |
| 83 | term_T *tl_next; |
| 84 | |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 85 | #ifdef WIN3264 |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 86 | void *tl_winpty_config; |
| 87 | void *tl_winpty; |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 88 | #endif |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 89 | VTerm *tl_vterm; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 90 | job_T *tl_job; |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 91 | buf_T *tl_buffer; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 92 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 93 | /* last known vterm size */ |
| 94 | int tl_rows; |
| 95 | int tl_cols; |
| 96 | /* vterm size does not follow window size */ |
| 97 | int tl_rows_fixed; |
| 98 | int tl_cols_fixed; |
| 99 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 100 | /* Range of screen rows to update. Zero based. */ |
| 101 | int tl_dirty_row_start; /* -1 if nothing dirty */ |
| 102 | int tl_dirty_row_end; /* row below last one to update */ |
| 103 | |
| 104 | pos_T tl_cursor; |
| 105 | }; |
| 106 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 107 | /* |
| 108 | * List of all active terminals. |
| 109 | */ |
| 110 | static term_T *first_term = NULL; |
| 111 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 112 | |
| 113 | #define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */ |
| 114 | #define KEY_BUF_LEN 200 |
| 115 | |
| 116 | /* |
| 117 | * Functions with separate implementation for MS-Windows and Unix-like systems. |
| 118 | */ |
| 119 | static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd); |
Bram Moolenaar | 43da3e3 | 2017-07-23 17:27:54 +0200 | [diff] [blame] | 120 | static void term_report_winsize(term_T *term, int rows, int cols); |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 121 | static void term_free(term_T *term); |
| 122 | |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 123 | /************************************** |
| 124 | * 1. Generic code for all systems. |
| 125 | */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 126 | |
| 127 | /* |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 128 | * Determine the terminal size from 'termsize' and the current window. |
| 129 | * Assumes term->tl_rows and term->tl_cols are zero. |
| 130 | */ |
| 131 | static void |
| 132 | set_term_and_win_size(term_T *term) |
| 133 | { |
| 134 | if (*curwin->w_p_tms != NUL) |
| 135 | { |
| 136 | char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1; |
| 137 | |
| 138 | term->tl_rows = atoi((char *)curwin->w_p_tms); |
| 139 | term->tl_cols = atoi((char *)p); |
| 140 | } |
| 141 | if (term->tl_rows == 0) |
| 142 | term->tl_rows = curwin->w_height; |
| 143 | else |
| 144 | { |
| 145 | win_setheight_win(term->tl_rows, curwin); |
| 146 | term->tl_rows_fixed = TRUE; |
| 147 | } |
| 148 | if (term->tl_cols == 0) |
| 149 | term->tl_cols = curwin->w_width; |
| 150 | else |
| 151 | { |
| 152 | win_setwidth_win(term->tl_cols, curwin); |
| 153 | term->tl_cols_fixed = TRUE; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 158 | * ":terminal": open a terminal window and execute a job in it. |
| 159 | */ |
| 160 | void |
| 161 | ex_terminal(exarg_T *eap) |
| 162 | { |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 163 | exarg_T split_ea; |
| 164 | win_T *old_curwin = curwin; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 165 | term_T *term; |
Bram Moolenaar | e173fd0 | 2017-07-22 19:03:32 +0200 | [diff] [blame] | 166 | char_u *cmd = eap->arg; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 167 | |
| 168 | if (check_restricted() || check_secure()) |
| 169 | return; |
| 170 | |
| 171 | term = (term_T *)alloc_clear(sizeof(term_T)); |
| 172 | if (term == NULL) |
| 173 | return; |
| 174 | term->tl_dirty_row_end = MAX_ROW; |
| 175 | |
| 176 | /* Open a new window or tab. */ |
| 177 | vim_memset(&split_ea, 0, sizeof(split_ea)); |
| 178 | split_ea.cmdidx = CMD_new; |
| 179 | split_ea.cmd = (char_u *)"new"; |
| 180 | split_ea.arg = (char_u *)""; |
| 181 | ex_splitview(&split_ea); |
| 182 | if (curwin == old_curwin) |
| 183 | { |
| 184 | /* split failed */ |
| 185 | vim_free(term); |
| 186 | return; |
| 187 | } |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 188 | term->tl_buffer = curbuf; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 189 | curbuf->b_term = term; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 190 | |
| 191 | /* Link the new terminal in the list of active terminals. */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 192 | term->tl_next = first_term; |
| 193 | first_term = term; |
| 194 | |
| 195 | /* TODO: set buffer type, hidden, etc. */ |
| 196 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 197 | set_term_and_win_size(term); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 198 | |
Bram Moolenaar | e173fd0 | 2017-07-22 19:03:32 +0200 | [diff] [blame] | 199 | if (cmd == NULL || *cmd == NUL) |
| 200 | cmd = p_sh; |
| 201 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 202 | /* System dependent: setup the vterm and start the job in it. */ |
Bram Moolenaar | e173fd0 | 2017-07-22 19:03:32 +0200 | [diff] [blame] | 203 | if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK) |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 204 | { |
| 205 | /* store the size we ended up with */ |
| 206 | vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 207 | } |
| 208 | else |
| 209 | { |
Bram Moolenaar | 61a6605 | 2017-07-22 18:39:00 +0200 | [diff] [blame] | 210 | free_terminal(term); |
| 211 | curbuf->b_term = NULL; |
| 212 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 213 | /* Wiping out the buffer will also close the window and call |
| 214 | * free_terminal(). */ |
Bram Moolenaar | 96ca27a | 2017-07-17 23:20:24 +0200 | [diff] [blame] | 215 | do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE); |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 216 | } |
Bram Moolenaar | 96ca27a | 2017-07-17 23:20:24 +0200 | [diff] [blame] | 217 | |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 218 | /* TODO: Setup pty, see mch_call_shell(). */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 221 | /* |
Bram Moolenaar | 96ca27a | 2017-07-17 23:20:24 +0200 | [diff] [blame] | 222 | * Free a terminal and everything it refers to. |
| 223 | * Kills the job if there is one. |
| 224 | * Called when wiping out a buffer. |
| 225 | */ |
| 226 | void |
| 227 | free_terminal(term_T *term) |
| 228 | { |
| 229 | term_T *tp; |
| 230 | |
| 231 | if (term == NULL) |
| 232 | return; |
| 233 | if (first_term == term) |
| 234 | first_term = term->tl_next; |
| 235 | else |
| 236 | for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next) |
| 237 | if (tp->tl_next == term) |
| 238 | { |
| 239 | tp->tl_next = term->tl_next; |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | if (term->tl_job != NULL) |
| 244 | { |
Bram Moolenaar | 61a6605 | 2017-07-22 18:39:00 +0200 | [diff] [blame] | 245 | if (term->tl_job->jv_status != JOB_ENDED |
| 246 | && term->tl_job->jv_status != JOB_FAILED) |
Bram Moolenaar | 96ca27a | 2017-07-17 23:20:24 +0200 | [diff] [blame] | 247 | job_stop(term->tl_job, NULL, "kill"); |
| 248 | job_unref(term->tl_job); |
| 249 | } |
| 250 | |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 251 | term_free(term); |
Bram Moolenaar | 96ca27a | 2017-07-17 23:20:24 +0200 | [diff] [blame] | 252 | vim_free(term); |
| 253 | } |
| 254 | |
| 255 | /* |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 256 | * Write job output "msg[len]" to the vterm. |
| 257 | */ |
| 258 | static void |
| 259 | term_write_job_output(term_T *term, char_u *msg, size_t len) |
| 260 | { |
| 261 | VTerm *vterm = term->tl_vterm; |
| 262 | char_u *p; |
| 263 | size_t done; |
| 264 | size_t len_now; |
| 265 | |
| 266 | for (done = 0; done < len; done += len_now) |
| 267 | { |
| 268 | for (p = msg + done; p < msg + len; ) |
| 269 | { |
| 270 | if (*p == NL) |
| 271 | break; |
| 272 | p += utf_ptr2len_len(p, len - (p - msg)); |
| 273 | } |
| 274 | len_now = p - msg - done; |
| 275 | vterm_input_write(vterm, (char *)msg + done, len_now); |
| 276 | if (p < msg + len && *p == NL) |
| 277 | { |
| 278 | /* Convert NL to CR-NL, that appears to work best. */ |
| 279 | vterm_input_write(vterm, "\r\n", 2); |
| 280 | ++len_now; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* this invokes the damage callbacks */ |
| 285 | vterm_screen_flush_damage(vterm_obtain_screen(vterm)); |
| 286 | } |
| 287 | |
| 288 | /* |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 289 | * Invoked when "msg" output from a job was received. Write it to the terminal |
| 290 | * of "buffer". |
| 291 | */ |
| 292 | void |
| 293 | write_to_term(buf_T *buffer, char_u *msg, channel_T *channel) |
| 294 | { |
| 295 | size_t len = STRLEN(msg); |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 296 | term_T *term = buffer->b_term; |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 297 | |
| 298 | ch_logn(channel, "writing %d bytes to terminal", (int)len); |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 299 | term_write_job_output(term, msg, len); |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 300 | |
| 301 | /* TODO: only update once in a while. */ |
| 302 | update_screen(0); |
| 303 | setcursor(); |
| 304 | out_flush(); |
| 305 | } |
| 306 | |
| 307 | /* |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 308 | * Convert typed key "c" into bytes to send to the job. |
| 309 | * Return the number of bytes in "buf". |
| 310 | */ |
| 311 | static int |
| 312 | term_convert_key(int c, char *buf) |
| 313 | { |
| 314 | VTerm *vterm = curbuf->b_term->tl_vterm; |
| 315 | VTermKey key = VTERM_KEY_NONE; |
| 316 | VTermModifier mod = VTERM_MOD_NONE; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 317 | |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 318 | switch (c) |
| 319 | { |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 320 | case CAR: key = VTERM_KEY_ENTER; break; |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 321 | case ESC: key = VTERM_KEY_ESCAPE; break; |
Bram Moolenaar | e906ae8 | 2017-07-21 21:10:01 +0200 | [diff] [blame] | 322 | /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */ |
| 323 | case K_BS: c = BS; break; |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 324 | case K_DEL: key = VTERM_KEY_DEL; break; |
| 325 | case K_DOWN: key = VTERM_KEY_DOWN; break; |
| 326 | case K_END: key = VTERM_KEY_END; break; |
| 327 | case K_F10: key = VTERM_KEY_FUNCTION(10); break; |
| 328 | case K_F11: key = VTERM_KEY_FUNCTION(11); break; |
| 329 | case K_F12: key = VTERM_KEY_FUNCTION(12); break; |
| 330 | case K_F1: key = VTERM_KEY_FUNCTION(1); break; |
| 331 | case K_F2: key = VTERM_KEY_FUNCTION(2); break; |
| 332 | case K_F3: key = VTERM_KEY_FUNCTION(3); break; |
| 333 | case K_F4: key = VTERM_KEY_FUNCTION(4); break; |
| 334 | case K_F5: key = VTERM_KEY_FUNCTION(5); break; |
| 335 | case K_F6: key = VTERM_KEY_FUNCTION(6); break; |
| 336 | case K_F7: key = VTERM_KEY_FUNCTION(7); break; |
| 337 | case K_F8: key = VTERM_KEY_FUNCTION(8); break; |
| 338 | case K_F9: key = VTERM_KEY_FUNCTION(9); break; |
| 339 | case K_HOME: key = VTERM_KEY_HOME; break; |
| 340 | case K_INS: key = VTERM_KEY_INS; break; |
| 341 | case K_K0: key = VTERM_KEY_KP_0; break; |
| 342 | case K_K1: key = VTERM_KEY_KP_1; break; |
| 343 | case K_K2: key = VTERM_KEY_KP_2; break; |
| 344 | case K_K3: key = VTERM_KEY_KP_3; break; |
| 345 | case K_K4: key = VTERM_KEY_KP_4; break; |
| 346 | case K_K5: key = VTERM_KEY_KP_5; break; |
| 347 | case K_K6: key = VTERM_KEY_KP_6; break; |
| 348 | case K_K7: key = VTERM_KEY_KP_7; break; |
| 349 | case K_K8: key = VTERM_KEY_KP_8; break; |
| 350 | case K_K9: key = VTERM_KEY_KP_9; break; |
| 351 | case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */ |
| 352 | case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break; |
| 353 | case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */ |
| 354 | case K_KENTER: key = VTERM_KEY_KP_ENTER; break; |
| 355 | case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */ |
| 356 | case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */ |
| 357 | case K_KMINUS: key = VTERM_KEY_KP_MINUS; break; |
| 358 | case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break; |
| 359 | case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */ |
| 360 | case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */ |
| 361 | case K_KPLUS: key = VTERM_KEY_KP_PLUS; break; |
| 362 | case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break; |
| 363 | case K_LEFT: key = VTERM_KEY_LEFT; break; |
| 364 | case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break; |
| 365 | case K_PAGEUP: key = VTERM_KEY_PAGEUP; break; |
| 366 | case K_RIGHT: key = VTERM_KEY_RIGHT; break; |
| 367 | case K_UP: key = VTERM_KEY_UP; break; |
| 368 | case TAB: key = VTERM_KEY_TAB; break; |
Bram Moolenaar | e825d8b | 2017-07-19 23:20:19 +0200 | [diff] [blame] | 369 | |
| 370 | case K_MOUSEUP: /* TODO */ break; |
| 371 | case K_MOUSEDOWN: /* TODO */ break; |
| 372 | case K_MOUSELEFT: /* TODO */ break; |
| 373 | case K_MOUSERIGHT: /* TODO */ break; |
| 374 | |
| 375 | case K_LEFTMOUSE: /* TODO */ break; |
| 376 | case K_LEFTMOUSE_NM: /* TODO */ break; |
| 377 | case K_LEFTDRAG: /* TODO */ break; |
| 378 | case K_LEFTRELEASE: /* TODO */ break; |
| 379 | case K_LEFTRELEASE_NM: /* TODO */ break; |
| 380 | case K_MIDDLEMOUSE: /* TODO */ break; |
| 381 | case K_MIDDLEDRAG: /* TODO */ break; |
| 382 | case K_MIDDLERELEASE: /* TODO */ break; |
| 383 | case K_RIGHTMOUSE: /* TODO */ break; |
| 384 | case K_RIGHTDRAG: /* TODO */ break; |
| 385 | case K_RIGHTRELEASE: /* TODO */ break; |
| 386 | case K_X1MOUSE: /* TODO */ break; |
| 387 | case K_X1DRAG: /* TODO */ break; |
| 388 | case K_X1RELEASE: /* TODO */ break; |
| 389 | case K_X2MOUSE: /* TODO */ break; |
| 390 | case K_X2DRAG: /* TODO */ break; |
| 391 | case K_X2RELEASE: /* TODO */ break; |
| 392 | |
| 393 | /* TODO: handle all special keys and modifiers that terminal_loop() |
| 394 | * does not handle. */ |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Convert special keys to vterm keys: |
| 399 | * - Write keys to vterm: vterm_keyboard_key() |
| 400 | * - Write output to channel. |
| 401 | */ |
| 402 | if (key != VTERM_KEY_NONE) |
| 403 | /* Special key, let vterm convert it. */ |
| 404 | vterm_keyboard_key(vterm, key, mod); |
| 405 | else |
| 406 | /* Normal character, let vterm convert it. */ |
| 407 | vterm_keyboard_unichar(vterm, c, mod); |
| 408 | |
| 409 | /* Read back the converted escape sequence. */ |
| 410 | return vterm_output_read(vterm, buf, KEY_BUF_LEN); |
| 411 | } |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 412 | |
Bram Moolenaar | 938783d | 2017-07-16 20:13:26 +0200 | [diff] [blame] | 413 | /* |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 414 | * Wait for input and send it to the job. |
| 415 | * Return when the start of a CTRL-W command is typed or anything else that |
| 416 | * should be handled as a Normal mode command. |
| 417 | */ |
| 418 | void |
| 419 | terminal_loop(void) |
| 420 | { |
| 421 | char buf[KEY_BUF_LEN]; |
| 422 | int c; |
| 423 | size_t len; |
| 424 | static int mouse_was_outside = FALSE; |
| 425 | int dragging_outside = FALSE; |
| 426 | |
| 427 | for (;;) |
| 428 | { |
| 429 | /* TODO: skip screen update when handling a sequence of keys. */ |
| 430 | update_screen(0); |
| 431 | setcursor(); |
| 432 | out_flush(); |
| 433 | ++no_mapping; |
| 434 | ++allow_keys; |
| 435 | got_int = FALSE; |
| 436 | c = vgetc(); |
| 437 | --no_mapping; |
| 438 | --allow_keys; |
| 439 | |
| 440 | /* Catch keys that need to be handled as in Normal mode. */ |
| 441 | switch (c) |
| 442 | { |
| 443 | case Ctrl_W: |
| 444 | case NUL: |
| 445 | case K_ZERO: |
| 446 | stuffcharReadbuff(c); |
| 447 | return; |
| 448 | |
| 449 | case K_IGNORE: continue; |
| 450 | |
| 451 | case K_LEFTDRAG: |
| 452 | case K_MIDDLEDRAG: |
| 453 | case K_RIGHTDRAG: |
| 454 | case K_X1DRAG: |
| 455 | case K_X2DRAG: |
| 456 | dragging_outside = mouse_was_outside; |
| 457 | /* FALLTHROUGH */ |
| 458 | case K_LEFTMOUSE: |
| 459 | case K_LEFTMOUSE_NM: |
| 460 | case K_LEFTRELEASE: |
| 461 | case K_LEFTRELEASE_NM: |
| 462 | case K_MIDDLEMOUSE: |
| 463 | case K_MIDDLERELEASE: |
| 464 | case K_RIGHTMOUSE: |
| 465 | case K_RIGHTRELEASE: |
| 466 | case K_X1MOUSE: |
| 467 | case K_X1RELEASE: |
| 468 | case K_X2MOUSE: |
| 469 | case K_X2RELEASE: |
| 470 | if (mouse_row < W_WINROW(curwin) |
| 471 | || mouse_row >= (W_WINROW(curwin) + curwin->w_height) |
| 472 | || mouse_col < W_WINCOL(curwin) |
| 473 | || mouse_col >= W_ENDCOL(curwin) |
| 474 | || dragging_outside) |
| 475 | { |
| 476 | /* click outside the current window */ |
| 477 | stuffcharReadbuff(c); |
| 478 | mouse_was_outside = TRUE; |
| 479 | return; |
| 480 | } |
| 481 | } |
| 482 | mouse_was_outside = FALSE; |
| 483 | |
| 484 | /* Convert the typed key to a sequence of bytes for the job. */ |
| 485 | len = term_convert_key(c, buf); |
| 486 | if (len > 0) |
| 487 | /* TODO: if FAIL is returned, stop? */ |
| 488 | channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN, |
| 489 | (char_u *)buf, len, NULL); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | static void |
| 494 | position_cursor(win_T *wp, VTermPos *pos) |
| 495 | { |
| 496 | wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1)); |
| 497 | wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1)); |
| 498 | } |
| 499 | |
| 500 | static int handle_damage(VTermRect rect, void *user); |
| 501 | static int handle_moverect(VTermRect dest, VTermRect src, void *user); |
| 502 | static int handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user); |
| 503 | static int handle_resize(int rows, int cols, void *user); |
| 504 | |
| 505 | static VTermScreenCallbacks screen_callbacks = { |
| 506 | handle_damage, /* damage */ |
| 507 | handle_moverect, /* moverect */ |
| 508 | handle_movecursor, /* movecursor */ |
| 509 | NULL, /* settermprop */ |
| 510 | NULL, /* bell */ |
| 511 | handle_resize, /* resize */ |
| 512 | NULL, /* sb_pushline */ |
| 513 | NULL /* sb_popline */ |
| 514 | }; |
| 515 | |
| 516 | static int |
| 517 | handle_damage(VTermRect rect, void *user) |
| 518 | { |
| 519 | term_T *term = (term_T *)user; |
| 520 | |
| 521 | term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row); |
| 522 | term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row); |
| 523 | redraw_buf_later(term->tl_buffer, NOT_VALID); |
| 524 | return 1; |
| 525 | } |
| 526 | |
| 527 | static int |
| 528 | handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user) |
| 529 | { |
| 530 | term_T *term = (term_T *)user; |
| 531 | |
| 532 | /* TODO */ |
| 533 | redraw_buf_later(term->tl_buffer, NOT_VALID); |
| 534 | return 1; |
| 535 | } |
| 536 | |
| 537 | static int |
| 538 | handle_movecursor( |
| 539 | VTermPos pos, |
| 540 | VTermPos oldpos UNUSED, |
| 541 | int visible UNUSED, |
| 542 | void *user) |
| 543 | { |
| 544 | term_T *term = (term_T *)user; |
| 545 | win_T *wp; |
| 546 | int is_current = FALSE; |
| 547 | |
| 548 | FOR_ALL_WINDOWS(wp) |
| 549 | { |
| 550 | if (wp->w_buffer == term->tl_buffer) |
| 551 | { |
| 552 | position_cursor(wp, &pos); |
| 553 | if (wp == curwin) |
| 554 | is_current = TRUE; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (is_current) |
| 559 | { |
| 560 | setcursor(); |
| 561 | out_flush(); |
| 562 | } |
| 563 | |
| 564 | return 1; |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * The job running in the terminal resized the terminal. |
| 569 | */ |
| 570 | static int |
| 571 | handle_resize(int rows, int cols, void *user) |
| 572 | { |
| 573 | term_T *term = (term_T *)user; |
| 574 | win_T *wp; |
| 575 | |
| 576 | term->tl_rows = rows; |
| 577 | term->tl_cols = cols; |
| 578 | FOR_ALL_WINDOWS(wp) |
| 579 | { |
| 580 | if (wp->w_buffer == term->tl_buffer) |
| 581 | { |
| 582 | win_setheight_win(rows, wp); |
| 583 | win_setwidth_win(cols, wp); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | redraw_buf_later(term->tl_buffer, NOT_VALID); |
| 588 | return 1; |
| 589 | } |
| 590 | |
| 591 | /* |
Bram Moolenaar | eeac677 | 2017-07-23 15:48:37 +0200 | [diff] [blame] | 592 | * Reverse engineer the RGB value into a cterm color index. |
| 593 | * First color is 1. Return 0 if no match found. |
| 594 | */ |
| 595 | static int |
| 596 | color2index(VTermColor *color) |
| 597 | { |
| 598 | int red = color->red; |
| 599 | int blue = color->blue; |
| 600 | int green = color->green; |
| 601 | |
| 602 | if (red == 0) |
| 603 | { |
| 604 | if (green == 0) |
| 605 | { |
| 606 | if (blue == 0) |
| 607 | return 1; /* black */ |
| 608 | if (blue == 224) |
| 609 | return 5; /* blue */ |
| 610 | } |
| 611 | else if (green == 224) |
| 612 | { |
| 613 | if (blue == 0) |
| 614 | return 3; /* green */ |
| 615 | if (blue == 224) |
| 616 | return 7; /* cyan */ |
| 617 | } |
| 618 | } |
| 619 | else if (red == 224) |
| 620 | { |
| 621 | if (green == 0) |
| 622 | { |
| 623 | if (blue == 0) |
| 624 | return 2; /* red */ |
| 625 | if (blue == 224) |
| 626 | return 6; /* magenta */ |
| 627 | } |
| 628 | else if (green == 224) |
| 629 | { |
| 630 | if (blue == 0) |
| 631 | return 4; /* yellow */ |
| 632 | if (blue == 224) |
| 633 | return 8; /* white */ |
| 634 | } |
| 635 | } |
| 636 | else if (red == 128) |
| 637 | { |
| 638 | if (green == 128 && blue == 128) |
| 639 | return 9; /* high intensity bladk */ |
| 640 | } |
| 641 | else if (red == 255) |
| 642 | { |
| 643 | if (green == 64) |
| 644 | { |
| 645 | if (blue == 64) |
| 646 | return 10; /* high intensity red */ |
| 647 | if (blue == 255) |
| 648 | return 14; /* high intensity magenta */ |
| 649 | } |
| 650 | else if (green == 255) |
| 651 | { |
| 652 | if (blue == 64) |
| 653 | return 12; /* high intensity yellow */ |
| 654 | if (blue == 255) |
| 655 | return 16; /* high intensity white */ |
| 656 | } |
| 657 | } |
| 658 | else if (red == 64) |
| 659 | { |
| 660 | if (green == 64) |
| 661 | { |
| 662 | if (blue == 255) |
| 663 | return 13; /* high intensity blue */ |
| 664 | } |
| 665 | else if (green == 255) |
| 666 | { |
| 667 | if (blue == 64) |
| 668 | return 11; /* high intensity green */ |
| 669 | if (blue == 255) |
| 670 | return 15; /* high intensity cyan */ |
| 671 | } |
| 672 | } |
| 673 | if (t_colors >= 256) |
| 674 | { |
| 675 | if (red == blue && red == green) |
| 676 | { |
| 677 | /* 24-color greyscale */ |
| 678 | static int cutoff[23] = { |
| 679 | 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52, |
| 680 | 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB, |
| 681 | 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9}; |
| 682 | int i; |
| 683 | |
| 684 | for (i = 0; i < 23; ++i) |
| 685 | if (red < cutoff[i]) |
| 686 | return i + 233; |
| 687 | return 256; |
| 688 | } |
| 689 | |
| 690 | /* 216-color cube */ |
| 691 | return 17 + ((red + 25) / 0x33) * 36 |
| 692 | + ((green + 25) / 0x33) * 6 |
| 693 | + (blue + 25) / 0x33; |
| 694 | } |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Convert the attributes of a vterm cell into an attribute index. |
| 700 | */ |
| 701 | static int |
| 702 | cell2attr(VTermScreenCell *cell) |
| 703 | { |
| 704 | int attr = 0; |
| 705 | |
| 706 | if (cell->attrs.bold) |
| 707 | attr |= HL_BOLD; |
| 708 | if (cell->attrs.underline) |
| 709 | attr |= HL_UNDERLINE; |
| 710 | if (cell->attrs.italic) |
| 711 | attr |= HL_ITALIC; |
| 712 | if (cell->attrs.strike) |
| 713 | attr |= HL_STANDOUT; |
| 714 | if (cell->attrs.reverse) |
| 715 | attr |= HL_INVERSE; |
| 716 | if (cell->attrs.strike) |
| 717 | attr |= HL_UNDERLINE; |
| 718 | |
| 719 | #ifdef FEAT_GUI |
| 720 | if (gui.in_use) |
| 721 | { |
Bram Moolenaar | 26af85d | 2017-07-23 16:45:10 +0200 | [diff] [blame] | 722 | guicolor_T fg, bg; |
| 723 | |
| 724 | fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue); |
| 725 | bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue); |
| 726 | return get_gui_attr_idx(attr, fg, bg); |
Bram Moolenaar | eeac677 | 2017-07-23 15:48:37 +0200 | [diff] [blame] | 727 | } |
| 728 | else |
| 729 | #endif |
| 730 | #ifdef FEAT_TERMGUICOLORS |
| 731 | if (p_tgc) |
| 732 | { |
Bram Moolenaar | 065f41c | 2017-07-23 18:07:56 +0200 | [diff] [blame^] | 733 | guicolor_T fg, bg; |
| 734 | |
| 735 | fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue); |
| 736 | bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue); |
| 737 | |
| 738 | return get_tgc_attr_idx(attr, fg, bg); |
Bram Moolenaar | eeac677 | 2017-07-23 15:48:37 +0200 | [diff] [blame] | 739 | } |
Bram Moolenaar | 065f41c | 2017-07-23 18:07:56 +0200 | [diff] [blame^] | 740 | else |
Bram Moolenaar | eeac677 | 2017-07-23 15:48:37 +0200 | [diff] [blame] | 741 | #endif |
| 742 | { |
| 743 | return get_cterm_attr_idx(attr, color2index(&cell->fg), |
| 744 | color2index(&cell->bg)); |
| 745 | } |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | /* |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 750 | * Called to update the window that contains the terminal. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 751 | */ |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 752 | void |
| 753 | term_update_window(win_T *wp) |
Bram Moolenaar | 938783d | 2017-07-16 20:13:26 +0200 | [diff] [blame] | 754 | { |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 755 | term_T *term = wp->w_buffer->b_term; |
| 756 | VTerm *vterm = term->tl_vterm; |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 757 | VTermScreen *screen = vterm_obtain_screen(vterm); |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 758 | VTermState *state = vterm_obtain_state(vterm); |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 759 | VTermPos pos; |
Bram Moolenaar | 938783d | 2017-07-16 20:13:26 +0200 | [diff] [blame] | 760 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 761 | /* |
| 762 | * If the window was resized a redraw will be triggered and we get here. |
| 763 | * Adjust the size of the vterm unless 'termsize' specifies a fixed size. |
| 764 | */ |
| 765 | if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height) |
| 766 | || (!term->tl_cols_fixed && term->tl_cols != wp->w_width)) |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 767 | { |
| 768 | int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height; |
| 769 | int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width; |
| 770 | |
| 771 | vterm_set_size(vterm, rows, cols); |
| 772 | ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines", |
| 773 | rows); |
Bram Moolenaar | 43da3e3 | 2017-07-23 17:27:54 +0200 | [diff] [blame] | 774 | term_report_winsize(term, rows, cols); |
Bram Moolenaar | b13501f | 2017-07-22 22:32:56 +0200 | [diff] [blame] | 775 | } |
Bram Moolenaar | 58556cd | 2017-07-20 23:04:46 +0200 | [diff] [blame] | 776 | |
| 777 | /* The cursor may have been moved when resizing. */ |
| 778 | vterm_state_get_cursorpos(state, &pos); |
| 779 | position_cursor(wp, &pos); |
| 780 | |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 781 | /* TODO: Only redraw what changed. */ |
| 782 | for (pos.row = 0; pos.row < wp->w_height; ++pos.row) |
Bram Moolenaar | 938783d | 2017-07-16 20:13:26 +0200 | [diff] [blame] | 783 | { |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 784 | int off = screen_get_current_line_off(); |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 785 | int max_col = MIN(wp->w_width, term->tl_cols); |
Bram Moolenaar | 938783d | 2017-07-16 20:13:26 +0200 | [diff] [blame] | 786 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 787 | if (pos.row < term->tl_rows) |
| 788 | { |
| 789 | for (pos.col = 0; pos.col < max_col; ) |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 790 | { |
| 791 | VTermScreenCell cell; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 792 | int c; |
Bram Moolenaar | 938783d | 2017-07-16 20:13:26 +0200 | [diff] [blame] | 793 | |
Bram Moolenaar | eeac677 | 2017-07-23 15:48:37 +0200 | [diff] [blame] | 794 | if (vterm_screen_get_cell(screen, pos, &cell) == 0) |
| 795 | vim_memset(&cell, 0, sizeof(cell)); |
| 796 | |
| 797 | /* TODO: composing chars */ |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 798 | c = cell.chars[0]; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 799 | if (c == NUL) |
| 800 | { |
| 801 | ScreenLines[off] = ' '; |
| 802 | ScreenLinesUC[off] = NUL; |
| 803 | } |
| 804 | else |
| 805 | { |
| 806 | #if defined(FEAT_MBYTE) |
| 807 | if (enc_utf8 && c >= 0x80) |
Bram Moolenaar | 9f1f49b | 2017-07-22 18:14:17 +0200 | [diff] [blame] | 808 | { |
| 809 | ScreenLines[off] = ' '; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 810 | ScreenLinesUC[off] = c; |
Bram Moolenaar | 9f1f49b | 2017-07-22 18:14:17 +0200 | [diff] [blame] | 811 | } |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 812 | else |
Bram Moolenaar | 9f1f49b | 2017-07-22 18:14:17 +0200 | [diff] [blame] | 813 | { |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 814 | ScreenLines[off] = c; |
Bram Moolenaar | 9f1f49b | 2017-07-22 18:14:17 +0200 | [diff] [blame] | 815 | ScreenLinesUC[off] = NUL; |
| 816 | } |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 817 | #else |
| 818 | ScreenLines[off] = c; |
| 819 | #endif |
| 820 | } |
Bram Moolenaar | eeac677 | 2017-07-23 15:48:37 +0200 | [diff] [blame] | 821 | ScreenAttrs[off] = cell2attr(&cell); |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 822 | |
| 823 | ++pos.col; |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 824 | ++off; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 825 | if (cell.width == 2) |
| 826 | { |
Bram Moolenaar | 9f1f49b | 2017-07-22 18:14:17 +0200 | [diff] [blame] | 827 | ScreenLines[off] = NUL; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 828 | ScreenLinesUC[off] = NUL; |
| 829 | ++pos.col; |
| 830 | ++off; |
| 831 | } |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 832 | } |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 833 | } |
Bram Moolenaar | e825d8b | 2017-07-19 23:20:19 +0200 | [diff] [blame] | 834 | else |
| 835 | pos.col = 0; |
Bram Moolenaar | 938783d | 2017-07-16 20:13:26 +0200 | [diff] [blame] | 836 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 837 | screen_line(wp->w_winrow + pos.row, wp->w_wincol, |
| 838 | pos.col, wp->w_width, FALSE); |
Bram Moolenaar | 938783d | 2017-07-16 20:13:26 +0200 | [diff] [blame] | 839 | } |
| 840 | } |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 841 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 842 | /* |
| 843 | * Set job options common for Unix and MS-Windows. |
| 844 | */ |
| 845 | static void |
| 846 | setup_job_options(jobopt_T *opt, int rows, int cols) |
| 847 | { |
| 848 | clear_job_options(opt); |
| 849 | opt->jo_mode = MODE_RAW; |
| 850 | opt->jo_out_mode = MODE_RAW; |
| 851 | opt->jo_err_mode = MODE_RAW; |
| 852 | opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE; |
| 853 | opt->jo_io[PART_OUT] = JIO_BUFFER; |
| 854 | opt->jo_io[PART_ERR] = JIO_BUFFER; |
| 855 | opt->jo_set |= JO_OUT_IO + (JO_OUT_IO << (PART_ERR - PART_OUT)); |
| 856 | opt->jo_io_buf[PART_OUT] = curbuf->b_fnum; |
| 857 | opt->jo_io_buf[PART_ERR] = curbuf->b_fnum; |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 858 | opt->jo_pty = TRUE; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 859 | opt->jo_set |= JO_OUT_BUF + (JO_OUT_BUF << (PART_ERR - PART_OUT)); |
| 860 | opt->jo_term_rows = rows; |
| 861 | opt->jo_term_cols = cols; |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | * Create a new vterm and initialize it. |
| 866 | */ |
| 867 | static void |
| 868 | create_vterm(term_T *term, int rows, int cols) |
| 869 | { |
| 870 | VTerm *vterm; |
| 871 | VTermScreen *screen; |
| 872 | |
| 873 | vterm = vterm_new(rows, cols); |
| 874 | term->tl_vterm = vterm; |
| 875 | screen = vterm_obtain_screen(vterm); |
| 876 | vterm_screen_set_callbacks(screen, &screen_callbacks, term); |
| 877 | /* TODO: depends on 'encoding'. */ |
| 878 | vterm_set_utf8(vterm, 1); |
Bram Moolenaar | eeac677 | 2017-07-23 15:48:37 +0200 | [diff] [blame] | 879 | |
| 880 | /* Vterm uses a default black background. Set it to white when |
| 881 | * 'background' is "light". */ |
| 882 | if (*p_bg == 'l') |
| 883 | { |
| 884 | VTermColor fg, bg; |
| 885 | |
| 886 | fg.red = fg.green = fg.blue = 0; |
| 887 | bg.red = bg.green = bg.blue = 255; |
| 888 | vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg); |
| 889 | } |
| 890 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 891 | /* Required to initialize most things. */ |
| 892 | vterm_screen_reset(screen, 1 /* hard */); |
| 893 | } |
| 894 | |
| 895 | # ifdef WIN3264 |
| 896 | |
| 897 | #define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul |
| 898 | #define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull |
| 899 | |
| 900 | void* (*winpty_config_new)(int, void*); |
| 901 | void* (*winpty_open)(void*, void*); |
| 902 | void* (*winpty_spawn_config_new)(int, void*, LPCWSTR, void*, void*, void*); |
| 903 | BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*); |
| 904 | void (*winpty_config_set_initial_size)(void*, int, int); |
| 905 | LPCWSTR (*winpty_conin_name)(void*); |
| 906 | LPCWSTR (*winpty_conout_name)(void*); |
| 907 | LPCWSTR (*winpty_conerr_name)(void*); |
| 908 | void (*winpty_free)(void*); |
| 909 | void (*winpty_config_free)(void*); |
| 910 | void (*winpty_spawn_config_free)(void*); |
| 911 | void (*winpty_error_free)(void*); |
| 912 | LPCWSTR (*winpty_error_msg)(void*); |
Bram Moolenaar | 43da3e3 | 2017-07-23 17:27:54 +0200 | [diff] [blame] | 913 | BOOL (*winpty_set_size)(void*, int, int, void*); |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 914 | |
| 915 | /************************************** |
| 916 | * 2. MS-Windows implementation. |
| 917 | */ |
| 918 | |
| 919 | #define WINPTY_DLL "winpty.dll" |
| 920 | |
| 921 | static HINSTANCE hWinPtyDLL = NULL; |
| 922 | |
| 923 | int |
| 924 | dyn_winpty_init(void) |
| 925 | { |
| 926 | int i; |
| 927 | static struct |
| 928 | { |
| 929 | char *name; |
| 930 | FARPROC *ptr; |
| 931 | } winpty_entry[] = |
| 932 | { |
| 933 | {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name}, |
| 934 | {"winpty_config_free", (FARPROC*)&winpty_config_free}, |
| 935 | {"winpty_config_new", (FARPROC*)&winpty_config_new}, |
| 936 | {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size}, |
| 937 | {"winpty_conin_name", (FARPROC*)&winpty_conin_name}, |
| 938 | {"winpty_conout_name", (FARPROC*)&winpty_conout_name}, |
| 939 | {"winpty_error_free", (FARPROC*)&winpty_error_free}, |
| 940 | {"winpty_free", (FARPROC*)&winpty_free}, |
| 941 | {"winpty_open", (FARPROC*)&winpty_open}, |
| 942 | {"winpty_spawn", (FARPROC*)&winpty_spawn}, |
| 943 | {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free}, |
| 944 | {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new}, |
| 945 | {"winpty_error_msg", (FARPROC*)&winpty_error_msg}, |
Bram Moolenaar | 43da3e3 | 2017-07-23 17:27:54 +0200 | [diff] [blame] | 946 | {"winpty_set_size", (FARPROC*)&winpty_set_size}, |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 947 | {NULL, NULL} |
| 948 | }; |
| 949 | |
| 950 | /* No need to initialize twice. */ |
| 951 | if (hWinPtyDLL) |
| 952 | return 1; |
| 953 | /* Load winpty.dll */ |
| 954 | hWinPtyDLL = vimLoadLib(WINPTY_DLL); |
| 955 | if (!hWinPtyDLL) |
| 956 | { |
| 957 | EMSG2(_(e_loadlib), WINPTY_DLL); |
| 958 | return 0; |
| 959 | } |
| 960 | for (i = 0; winpty_entry[i].name != NULL |
| 961 | && winpty_entry[i].ptr != NULL; ++i) |
| 962 | { |
| 963 | if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL, |
| 964 | winpty_entry[i].name)) == NULL) |
| 965 | { |
| 966 | EMSG2(_(e_loadfunc), winpty_entry[i].name); |
| 967 | return 0; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | return 1; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * Create a new terminal of "rows" by "cols" cells. |
| 976 | * Store a reference in "term". |
| 977 | * Return OK or FAIL. |
| 978 | */ |
| 979 | static int |
| 980 | term_and_job_init(term_T *term, int rows, int cols, char_u *cmd) |
| 981 | { |
| 982 | WCHAR *p = enc_to_utf16(cmd, NULL); |
| 983 | channel_T *channel = NULL; |
| 984 | job_T *job = NULL; |
| 985 | jobopt_T opt; |
| 986 | DWORD error; |
| 987 | HANDLE jo = NULL, child_process_handle, child_thread_handle; |
| 988 | void *winpty_err; |
| 989 | void *spawn_config; |
| 990 | |
| 991 | if (!dyn_winpty_init()) |
| 992 | return FAIL; |
| 993 | |
| 994 | if (p == NULL) |
| 995 | return FAIL; |
| 996 | |
| 997 | job = job_alloc(); |
| 998 | if (job == NULL) |
| 999 | goto failed; |
| 1000 | |
| 1001 | channel = add_channel(); |
| 1002 | if (channel == NULL) |
| 1003 | goto failed; |
| 1004 | |
| 1005 | term->tl_winpty_config = winpty_config_new(0, &winpty_err); |
| 1006 | if (term->tl_winpty_config == NULL) |
| 1007 | goto failed; |
| 1008 | |
| 1009 | winpty_config_set_initial_size(term->tl_winpty_config, cols, rows); |
| 1010 | term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err); |
| 1011 | if (term->tl_winpty == NULL) |
| 1012 | goto failed; |
| 1013 | |
| 1014 | spawn_config = winpty_spawn_config_new( |
| 1015 | WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN | |
| 1016 | WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN, |
| 1017 | NULL, |
| 1018 | p, |
| 1019 | NULL, |
| 1020 | NULL, |
| 1021 | &winpty_err); |
| 1022 | if (spawn_config == NULL) |
| 1023 | goto failed; |
| 1024 | |
| 1025 | channel = add_channel(); |
| 1026 | if (channel == NULL) |
| 1027 | goto failed; |
| 1028 | |
| 1029 | job = job_alloc(); |
| 1030 | if (job == NULL) |
| 1031 | goto failed; |
| 1032 | |
| 1033 | if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle, |
| 1034 | &child_thread_handle, &error, &winpty_err)) |
| 1035 | goto failed; |
| 1036 | |
| 1037 | channel_set_pipes(channel, |
| 1038 | (sock_T) CreateFileW( |
| 1039 | winpty_conin_name(term->tl_winpty), |
| 1040 | GENERIC_WRITE, 0, NULL, |
| 1041 | OPEN_EXISTING, 0, NULL), |
| 1042 | (sock_T) CreateFileW( |
| 1043 | winpty_conout_name(term->tl_winpty), |
| 1044 | GENERIC_READ, 0, NULL, |
| 1045 | OPEN_EXISTING, 0, NULL), |
| 1046 | (sock_T) CreateFileW( |
| 1047 | winpty_conerr_name(term->tl_winpty), |
| 1048 | GENERIC_READ, 0, NULL, |
| 1049 | OPEN_EXISTING, 0, NULL)); |
| 1050 | |
| 1051 | jo = CreateJobObject(NULL, NULL); |
| 1052 | if (jo == NULL) |
| 1053 | goto failed; |
| 1054 | |
| 1055 | if (!AssignProcessToJobObject(jo, child_process_handle)) |
| 1056 | goto failed; |
| 1057 | |
| 1058 | winpty_spawn_config_free(spawn_config); |
| 1059 | |
| 1060 | create_vterm(term, rows, cols); |
| 1061 | |
| 1062 | setup_job_options(&opt, rows, cols); |
| 1063 | channel_set_job(channel, job, &opt); |
| 1064 | |
| 1065 | job->jv_channel = channel; |
| 1066 | job->jv_proc_info.hProcess = child_process_handle; |
| 1067 | job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle); |
| 1068 | job->jv_job_object = jo; |
| 1069 | job->jv_status = JOB_STARTED; |
| 1070 | term->tl_job = job; |
| 1071 | |
| 1072 | return OK; |
| 1073 | |
| 1074 | failed: |
| 1075 | if (channel != NULL) |
| 1076 | channel_clear(channel); |
| 1077 | if (job != NULL) |
Bram Moolenaar | cdeae99 | 2017-07-23 17:22:35 +0200 | [diff] [blame] | 1078 | { |
| 1079 | job->jv_channel = NULL; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 1080 | job_cleanup(job); |
Bram Moolenaar | cdeae99 | 2017-07-23 17:22:35 +0200 | [diff] [blame] | 1081 | } |
| 1082 | term->tl_job = NULL; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 1083 | if (jo != NULL) |
| 1084 | CloseHandle(jo); |
| 1085 | if (term->tl_winpty != NULL) |
| 1086 | winpty_free(term->tl_winpty); |
Bram Moolenaar | cdeae99 | 2017-07-23 17:22:35 +0200 | [diff] [blame] | 1087 | term->tl_winpty = NULL; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 1088 | if (term->tl_winpty_config != NULL) |
| 1089 | winpty_config_free(term->tl_winpty_config); |
Bram Moolenaar | cdeae99 | 2017-07-23 17:22:35 +0200 | [diff] [blame] | 1090 | term->tl_winpty_config = NULL; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 1091 | if (winpty_err != NULL) |
| 1092 | { |
| 1093 | char_u *msg = utf16_to_enc( |
| 1094 | (short_u *)winpty_error_msg(winpty_err), NULL); |
| 1095 | |
| 1096 | EMSG(msg); |
| 1097 | winpty_error_free(winpty_err); |
| 1098 | } |
| 1099 | return FAIL; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Free the terminal emulator part of "term". |
| 1104 | */ |
| 1105 | static void |
| 1106 | term_free(term_T *term) |
| 1107 | { |
Bram Moolenaar | cdeae99 | 2017-07-23 17:22:35 +0200 | [diff] [blame] | 1108 | if (term->tl_winpty != NULL) |
| 1109 | winpty_free(term->tl_winpty); |
| 1110 | if (term->tl_winpty_config != NULL) |
| 1111 | winpty_config_free(term->tl_winpty_config); |
| 1112 | if (term->tl_vterm != NULL) |
| 1113 | vterm_free(term->tl_vterm); |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 1114 | } |
| 1115 | |
Bram Moolenaar | 43da3e3 | 2017-07-23 17:27:54 +0200 | [diff] [blame] | 1116 | /* |
| 1117 | * Request size to terminal. |
| 1118 | */ |
| 1119 | static void |
| 1120 | term_report_winsize(term_T *term, int rows, int cols) |
| 1121 | { |
| 1122 | winpty_set_size(term->tl_winpty, cols, rows, NULL); |
| 1123 | } |
| 1124 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 1125 | # else |
| 1126 | |
| 1127 | /************************************** |
| 1128 | * 3. Unix-like implementation. |
| 1129 | */ |
| 1130 | |
| 1131 | /* |
| 1132 | * Create a new terminal of "rows" by "cols" cells. |
| 1133 | * Start job for "cmd". |
| 1134 | * Store the pointers in "term". |
| 1135 | * Return OK or FAIL. |
| 1136 | */ |
| 1137 | static int |
| 1138 | term_and_job_init(term_T *term, int rows, int cols, char_u *cmd) |
| 1139 | { |
| 1140 | typval_T argvars[2]; |
| 1141 | jobopt_T opt; |
| 1142 | |
| 1143 | create_vterm(term, rows, cols); |
| 1144 | |
| 1145 | argvars[0].v_type = VAR_STRING; |
| 1146 | argvars[0].vval.v_string = cmd; |
| 1147 | setup_job_options(&opt, rows, cols); |
| 1148 | term->tl_job = job_start(argvars, &opt); |
| 1149 | |
Bram Moolenaar | 61a6605 | 2017-07-22 18:39:00 +0200 | [diff] [blame] | 1150 | return term->tl_job != NULL |
| 1151 | && term->tl_job->jv_channel != NULL |
| 1152 | && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL; |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | /* |
| 1156 | * Free the terminal emulator part of "term". |
| 1157 | */ |
| 1158 | static void |
| 1159 | term_free(term_T *term) |
| 1160 | { |
Bram Moolenaar | cdeae99 | 2017-07-23 17:22:35 +0200 | [diff] [blame] | 1161 | if (term->tl_vterm != NULL) |
| 1162 | vterm_free(term->tl_vterm); |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 1163 | } |
Bram Moolenaar | 43da3e3 | 2017-07-23 17:27:54 +0200 | [diff] [blame] | 1164 | |
| 1165 | /* |
| 1166 | * Request size to terminal. |
| 1167 | */ |
| 1168 | static void |
| 1169 | term_report_winsize(term_T *term, int rows, int cols) |
| 1170 | { |
| 1171 | /* Use an ioctl() to report the new window size to the job. */ |
| 1172 | if (term->tl_job != NULL && term->tl_job->jv_channel != NULL) |
| 1173 | { |
| 1174 | int fd = -1; |
| 1175 | int part; |
| 1176 | |
| 1177 | for (part = PART_OUT; part < PART_COUNT; ++part) |
| 1178 | { |
| 1179 | fd = term->tl_job->jv_channel->ch_part[part].ch_fd; |
| 1180 | if (isatty(fd)) |
| 1181 | break; |
| 1182 | } |
| 1183 | if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK) |
| 1184 | mch_stop_job(term->tl_job, (char_u *)"winch"); |
| 1185 | } |
| 1186 | } |
| 1187 | |
Bram Moolenaar | 8f84c3a | 2017-07-22 16:14:44 +0200 | [diff] [blame] | 1188 | # endif |
Bram Moolenaar | 8c0095c | 2017-07-18 22:53:21 +0200 | [diff] [blame] | 1189 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 1190 | #endif /* FEAT_TERMINAL */ |