Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | * ui.c: functions that handle the user interface. |
| 12 | * 1. Keyboard input stuff, and a bit of windowing stuff. These are called |
| 13 | * before the machine specific stuff (mch_*) so that we can call the GUI |
| 14 | * stuff instead if the GUI is running. |
Bram Moolenaar | 45fffdf | 2020-03-24 21:42:01 +0100 | [diff] [blame] | 15 | * 2. Input buffer stuff. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "vim.h" |
| 19 | |
| 20 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 21 | ui_write(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | { |
| 23 | #ifdef FEAT_GUI |
| 24 | if (gui.in_use && !gui.dying && !gui.starting) |
| 25 | { |
| 26 | gui_write(s, len); |
| 27 | if (p_wd) |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 28 | gui_wait_for_chars(p_wd, typebuf.tb_change_cnt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | return; |
| 30 | } |
| 31 | #endif |
| 32 | #ifndef NO_CONSOLE |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 33 | // Don't output anything in silent mode ("ex -s") unless 'verbose' set |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 34 | if (!(silent_mode && p_verbose == 0)) |
| 35 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 36 | #if !defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 37 | char_u *tofree = NULL; |
| 38 | |
| 39 | if (output_conv.vc_type != CONV_NONE) |
| 40 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 41 | // Convert characters from 'encoding' to 'termencoding'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 42 | tofree = string_convert(&output_conv, s, &len); |
| 43 | if (tofree != NULL) |
| 44 | s = tofree; |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | mch_write(s, len); |
| 49 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 50 | # if !defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | if (output_conv.vc_type != CONV_NONE) |
| 52 | vim_free(tofree); |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 53 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 54 | } |
| 55 | #endif |
| 56 | } |
| 57 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 58 | #if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | /* |
| 60 | * When executing an external program, there may be some typed characters that |
| 61 | * are not consumed by it. Give them back to ui_inchar() and they are stored |
| 62 | * here for the next call. |
| 63 | */ |
| 64 | static char_u *ta_str = NULL; |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 65 | static int ta_off; // offset for next char to use when ta_str != NULL |
| 66 | static int ta_len; // length of ta_str when it's not NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 67 | |
| 68 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 69 | ui_inchar_undo(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | { |
| 71 | char_u *new; |
| 72 | int newlen; |
| 73 | |
| 74 | newlen = len; |
| 75 | if (ta_str != NULL) |
| 76 | newlen += ta_len - ta_off; |
| 77 | new = alloc(newlen); |
| 78 | if (new != NULL) |
| 79 | { |
| 80 | if (ta_str != NULL) |
| 81 | { |
| 82 | mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off)); |
| 83 | mch_memmove(new + ta_len - ta_off, s, (size_t)len); |
| 84 | vim_free(ta_str); |
| 85 | } |
| 86 | else |
| 87 | mch_memmove(new, s, (size_t)len); |
| 88 | ta_str = new; |
| 89 | ta_len = newlen; |
| 90 | ta_off = 0; |
| 91 | } |
| 92 | } |
| 93 | #endif |
| 94 | |
| 95 | /* |
Bram Moolenaar | b6101cf | 2012-10-21 00:58:39 +0200 | [diff] [blame] | 96 | * ui_inchar(): low level input function. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | * Get characters from the keyboard. |
| 98 | * Return the number of characters that are available. |
| 99 | * If "wtime" == 0 do not wait for characters. |
| 100 | * If "wtime" == -1 wait forever for characters. |
| 101 | * If "wtime" > 0 wait "wtime" milliseconds for a character. |
| 102 | * |
| 103 | * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into |
| 104 | * it. When typebuf.tb_change_cnt changes (e.g., when a message is received |
| 105 | * from a remote client) "buf" can no longer be used. "tb_change_cnt" is NULL |
| 106 | * otherwise. |
| 107 | */ |
| 108 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 109 | ui_inchar( |
| 110 | char_u *buf, |
| 111 | int maxlen, |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 112 | long wtime, // don't use "time", MIPS cannot handle it |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 113 | int tb_change_cnt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 114 | { |
| 115 | int retval = 0; |
| 116 | |
| 117 | #if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS)) |
| 118 | /* |
| 119 | * Use the typeahead if there is any. |
| 120 | */ |
| 121 | if (ta_str != NULL) |
| 122 | { |
| 123 | if (maxlen >= ta_len - ta_off) |
| 124 | { |
| 125 | mch_memmove(buf, ta_str + ta_off, (size_t)ta_len); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 126 | VIM_CLEAR(ta_str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 127 | return ta_len; |
| 128 | } |
| 129 | mch_memmove(buf, ta_str + ta_off, (size_t)maxlen); |
| 130 | ta_off += maxlen; |
| 131 | return maxlen; |
| 132 | } |
| 133 | #endif |
| 134 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 135 | #ifdef FEAT_PROFILE |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 136 | if (do_profiling == PROF_YES && wtime != 0) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 137 | prof_inchar_enter(); |
| 138 | #endif |
| 139 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 140 | #ifdef NO_CONSOLE_INPUT |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 141 | // Don't wait for character input when the window hasn't been opened yet. |
| 142 | // Do try reading, this works when redirecting stdin from a file. |
| 143 | // Must return something, otherwise we'll loop forever. If we run into |
| 144 | // this very often we probably got stuck, exit Vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 145 | if (no_console_input()) |
| 146 | { |
| 147 | static int count = 0; |
| 148 | |
| 149 | # ifndef NO_CONSOLE |
Bram Moolenaar | 1341024 | 2018-11-26 21:19:11 +0100 | [diff] [blame] | 150 | retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt); |
Bram Moolenaar | 43b604c | 2005-03-22 23:06:55 +0000 | [diff] [blame] | 151 | if (retval > 0 || typebuf_changed(tb_change_cnt) || wtime >= 0) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 152 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 153 | # endif |
| 154 | if (wtime == -1 && ++count == 1000) |
| 155 | read_error_exit(); |
| 156 | buf[0] = CAR; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 157 | retval = 1; |
| 158 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 159 | } |
| 160 | #endif |
| 161 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 162 | // If we are going to wait for some time or block... |
Bram Moolenaar | c8da311 | 2007-03-08 12:36:46 +0000 | [diff] [blame] | 163 | if (wtime == -1 || wtime > 100L) |
| 164 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 165 | // ... allow signals to kill us. |
Bram Moolenaar | c8da311 | 2007-03-08 12:36:46 +0000 | [diff] [blame] | 166 | (void)vim_handle_signal(SIGNAL_UNBLOCK); |
| 167 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 168 | // ... there is no need for CTRL-C to interrupt something, don't let |
| 169 | // it set got_int when it was mapped. |
Bram Moolenaar | 5000869 | 2015-01-14 16:08:32 +0100 | [diff] [blame] | 170 | if ((mapped_ctrl_c | curbuf->b_mapped_ctrl_c) & get_real_state()) |
Bram Moolenaar | c8da311 | 2007-03-08 12:36:46 +0000 | [diff] [blame] | 171 | ctrl_c_interrupts = FALSE; |
| 172 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 173 | |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 174 | /* |
| 175 | * Here we call gui_inchar() or mch_inchar(), the GUI or machine-dependent |
| 176 | * input function. The functionality they implement is like this: |
| 177 | * |
| 178 | * while (not timed out) |
| 179 | * { |
| 180 | * handle-resize; |
| 181 | * parse-queued-messages; |
| 182 | * if (waited for 'updatetime') |
| 183 | * trigger-cursorhold; |
| 184 | * ui_wait_for_chars_or_timer() |
| 185 | * if (character available) |
| 186 | * break; |
| 187 | * } |
| 188 | * |
| 189 | * ui_wait_for_chars_or_timer() does: |
| 190 | * |
| 191 | * while (not timed out) |
| 192 | * { |
| 193 | * if (any-timer-triggered) |
| 194 | * invoke-timer-callback; |
| 195 | * wait-for-character(); |
| 196 | * if (character available) |
| 197 | * break; |
| 198 | * } |
| 199 | * |
| 200 | * wait-for-character() does: |
| 201 | * while (not timed out) |
| 202 | * { |
| 203 | * Wait for event; |
| 204 | * if (something on channel) |
| 205 | * read/write channel; |
| 206 | * else if (resized) |
| 207 | * handle_resize(); |
| 208 | * else if (system event) |
| 209 | * deal-with-system-event; |
| 210 | * else if (character available) |
| 211 | * break; |
| 212 | * } |
| 213 | * |
| 214 | */ |
| 215 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 216 | #ifdef FEAT_GUI |
| 217 | if (gui.in_use) |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 218 | retval = gui_inchar(buf, maxlen, wtime, tb_change_cnt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 219 | #endif |
| 220 | #ifndef NO_CONSOLE |
| 221 | # ifdef FEAT_GUI |
| 222 | else |
| 223 | # endif |
| 224 | retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt); |
| 225 | #endif |
| 226 | |
Bram Moolenaar | c8da311 | 2007-03-08 12:36:46 +0000 | [diff] [blame] | 227 | if (wtime == -1 || wtime > 100L) |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 228 | // block SIGHUP et al. |
Bram Moolenaar | c8da311 | 2007-03-08 12:36:46 +0000 | [diff] [blame] | 229 | (void)vim_handle_signal(SIGNAL_BLOCK); |
| 230 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 231 | ctrl_c_interrupts = TRUE; |
| 232 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 233 | #ifdef NO_CONSOLE_INPUT |
| 234 | theend: |
| 235 | #endif |
| 236 | #ifdef FEAT_PROFILE |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 237 | if (do_profiling == PROF_YES && wtime != 0) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 238 | prof_inchar_exit(); |
| 239 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 240 | return retval; |
| 241 | } |
| 242 | |
Bram Moolenaar | 95f0b6e | 2019-12-15 12:54:18 +0100 | [diff] [blame] | 243 | #if defined(UNIX) || defined(VMS) || defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 244 | /* |
| 245 | * Common code for mch_inchar() and gui_inchar(): Wait for a while or |
| 246 | * indefinitely until characters are available, dealing with timers and |
| 247 | * messages on channels. |
| 248 | * |
| 249 | * "buf" may be NULL if the available characters are not to be returned, only |
| 250 | * check if they are available. |
| 251 | * |
| 252 | * Return the number of characters that are available. |
| 253 | * If "wtime" == 0 do not wait for characters. |
| 254 | * If "wtime" == n wait a short time for characters. |
| 255 | * If "wtime" == -1 wait forever for characters. |
| 256 | */ |
| 257 | int |
| 258 | inchar_loop( |
| 259 | char_u *buf, |
| 260 | int maxlen, |
| 261 | long wtime, // don't use "time", MIPS cannot handle it |
| 262 | int tb_change_cnt, |
| 263 | int (*wait_func)(long wtime, int *interrupted, int ignore_input), |
| 264 | int (*resize_func)(int check_only)) |
| 265 | { |
| 266 | int len; |
| 267 | int interrupted = FALSE; |
Bram Moolenaar | 12dfc9e | 2019-01-28 22:32:58 +0100 | [diff] [blame] | 268 | int did_call_wait_func = FALSE; |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 269 | int did_start_blocking = FALSE; |
| 270 | long wait_time; |
| 271 | long elapsed_time = 0; |
| 272 | #ifdef ELAPSED_FUNC |
| 273 | elapsed_T start_tv; |
| 274 | |
| 275 | ELAPSED_INIT(start_tv); |
| 276 | #endif |
| 277 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 278 | // repeat until we got a character or waited long enough |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 279 | for (;;) |
| 280 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 281 | // Check if window changed size while we were busy, perhaps the ":set |
| 282 | // columns=99" command was used. |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 283 | if (resize_func != NULL) |
| 284 | resize_func(FALSE); |
| 285 | |
| 286 | #ifdef MESSAGE_QUEUE |
| 287 | // Only process messages when waiting. |
| 288 | if (wtime != 0) |
| 289 | { |
| 290 | parse_queued_messages(); |
| 291 | // If input was put directly in typeahead buffer bail out here. |
| 292 | if (typebuf_changed(tb_change_cnt)) |
| 293 | return 0; |
| 294 | } |
| 295 | #endif |
| 296 | if (wtime < 0 && did_start_blocking) |
| 297 | // blocking and already waited for p_ut |
| 298 | wait_time = -1; |
| 299 | else |
| 300 | { |
| 301 | if (wtime >= 0) |
| 302 | wait_time = wtime; |
| 303 | else |
| 304 | // going to block after p_ut |
| 305 | wait_time = p_ut; |
| 306 | #ifdef ELAPSED_FUNC |
| 307 | elapsed_time = ELAPSED_FUNC(start_tv); |
| 308 | #endif |
| 309 | wait_time -= elapsed_time; |
Bram Moolenaar | 12dfc9e | 2019-01-28 22:32:58 +0100 | [diff] [blame] | 310 | |
| 311 | // If the waiting time is now zero or less, we timed out. However, |
| 312 | // loop at least once to check for characters and events. Matters |
| 313 | // when "wtime" is zero. |
| 314 | if (wait_time <= 0 && did_call_wait_func) |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 315 | { |
| 316 | if (wtime >= 0) |
| 317 | // no character available within "wtime" |
| 318 | return 0; |
| 319 | |
| 320 | // No character available within 'updatetime'. |
| 321 | did_start_blocking = TRUE; |
| 322 | if (trigger_cursorhold() && maxlen >= 3 |
| 323 | && !typebuf_changed(tb_change_cnt)) |
| 324 | { |
| 325 | // Put K_CURSORHOLD in the input buffer or return it. |
| 326 | if (buf == NULL) |
| 327 | { |
| 328 | char_u ibuf[3]; |
| 329 | |
| 330 | ibuf[0] = CSI; |
| 331 | ibuf[1] = KS_EXTRA; |
| 332 | ibuf[2] = (int)KE_CURSORHOLD; |
| 333 | add_to_input_buf(ibuf, 3); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | buf[0] = K_SPECIAL; |
| 338 | buf[1] = KS_EXTRA; |
| 339 | buf[2] = (int)KE_CURSORHOLD; |
| 340 | } |
| 341 | return 3; |
| 342 | } |
| 343 | |
| 344 | // There is no character available within 'updatetime' seconds: |
| 345 | // flush all the swap files to disk. Also done when |
| 346 | // interrupted by SIGWINCH. |
| 347 | before_blocking(); |
| 348 | continue; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | #ifdef FEAT_JOB_CHANNEL |
| 353 | if (wait_time < 0 || wait_time > 100L) |
| 354 | { |
| 355 | // Checking if a job ended requires polling. Do this at least |
| 356 | // every 100 msec. |
| 357 | if (has_pending_job()) |
| 358 | wait_time = 100L; |
| 359 | |
| 360 | // If there is readahead then parse_queued_messages() timed out and |
| 361 | // we should call it again soon. |
| 362 | if (channel_any_readahead()) |
| 363 | wait_time = 10L; |
| 364 | } |
| 365 | #endif |
| 366 | #ifdef FEAT_BEVAL_GUI |
| 367 | if (p_beval && wait_time > 100L) |
| 368 | // The 'balloonexpr' may indirectly invoke a callback while waiting |
| 369 | // for a character, need to check often. |
| 370 | wait_time = 100L; |
| 371 | #endif |
| 372 | |
| 373 | // Wait for a character to be typed or another event, such as the winch |
| 374 | // signal or an event on the monitored file descriptors. |
Bram Moolenaar | 12dfc9e | 2019-01-28 22:32:58 +0100 | [diff] [blame] | 375 | did_call_wait_func = TRUE; |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 376 | if (wait_func(wait_time, &interrupted, FALSE)) |
| 377 | { |
| 378 | // If input was put directly in typeahead buffer bail out here. |
| 379 | if (typebuf_changed(tb_change_cnt)) |
| 380 | return 0; |
| 381 | |
| 382 | // We might have something to return now. |
| 383 | if (buf == NULL) |
| 384 | // "buf" is NULL, we were just waiting, not actually getting |
| 385 | // input. |
| 386 | return input_available(); |
| 387 | |
| 388 | len = read_from_input_buf(buf, (long)maxlen); |
| 389 | if (len > 0) |
| 390 | return len; |
| 391 | continue; |
| 392 | } |
| 393 | // Timed out or interrupted with no character available. |
| 394 | |
| 395 | #ifndef ELAPSED_FUNC |
| 396 | // estimate the elapsed time |
| 397 | elapsed_time += wait_time; |
| 398 | #endif |
| 399 | |
| 400 | if ((resize_func != NULL && resize_func(TRUE)) |
Bram Moolenaar | 3e9d4d8 | 2019-01-27 17:08:40 +0100 | [diff] [blame] | 401 | #if defined(FEAT_CLIENTSERVER) && defined(UNIX) |
Bram Moolenaar | e40b9d4 | 2019-01-27 16:55:47 +0100 | [diff] [blame] | 402 | || server_waiting() |
| 403 | #endif |
| 404 | #ifdef MESSAGE_QUEUE |
| 405 | || interrupted |
| 406 | #endif |
| 407 | || wait_time > 0 |
| 408 | || (wtime < 0 && !did_start_blocking)) |
| 409 | // no character available, but something to be done, keep going |
| 410 | continue; |
| 411 | |
| 412 | // no character available or interrupted, return zero |
| 413 | break; |
| 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 | #endif |
| 418 | |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 419 | #if defined(FEAT_TIMERS) || defined(PROTO) |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 420 | /* |
| 421 | * Wait for a timer to fire or "wait_func" to return non-zero. |
| 422 | * Returns OK when something was read. |
| 423 | * Returns FAIL when it timed out or was interrupted. |
| 424 | */ |
| 425 | int |
| 426 | ui_wait_for_chars_or_timer( |
| 427 | long wtime, |
| 428 | int (*wait_func)(long wtime, int *interrupted, int ignore_input), |
| 429 | int *interrupted, |
| 430 | int ignore_input) |
| 431 | { |
| 432 | int due_time; |
| 433 | long remaining = wtime; |
| 434 | int tb_change_cnt = typebuf.tb_change_cnt; |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 435 | # ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | e299bbd | 2019-01-17 14:12:02 +0100 | [diff] [blame] | 436 | int brief_wait = FALSE; |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 437 | # endif |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 438 | |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 439 | // When waiting very briefly don't trigger timers. |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 440 | if (wtime >= 0 && wtime < 10L) |
| 441 | return wait_func(wtime, NULL, ignore_input); |
| 442 | |
| 443 | while (wtime < 0 || remaining > 0) |
| 444 | { |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 445 | // Trigger timers and then get the time in wtime until the next one is |
| 446 | // due. Wait up to that time. |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 447 | due_time = check_due_timer(); |
| 448 | if (typebuf.tb_change_cnt != tb_change_cnt) |
| 449 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 450 | // timer may have used feedkeys() |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 451 | return FAIL; |
| 452 | } |
| 453 | if (due_time <= 0 || (wtime > 0 && due_time > remaining)) |
| 454 | due_time = remaining; |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 455 | # if defined(FEAT_JOB_CHANNEL) || defined(FEAT_SOUND_CANBERRA) |
| 456 | if ((due_time < 0 || due_time > 10L) && ( |
| 457 | # if defined(FEAT_JOB_CHANNEL) |
| 458 | ( |
| 459 | # if defined(FEAT_GUI) |
| 460 | !gui.in_use && |
| 461 | # endif |
| 462 | (has_pending_job() || channel_any_readahead())) |
| 463 | # ifdef FEAT_SOUND_CANBERRA |
| 464 | || |
| 465 | # endif |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 466 | # endif |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 467 | # ifdef FEAT_SOUND_CANBERRA |
| 468 | has_any_sound_callback() |
| 469 | # endif |
| 470 | )) |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 471 | { |
| 472 | // There is a pending job or channel, should return soon in order |
| 473 | // to handle them ASAP. Do check for input briefly. |
| 474 | due_time = 10L; |
Bram Moolenaar | 6cdce2a | 2019-09-07 23:25:09 +0200 | [diff] [blame] | 475 | # ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 476 | brief_wait = TRUE; |
Bram Moolenaar | 6cdce2a | 2019-09-07 23:25:09 +0200 | [diff] [blame] | 477 | # endif |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 478 | } |
| 479 | # endif |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 480 | if (wait_func(due_time, interrupted, ignore_input)) |
| 481 | return OK; |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 482 | if ((interrupted != NULL && *interrupted) |
| 483 | # ifdef FEAT_JOB_CHANNEL |
| 484 | || brief_wait |
| 485 | # endif |
| 486 | ) |
| 487 | // Nothing available, but need to return so that side effects get |
| 488 | // handled, such as handling a message on a channel. |
Bram Moolenaar | a338adc | 2018-01-31 20:51:47 +0100 | [diff] [blame] | 489 | return FAIL; |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 490 | if (wtime > 0) |
| 491 | remaining -= due_time; |
| 492 | } |
| 493 | return FAIL; |
| 494 | } |
| 495 | #endif |
| 496 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 497 | /* |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 498 | * Return non-zero if a character is available. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 499 | */ |
| 500 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 501 | ui_char_avail(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 502 | { |
| 503 | #ifdef FEAT_GUI |
| 504 | if (gui.in_use) |
| 505 | { |
| 506 | gui_mch_update(); |
| 507 | return input_available(); |
| 508 | } |
| 509 | #endif |
| 510 | #ifndef NO_CONSOLE |
| 511 | # ifdef NO_CONSOLE_INPUT |
| 512 | if (no_console_input()) |
| 513 | return 0; |
| 514 | # endif |
| 515 | return mch_char_avail(); |
| 516 | #else |
| 517 | return 0; |
| 518 | #endif |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Delay for the given number of milliseconds. If ignoreinput is FALSE then we |
| 523 | * cancel the delay if a key is hit. |
| 524 | */ |
| 525 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 526 | ui_delay(long msec, int ignoreinput) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 527 | { |
Bram Moolenaar | eda1da0 | 2019-11-17 17:06:33 +0100 | [diff] [blame] | 528 | #ifdef FEAT_JOB_CHANNEL |
| 529 | ch_log(NULL, "ui_delay(%ld)", msec); |
| 530 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 531 | #ifdef FEAT_GUI |
| 532 | if (gui.in_use && !ignoreinput) |
Bram Moolenaar | c9e649a | 2017-12-18 18:14:47 +0100 | [diff] [blame] | 533 | gui_wait_for_chars(msec, typebuf.tb_change_cnt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 534 | else |
| 535 | #endif |
| 536 | mch_delay(msec, ignoreinput); |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * If the machine has job control, use it to suspend the program, |
| 541 | * otherwise fake it by starting a new shell. |
| 542 | * When running the GUI iconify the window. |
| 543 | */ |
| 544 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 545 | ui_suspend(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 546 | { |
| 547 | #ifdef FEAT_GUI |
| 548 | if (gui.in_use) |
| 549 | { |
| 550 | gui_mch_iconify(); |
| 551 | return; |
| 552 | } |
| 553 | #endif |
| 554 | mch_suspend(); |
| 555 | } |
| 556 | |
| 557 | #if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__) |
| 558 | /* |
| 559 | * When the OS can't really suspend, call this function to start a shell. |
| 560 | * This is never called in the GUI. |
| 561 | */ |
| 562 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 563 | suspend_shell(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 564 | { |
| 565 | if (*p_sh == NUL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 566 | emsg(_(e_shellempty)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 567 | else |
| 568 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 569 | msg_puts(_("new shell started\n")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 570 | do_shell(NULL, 0); |
| 571 | } |
| 572 | } |
| 573 | #endif |
| 574 | |
| 575 | /* |
| 576 | * Try to get the current Vim shell size. Put the result in Rows and Columns. |
| 577 | * Use the new sizes as defaults for 'columns' and 'lines'. |
| 578 | * Return OK when size could be determined, FAIL otherwise. |
| 579 | */ |
| 580 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 581 | ui_get_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 582 | { |
| 583 | int retval; |
| 584 | |
| 585 | #ifdef FEAT_GUI |
| 586 | if (gui.in_use) |
| 587 | retval = gui_get_shellsize(); |
| 588 | else |
| 589 | #endif |
| 590 | retval = mch_get_shellsize(); |
| 591 | |
| 592 | check_shellsize(); |
| 593 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 594 | // adjust the default for 'lines' and 'columns' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 595 | if (retval == OK) |
| 596 | { |
| 597 | set_number_default("lines", Rows); |
| 598 | set_number_default("columns", Columns); |
| 599 | } |
| 600 | return retval; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Set the size of the Vim shell according to Rows and Columns, if possible. |
| 605 | * The gui_set_shellsize() or mch_set_shellsize() function will try to set the |
| 606 | * new size. If this is not possible, it will adjust Rows and Columns. |
| 607 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 608 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 609 | ui_set_shellsize( |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 610 | int mustset UNUSED) // set by the user |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 611 | { |
| 612 | #ifdef FEAT_GUI |
| 613 | if (gui.in_use) |
Bram Moolenaar | 8968a31 | 2013-07-03 16:58:44 +0200 | [diff] [blame] | 614 | gui_set_shellsize(mustset, TRUE, RESIZE_BOTH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 615 | else |
| 616 | #endif |
| 617 | mch_set_shellsize(); |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * Called when Rows and/or Columns changed. Adjust scroll region and mouse |
| 622 | * region. |
| 623 | */ |
| 624 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 625 | ui_new_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 626 | { |
| 627 | if (full_screen && !exiting) |
| 628 | { |
| 629 | #ifdef FEAT_GUI |
| 630 | if (gui.in_use) |
| 631 | gui_new_shellsize(); |
| 632 | else |
| 633 | #endif |
| 634 | mch_new_shellsize(); |
| 635 | } |
| 636 | } |
| 637 | |
Bram Moolenaar | 6bc9305 | 2019-04-06 20:00:19 +0200 | [diff] [blame] | 638 | #if ((defined(FEAT_EVAL) || defined(FEAT_TERMINAL)) \ |
Bram Moolenaar | 3d3f217 | 2019-04-06 17:56:05 +0200 | [diff] [blame] | 639 | && (defined(FEAT_GUI) \ |
Bram Moolenaar | 16c34c3 | 2019-04-06 22:01:24 +0200 | [diff] [blame] | 640 | || defined(MSWIN) \ |
Bram Moolenaar | 3d3f217 | 2019-04-06 17:56:05 +0200 | [diff] [blame] | 641 | || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)))) \ |
Bram Moolenaar | fa1e90c | 2019-04-06 17:47:40 +0200 | [diff] [blame] | 642 | || defined(PROTO) |
| 643 | /* |
| 644 | * Get the window position in pixels, if possible. |
| 645 | * Return FAIL when not possible. |
| 646 | */ |
| 647 | int |
Bram Moolenaar | bd67aac | 2019-09-21 23:09:04 +0200 | [diff] [blame] | 648 | ui_get_winpos(int *x, int *y, varnumber_T timeout UNUSED) |
Bram Moolenaar | fa1e90c | 2019-04-06 17:47:40 +0200 | [diff] [blame] | 649 | { |
| 650 | # ifdef FEAT_GUI |
| 651 | if (gui.in_use) |
| 652 | return gui_mch_get_winpos(x, y); |
| 653 | # endif |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 654 | # if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL)) |
Bram Moolenaar | 16c34c3 | 2019-04-06 22:01:24 +0200 | [diff] [blame] | 655 | return mch_get_winpos(x, y); |
Bram Moolenaar | 6bc9305 | 2019-04-06 20:00:19 +0200 | [diff] [blame] | 656 | # else |
Bram Moolenaar | 16c34c3 | 2019-04-06 22:01:24 +0200 | [diff] [blame] | 657 | # if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE) |
| 658 | return term_get_winpos(x, y, timeout); |
| 659 | # else |
Bram Moolenaar | 6bc9305 | 2019-04-06 20:00:19 +0200 | [diff] [blame] | 660 | return FAIL; |
Bram Moolenaar | 16c34c3 | 2019-04-06 22:01:24 +0200 | [diff] [blame] | 661 | # endif |
Bram Moolenaar | fa1e90c | 2019-04-06 17:47:40 +0200 | [diff] [blame] | 662 | # endif |
| 663 | } |
| 664 | #endif |
| 665 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 666 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 667 | ui_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 668 | { |
Bram Moolenaar | b9c31e7 | 2016-09-29 15:18:57 +0200 | [diff] [blame] | 669 | ui_breakcheck_force(FALSE); |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * When "force" is true also check when the terminal is not in raw mode. |
| 674 | * This is useful to read input on channels. |
| 675 | */ |
| 676 | void |
| 677 | ui_breakcheck_force(int force) |
| 678 | { |
Bram Moolenaar | 48d23bb | 2018-11-20 02:42:43 +0100 | [diff] [blame] | 679 | static int recursive = FALSE; |
| 680 | int save_updating_screen = updating_screen; |
Bram Moolenaar | e3caa11 | 2017-01-31 22:07:42 +0100 | [diff] [blame] | 681 | |
Bram Moolenaar | 48d23bb | 2018-11-20 02:42:43 +0100 | [diff] [blame] | 682 | // We could be called recursively if stderr is redirected, calling |
| 683 | // fill_input_buf() calls settmode() when stdin isn't a tty. settmode() |
| 684 | // calls vgetorpeek() which calls ui_breakcheck() again. |
| 685 | if (recursive) |
| 686 | return; |
| 687 | recursive = TRUE; |
| 688 | |
| 689 | // We do not want gui_resize_shell() to redraw the screen here. |
Bram Moolenaar | e3caa11 | 2017-01-31 22:07:42 +0100 | [diff] [blame] | 690 | ++updating_screen; |
| 691 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 692 | #ifdef FEAT_GUI |
| 693 | if (gui.in_use) |
| 694 | gui_mch_update(); |
| 695 | else |
| 696 | #endif |
Bram Moolenaar | b9c31e7 | 2016-09-29 15:18:57 +0200 | [diff] [blame] | 697 | mch_breakcheck(force); |
Bram Moolenaar | e3caa11 | 2017-01-31 22:07:42 +0100 | [diff] [blame] | 698 | |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 699 | if (save_updating_screen) |
| 700 | updating_screen = TRUE; |
Bram Moolenaar | 0cb8ac7 | 2018-05-11 22:01:51 +0200 | [diff] [blame] | 701 | else |
Bram Moolenaar | 68a4b04 | 2019-05-29 22:28:29 +0200 | [diff] [blame] | 702 | after_updating_screen(FALSE); |
Bram Moolenaar | 48d23bb | 2018-11-20 02:42:43 +0100 | [diff] [blame] | 703 | |
| 704 | recursive = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 707 | ////////////////////////////////////////////////////////////////////////////// |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 708 | // Functions that handle the input buffer. |
| 709 | // This is used for any GUI version, and the unix terminal version. |
| 710 | // |
| 711 | // For Unix, the input characters are buffered to be able to check for a |
| 712 | // CTRL-C. This should be done with signals, but I don't know how to do that |
| 713 | // in a portable way for a tty in RAW mode. |
| 714 | // |
| 715 | // For the client-server code in the console the received keys are put in the |
| 716 | // input buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 717 | |
| 718 | #if defined(USE_INPUT_BUF) || defined(PROTO) |
| 719 | |
| 720 | /* |
| 721 | * Internal typeahead buffer. Includes extra space for long key code |
| 722 | * descriptions which would otherwise overflow. The buffer is considered full |
| 723 | * when only this extra space (or part of it) remains. |
| 724 | */ |
Bram Moolenaar | bb1969b | 2019-01-17 15:45:25 +0100 | [diff] [blame] | 725 | #if defined(FEAT_JOB_CHANNEL) || defined(FEAT_CLIENTSERVER) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 726 | /* |
Bram Moolenaar | bb1969b | 2019-01-17 15:45:25 +0100 | [diff] [blame] | 727 | * NetBeans stuffs debugger commands into the input buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 728 | * This requires a larger buffer... |
| 729 | * (Madsen) Go with this for remote input as well ... |
| 730 | */ |
| 731 | # define INBUFLEN 4096 |
| 732 | #else |
| 733 | # define INBUFLEN 250 |
| 734 | #endif |
| 735 | |
| 736 | static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN]; |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 737 | static int inbufcount = 0; // number of chars in inbuf[] |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 738 | |
| 739 | /* |
| 740 | * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and |
| 741 | * trash_input_buf() are functions for manipulating the input buffer. These |
| 742 | * are used by the gui_* calls when a GUI is used to handle keyboard input. |
| 743 | */ |
| 744 | |
| 745 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 746 | vim_is_input_buf_full(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 747 | { |
| 748 | return (inbufcount >= INBUFLEN); |
| 749 | } |
| 750 | |
| 751 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 752 | vim_is_input_buf_empty(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 753 | { |
| 754 | return (inbufcount == 0); |
| 755 | } |
| 756 | |
| 757 | #if defined(FEAT_OLE) || defined(PROTO) |
| 758 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 759 | vim_free_in_input_buf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 760 | { |
| 761 | return (INBUFLEN - inbufcount); |
| 762 | } |
| 763 | #endif |
| 764 | |
Bram Moolenaar | 241a8aa | 2005-12-06 20:04:44 +0000 | [diff] [blame] | 765 | #if defined(FEAT_GUI_GTK) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 766 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 767 | vim_used_in_input_buf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 768 | { |
| 769 | return inbufcount; |
| 770 | } |
| 771 | #endif |
| 772 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 773 | /* |
| 774 | * Return the current contents of the input buffer and make it empty. |
| 775 | * The returned pointer must be passed to set_input_buf() later. |
| 776 | */ |
| 777 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 778 | get_input_buf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 779 | { |
| 780 | garray_T *gap; |
| 781 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 782 | // We use a growarray to store the data pointer and the length. |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 783 | gap = ALLOC_ONE(garray_T); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 784 | if (gap != NULL) |
| 785 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 786 | // Add one to avoid a zero size. |
Bram Moolenaar | 18a4ba2 | 2019-05-24 19:39:03 +0200 | [diff] [blame] | 787 | gap->ga_data = alloc(inbufcount + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 788 | if (gap->ga_data != NULL) |
| 789 | mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount); |
| 790 | gap->ga_len = inbufcount; |
| 791 | } |
| 792 | trash_input_buf(); |
| 793 | return (char_u *)gap; |
| 794 | } |
| 795 | |
| 796 | /* |
| 797 | * Restore the input buffer with a pointer returned from get_input_buf(). |
| 798 | * The allocated memory is freed, this only works once! |
| 799 | */ |
| 800 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 801 | set_input_buf(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 802 | { |
| 803 | garray_T *gap = (garray_T *)p; |
| 804 | |
| 805 | if (gap != NULL) |
| 806 | { |
| 807 | if (gap->ga_data != NULL) |
| 808 | { |
| 809 | mch_memmove(inbuf, gap->ga_data, gap->ga_len); |
| 810 | inbufcount = gap->ga_len; |
| 811 | vim_free(gap->ga_data); |
| 812 | } |
| 813 | vim_free(gap); |
| 814 | } |
| 815 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 816 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 817 | /* |
| 818 | * Add the given bytes to the input buffer |
| 819 | * Special keys start with CSI. A real CSI must have been translated to |
| 820 | * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation. |
| 821 | */ |
| 822 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 823 | add_to_input_buf(char_u *s, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 824 | { |
| 825 | if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN) |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 826 | return; // Shouldn't ever happen! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 827 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 828 | while (len--) |
| 829 | inbuf[inbufcount++] = *s++; |
| 830 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 831 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 832 | /* |
| 833 | * Add "str[len]" to the input buffer while escaping CSI bytes. |
| 834 | */ |
| 835 | void |
| 836 | add_to_input_buf_csi(char_u *str, int len) |
| 837 | { |
| 838 | int i; |
| 839 | char_u buf[2]; |
| 840 | |
| 841 | for (i = 0; i < len; ++i) |
| 842 | { |
| 843 | add_to_input_buf(str + i, 1); |
| 844 | if (str[i] == CSI) |
| 845 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 846 | // Turn CSI into K_CSI. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 847 | buf[0] = KS_EXTRA; |
| 848 | buf[1] = (int)KE_CSI; |
| 849 | add_to_input_buf(buf, 2); |
| 850 | } |
| 851 | } |
| 852 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 853 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 854 | /* |
| 855 | * Remove everything from the input buffer. Called when ^C is found. |
| 856 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 857 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 858 | trash_input_buf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 859 | { |
| 860 | inbufcount = 0; |
| 861 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 862 | |
| 863 | /* |
| 864 | * Read as much data from the input buffer as possible up to maxlen, and store |
| 865 | * it in buf. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 866 | */ |
| 867 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 868 | read_from_input_buf(char_u *buf, long maxlen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 869 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 870 | if (inbufcount == 0) // if the buffer is empty, fill it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 871 | fill_input_buf(TRUE); |
| 872 | if (maxlen > inbufcount) |
| 873 | maxlen = inbufcount; |
| 874 | mch_memmove(buf, inbuf, (size_t)maxlen); |
| 875 | inbufcount -= maxlen; |
| 876 | if (inbufcount) |
| 877 | mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount); |
| 878 | return (int)maxlen; |
| 879 | } |
| 880 | |
| 881 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 882 | fill_input_buf(int exit_on_error UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 883 | { |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 884 | #if defined(UNIX) || defined(VMS) || defined(MACOS_X) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 885 | int len; |
| 886 | int try; |
| 887 | static int did_read_something = FALSE; |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 888 | static char_u *rest = NULL; // unconverted rest of previous read |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 889 | static int restlen = 0; |
| 890 | int unconverted; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 891 | #endif |
| 892 | |
| 893 | #ifdef FEAT_GUI |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 894 | if (gui.in_use |
| 895 | # ifdef NO_CONSOLE_INPUT |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 896 | // Don't use the GUI input when the window hasn't been opened yet. |
| 897 | // We get here from ui_inchar() when we should try reading from stdin. |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 898 | && !no_console_input() |
| 899 | # endif |
| 900 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 901 | { |
| 902 | gui_mch_update(); |
| 903 | return; |
| 904 | } |
| 905 | #endif |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 906 | #if defined(UNIX) || defined(VMS) || defined(MACOS_X) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 907 | if (vim_is_input_buf_full()) |
| 908 | return; |
| 909 | /* |
| 910 | * Fill_input_buf() is only called when we really need a character. |
| 911 | * If we can't get any, but there is some in the buffer, just return. |
| 912 | * If we can't get any, and there isn't any in the buffer, we give up and |
| 913 | * exit Vim. |
| 914 | */ |
| 915 | # ifdef __BEOS__ |
| 916 | /* |
| 917 | * On the BeBox version (for now), all input is secretly performed within |
| 918 | * beos_select() which is called from RealWaitForChar(). |
| 919 | */ |
| 920 | while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL)) |
| 921 | ; |
| 922 | len = inbufcount; |
| 923 | inbufcount = 0; |
| 924 | # else |
| 925 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 926 | if (rest != NULL) |
| 927 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 928 | // Use remainder of previous call, starts with an invalid character |
| 929 | // that may become valid when reading more. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 930 | if (restlen > INBUFLEN - inbufcount) |
| 931 | unconverted = INBUFLEN - inbufcount; |
| 932 | else |
| 933 | unconverted = restlen; |
| 934 | mch_memmove(inbuf + inbufcount, rest, unconverted); |
| 935 | if (unconverted == restlen) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 936 | VIM_CLEAR(rest); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 937 | else |
| 938 | { |
| 939 | restlen -= unconverted; |
| 940 | mch_memmove(rest, rest + unconverted, restlen); |
| 941 | } |
| 942 | inbufcount += unconverted; |
| 943 | } |
| 944 | else |
| 945 | unconverted = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 946 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 947 | len = 0; // to avoid gcc warning |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 948 | for (try = 0; try < 100; ++try) |
| 949 | { |
Bram Moolenaar | 4e601e3 | 2018-04-24 13:29:51 +0200 | [diff] [blame] | 950 | size_t readlen = (size_t)((INBUFLEN - inbufcount) |
Bram Moolenaar | 9645e2d | 2020-03-20 20:48:49 +0100 | [diff] [blame] | 951 | / input_conv.vc_factor); |
Bram Moolenaar | 4e601e3 | 2018-04-24 13:29:51 +0200 | [diff] [blame] | 952 | # ifdef VMS |
Bram Moolenaar | 4994373 | 2018-04-24 20:27:26 +0200 | [diff] [blame] | 953 | len = vms_read((char *)inbuf + inbufcount, readlen); |
Bram Moolenaar | 4e601e3 | 2018-04-24 13:29:51 +0200 | [diff] [blame] | 954 | # else |
| 955 | len = read(read_cmd_fd, (char *)inbuf + inbufcount, readlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 956 | # endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 957 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 958 | if (len > 0 || got_int) |
| 959 | break; |
| 960 | /* |
| 961 | * If reading stdin results in an error, continue reading stderr. |
| 962 | * This helps when using "foo | xargs vim". |
| 963 | */ |
| 964 | if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0) |
| 965 | { |
| 966 | int m = cur_tmode; |
| 967 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 968 | // We probably set the wrong file descriptor to raw mode. Switch |
| 969 | // back to cooked mode, use another descriptor and set the mode to |
| 970 | // what it was. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 971 | settmode(TMODE_COOK); |
| 972 | #ifdef HAVE_DUP |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 973 | // Use stderr for stdin, also works for shell commands. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 974 | close(0); |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 975 | vim_ignored = dup(2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 976 | #else |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 977 | read_cmd_fd = 2; // read from stderr instead of stdin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 978 | #endif |
| 979 | settmode(m); |
| 980 | } |
| 981 | if (!exit_on_error) |
| 982 | return; |
| 983 | } |
| 984 | # endif |
| 985 | if (len <= 0 && !got_int) |
| 986 | read_error_exit(); |
| 987 | if (len > 0) |
| 988 | did_read_something = TRUE; |
| 989 | if (got_int) |
| 990 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 991 | // Interrupted, pretend a CTRL-C was typed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 992 | inbuf[0] = 3; |
| 993 | inbufcount = 1; |
| 994 | } |
| 995 | else |
| 996 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 997 | /* |
| 998 | * May perform conversion on the input characters. |
| 999 | * Include the unconverted rest of the previous call. |
| 1000 | * If there is an incomplete char at the end it is kept for the next |
| 1001 | * time, reading more bytes should make conversion possible. |
| 1002 | * Don't do this in the unlikely event that the input buffer is too |
| 1003 | * small ("rest" still contains more bytes). |
| 1004 | */ |
| 1005 | if (input_conv.vc_type != CONV_NONE) |
| 1006 | { |
| 1007 | inbufcount -= unconverted; |
| 1008 | len = convert_input_safe(inbuf + inbufcount, |
| 1009 | len + unconverted, INBUFLEN - inbufcount, |
| 1010 | rest == NULL ? &rest : NULL, &restlen); |
| 1011 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1012 | while (len-- > 0) |
| 1013 | { |
| 1014 | /* |
Bram Moolenaar | 9645e2d | 2020-03-20 20:48:49 +0100 | [diff] [blame] | 1015 | * If a CTRL-C was typed, remove it from the buffer and set |
| 1016 | * got_int. Also recognize CTRL-C with modifyOtherKeys set. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1017 | */ |
Bram Moolenaar | 9645e2d | 2020-03-20 20:48:49 +0100 | [diff] [blame] | 1018 | if (ctrl_c_interrupts && (inbuf[inbufcount] == 3 |
| 1019 | || (len >= 9 && STRNCMP(inbuf + inbufcount, |
Bram Moolenaar | 56ba21a | 2020-03-23 19:17:29 +0100 | [diff] [blame] | 1020 | "\033[27;5;99~", 10) == 0))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1021 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 1022 | // remove everything typed before the CTRL-C |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1023 | mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1)); |
| 1024 | inbufcount = 0; |
| 1025 | got_int = TRUE; |
| 1026 | } |
| 1027 | ++inbufcount; |
| 1028 | } |
| 1029 | } |
Bram Moolenaar | 74ee5e2 | 2019-12-13 18:13:22 +0100 | [diff] [blame] | 1030 | #endif // UNIX || VMS || MACOS_X |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1031 | } |
Bram Moolenaar | 74ee5e2 | 2019-12-13 18:13:22 +0100 | [diff] [blame] | 1032 | #endif // USE_INPUT_BUF |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1033 | |
| 1034 | /* |
| 1035 | * Exit because of an input read error. |
| 1036 | */ |
| 1037 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1038 | read_error_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1039 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 1040 | if (silent_mode) // Normal way to exit for "ex -s" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1041 | getout(0); |
| 1042 | STRCPY(IObuff, _("Vim: Error reading input, exiting...\n")); |
| 1043 | preserve_exit(); |
| 1044 | } |
| 1045 | |
| 1046 | #if defined(CURSOR_SHAPE) || defined(PROTO) |
| 1047 | /* |
| 1048 | * May update the shape of the cursor. |
| 1049 | */ |
| 1050 | void |
Bram Moolenaar | 3cd43cc | 2017-08-12 19:51:41 +0200 | [diff] [blame] | 1051 | ui_cursor_shape_forced(int forced) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1052 | { |
| 1053 | # ifdef FEAT_GUI |
| 1054 | if (gui.in_use) |
| 1055 | gui_update_cursor_later(); |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1056 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1057 | # endif |
Bram Moolenaar | 3cd43cc | 2017-08-12 19:51:41 +0200 | [diff] [blame] | 1058 | term_cursor_mode(forced); |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1059 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1060 | # ifdef MCH_CURSOR_SHAPE |
| 1061 | mch_update_cursor(); |
| 1062 | # endif |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1063 | |
| 1064 | # ifdef FEAT_CONCEAL |
Bram Moolenaar | b946482 | 2018-05-10 15:09:49 +0200 | [diff] [blame] | 1065 | conceal_check_cursor_line(); |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1066 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1067 | } |
Bram Moolenaar | 3cd43cc | 2017-08-12 19:51:41 +0200 | [diff] [blame] | 1068 | |
| 1069 | void |
| 1070 | ui_cursor_shape(void) |
| 1071 | { |
| 1072 | ui_cursor_shape_forced(FALSE); |
| 1073 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1074 | #endif |
| 1075 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1076 | /* |
| 1077 | * Check bounds for column number |
| 1078 | */ |
| 1079 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1080 | check_col(int col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1081 | { |
| 1082 | if (col < 0) |
| 1083 | return 0; |
| 1084 | if (col >= (int)screen_Columns) |
| 1085 | return (int)screen_Columns - 1; |
| 1086 | return col; |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * Check bounds for row number |
| 1091 | */ |
| 1092 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1093 | check_row(int row) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1094 | { |
| 1095 | if (row < 0) |
| 1096 | return 0; |
| 1097 | if (row >= (int)screen_Rows) |
| 1098 | return (int)screen_Rows - 1; |
| 1099 | return row; |
| 1100 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1101 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1102 | #if defined(FEAT_GUI) || defined(MSWIN) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1103 | /* |
| 1104 | * Called when focus changed. Used for the GUI or for systems where this can |
| 1105 | * be done in the console (Win32). |
| 1106 | */ |
| 1107 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1108 | ui_focus_change( |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 1109 | int in_focus) // TRUE if focus gained. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1110 | { |
| 1111 | static time_t last_time = (time_t)0; |
| 1112 | int need_redraw = FALSE; |
| 1113 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 1114 | // When activated: Check if any file was modified outside of Vim. |
| 1115 | // Only do this when not done within the last two seconds (could get |
| 1116 | // several events in a row). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1117 | if (in_focus && last_time + 2 < time(NULL)) |
| 1118 | { |
| 1119 | need_redraw = check_timestamps( |
| 1120 | # ifdef FEAT_GUI |
| 1121 | gui.in_use |
| 1122 | # else |
| 1123 | FALSE |
| 1124 | # endif |
| 1125 | ); |
| 1126 | last_time = time(NULL); |
| 1127 | } |
| 1128 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1129 | /* |
| 1130 | * Fire the focus gained/lost autocommand. |
| 1131 | */ |
| 1132 | need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED |
| 1133 | : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1134 | |
| 1135 | if (need_redraw) |
| 1136 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 1137 | // Something was executed, make sure the cursor is put back where it |
| 1138 | // belongs. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1139 | need_wait_return = FALSE; |
| 1140 | |
| 1141 | if (State & CMDLINE) |
| 1142 | redrawcmdline(); |
| 1143 | else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE |
| 1144 | || State == EXTERNCMD || State == CONFIRM || exmode_active) |
| 1145 | repeat_message(); |
| 1146 | else if ((State & NORMAL) || (State & INSERT)) |
| 1147 | { |
| 1148 | if (must_redraw != 0) |
| 1149 | update_screen(0); |
| 1150 | setcursor(); |
| 1151 | } |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 1152 | cursor_on(); // redrawing may have switched it off |
Bram Moolenaar | a338adc | 2018-01-31 20:51:47 +0100 | [diff] [blame] | 1153 | out_flush_cursor(FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1154 | # ifdef FEAT_GUI |
| 1155 | if (gui.in_use) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1156 | gui_update_scrollbars(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1157 | # endif |
| 1158 | } |
| 1159 | #ifdef FEAT_TITLE |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 1160 | // File may have been changed from 'readonly' to 'noreadonly' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | if (need_maketitle) |
| 1162 | maketitle(); |
| 1163 | #endif |
| 1164 | } |
| 1165 | #endif |
| 1166 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1167 | #if defined(HAVE_INPUT_METHOD) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1168 | /* |
| 1169 | * Save current Input Method status to specified place. |
| 1170 | */ |
| 1171 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1172 | im_save_status(long *psave) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1173 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 1174 | // Don't save when 'imdisable' is set or "xic" is NULL, IM is always |
| 1175 | // disabled then (but might start later). |
| 1176 | // Also don't save when inside a mapping, vgetc_im_active has not been set |
| 1177 | // then. |
| 1178 | // And don't save when the keys were stuffed (e.g., for a "." command). |
| 1179 | // And don't save when the GUI is running but our window doesn't have |
| 1180 | // input focus (e.g., when a find dialog is open). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1181 | if (!p_imdisable && KeyTyped && !KeyStuffed |
| 1182 | # ifdef FEAT_XIM |
| 1183 | && xic != NULL |
| 1184 | # endif |
| 1185 | # ifdef FEAT_GUI |
| 1186 | && (!gui.in_use || gui.in_focus) |
| 1187 | # endif |
| 1188 | ) |
| 1189 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 1190 | // Do save when IM is on, or IM is off and saved status is on. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1191 | if (vgetc_im_active) |
| 1192 | *psave = B_IMODE_IM; |
| 1193 | else if (*psave == B_IMODE_IM) |
| 1194 | *psave = B_IMODE_NONE; |
| 1195 | } |
| 1196 | } |
| 1197 | #endif |