Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 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. |
| 15 | * 2. Clipboard stuff. |
| 16 | * 3. Input buffer stuff. |
| 17 | */ |
| 18 | |
| 19 | #include "vim.h" |
| 20 | |
| 21 | void |
| 22 | ui_write(s, len) |
| 23 | char_u *s; |
| 24 | int len; |
| 25 | { |
| 26 | #ifdef FEAT_GUI |
| 27 | if (gui.in_use && !gui.dying && !gui.starting) |
| 28 | { |
| 29 | gui_write(s, len); |
| 30 | if (p_wd) |
| 31 | gui_wait_for_chars(p_wd); |
| 32 | return; |
| 33 | } |
| 34 | #endif |
| 35 | #ifndef NO_CONSOLE |
| 36 | /* Don't output anything in silent mode ("ex -s") unless 'verbose' set */ |
| 37 | if (!(silent_mode && p_verbose == 0)) |
| 38 | { |
| 39 | #ifdef FEAT_MBYTE |
| 40 | char_u *tofree = NULL; |
| 41 | |
| 42 | if (output_conv.vc_type != CONV_NONE) |
| 43 | { |
| 44 | /* Convert characters from 'encoding' to 'termencoding'. */ |
| 45 | tofree = string_convert(&output_conv, s, &len); |
| 46 | if (tofree != NULL) |
| 47 | s = tofree; |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | mch_write(s, len); |
| 52 | |
| 53 | #ifdef FEAT_MBYTE |
| 54 | if (output_conv.vc_type != CONV_NONE) |
| 55 | vim_free(tofree); |
| 56 | #endif |
| 57 | } |
| 58 | #endif |
| 59 | } |
| 60 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 61 | #if defined(UNIX) || defined(VMS) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 62 | /* |
| 63 | * When executing an external program, there may be some typed characters that |
| 64 | * are not consumed by it. Give them back to ui_inchar() and they are stored |
| 65 | * here for the next call. |
| 66 | */ |
| 67 | static char_u *ta_str = NULL; |
| 68 | static int ta_off; /* offset for next char to use when ta_str != NULL */ |
| 69 | static int ta_len; /* length of ta_str when it's not NULL*/ |
| 70 | |
| 71 | void |
| 72 | ui_inchar_undo(s, len) |
| 73 | char_u *s; |
| 74 | int len; |
| 75 | { |
| 76 | char_u *new; |
| 77 | int newlen; |
| 78 | |
| 79 | newlen = len; |
| 80 | if (ta_str != NULL) |
| 81 | newlen += ta_len - ta_off; |
| 82 | new = alloc(newlen); |
| 83 | if (new != NULL) |
| 84 | { |
| 85 | if (ta_str != NULL) |
| 86 | { |
| 87 | mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off)); |
| 88 | mch_memmove(new + ta_len - ta_off, s, (size_t)len); |
| 89 | vim_free(ta_str); |
| 90 | } |
| 91 | else |
| 92 | mch_memmove(new, s, (size_t)len); |
| 93 | ta_str = new; |
| 94 | ta_len = newlen; |
| 95 | ta_off = 0; |
| 96 | } |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | /* |
| 101 | * ui_inchar(): low level input funcion. |
| 102 | * Get characters from the keyboard. |
| 103 | * Return the number of characters that are available. |
| 104 | * If "wtime" == 0 do not wait for characters. |
| 105 | * If "wtime" == -1 wait forever for characters. |
| 106 | * If "wtime" > 0 wait "wtime" milliseconds for a character. |
| 107 | * |
| 108 | * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into |
| 109 | * it. When typebuf.tb_change_cnt changes (e.g., when a message is received |
| 110 | * from a remote client) "buf" can no longer be used. "tb_change_cnt" is NULL |
| 111 | * otherwise. |
| 112 | */ |
| 113 | int |
| 114 | ui_inchar(buf, maxlen, wtime, tb_change_cnt) |
| 115 | char_u *buf; |
| 116 | int maxlen; |
| 117 | long wtime; /* don't use "time", MIPS cannot handle it */ |
| 118 | int tb_change_cnt; |
| 119 | { |
| 120 | int retval = 0; |
| 121 | |
| 122 | #if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS)) |
| 123 | /* |
| 124 | * Use the typeahead if there is any. |
| 125 | */ |
| 126 | if (ta_str != NULL) |
| 127 | { |
| 128 | if (maxlen >= ta_len - ta_off) |
| 129 | { |
| 130 | mch_memmove(buf, ta_str + ta_off, (size_t)ta_len); |
| 131 | vim_free(ta_str); |
| 132 | ta_str = NULL; |
| 133 | return ta_len; |
| 134 | } |
| 135 | mch_memmove(buf, ta_str + ta_off, (size_t)maxlen); |
| 136 | ta_off += maxlen; |
| 137 | return maxlen; |
| 138 | } |
| 139 | #endif |
| 140 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 141 | #ifdef FEAT_PROFILE |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 142 | if (do_profiling == PROF_YES && wtime != 0) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 143 | prof_inchar_enter(); |
| 144 | #endif |
| 145 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 146 | #ifdef NO_CONSOLE_INPUT |
| 147 | /* Don't wait for character input when the window hasn't been opened yet. |
| 148 | * Do try reading, this works when redirecting stdin from a file. |
| 149 | * Must return something, otherwise we'll loop forever. If we run into |
| 150 | * this very often we probably got stuck, exit Vim. */ |
| 151 | if (no_console_input()) |
| 152 | { |
| 153 | static int count = 0; |
| 154 | |
| 155 | # ifndef NO_CONSOLE |
Bram Moolenaar | 43b604c | 2005-03-22 23:06:55 +0000 | [diff] [blame] | 156 | retval = mch_inchar(buf, maxlen, (wtime >= 0 && wtime < 10) |
| 157 | ? 10L : wtime, tb_change_cnt); |
| 158 | if (retval > 0 || typebuf_changed(tb_change_cnt) || wtime >= 0) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 159 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | # endif |
| 161 | if (wtime == -1 && ++count == 1000) |
| 162 | read_error_exit(); |
| 163 | buf[0] = CAR; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 164 | retval = 1; |
| 165 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 166 | } |
| 167 | #endif |
| 168 | |
Bram Moolenaar | c8da311 | 2007-03-08 12:36:46 +0000 | [diff] [blame] | 169 | /* If we are going to wait for some time or block... */ |
| 170 | if (wtime == -1 || wtime > 100L) |
| 171 | { |
| 172 | /* ... allow signals to kill us. */ |
| 173 | (void)vim_handle_signal(SIGNAL_UNBLOCK); |
| 174 | |
| 175 | /* ... there is no need for CTRL-C to interrupt something, don't let |
| 176 | * it set got_int when it was mapped. */ |
| 177 | if (mapped_ctrl_c) |
| 178 | ctrl_c_interrupts = FALSE; |
| 179 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 180 | |
| 181 | #ifdef FEAT_GUI |
| 182 | if (gui.in_use) |
| 183 | { |
| 184 | if (gui_wait_for_chars(wtime) && !typebuf_changed(tb_change_cnt)) |
| 185 | retval = read_from_input_buf(buf, (long)maxlen); |
| 186 | } |
| 187 | #endif |
| 188 | #ifndef NO_CONSOLE |
| 189 | # ifdef FEAT_GUI |
| 190 | else |
| 191 | # endif |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 192 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 193 | retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt); |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 194 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | #endif |
| 196 | |
Bram Moolenaar | c8da311 | 2007-03-08 12:36:46 +0000 | [diff] [blame] | 197 | if (wtime == -1 || wtime > 100L) |
| 198 | /* block SIGHUP et al. */ |
| 199 | (void)vim_handle_signal(SIGNAL_BLOCK); |
| 200 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 201 | ctrl_c_interrupts = TRUE; |
| 202 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 203 | #ifdef NO_CONSOLE_INPUT |
| 204 | theend: |
| 205 | #endif |
| 206 | #ifdef FEAT_PROFILE |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 207 | if (do_profiling == PROF_YES && wtime != 0) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 208 | prof_inchar_exit(); |
| 209 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 210 | return retval; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * return non-zero if a character is available |
| 215 | */ |
| 216 | int |
| 217 | ui_char_avail() |
| 218 | { |
| 219 | #ifdef FEAT_GUI |
| 220 | if (gui.in_use) |
| 221 | { |
| 222 | gui_mch_update(); |
| 223 | return input_available(); |
| 224 | } |
| 225 | #endif |
| 226 | #ifndef NO_CONSOLE |
| 227 | # ifdef NO_CONSOLE_INPUT |
| 228 | if (no_console_input()) |
| 229 | return 0; |
| 230 | # endif |
| 231 | return mch_char_avail(); |
| 232 | #else |
| 233 | return 0; |
| 234 | #endif |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Delay for the given number of milliseconds. If ignoreinput is FALSE then we |
| 239 | * cancel the delay if a key is hit. |
| 240 | */ |
| 241 | void |
| 242 | ui_delay(msec, ignoreinput) |
| 243 | long msec; |
| 244 | int ignoreinput; |
| 245 | { |
| 246 | #ifdef FEAT_GUI |
| 247 | if (gui.in_use && !ignoreinput) |
| 248 | gui_wait_for_chars(msec); |
| 249 | else |
| 250 | #endif |
| 251 | mch_delay(msec, ignoreinput); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * If the machine has job control, use it to suspend the program, |
| 256 | * otherwise fake it by starting a new shell. |
| 257 | * When running the GUI iconify the window. |
| 258 | */ |
| 259 | void |
| 260 | ui_suspend() |
| 261 | { |
| 262 | #ifdef FEAT_GUI |
| 263 | if (gui.in_use) |
| 264 | { |
| 265 | gui_mch_iconify(); |
| 266 | return; |
| 267 | } |
| 268 | #endif |
| 269 | mch_suspend(); |
| 270 | } |
| 271 | |
| 272 | #if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__) |
| 273 | /* |
| 274 | * When the OS can't really suspend, call this function to start a shell. |
| 275 | * This is never called in the GUI. |
| 276 | */ |
| 277 | void |
| 278 | suspend_shell() |
| 279 | { |
| 280 | if (*p_sh == NUL) |
| 281 | EMSG(_(e_shellempty)); |
| 282 | else |
| 283 | { |
| 284 | MSG_PUTS(_("new shell started\n")); |
| 285 | do_shell(NULL, 0); |
| 286 | } |
| 287 | } |
| 288 | #endif |
| 289 | |
| 290 | /* |
| 291 | * Try to get the current Vim shell size. Put the result in Rows and Columns. |
| 292 | * Use the new sizes as defaults for 'columns' and 'lines'. |
| 293 | * Return OK when size could be determined, FAIL otherwise. |
| 294 | */ |
| 295 | int |
| 296 | ui_get_shellsize() |
| 297 | { |
| 298 | int retval; |
| 299 | |
| 300 | #ifdef FEAT_GUI |
| 301 | if (gui.in_use) |
| 302 | retval = gui_get_shellsize(); |
| 303 | else |
| 304 | #endif |
| 305 | retval = mch_get_shellsize(); |
| 306 | |
| 307 | check_shellsize(); |
| 308 | |
| 309 | /* adjust the default for 'lines' and 'columns' */ |
| 310 | if (retval == OK) |
| 311 | { |
| 312 | set_number_default("lines", Rows); |
| 313 | set_number_default("columns", Columns); |
| 314 | } |
| 315 | return retval; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Set the size of the Vim shell according to Rows and Columns, if possible. |
| 320 | * The gui_set_shellsize() or mch_set_shellsize() function will try to set the |
| 321 | * new size. If this is not possible, it will adjust Rows and Columns. |
| 322 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 323 | void |
| 324 | ui_set_shellsize(mustset) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 325 | int mustset UNUSED; /* set by the user */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 326 | { |
| 327 | #ifdef FEAT_GUI |
| 328 | if (gui.in_use) |
| 329 | gui_set_shellsize(mustset, |
| 330 | # ifdef WIN3264 |
| 331 | TRUE |
| 332 | # else |
| 333 | FALSE |
| 334 | # endif |
Bram Moolenaar | 04a9d45 | 2006-03-27 21:03:26 +0000 | [diff] [blame] | 335 | , RESIZE_BOTH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 336 | else |
| 337 | #endif |
| 338 | mch_set_shellsize(); |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Called when Rows and/or Columns changed. Adjust scroll region and mouse |
| 343 | * region. |
| 344 | */ |
| 345 | void |
| 346 | ui_new_shellsize() |
| 347 | { |
| 348 | if (full_screen && !exiting) |
| 349 | { |
| 350 | #ifdef FEAT_GUI |
| 351 | if (gui.in_use) |
| 352 | gui_new_shellsize(); |
| 353 | else |
| 354 | #endif |
| 355 | mch_new_shellsize(); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | void |
| 360 | ui_breakcheck() |
| 361 | { |
| 362 | #ifdef FEAT_GUI |
| 363 | if (gui.in_use) |
| 364 | gui_mch_update(); |
| 365 | else |
| 366 | #endif |
| 367 | mch_breakcheck(); |
| 368 | } |
| 369 | |
| 370 | /***************************************************************************** |
| 371 | * Functions for copying and pasting text between applications. |
| 372 | * This is always included in a GUI version, but may also be included when the |
| 373 | * clipboard and mouse is available to a terminal version such as xterm. |
| 374 | * Note: there are some more functions in ops.c that handle selection stuff. |
| 375 | * |
| 376 | * Also note that the majority of functions here deal with the X 'primary' |
| 377 | * (visible - for Visual mode use) selection, and only that. There are no |
| 378 | * versions of these for the 'clipboard' selection, as Visual mode has no use |
| 379 | * for them. |
| 380 | */ |
| 381 | |
| 382 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 383 | |
| 384 | /* |
| 385 | * Selection stuff using Visual mode, for cutting and pasting text to other |
| 386 | * windows. |
| 387 | */ |
| 388 | |
| 389 | /* |
| 390 | * Call this to initialise the clipboard. Pass it FALSE if the clipboard code |
| 391 | * is included, but the clipboard can not be used, or TRUE if the clipboard can |
| 392 | * be used. Eg unix may call this with FALSE, then call it again with TRUE if |
| 393 | * the GUI starts. |
| 394 | */ |
| 395 | void |
| 396 | clip_init(can_use) |
| 397 | int can_use; |
| 398 | { |
| 399 | VimClipboard *cb; |
| 400 | |
| 401 | cb = &clip_star; |
| 402 | for (;;) |
| 403 | { |
| 404 | cb->available = can_use; |
| 405 | cb->owned = FALSE; |
| 406 | cb->start.lnum = 0; |
| 407 | cb->start.col = 0; |
| 408 | cb->end.lnum = 0; |
| 409 | cb->end.col = 0; |
| 410 | cb->state = SELECT_CLEARED; |
| 411 | |
| 412 | if (cb == &clip_plus) |
| 413 | break; |
| 414 | cb = &clip_plus; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Check whether the VIsual area has changed, and if so try to become the owner |
| 420 | * of the selection, and free any old converted selection we may still have |
| 421 | * lying around. If the VIsual mode has ended, make a copy of what was |
| 422 | * selected so we can still give it to others. Will probably have to make sure |
| 423 | * this is called whenever VIsual mode is ended. |
| 424 | */ |
| 425 | void |
| 426 | clip_update_selection() |
| 427 | { |
| 428 | pos_T start, end; |
| 429 | |
| 430 | /* If visual mode is only due to a redo command ("."), then ignore it */ |
| 431 | if (!redo_VIsual_busy && VIsual_active && (State & NORMAL)) |
| 432 | { |
| 433 | if (lt(VIsual, curwin->w_cursor)) |
| 434 | { |
| 435 | start = VIsual; |
| 436 | end = curwin->w_cursor; |
| 437 | #ifdef FEAT_MBYTE |
| 438 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 439 | end.col += (*mb_ptr2len)(ml_get_cursor()) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 440 | #endif |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | start = curwin->w_cursor; |
| 445 | end = VIsual; |
| 446 | } |
| 447 | if (!equalpos(clip_star.start, start) |
| 448 | || !equalpos(clip_star.end, end) |
| 449 | || clip_star.vmode != VIsual_mode) |
| 450 | { |
| 451 | clip_clear_selection(); |
| 452 | clip_star.start = start; |
| 453 | clip_star.end = end; |
| 454 | clip_star.vmode = VIsual_mode; |
| 455 | clip_free_selection(&clip_star); |
| 456 | clip_own_selection(&clip_star); |
| 457 | clip_gen_set_selection(&clip_star); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void |
| 463 | clip_own_selection(cbd) |
| 464 | VimClipboard *cbd; |
| 465 | { |
| 466 | /* |
| 467 | * Also want to check somehow that we are reading from the keyboard rather |
| 468 | * than a mapping etc. |
| 469 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 470 | #ifdef FEAT_X11 |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 471 | /* Always own the selection, we might have lost it without being |
| 472 | * notified. */ |
| 473 | if (cbd->available) |
| 474 | { |
| 475 | int was_owned = cbd->owned; |
| 476 | |
| 477 | cbd->owned = (clip_gen_own_selection(cbd) == OK); |
| 478 | if (!was_owned && cbd == &clip_star) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 479 | { |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 480 | /* May have to show a different kind of highlighting for the |
| 481 | * selected area. There is no specific redraw command for this, |
| 482 | * just redraw all windows on the current buffer. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 483 | if (cbd->owned |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 484 | && (get_real_state() == VISUAL |
| 485 | || get_real_state() == SELECTMODE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 486 | && clip_isautosel() |
| 487 | && hl_attr(HLF_V) != hl_attr(HLF_VNC)) |
| 488 | redraw_curbuf_later(INVERTED_ALL); |
| 489 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 490 | } |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 491 | #else |
| 492 | /* Only own the clibpard when we didn't own it yet. */ |
| 493 | if (!cbd->owned && cbd->available) |
| 494 | cbd->owned = (clip_gen_own_selection(cbd) == OK); |
| 495 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void |
| 499 | clip_lose_selection(cbd) |
| 500 | VimClipboard *cbd; |
| 501 | { |
| 502 | #ifdef FEAT_X11 |
| 503 | int was_owned = cbd->owned; |
| 504 | #endif |
| 505 | int visual_selection = (cbd == &clip_star); |
| 506 | |
| 507 | clip_free_selection(cbd); |
| 508 | cbd->owned = FALSE; |
| 509 | if (visual_selection) |
| 510 | clip_clear_selection(); |
| 511 | clip_gen_lose_selection(cbd); |
| 512 | #ifdef FEAT_X11 |
| 513 | if (visual_selection) |
| 514 | { |
| 515 | /* May have to show a different kind of highlighting for the selected |
| 516 | * area. There is no specific redraw command for this, just redraw all |
| 517 | * windows on the current buffer. */ |
| 518 | if (was_owned |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 519 | && (get_real_state() == VISUAL |
| 520 | || get_real_state() == SELECTMODE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 521 | && clip_isautosel() |
| 522 | && hl_attr(HLF_V) != hl_attr(HLF_VNC)) |
| 523 | { |
| 524 | update_curbuf(INVERTED_ALL); |
| 525 | setcursor(); |
| 526 | cursor_on(); |
| 527 | out_flush(); |
| 528 | # ifdef FEAT_GUI |
| 529 | if (gui.in_use) |
| 530 | gui_update_cursor(TRUE, FALSE); |
| 531 | # endif |
| 532 | } |
| 533 | } |
| 534 | #endif |
| 535 | } |
| 536 | |
| 537 | void |
| 538 | clip_copy_selection() |
| 539 | { |
| 540 | if (VIsual_active && (State & NORMAL) && clip_star.available) |
| 541 | { |
| 542 | if (clip_isautosel()) |
| 543 | clip_update_selection(); |
| 544 | clip_free_selection(&clip_star); |
| 545 | clip_own_selection(&clip_star); |
| 546 | if (clip_star.owned) |
| 547 | clip_get_selection(&clip_star); |
| 548 | clip_gen_set_selection(&clip_star); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Called when Visual mode is ended: update the selection. |
| 554 | */ |
| 555 | void |
| 556 | clip_auto_select() |
| 557 | { |
| 558 | if (clip_isautosel()) |
| 559 | clip_copy_selection(); |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * Return TRUE if automatic selection of Visual area is desired. |
| 564 | */ |
| 565 | int |
| 566 | clip_isautosel() |
| 567 | { |
| 568 | return ( |
| 569 | #ifdef FEAT_GUI |
| 570 | gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) : |
| 571 | #endif |
| 572 | clip_autoselect); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | /* |
| 577 | * Stuff for general mouse selection, without using Visual mode. |
| 578 | */ |
| 579 | |
| 580 | static int clip_compare_pos __ARGS((int row1, int col1, int row2, int col2)); |
| 581 | static void clip_invert_area __ARGS((int, int, int, int, int how)); |
| 582 | static void clip_invert_rectangle __ARGS((int row, int col, int height, int width, int invert)); |
| 583 | static void clip_get_word_boundaries __ARGS((VimClipboard *, int, int)); |
| 584 | static int clip_get_line_end __ARGS((int)); |
| 585 | static void clip_update_modeless_selection __ARGS((VimClipboard *, int, int, |
| 586 | int, int)); |
| 587 | |
| 588 | /* flags for clip_invert_area() */ |
| 589 | #define CLIP_CLEAR 1 |
| 590 | #define CLIP_SET 2 |
| 591 | #define CLIP_TOGGLE 3 |
| 592 | |
| 593 | /* |
| 594 | * Start, continue or end a modeless selection. Used when editing the |
| 595 | * command-line and in the cmdline window. |
| 596 | */ |
| 597 | void |
| 598 | clip_modeless(button, is_click, is_drag) |
| 599 | int button; |
| 600 | int is_click; |
| 601 | int is_drag; |
| 602 | { |
| 603 | int repeat; |
| 604 | |
| 605 | repeat = ((clip_star.mode == SELECT_MODE_CHAR |
| 606 | || clip_star.mode == SELECT_MODE_LINE) |
| 607 | && (mod_mask & MOD_MASK_2CLICK)) |
| 608 | || (clip_star.mode == SELECT_MODE_WORD |
| 609 | && (mod_mask & MOD_MASK_3CLICK)); |
| 610 | if (is_click && button == MOUSE_RIGHT) |
| 611 | { |
| 612 | /* Right mouse button: If there was no selection, start one. |
| 613 | * Otherwise extend the existing selection. */ |
| 614 | if (clip_star.state == SELECT_CLEARED) |
| 615 | clip_start_selection(mouse_col, mouse_row, FALSE); |
| 616 | clip_process_selection(button, mouse_col, mouse_row, repeat); |
| 617 | } |
| 618 | else if (is_click) |
| 619 | clip_start_selection(mouse_col, mouse_row, repeat); |
| 620 | else if (is_drag) |
| 621 | { |
| 622 | /* Don't try extending a selection if there isn't one. Happens when |
| 623 | * button-down is in the cmdline and them moving mouse upwards. */ |
| 624 | if (clip_star.state != SELECT_CLEARED) |
| 625 | clip_process_selection(button, mouse_col, mouse_row, repeat); |
| 626 | } |
| 627 | else /* release */ |
| 628 | clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE); |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Compare two screen positions ala strcmp() |
| 633 | */ |
| 634 | static int |
| 635 | clip_compare_pos(row1, col1, row2, col2) |
| 636 | int row1; |
| 637 | int col1; |
| 638 | int row2; |
| 639 | int col2; |
| 640 | { |
| 641 | if (row1 > row2) return(1); |
| 642 | if (row1 < row2) return(-1); |
| 643 | if (col1 > col2) return(1); |
| 644 | if (col1 < col2) return(-1); |
| 645 | return(0); |
| 646 | } |
| 647 | |
| 648 | /* |
| 649 | * Start the selection |
| 650 | */ |
| 651 | void |
| 652 | clip_start_selection(col, row, repeated_click) |
| 653 | int col; |
| 654 | int row; |
| 655 | int repeated_click; |
| 656 | { |
| 657 | VimClipboard *cb = &clip_star; |
| 658 | |
| 659 | if (cb->state == SELECT_DONE) |
| 660 | clip_clear_selection(); |
| 661 | |
| 662 | row = check_row(row); |
| 663 | col = check_col(col); |
| 664 | #ifdef FEAT_MBYTE |
| 665 | col = mb_fix_col(col, row); |
| 666 | #endif |
| 667 | |
| 668 | cb->start.lnum = row; |
| 669 | cb->start.col = col; |
| 670 | cb->end = cb->start; |
| 671 | cb->origin_row = (short_u)cb->start.lnum; |
| 672 | cb->state = SELECT_IN_PROGRESS; |
| 673 | |
| 674 | if (repeated_click) |
| 675 | { |
| 676 | if (++cb->mode > SELECT_MODE_LINE) |
| 677 | cb->mode = SELECT_MODE_CHAR; |
| 678 | } |
| 679 | else |
| 680 | cb->mode = SELECT_MODE_CHAR; |
| 681 | |
| 682 | #ifdef FEAT_GUI |
| 683 | /* clear the cursor until the selection is made */ |
| 684 | if (gui.in_use) |
| 685 | gui_undraw_cursor(); |
| 686 | #endif |
| 687 | |
| 688 | switch (cb->mode) |
| 689 | { |
| 690 | case SELECT_MODE_CHAR: |
| 691 | cb->origin_start_col = cb->start.col; |
| 692 | cb->word_end_col = clip_get_line_end((int)cb->start.lnum); |
| 693 | break; |
| 694 | |
| 695 | case SELECT_MODE_WORD: |
| 696 | clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col); |
| 697 | cb->origin_start_col = cb->word_start_col; |
| 698 | cb->origin_end_col = cb->word_end_col; |
| 699 | |
| 700 | clip_invert_area((int)cb->start.lnum, cb->word_start_col, |
| 701 | (int)cb->end.lnum, cb->word_end_col, CLIP_SET); |
| 702 | cb->start.col = cb->word_start_col; |
| 703 | cb->end.col = cb->word_end_col; |
| 704 | break; |
| 705 | |
| 706 | case SELECT_MODE_LINE: |
| 707 | clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum, |
| 708 | (int)Columns, CLIP_SET); |
| 709 | cb->start.col = 0; |
| 710 | cb->end.col = Columns; |
| 711 | break; |
| 712 | } |
| 713 | |
| 714 | cb->prev = cb->start; |
| 715 | |
| 716 | #ifdef DEBUG_SELECTION |
| 717 | printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col); |
| 718 | #endif |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Continue processing the selection |
| 723 | */ |
| 724 | void |
| 725 | clip_process_selection(button, col, row, repeated_click) |
| 726 | int button; |
| 727 | int col; |
| 728 | int row; |
| 729 | int_u repeated_click; |
| 730 | { |
| 731 | VimClipboard *cb = &clip_star; |
| 732 | int diff; |
| 733 | int slen = 1; /* cursor shape width */ |
| 734 | |
| 735 | if (button == MOUSE_RELEASE) |
| 736 | { |
| 737 | /* Check to make sure we have something selected */ |
| 738 | if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col) |
| 739 | { |
| 740 | #ifdef FEAT_GUI |
| 741 | if (gui.in_use) |
| 742 | gui_update_cursor(FALSE, FALSE); |
| 743 | #endif |
| 744 | cb->state = SELECT_CLEARED; |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | #ifdef DEBUG_SELECTION |
| 749 | printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum, |
| 750 | cb->start.col, cb->end.lnum, cb->end.col); |
| 751 | #endif |
| 752 | if (clip_isautosel() |
| 753 | || ( |
| 754 | #ifdef FEAT_GUI |
| 755 | gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) : |
| 756 | #endif |
| 757 | clip_autoselectml)) |
| 758 | clip_copy_modeless_selection(FALSE); |
| 759 | #ifdef FEAT_GUI |
| 760 | if (gui.in_use) |
| 761 | gui_update_cursor(FALSE, FALSE); |
| 762 | #endif |
| 763 | |
| 764 | cb->state = SELECT_DONE; |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | row = check_row(row); |
| 769 | col = check_col(col); |
| 770 | #ifdef FEAT_MBYTE |
| 771 | col = mb_fix_col(col, row); |
| 772 | #endif |
| 773 | |
| 774 | if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click) |
| 775 | return; |
| 776 | |
| 777 | /* |
| 778 | * When extending the selection with the right mouse button, swap the |
| 779 | * start and end if the position is before half the selection |
| 780 | */ |
| 781 | if (cb->state == SELECT_DONE && button == MOUSE_RIGHT) |
| 782 | { |
| 783 | /* |
| 784 | * If the click is before the start, or the click is inside the |
| 785 | * selection and the start is the closest side, set the origin to the |
| 786 | * end of the selection. |
| 787 | */ |
| 788 | if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0 |
| 789 | || (clip_compare_pos(row, col, |
| 790 | (int)cb->end.lnum, cb->end.col) < 0 |
| 791 | && (((cb->start.lnum == cb->end.lnum |
| 792 | && cb->end.col - col > col - cb->start.col)) |
| 793 | || ((diff = (cb->end.lnum - row) - |
| 794 | (row - cb->start.lnum)) > 0 |
| 795 | || (diff == 0 && col < (int)(cb->start.col + |
| 796 | cb->end.col) / 2))))) |
| 797 | { |
| 798 | cb->origin_row = (short_u)cb->end.lnum; |
| 799 | cb->origin_start_col = cb->end.col - 1; |
| 800 | cb->origin_end_col = cb->end.col; |
| 801 | } |
| 802 | else |
| 803 | { |
| 804 | cb->origin_row = (short_u)cb->start.lnum; |
| 805 | cb->origin_start_col = cb->start.col; |
| 806 | cb->origin_end_col = cb->start.col; |
| 807 | } |
| 808 | if (cb->mode == SELECT_MODE_WORD && !repeated_click) |
| 809 | cb->mode = SELECT_MODE_CHAR; |
| 810 | } |
| 811 | |
| 812 | /* set state, for when using the right mouse button */ |
| 813 | cb->state = SELECT_IN_PROGRESS; |
| 814 | |
| 815 | #ifdef DEBUG_SELECTION |
| 816 | printf("Selection extending to (%d,%d)\n", row, col); |
| 817 | #endif |
| 818 | |
| 819 | if (repeated_click && ++cb->mode > SELECT_MODE_LINE) |
| 820 | cb->mode = SELECT_MODE_CHAR; |
| 821 | |
| 822 | switch (cb->mode) |
| 823 | { |
| 824 | case SELECT_MODE_CHAR: |
| 825 | /* If we're on a different line, find where the line ends */ |
| 826 | if (row != cb->prev.lnum) |
| 827 | cb->word_end_col = clip_get_line_end(row); |
| 828 | |
| 829 | /* See if we are before or after the origin of the selection */ |
| 830 | if (clip_compare_pos(row, col, cb->origin_row, |
| 831 | cb->origin_start_col) >= 0) |
| 832 | { |
| 833 | if (col >= (int)cb->word_end_col) |
| 834 | clip_update_modeless_selection(cb, cb->origin_row, |
| 835 | cb->origin_start_col, row, (int)Columns); |
| 836 | else |
| 837 | { |
| 838 | #ifdef FEAT_MBYTE |
| 839 | if (has_mbyte && mb_lefthalve(row, col)) |
| 840 | slen = 2; |
| 841 | #endif |
| 842 | clip_update_modeless_selection(cb, cb->origin_row, |
| 843 | cb->origin_start_col, row, col + slen); |
| 844 | } |
| 845 | } |
| 846 | else |
| 847 | { |
| 848 | #ifdef FEAT_MBYTE |
| 849 | if (has_mbyte |
| 850 | && mb_lefthalve(cb->origin_row, cb->origin_start_col)) |
| 851 | slen = 2; |
| 852 | #endif |
| 853 | if (col >= (int)cb->word_end_col) |
| 854 | clip_update_modeless_selection(cb, row, cb->word_end_col, |
| 855 | cb->origin_row, cb->origin_start_col + slen); |
| 856 | else |
| 857 | clip_update_modeless_selection(cb, row, col, |
| 858 | cb->origin_row, cb->origin_start_col + slen); |
| 859 | } |
| 860 | break; |
| 861 | |
| 862 | case SELECT_MODE_WORD: |
| 863 | /* If we are still within the same word, do nothing */ |
| 864 | if (row == cb->prev.lnum && col >= (int)cb->word_start_col |
| 865 | && col < (int)cb->word_end_col && !repeated_click) |
| 866 | return; |
| 867 | |
| 868 | /* Get new word boundaries */ |
| 869 | clip_get_word_boundaries(cb, row, col); |
| 870 | |
| 871 | /* Handle being after the origin point of selection */ |
| 872 | if (clip_compare_pos(row, col, cb->origin_row, |
| 873 | cb->origin_start_col) >= 0) |
| 874 | clip_update_modeless_selection(cb, cb->origin_row, |
| 875 | cb->origin_start_col, row, cb->word_end_col); |
| 876 | else |
| 877 | clip_update_modeless_selection(cb, row, cb->word_start_col, |
| 878 | cb->origin_row, cb->origin_end_col); |
| 879 | break; |
| 880 | |
| 881 | case SELECT_MODE_LINE: |
| 882 | if (row == cb->prev.lnum && !repeated_click) |
| 883 | return; |
| 884 | |
| 885 | if (clip_compare_pos(row, col, cb->origin_row, |
| 886 | cb->origin_start_col) >= 0) |
| 887 | clip_update_modeless_selection(cb, cb->origin_row, 0, row, |
| 888 | (int)Columns); |
| 889 | else |
| 890 | clip_update_modeless_selection(cb, row, 0, cb->origin_row, |
| 891 | (int)Columns); |
| 892 | break; |
| 893 | } |
| 894 | |
| 895 | cb->prev.lnum = row; |
| 896 | cb->prev.col = col; |
| 897 | |
| 898 | #ifdef DEBUG_SELECTION |
| 899 | printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum, |
| 900 | cb->start.col, cb->end.lnum, cb->end.col); |
| 901 | #endif |
| 902 | } |
| 903 | |
| 904 | #if 0 /* not used */ |
| 905 | /* |
| 906 | * Called after an Expose event to redraw the selection |
| 907 | */ |
| 908 | void |
| 909 | clip_redraw_selection(x, y, w, h) |
| 910 | int x; |
| 911 | int y; |
| 912 | int w; |
| 913 | int h; |
| 914 | { |
| 915 | VimClipboard *cb = &clip_star; |
| 916 | int row1, col1, row2, col2; |
| 917 | int row; |
| 918 | int start; |
| 919 | int end; |
| 920 | |
| 921 | if (cb->state == SELECT_CLEARED) |
| 922 | return; |
| 923 | |
| 924 | row1 = check_row(Y_2_ROW(y)); |
| 925 | col1 = check_col(X_2_COL(x)); |
| 926 | row2 = check_row(Y_2_ROW(y + h - 1)); |
| 927 | col2 = check_col(X_2_COL(x + w - 1)); |
| 928 | |
| 929 | /* Limit the rows that need to be re-drawn */ |
| 930 | if (cb->start.lnum > row1) |
| 931 | row1 = cb->start.lnum; |
| 932 | if (cb->end.lnum < row2) |
| 933 | row2 = cb->end.lnum; |
| 934 | |
| 935 | /* Look at each row that might need to be re-drawn */ |
| 936 | for (row = row1; row <= row2; row++) |
| 937 | { |
| 938 | /* For the first selection row, use the starting selection column */ |
| 939 | if (row == cb->start.lnum) |
| 940 | start = cb->start.col; |
| 941 | else |
| 942 | start = 0; |
| 943 | |
| 944 | /* For the last selection row, use the ending selection column */ |
| 945 | if (row == cb->end.lnum) |
| 946 | end = cb->end.col; |
| 947 | else |
| 948 | end = Columns; |
| 949 | |
| 950 | if (col1 > start) |
| 951 | start = col1; |
| 952 | |
| 953 | if (col2 < end) |
| 954 | end = col2 + 1; |
| 955 | |
| 956 | if (end > start) |
| 957 | gui_mch_invert_rectangle(row, start, 1, end - start); |
| 958 | } |
| 959 | } |
| 960 | #endif |
| 961 | |
| 962 | # if defined(FEAT_GUI) || defined(PROTO) |
| 963 | /* |
| 964 | * Redraw part of the selection if character at "row,col" is inside of it. |
| 965 | * Only used for the GUI. |
| 966 | */ |
| 967 | void |
| 968 | clip_may_redraw_selection(row, col, len) |
| 969 | int row, col; |
| 970 | int len; |
| 971 | { |
| 972 | int start = col; |
| 973 | int end = col + len; |
| 974 | |
| 975 | if (clip_star.state != SELECT_CLEARED |
| 976 | && row >= clip_star.start.lnum |
| 977 | && row <= clip_star.end.lnum) |
| 978 | { |
| 979 | if (row == clip_star.start.lnum && start < (int)clip_star.start.col) |
| 980 | start = clip_star.start.col; |
| 981 | if (row == clip_star.end.lnum && end > (int)clip_star.end.col) |
| 982 | end = clip_star.end.col; |
| 983 | if (end > start) |
| 984 | clip_invert_area(row, start, row, end, 0); |
| 985 | } |
| 986 | } |
| 987 | # endif |
| 988 | |
| 989 | /* |
| 990 | * Called from outside to clear selected region from the display |
| 991 | */ |
| 992 | void |
| 993 | clip_clear_selection() |
| 994 | { |
| 995 | VimClipboard *cb = &clip_star; |
| 996 | |
| 997 | if (cb->state == SELECT_CLEARED) |
| 998 | return; |
| 999 | |
| 1000 | clip_invert_area((int)cb->start.lnum, cb->start.col, (int)cb->end.lnum, |
| 1001 | cb->end.col, CLIP_CLEAR); |
| 1002 | cb->state = SELECT_CLEARED; |
| 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * Clear the selection if any lines from "row1" to "row2" are inside of it. |
| 1007 | */ |
| 1008 | void |
| 1009 | clip_may_clear_selection(row1, row2) |
| 1010 | int row1, row2; |
| 1011 | { |
| 1012 | if (clip_star.state == SELECT_DONE |
| 1013 | && row2 >= clip_star.start.lnum |
| 1014 | && row1 <= clip_star.end.lnum) |
| 1015 | clip_clear_selection(); |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * Called before the screen is scrolled up or down. Adjusts the line numbers |
| 1020 | * of the selection. Call with big number when clearing the screen. |
| 1021 | */ |
| 1022 | void |
| 1023 | clip_scroll_selection(rows) |
| 1024 | int rows; /* negative for scroll down */ |
| 1025 | { |
| 1026 | int lnum; |
| 1027 | |
| 1028 | if (clip_star.state == SELECT_CLEARED) |
| 1029 | return; |
| 1030 | |
| 1031 | lnum = clip_star.start.lnum - rows; |
| 1032 | if (lnum <= 0) |
| 1033 | clip_star.start.lnum = 0; |
| 1034 | else if (lnum >= screen_Rows) /* scrolled off of the screen */ |
| 1035 | clip_star.state = SELECT_CLEARED; |
| 1036 | else |
| 1037 | clip_star.start.lnum = lnum; |
| 1038 | |
| 1039 | lnum = clip_star.end.lnum - rows; |
| 1040 | if (lnum < 0) /* scrolled off of the screen */ |
| 1041 | clip_star.state = SELECT_CLEARED; |
| 1042 | else if (lnum >= screen_Rows) |
| 1043 | clip_star.end.lnum = screen_Rows - 1; |
| 1044 | else |
| 1045 | clip_star.end.lnum = lnum; |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * Invert a region of the display between a starting and ending row and column |
| 1050 | * Values for "how": |
| 1051 | * CLIP_CLEAR: undo inversion |
| 1052 | * CLIP_SET: set inversion |
| 1053 | * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise. |
| 1054 | * 0: invert (GUI only). |
| 1055 | */ |
| 1056 | static void |
| 1057 | clip_invert_area(row1, col1, row2, col2, how) |
| 1058 | int row1; |
| 1059 | int col1; |
| 1060 | int row2; |
| 1061 | int col2; |
| 1062 | int how; |
| 1063 | { |
| 1064 | int invert = FALSE; |
| 1065 | |
| 1066 | if (how == CLIP_SET) |
| 1067 | invert = TRUE; |
| 1068 | |
| 1069 | /* Swap the from and to positions so the from is always before */ |
| 1070 | if (clip_compare_pos(row1, col1, row2, col2) > 0) |
| 1071 | { |
| 1072 | int tmp_row, tmp_col; |
| 1073 | |
| 1074 | tmp_row = row1; |
| 1075 | tmp_col = col1; |
| 1076 | row1 = row2; |
| 1077 | col1 = col2; |
| 1078 | row2 = tmp_row; |
| 1079 | col2 = tmp_col; |
| 1080 | } |
| 1081 | else if (how == CLIP_TOGGLE) |
| 1082 | invert = TRUE; |
| 1083 | |
| 1084 | /* If all on the same line, do it the easy way */ |
| 1085 | if (row1 == row2) |
| 1086 | { |
| 1087 | clip_invert_rectangle(row1, col1, 1, col2 - col1, invert); |
| 1088 | } |
| 1089 | else |
| 1090 | { |
| 1091 | /* Handle a piece of the first line */ |
| 1092 | if (col1 > 0) |
| 1093 | { |
| 1094 | clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert); |
| 1095 | row1++; |
| 1096 | } |
| 1097 | |
| 1098 | /* Handle a piece of the last line */ |
| 1099 | if (col2 < Columns - 1) |
| 1100 | { |
| 1101 | clip_invert_rectangle(row2, 0, 1, col2, invert); |
| 1102 | row2--; |
| 1103 | } |
| 1104 | |
| 1105 | /* Handle the rectangle thats left */ |
| 1106 | if (row2 >= row1) |
| 1107 | clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns, |
| 1108 | invert); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * Invert or un-invert a rectangle of the screen. |
| 1114 | * "invert" is true if the result is inverted. |
| 1115 | */ |
| 1116 | static void |
| 1117 | clip_invert_rectangle(row, col, height, width, invert) |
| 1118 | int row; |
| 1119 | int col; |
| 1120 | int height; |
| 1121 | int width; |
| 1122 | int invert; |
| 1123 | { |
| 1124 | #ifdef FEAT_GUI |
| 1125 | if (gui.in_use) |
| 1126 | gui_mch_invert_rectangle(row, col, height, width); |
| 1127 | else |
| 1128 | #endif |
| 1129 | screen_draw_rectangle(row, col, height, width, invert); |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * Copy the currently selected area into the '*' register so it will be |
| 1134 | * available for pasting. |
| 1135 | * When "both" is TRUE also copy to the '+' register. |
| 1136 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1137 | void |
| 1138 | clip_copy_modeless_selection(both) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 1139 | int both UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1140 | { |
| 1141 | char_u *buffer; |
| 1142 | char_u *bufp; |
| 1143 | int row; |
| 1144 | int start_col; |
| 1145 | int end_col; |
| 1146 | int line_end_col; |
| 1147 | int add_newline_flag = FALSE; |
| 1148 | int len; |
| 1149 | #ifdef FEAT_MBYTE |
| 1150 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1151 | #endif |
| 1152 | int row1 = clip_star.start.lnum; |
| 1153 | int col1 = clip_star.start.col; |
| 1154 | int row2 = clip_star.end.lnum; |
| 1155 | int col2 = clip_star.end.col; |
| 1156 | |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1157 | /* Can't use ScreenLines unless initialized */ |
| 1158 | if (ScreenLines == NULL) |
| 1159 | return; |
| 1160 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | /* |
| 1162 | * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2. |
| 1163 | */ |
| 1164 | if (row1 > row2) |
| 1165 | { |
| 1166 | row = row1; row1 = row2; row2 = row; |
| 1167 | row = col1; col1 = col2; col2 = row; |
| 1168 | } |
| 1169 | else if (row1 == row2 && col1 > col2) |
| 1170 | { |
| 1171 | row = col1; col1 = col2; col2 = row; |
| 1172 | } |
| 1173 | #ifdef FEAT_MBYTE |
| 1174 | /* correct starting point for being on right halve of double-wide char */ |
| 1175 | p = ScreenLines + LineOffset[row1]; |
| 1176 | if (enc_dbcs != 0) |
| 1177 | col1 -= (*mb_head_off)(p, p + col1); |
| 1178 | else if (enc_utf8 && p[col1] == 0) |
| 1179 | --col1; |
| 1180 | #endif |
| 1181 | |
| 1182 | /* Create a temporary buffer for storing the text */ |
| 1183 | len = (row2 - row1 + 1) * Columns + 1; |
| 1184 | #ifdef FEAT_MBYTE |
| 1185 | if (enc_dbcs != 0) |
| 1186 | len *= 2; /* max. 2 bytes per display cell */ |
| 1187 | else if (enc_utf8) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1188 | len *= MB_MAXBYTES; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1189 | #endif |
| 1190 | buffer = lalloc((long_u)len, TRUE); |
| 1191 | if (buffer == NULL) /* out of memory */ |
| 1192 | return; |
| 1193 | |
| 1194 | /* Process each row in the selection */ |
| 1195 | for (bufp = buffer, row = row1; row <= row2; row++) |
| 1196 | { |
| 1197 | if (row == row1) |
| 1198 | start_col = col1; |
| 1199 | else |
| 1200 | start_col = 0; |
| 1201 | |
| 1202 | if (row == row2) |
| 1203 | end_col = col2; |
| 1204 | else |
| 1205 | end_col = Columns; |
| 1206 | |
| 1207 | line_end_col = clip_get_line_end(row); |
| 1208 | |
| 1209 | /* See if we need to nuke some trailing whitespace */ |
| 1210 | if (end_col >= Columns && (row < row2 || end_col > line_end_col)) |
| 1211 | { |
| 1212 | /* Get rid of trailing whitespace */ |
| 1213 | end_col = line_end_col; |
| 1214 | if (end_col < start_col) |
| 1215 | end_col = start_col; |
| 1216 | |
| 1217 | /* If the last line extended to the end, add an extra newline */ |
| 1218 | if (row == row2) |
| 1219 | add_newline_flag = TRUE; |
| 1220 | } |
| 1221 | |
| 1222 | /* If after the first row, we need to always add a newline */ |
| 1223 | if (row > row1 && !LineWraps[row - 1]) |
| 1224 | *bufp++ = NL; |
| 1225 | |
| 1226 | if (row < screen_Rows && end_col <= screen_Columns) |
| 1227 | { |
| 1228 | #ifdef FEAT_MBYTE |
| 1229 | if (enc_dbcs != 0) |
| 1230 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 1231 | int i; |
| 1232 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1233 | p = ScreenLines + LineOffset[row]; |
| 1234 | for (i = start_col; i < end_col; ++i) |
| 1235 | if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e) |
| 1236 | { |
| 1237 | /* single-width double-byte char */ |
| 1238 | *bufp++ = 0x8e; |
| 1239 | *bufp++ = ScreenLines2[LineOffset[row] + i]; |
| 1240 | } |
| 1241 | else |
| 1242 | { |
| 1243 | *bufp++ = p[i]; |
| 1244 | if (MB_BYTE2LEN(p[i]) == 2) |
| 1245 | *bufp++ = p[++i]; |
| 1246 | } |
| 1247 | } |
| 1248 | else if (enc_utf8) |
| 1249 | { |
| 1250 | int off; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1251 | int i; |
Bram Moolenaar | 34e9e2f | 2006-03-14 23:07:19 +0000 | [diff] [blame] | 1252 | int ci; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1253 | |
| 1254 | off = LineOffset[row]; |
| 1255 | for (i = start_col; i < end_col; ++i) |
| 1256 | { |
| 1257 | /* The base character is either in ScreenLinesUC[] or |
| 1258 | * ScreenLines[]. */ |
| 1259 | if (ScreenLinesUC[off + i] == 0) |
| 1260 | *bufp++ = ScreenLines[off + i]; |
| 1261 | else |
| 1262 | { |
| 1263 | bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp); |
Bram Moolenaar | 34e9e2f | 2006-03-14 23:07:19 +0000 | [diff] [blame] | 1264 | for (ci = 0; ci < Screen_mco; ++ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1265 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1266 | /* Add a composing character. */ |
Bram Moolenaar | 34e9e2f | 2006-03-14 23:07:19 +0000 | [diff] [blame] | 1267 | if (ScreenLinesC[ci][off + i] == 0) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1268 | break; |
Bram Moolenaar | 34e9e2f | 2006-03-14 23:07:19 +0000 | [diff] [blame] | 1269 | bufp += utf_char2bytes(ScreenLinesC[ci][off + i], |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1270 | bufp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | /* Skip right halve of double-wide character. */ |
| 1274 | if (ScreenLines[off + i + 1] == 0) |
| 1275 | ++i; |
| 1276 | } |
| 1277 | } |
| 1278 | else |
| 1279 | #endif |
| 1280 | { |
| 1281 | STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col, |
| 1282 | end_col - start_col); |
| 1283 | bufp += end_col - start_col; |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | /* Add a newline at the end if the selection ended there */ |
| 1289 | if (add_newline_flag) |
| 1290 | *bufp++ = NL; |
| 1291 | |
| 1292 | /* First cleanup any old selection and become the owner. */ |
| 1293 | clip_free_selection(&clip_star); |
| 1294 | clip_own_selection(&clip_star); |
| 1295 | |
| 1296 | /* Yank the text into the '*' register. */ |
| 1297 | clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star); |
| 1298 | |
| 1299 | /* Make the register contents available to the outside world. */ |
| 1300 | clip_gen_set_selection(&clip_star); |
| 1301 | |
| 1302 | #ifdef FEAT_X11 |
| 1303 | if (both) |
| 1304 | { |
| 1305 | /* Do the same for the '+' register. */ |
| 1306 | clip_free_selection(&clip_plus); |
| 1307 | clip_own_selection(&clip_plus); |
| 1308 | clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus); |
| 1309 | clip_gen_set_selection(&clip_plus); |
| 1310 | } |
| 1311 | #endif |
| 1312 | vim_free(buffer); |
| 1313 | } |
| 1314 | |
| 1315 | /* |
| 1316 | * Find the starting and ending positions of the word at the given row and |
| 1317 | * column. Only white-separated words are recognized here. |
| 1318 | */ |
| 1319 | #define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c)) |
| 1320 | |
| 1321 | static void |
| 1322 | clip_get_word_boundaries(cb, row, col) |
| 1323 | VimClipboard *cb; |
| 1324 | int row; |
| 1325 | int col; |
| 1326 | { |
| 1327 | int start_class; |
| 1328 | int temp_col; |
| 1329 | char_u *p; |
| 1330 | #ifdef FEAT_MBYTE |
| 1331 | int mboff; |
| 1332 | #endif |
| 1333 | |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1334 | if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1335 | return; |
| 1336 | |
| 1337 | p = ScreenLines + LineOffset[row]; |
| 1338 | #ifdef FEAT_MBYTE |
| 1339 | /* Correct for starting in the right halve of a double-wide char */ |
| 1340 | if (enc_dbcs != 0) |
| 1341 | col -= dbcs_screen_head_off(p, p + col); |
| 1342 | else if (enc_utf8 && p[col] == 0) |
| 1343 | --col; |
| 1344 | #endif |
| 1345 | start_class = CHAR_CLASS(p[col]); |
| 1346 | |
| 1347 | temp_col = col; |
| 1348 | for ( ; temp_col > 0; temp_col--) |
| 1349 | #ifdef FEAT_MBYTE |
| 1350 | if (enc_dbcs != 0 |
| 1351 | && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0) |
| 1352 | temp_col -= mboff; |
| 1353 | else |
| 1354 | #endif |
| 1355 | if (CHAR_CLASS(p[temp_col - 1]) != start_class |
| 1356 | #ifdef FEAT_MBYTE |
| 1357 | && !(enc_utf8 && p[temp_col - 1] == 0) |
| 1358 | #endif |
| 1359 | ) |
| 1360 | break; |
| 1361 | cb->word_start_col = temp_col; |
| 1362 | |
| 1363 | temp_col = col; |
| 1364 | for ( ; temp_col < screen_Columns; temp_col++) |
| 1365 | #ifdef FEAT_MBYTE |
| 1366 | if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2) |
| 1367 | ++temp_col; |
| 1368 | else |
| 1369 | #endif |
| 1370 | if (CHAR_CLASS(p[temp_col]) != start_class |
| 1371 | #ifdef FEAT_MBYTE |
| 1372 | && !(enc_utf8 && p[temp_col] == 0) |
| 1373 | #endif |
| 1374 | ) |
| 1375 | break; |
| 1376 | cb->word_end_col = temp_col; |
| 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | * Find the column position for the last non-whitespace character on the given |
| 1381 | * line. |
| 1382 | */ |
| 1383 | static int |
| 1384 | clip_get_line_end(row) |
| 1385 | int row; |
| 1386 | { |
| 1387 | int i; |
| 1388 | |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1389 | if (row >= screen_Rows || ScreenLines == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1390 | return 0; |
| 1391 | for (i = screen_Columns; i > 0; i--) |
| 1392 | if (ScreenLines[LineOffset[row] + i - 1] != ' ') |
| 1393 | break; |
| 1394 | return i; |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * Update the currently selected region by adding and/or subtracting from the |
| 1399 | * beginning or end and inverting the changed area(s). |
| 1400 | */ |
| 1401 | static void |
| 1402 | clip_update_modeless_selection(cb, row1, col1, row2, col2) |
| 1403 | VimClipboard *cb; |
| 1404 | int row1; |
| 1405 | int col1; |
| 1406 | int row2; |
| 1407 | int col2; |
| 1408 | { |
| 1409 | /* See if we changed at the beginning of the selection */ |
| 1410 | if (row1 != cb->start.lnum || col1 != (int)cb->start.col) |
| 1411 | { |
| 1412 | clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col, |
| 1413 | CLIP_TOGGLE); |
| 1414 | cb->start.lnum = row1; |
| 1415 | cb->start.col = col1; |
| 1416 | } |
| 1417 | |
| 1418 | /* See if we changed at the end of the selection */ |
| 1419 | if (row2 != cb->end.lnum || col2 != (int)cb->end.col) |
| 1420 | { |
| 1421 | clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2, |
| 1422 | CLIP_TOGGLE); |
| 1423 | cb->end.lnum = row2; |
| 1424 | cb->end.col = col2; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | int |
| 1429 | clip_gen_own_selection(cbd) |
| 1430 | VimClipboard *cbd; |
| 1431 | { |
| 1432 | #ifdef FEAT_XCLIPBOARD |
| 1433 | # ifdef FEAT_GUI |
| 1434 | if (gui.in_use) |
| 1435 | return clip_mch_own_selection(cbd); |
| 1436 | else |
| 1437 | # endif |
| 1438 | return clip_xterm_own_selection(cbd); |
| 1439 | #else |
| 1440 | return clip_mch_own_selection(cbd); |
| 1441 | #endif |
| 1442 | } |
| 1443 | |
| 1444 | void |
| 1445 | clip_gen_lose_selection(cbd) |
| 1446 | VimClipboard *cbd; |
| 1447 | { |
| 1448 | #ifdef FEAT_XCLIPBOARD |
| 1449 | # ifdef FEAT_GUI |
| 1450 | if (gui.in_use) |
| 1451 | clip_mch_lose_selection(cbd); |
| 1452 | else |
| 1453 | # endif |
| 1454 | clip_xterm_lose_selection(cbd); |
| 1455 | #else |
| 1456 | clip_mch_lose_selection(cbd); |
| 1457 | #endif |
| 1458 | } |
| 1459 | |
| 1460 | void |
| 1461 | clip_gen_set_selection(cbd) |
| 1462 | VimClipboard *cbd; |
| 1463 | { |
| 1464 | #ifdef FEAT_XCLIPBOARD |
| 1465 | # ifdef FEAT_GUI |
| 1466 | if (gui.in_use) |
| 1467 | clip_mch_set_selection(cbd); |
| 1468 | else |
| 1469 | # endif |
| 1470 | clip_xterm_set_selection(cbd); |
| 1471 | #else |
| 1472 | clip_mch_set_selection(cbd); |
| 1473 | #endif |
| 1474 | } |
| 1475 | |
| 1476 | void |
| 1477 | clip_gen_request_selection(cbd) |
| 1478 | VimClipboard *cbd; |
| 1479 | { |
| 1480 | #ifdef FEAT_XCLIPBOARD |
| 1481 | # ifdef FEAT_GUI |
| 1482 | if (gui.in_use) |
| 1483 | clip_mch_request_selection(cbd); |
| 1484 | else |
| 1485 | # endif |
| 1486 | clip_xterm_request_selection(cbd); |
| 1487 | #else |
| 1488 | clip_mch_request_selection(cbd); |
| 1489 | #endif |
| 1490 | } |
| 1491 | |
| 1492 | #endif /* FEAT_CLIPBOARD */ |
| 1493 | |
| 1494 | /***************************************************************************** |
| 1495 | * Functions that handle the input buffer. |
| 1496 | * This is used for any GUI version, and the unix terminal version. |
| 1497 | * |
| 1498 | * For Unix, the input characters are buffered to be able to check for a |
| 1499 | * CTRL-C. This should be done with signals, but I don't know how to do that |
| 1500 | * in a portable way for a tty in RAW mode. |
| 1501 | * |
| 1502 | * For the client-server code in the console the received keys are put in the |
| 1503 | * input buffer. |
| 1504 | */ |
| 1505 | |
| 1506 | #if defined(USE_INPUT_BUF) || defined(PROTO) |
| 1507 | |
| 1508 | /* |
| 1509 | * Internal typeahead buffer. Includes extra space for long key code |
| 1510 | * descriptions which would otherwise overflow. The buffer is considered full |
| 1511 | * when only this extra space (or part of it) remains. |
| 1512 | */ |
| 1513 | #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \ |
| 1514 | || defined(FEAT_CLIENTSERVER) |
| 1515 | /* |
| 1516 | * Sun WorkShop and NetBeans stuff debugger commands into the input buffer. |
| 1517 | * This requires a larger buffer... |
| 1518 | * (Madsen) Go with this for remote input as well ... |
| 1519 | */ |
| 1520 | # define INBUFLEN 4096 |
| 1521 | #else |
| 1522 | # define INBUFLEN 250 |
| 1523 | #endif |
| 1524 | |
| 1525 | static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN]; |
| 1526 | static int inbufcount = 0; /* number of chars in inbuf[] */ |
| 1527 | |
| 1528 | /* |
| 1529 | * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and |
| 1530 | * trash_input_buf() are functions for manipulating the input buffer. These |
| 1531 | * are used by the gui_* calls when a GUI is used to handle keyboard input. |
| 1532 | */ |
| 1533 | |
| 1534 | int |
| 1535 | vim_is_input_buf_full() |
| 1536 | { |
| 1537 | return (inbufcount >= INBUFLEN); |
| 1538 | } |
| 1539 | |
| 1540 | int |
| 1541 | vim_is_input_buf_empty() |
| 1542 | { |
| 1543 | return (inbufcount == 0); |
| 1544 | } |
| 1545 | |
| 1546 | #if defined(FEAT_OLE) || defined(PROTO) |
| 1547 | int |
| 1548 | vim_free_in_input_buf() |
| 1549 | { |
| 1550 | return (INBUFLEN - inbufcount); |
| 1551 | } |
| 1552 | #endif |
| 1553 | |
Bram Moolenaar | 241a8aa | 2005-12-06 20:04:44 +0000 | [diff] [blame] | 1554 | #if defined(FEAT_GUI_GTK) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1555 | int |
| 1556 | vim_used_in_input_buf() |
| 1557 | { |
| 1558 | return inbufcount; |
| 1559 | } |
| 1560 | #endif |
| 1561 | |
| 1562 | #if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO) |
| 1563 | /* |
| 1564 | * Return the current contents of the input buffer and make it empty. |
| 1565 | * The returned pointer must be passed to set_input_buf() later. |
| 1566 | */ |
| 1567 | char_u * |
| 1568 | get_input_buf() |
| 1569 | { |
| 1570 | garray_T *gap; |
| 1571 | |
| 1572 | /* We use a growarray to store the data pointer and the length. */ |
| 1573 | gap = (garray_T *)alloc((unsigned)sizeof(garray_T)); |
| 1574 | if (gap != NULL) |
| 1575 | { |
| 1576 | /* Add one to avoid a zero size. */ |
| 1577 | gap->ga_data = alloc((unsigned)inbufcount + 1); |
| 1578 | if (gap->ga_data != NULL) |
| 1579 | mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount); |
| 1580 | gap->ga_len = inbufcount; |
| 1581 | } |
| 1582 | trash_input_buf(); |
| 1583 | return (char_u *)gap; |
| 1584 | } |
| 1585 | |
| 1586 | /* |
| 1587 | * Restore the input buffer with a pointer returned from get_input_buf(). |
| 1588 | * The allocated memory is freed, this only works once! |
| 1589 | */ |
| 1590 | void |
| 1591 | set_input_buf(p) |
| 1592 | char_u *p; |
| 1593 | { |
| 1594 | garray_T *gap = (garray_T *)p; |
| 1595 | |
| 1596 | if (gap != NULL) |
| 1597 | { |
| 1598 | if (gap->ga_data != NULL) |
| 1599 | { |
| 1600 | mch_memmove(inbuf, gap->ga_data, gap->ga_len); |
| 1601 | inbufcount = gap->ga_len; |
| 1602 | vim_free(gap->ga_data); |
| 1603 | } |
| 1604 | vim_free(gap); |
| 1605 | } |
| 1606 | } |
| 1607 | #endif |
| 1608 | |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 1609 | #if defined(FEAT_GUI) \ |
| 1610 | || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1611 | || defined(FEAT_XCLIPBOARD) || defined(VMS) \ |
Bram Moolenaar | f52c725 | 2006-02-10 23:23:57 +0000 | [diff] [blame] | 1612 | || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \ |
Bram Moolenaar | f52c725 | 2006-02-10 23:23:57 +0000 | [diff] [blame] | 1613 | || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1614 | /* |
| 1615 | * Add the given bytes to the input buffer |
| 1616 | * Special keys start with CSI. A real CSI must have been translated to |
| 1617 | * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation. |
| 1618 | */ |
| 1619 | void |
| 1620 | add_to_input_buf(s, len) |
| 1621 | char_u *s; |
| 1622 | int len; |
| 1623 | { |
| 1624 | if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN) |
| 1625 | return; /* Shouldn't ever happen! */ |
| 1626 | |
| 1627 | #ifdef FEAT_HANGULIN |
| 1628 | if ((State & (INSERT|CMDLINE)) && hangul_input_state_get()) |
| 1629 | if ((len = hangul_input_process(s, len)) == 0) |
| 1630 | return; |
| 1631 | #endif |
| 1632 | |
| 1633 | while (len--) |
| 1634 | inbuf[inbufcount++] = *s++; |
| 1635 | } |
| 1636 | #endif |
| 1637 | |
Bram Moolenaar | 70c2a63 | 2007-08-15 18:08:50 +0000 | [diff] [blame] | 1638 | #if ((defined(FEAT_XIM) || defined(FEAT_DND)) && defined(FEAT_GUI_GTK)) \ |
| 1639 | || defined(FEAT_GUI_MSWIN) \ |
| 1640 | || defined(FEAT_GUI_MAC) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1641 | || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \ |
Bram Moolenaar | f52c725 | 2006-02-10 23:23:57 +0000 | [diff] [blame] | 1642 | || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \ |
| 1643 | || defined(FEAT_MENU))) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1644 | || defined(PROTO) |
| 1645 | /* |
| 1646 | * Add "str[len]" to the input buffer while escaping CSI bytes. |
| 1647 | */ |
| 1648 | void |
| 1649 | add_to_input_buf_csi(char_u *str, int len) |
| 1650 | { |
| 1651 | int i; |
| 1652 | char_u buf[2]; |
| 1653 | |
| 1654 | for (i = 0; i < len; ++i) |
| 1655 | { |
| 1656 | add_to_input_buf(str + i, 1); |
| 1657 | if (str[i] == CSI) |
| 1658 | { |
| 1659 | /* Turn CSI into K_CSI. */ |
| 1660 | buf[0] = KS_EXTRA; |
| 1661 | buf[1] = (int)KE_CSI; |
| 1662 | add_to_input_buf(buf, 2); |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | #endif |
| 1667 | |
| 1668 | #if defined(FEAT_HANGULIN) || defined(PROTO) |
| 1669 | void |
| 1670 | push_raw_key (s, len) |
| 1671 | char_u *s; |
| 1672 | int len; |
| 1673 | { |
| 1674 | while (len--) |
| 1675 | inbuf[inbufcount++] = *s++; |
| 1676 | } |
| 1677 | #endif |
| 1678 | |
| 1679 | #if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \ |
| 1680 | || defined(PROTO) |
| 1681 | /* Remove everything from the input buffer. Called when ^C is found */ |
| 1682 | void |
| 1683 | trash_input_buf() |
| 1684 | { |
| 1685 | inbufcount = 0; |
| 1686 | } |
| 1687 | #endif |
| 1688 | |
| 1689 | /* |
| 1690 | * Read as much data from the input buffer as possible up to maxlen, and store |
| 1691 | * it in buf. |
| 1692 | * Note: this function used to be Read() in unix.c |
| 1693 | */ |
| 1694 | int |
| 1695 | read_from_input_buf(buf, maxlen) |
| 1696 | char_u *buf; |
| 1697 | long maxlen; |
| 1698 | { |
| 1699 | if (inbufcount == 0) /* if the buffer is empty, fill it */ |
| 1700 | fill_input_buf(TRUE); |
| 1701 | if (maxlen > inbufcount) |
| 1702 | maxlen = inbufcount; |
| 1703 | mch_memmove(buf, inbuf, (size_t)maxlen); |
| 1704 | inbufcount -= maxlen; |
| 1705 | if (inbufcount) |
| 1706 | mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount); |
| 1707 | return (int)maxlen; |
| 1708 | } |
| 1709 | |
| 1710 | void |
| 1711 | fill_input_buf(exit_on_error) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 1712 | int exit_on_error UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1713 | { |
| 1714 | #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX) |
| 1715 | int len; |
| 1716 | int try; |
| 1717 | static int did_read_something = FALSE; |
| 1718 | # ifdef FEAT_MBYTE |
| 1719 | static char_u *rest = NULL; /* unconverted rest of previous read */ |
| 1720 | static int restlen = 0; |
| 1721 | int unconverted; |
| 1722 | # endif |
| 1723 | #endif |
| 1724 | |
| 1725 | #ifdef FEAT_GUI |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 1726 | if (gui.in_use |
| 1727 | # ifdef NO_CONSOLE_INPUT |
| 1728 | /* Don't use the GUI input when the window hasn't been opened yet. |
| 1729 | * We get here from ui_inchar() when we should try reading from stdin. */ |
| 1730 | && !no_console_input() |
| 1731 | # endif |
| 1732 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1733 | { |
| 1734 | gui_mch_update(); |
| 1735 | return; |
| 1736 | } |
| 1737 | #endif |
| 1738 | #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX) |
| 1739 | if (vim_is_input_buf_full()) |
| 1740 | return; |
| 1741 | /* |
| 1742 | * Fill_input_buf() is only called when we really need a character. |
| 1743 | * If we can't get any, but there is some in the buffer, just return. |
| 1744 | * If we can't get any, and there isn't any in the buffer, we give up and |
| 1745 | * exit Vim. |
| 1746 | */ |
| 1747 | # ifdef __BEOS__ |
| 1748 | /* |
| 1749 | * On the BeBox version (for now), all input is secretly performed within |
| 1750 | * beos_select() which is called from RealWaitForChar(). |
| 1751 | */ |
| 1752 | while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL)) |
| 1753 | ; |
| 1754 | len = inbufcount; |
| 1755 | inbufcount = 0; |
| 1756 | # else |
| 1757 | |
| 1758 | # ifdef FEAT_SNIFF |
| 1759 | if (sniff_request_waiting) |
| 1760 | { |
| 1761 | add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */ |
| 1762 | sniff_request_waiting = 0; |
| 1763 | want_sniff_request = 0; |
| 1764 | return; |
| 1765 | } |
| 1766 | # endif |
| 1767 | |
| 1768 | # ifdef FEAT_MBYTE |
| 1769 | if (rest != NULL) |
| 1770 | { |
| 1771 | /* Use remainder of previous call, starts with an invalid character |
| 1772 | * that may become valid when reading more. */ |
| 1773 | if (restlen > INBUFLEN - inbufcount) |
| 1774 | unconverted = INBUFLEN - inbufcount; |
| 1775 | else |
| 1776 | unconverted = restlen; |
| 1777 | mch_memmove(inbuf + inbufcount, rest, unconverted); |
| 1778 | if (unconverted == restlen) |
| 1779 | { |
| 1780 | vim_free(rest); |
| 1781 | rest = NULL; |
| 1782 | } |
| 1783 | else |
| 1784 | { |
| 1785 | restlen -= unconverted; |
| 1786 | mch_memmove(rest, rest + unconverted, restlen); |
| 1787 | } |
| 1788 | inbufcount += unconverted; |
| 1789 | } |
| 1790 | else |
| 1791 | unconverted = 0; |
| 1792 | #endif |
| 1793 | |
| 1794 | len = 0; /* to avoid gcc warning */ |
| 1795 | for (try = 0; try < 100; ++try) |
| 1796 | { |
| 1797 | # ifdef VMS |
| 1798 | len = vms_read( |
| 1799 | # else |
| 1800 | len = read(read_cmd_fd, |
| 1801 | # endif |
| 1802 | (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount) |
| 1803 | # ifdef FEAT_MBYTE |
| 1804 | / input_conv.vc_factor |
| 1805 | # endif |
| 1806 | )); |
| 1807 | # if 0 |
| 1808 | ) /* avoid syntax highlight error */ |
| 1809 | # endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1810 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1811 | if (len > 0 || got_int) |
| 1812 | break; |
| 1813 | /* |
| 1814 | * If reading stdin results in an error, continue reading stderr. |
| 1815 | * This helps when using "foo | xargs vim". |
| 1816 | */ |
| 1817 | if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0) |
| 1818 | { |
| 1819 | int m = cur_tmode; |
| 1820 | |
| 1821 | /* We probably set the wrong file descriptor to raw mode. Switch |
| 1822 | * back to cooked mode, use another descriptor and set the mode to |
| 1823 | * what it was. */ |
| 1824 | settmode(TMODE_COOK); |
| 1825 | #ifdef HAVE_DUP |
| 1826 | /* Use stderr for stdin, also works for shell commands. */ |
| 1827 | close(0); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 1828 | ignored = dup(2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1829 | #else |
| 1830 | read_cmd_fd = 2; /* read from stderr instead of stdin */ |
| 1831 | #endif |
| 1832 | settmode(m); |
| 1833 | } |
| 1834 | if (!exit_on_error) |
| 1835 | return; |
| 1836 | } |
| 1837 | # endif |
| 1838 | if (len <= 0 && !got_int) |
| 1839 | read_error_exit(); |
| 1840 | if (len > 0) |
| 1841 | did_read_something = TRUE; |
| 1842 | if (got_int) |
| 1843 | { |
| 1844 | /* Interrupted, pretend a CTRL-C was typed. */ |
| 1845 | inbuf[0] = 3; |
| 1846 | inbufcount = 1; |
| 1847 | } |
| 1848 | else |
| 1849 | { |
| 1850 | # ifdef FEAT_MBYTE |
| 1851 | /* |
| 1852 | * May perform conversion on the input characters. |
| 1853 | * Include the unconverted rest of the previous call. |
| 1854 | * If there is an incomplete char at the end it is kept for the next |
| 1855 | * time, reading more bytes should make conversion possible. |
| 1856 | * Don't do this in the unlikely event that the input buffer is too |
| 1857 | * small ("rest" still contains more bytes). |
| 1858 | */ |
| 1859 | if (input_conv.vc_type != CONV_NONE) |
| 1860 | { |
| 1861 | inbufcount -= unconverted; |
| 1862 | len = convert_input_safe(inbuf + inbufcount, |
| 1863 | len + unconverted, INBUFLEN - inbufcount, |
| 1864 | rest == NULL ? &rest : NULL, &restlen); |
| 1865 | } |
| 1866 | # endif |
| 1867 | while (len-- > 0) |
| 1868 | { |
| 1869 | /* |
| 1870 | * if a CTRL-C was typed, remove it from the buffer and set got_int |
| 1871 | */ |
| 1872 | if (inbuf[inbufcount] == 3 && ctrl_c_interrupts) |
| 1873 | { |
| 1874 | /* remove everything typed before the CTRL-C */ |
| 1875 | mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1)); |
| 1876 | inbufcount = 0; |
| 1877 | got_int = TRUE; |
| 1878 | } |
| 1879 | ++inbufcount; |
| 1880 | } |
| 1881 | } |
| 1882 | #endif /* UNIX or OS2 or VMS*/ |
| 1883 | } |
| 1884 | #endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */ |
| 1885 | |
| 1886 | /* |
| 1887 | * Exit because of an input read error. |
| 1888 | */ |
| 1889 | void |
| 1890 | read_error_exit() |
| 1891 | { |
| 1892 | if (silent_mode) /* Normal way to exit for "ex -s" */ |
| 1893 | getout(0); |
| 1894 | STRCPY(IObuff, _("Vim: Error reading input, exiting...\n")); |
| 1895 | preserve_exit(); |
| 1896 | } |
| 1897 | |
| 1898 | #if defined(CURSOR_SHAPE) || defined(PROTO) |
| 1899 | /* |
| 1900 | * May update the shape of the cursor. |
| 1901 | */ |
| 1902 | void |
| 1903 | ui_cursor_shape() |
| 1904 | { |
| 1905 | # ifdef FEAT_GUI |
| 1906 | if (gui.in_use) |
| 1907 | gui_update_cursor_later(); |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1908 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1909 | # endif |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1910 | term_cursor_shape(); |
| 1911 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1912 | # ifdef MCH_CURSOR_SHAPE |
| 1913 | mch_update_cursor(); |
| 1914 | # endif |
| 1915 | } |
| 1916 | #endif |
| 1917 | |
| 1918 | #if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \ |
Bram Moolenaar | af51e66 | 2008-07-14 19:48:05 +0000 | [diff] [blame] | 1919 | || defined(FEAT_MBYTE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1920 | /* |
| 1921 | * Check bounds for column number |
| 1922 | */ |
| 1923 | int |
| 1924 | check_col(col) |
| 1925 | int col; |
| 1926 | { |
| 1927 | if (col < 0) |
| 1928 | return 0; |
| 1929 | if (col >= (int)screen_Columns) |
| 1930 | return (int)screen_Columns - 1; |
| 1931 | return col; |
| 1932 | } |
| 1933 | |
| 1934 | /* |
| 1935 | * Check bounds for row number |
| 1936 | */ |
| 1937 | int |
| 1938 | check_row(row) |
| 1939 | int row; |
| 1940 | { |
| 1941 | if (row < 0) |
| 1942 | return 0; |
| 1943 | if (row >= (int)screen_Rows) |
| 1944 | return (int)screen_Rows - 1; |
| 1945 | return row; |
| 1946 | } |
| 1947 | #endif |
| 1948 | |
| 1949 | /* |
| 1950 | * Stuff for the X clipboard. Shared between VMS and Unix. |
| 1951 | */ |
| 1952 | |
| 1953 | #if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO) |
| 1954 | # include <X11/Xatom.h> |
| 1955 | # include <X11/Intrinsic.h> |
| 1956 | |
| 1957 | /* |
| 1958 | * Open the application context (if it hasn't been opened yet). |
| 1959 | * Used for Motif and Athena GUI and the xterm clipboard. |
| 1960 | */ |
| 1961 | void |
| 1962 | open_app_context() |
| 1963 | { |
| 1964 | if (app_context == NULL) |
| 1965 | { |
| 1966 | XtToolkitInitialize(); |
| 1967 | app_context = XtCreateApplicationContext(); |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | static Atom vim_atom; /* Vim's own special selection format */ |
| 1972 | #ifdef FEAT_MBYTE |
| 1973 | static Atom vimenc_atom; /* Vim's extended selection format */ |
| 1974 | #endif |
| 1975 | static Atom compound_text_atom; |
| 1976 | static Atom text_atom; |
| 1977 | static Atom targets_atom; |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 1978 | static Atom timestamp_atom; /* Used to get a timestamp */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1979 | |
| 1980 | void |
| 1981 | x11_setup_atoms(dpy) |
| 1982 | Display *dpy; |
| 1983 | { |
| 1984 | vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False); |
| 1985 | #ifdef FEAT_MBYTE |
| 1986 | vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False); |
| 1987 | #endif |
| 1988 | compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False); |
| 1989 | text_atom = XInternAtom(dpy, "TEXT", False); |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 1990 | targets_atom = XInternAtom(dpy, "TARGETS", False); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1991 | clip_star.sel_atom = XA_PRIMARY; |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 1992 | clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False); |
| 1993 | timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | /* |
| 1997 | * X Selection stuff, for cutting and pasting text to other windows. |
| 1998 | */ |
| 1999 | |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 2000 | static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *)); |
| 2001 | |
| 2002 | static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *)); |
| 2003 | |
| 2004 | static void clip_x11_timestamp_cb __ARGS((Widget w, XtPointer n, XEvent *event, Boolean *cont)); |
| 2005 | |
| 2006 | /* |
| 2007 | * Property callback to get a timestamp for XtOwnSelection. |
| 2008 | */ |
| 2009 | static void |
| 2010 | clip_x11_timestamp_cb(w, n, event, cont) |
| 2011 | Widget w; |
| 2012 | XtPointer n UNUSED; |
| 2013 | XEvent *event; |
| 2014 | Boolean *cont UNUSED; |
| 2015 | { |
| 2016 | Atom actual_type; |
| 2017 | int format; |
| 2018 | unsigned long nitems, bytes_after; |
| 2019 | unsigned char *prop=NULL; |
| 2020 | XPropertyEvent *xproperty=&event->xproperty; |
| 2021 | |
| 2022 | /* Must be a property notify, state can't be Delete (True), has to be |
| 2023 | * one of the supported selection types. */ |
| 2024 | if (event->type != PropertyNotify || xproperty->state |
| 2025 | || (xproperty->atom != clip_star.sel_atom |
| 2026 | && xproperty->atom != clip_plus.sel_atom)) |
| 2027 | return; |
| 2028 | |
| 2029 | if (XGetWindowProperty(xproperty->display, xproperty->window, |
| 2030 | xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format, |
| 2031 | &nitems, &bytes_after, &prop)) |
| 2032 | return; |
| 2033 | |
| 2034 | if (prop) |
| 2035 | XFree(prop); |
| 2036 | |
| 2037 | /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */ |
| 2038 | if (actual_type != timestamp_atom || format != 32) |
| 2039 | return; |
| 2040 | |
| 2041 | /* Get the selection, using the event timestamp. */ |
| 2042 | XtOwnSelection(w, xproperty->atom, xproperty->time, |
| 2043 | clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb, NULL); |
| 2044 | } |
| 2045 | |
| 2046 | void |
| 2047 | x11_setup_selection(w) |
| 2048 | Widget w; |
| 2049 | { |
| 2050 | XtAddEventHandler(w, PropertyChangeMask, False, |
| 2051 | /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL); |
| 2052 | } |
| 2053 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2054 | static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *)); |
| 2055 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2056 | static void |
| 2057 | clip_x11_request_selection_cb(w, success, sel_atom, type, value, length, |
| 2058 | format) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 2059 | Widget w UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2060 | XtPointer success; |
| 2061 | Atom *sel_atom; |
| 2062 | Atom *type; |
| 2063 | XtPointer value; |
| 2064 | long_u *length; |
| 2065 | int *format; |
| 2066 | { |
| 2067 | int motion_type; |
| 2068 | long_u len; |
| 2069 | char_u *p; |
| 2070 | char **text_list = NULL; |
| 2071 | VimClipboard *cbd; |
| 2072 | #ifdef FEAT_MBYTE |
| 2073 | char_u *tmpbuf = NULL; |
| 2074 | #endif |
| 2075 | |
| 2076 | if (*sel_atom == clip_plus.sel_atom) |
| 2077 | cbd = &clip_plus; |
| 2078 | else |
| 2079 | cbd = &clip_star; |
| 2080 | |
| 2081 | if (value == NULL || *length == 0) |
| 2082 | { |
Bram Moolenaar | 24d92ce | 2008-09-14 13:58:34 +0000 | [diff] [blame] | 2083 | clip_free_selection(cbd); /* nothing received, clear register */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2084 | *(int *)success = FALSE; |
| 2085 | return; |
| 2086 | } |
| 2087 | motion_type = MCHAR; |
| 2088 | p = (char_u *)value; |
| 2089 | len = *length; |
| 2090 | if (*type == vim_atom) |
| 2091 | { |
| 2092 | motion_type = *p++; |
| 2093 | len--; |
| 2094 | } |
| 2095 | |
| 2096 | #ifdef FEAT_MBYTE |
| 2097 | else if (*type == vimenc_atom) |
| 2098 | { |
| 2099 | char_u *enc; |
| 2100 | vimconv_T conv; |
| 2101 | int convlen; |
| 2102 | |
| 2103 | motion_type = *p++; |
| 2104 | --len; |
| 2105 | |
| 2106 | enc = p; |
| 2107 | p += STRLEN(p) + 1; |
| 2108 | len -= p - enc; |
| 2109 | |
| 2110 | /* If the encoding of the text is different from 'encoding', attempt |
| 2111 | * converting it. */ |
| 2112 | conv.vc_type = CONV_NONE; |
| 2113 | convert_setup(&conv, enc, p_enc); |
| 2114 | if (conv.vc_type != CONV_NONE) |
| 2115 | { |
| 2116 | convlen = len; /* Need to use an int here. */ |
| 2117 | tmpbuf = string_convert(&conv, p, &convlen); |
| 2118 | len = convlen; |
| 2119 | if (tmpbuf != NULL) |
| 2120 | p = tmpbuf; |
| 2121 | convert_setup(&conv, NULL, NULL); |
| 2122 | } |
| 2123 | } |
| 2124 | #endif |
| 2125 | |
| 2126 | else if (*type == compound_text_atom || ( |
| 2127 | #ifdef FEAT_MBYTE |
| 2128 | enc_dbcs != 0 && |
| 2129 | #endif |
| 2130 | *type == text_atom)) |
| 2131 | { |
| 2132 | XTextProperty text_prop; |
| 2133 | int n_text = 0; |
| 2134 | int status; |
| 2135 | |
| 2136 | text_prop.value = (unsigned char *)value; |
| 2137 | text_prop.encoding = *type; |
| 2138 | text_prop.format = *format; |
Bram Moolenaar | 24d92ce | 2008-09-14 13:58:34 +0000 | [diff] [blame] | 2139 | text_prop.nitems = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2140 | status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop, |
| 2141 | &text_list, &n_text); |
| 2142 | if (status != Success || n_text < 1) |
| 2143 | { |
| 2144 | *(int *)success = FALSE; |
| 2145 | return; |
| 2146 | } |
| 2147 | p = (char_u *)text_list[0]; |
| 2148 | len = STRLEN(p); |
| 2149 | } |
| 2150 | clip_yank_selection(motion_type, p, (long)len, cbd); |
| 2151 | |
| 2152 | if (text_list != NULL) |
| 2153 | XFreeStringList(text_list); |
| 2154 | #ifdef FEAT_MBYTE |
| 2155 | vim_free(tmpbuf); |
| 2156 | #endif |
| 2157 | XtFree((char *)value); |
| 2158 | *(int *)success = TRUE; |
| 2159 | } |
| 2160 | |
| 2161 | void |
| 2162 | clip_x11_request_selection(myShell, dpy, cbd) |
| 2163 | Widget myShell; |
| 2164 | Display *dpy; |
| 2165 | VimClipboard *cbd; |
| 2166 | { |
| 2167 | XEvent event; |
| 2168 | Atom type; |
| 2169 | static int success; |
| 2170 | int i; |
Bram Moolenaar | 89417b9 | 2008-09-07 19:48:53 +0000 | [diff] [blame] | 2171 | time_t start_time; |
| 2172 | int timed_out = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2173 | |
| 2174 | for (i = |
| 2175 | #ifdef FEAT_MBYTE |
| 2176 | 0 |
| 2177 | #else |
| 2178 | 1 |
| 2179 | #endif |
| 2180 | ; i < 5; i++) |
| 2181 | { |
| 2182 | switch (i) |
| 2183 | { |
| 2184 | #ifdef FEAT_MBYTE |
| 2185 | case 0: type = vimenc_atom; break; |
| 2186 | #endif |
| 2187 | case 1: type = vim_atom; break; |
| 2188 | case 2: type = compound_text_atom; break; |
| 2189 | case 3: type = text_atom; break; |
| 2190 | default: type = XA_STRING; |
| 2191 | } |
Bram Moolenaar | 24d92ce | 2008-09-14 13:58:34 +0000 | [diff] [blame] | 2192 | success = MAYBE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2193 | XtGetSelectionValue(myShell, cbd->sel_atom, type, |
| 2194 | clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime); |
| 2195 | |
| 2196 | /* Make sure the request for the selection goes out before waiting for |
| 2197 | * a response. */ |
| 2198 | XFlush(dpy); |
| 2199 | |
| 2200 | /* |
| 2201 | * Wait for result of selection request, otherwise if we type more |
| 2202 | * characters, then they will appear before the one that requested the |
| 2203 | * paste! Don't worry, we will catch up with any other events later. |
| 2204 | */ |
Bram Moolenaar | 89417b9 | 2008-09-07 19:48:53 +0000 | [diff] [blame] | 2205 | start_time = time(NULL); |
Bram Moolenaar | 24d92ce | 2008-09-14 13:58:34 +0000 | [diff] [blame] | 2206 | while (success == MAYBE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2207 | { |
Bram Moolenaar | 24d92ce | 2008-09-14 13:58:34 +0000 | [diff] [blame] | 2208 | if (XCheckTypedEvent(dpy, SelectionNotify, &event) |
| 2209 | || XCheckTypedEvent(dpy, SelectionRequest, &event) |
| 2210 | || XCheckTypedEvent(dpy, PropertyNotify, &event)) |
Bram Moolenaar | 89417b9 | 2008-09-07 19:48:53 +0000 | [diff] [blame] | 2211 | { |
Bram Moolenaar | 24d92ce | 2008-09-14 13:58:34 +0000 | [diff] [blame] | 2212 | /* This is where clip_x11_request_selection_cb() should be |
| 2213 | * called. It may actually happen a bit later, so we loop |
| 2214 | * until "success" changes. |
| 2215 | * We may get a SelectionRequest here and if we don't handle |
| 2216 | * it we hang. KDE klipper does this, for example. |
| 2217 | * We need to handle a PropertyNotify for large selections. */ |
Bram Moolenaar | 89417b9 | 2008-09-07 19:48:53 +0000 | [diff] [blame] | 2218 | XtDispatchEvent(&event); |
Bram Moolenaar | 24d92ce | 2008-09-14 13:58:34 +0000 | [diff] [blame] | 2219 | continue; |
Bram Moolenaar | 89417b9 | 2008-09-07 19:48:53 +0000 | [diff] [blame] | 2220 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2221 | |
Bram Moolenaar | 89417b9 | 2008-09-07 19:48:53 +0000 | [diff] [blame] | 2222 | /* Time out after 2 to 3 seconds to avoid that we hang when the |
| 2223 | * other process doesn't respond. Note that the SelectionNotify |
| 2224 | * event may still come later when the selection owner comes back |
Bram Moolenaar | 24d92ce | 2008-09-14 13:58:34 +0000 | [diff] [blame] | 2225 | * to life and the text gets inserted unexpectedly. Don't know |
| 2226 | * why that happens or how to avoid that :-(. */ |
Bram Moolenaar | 89417b9 | 2008-09-07 19:48:53 +0000 | [diff] [blame] | 2227 | if (time(NULL) > start_time + 2) |
| 2228 | { |
| 2229 | timed_out = TRUE; |
| 2230 | break; |
| 2231 | } |
| 2232 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2233 | /* Do we need this? Probably not. */ |
| 2234 | XSync(dpy, False); |
| 2235 | |
Bram Moolenaar | 89417b9 | 2008-09-07 19:48:53 +0000 | [diff] [blame] | 2236 | /* Wait for 1 msec to avoid that we eat up all CPU time. */ |
| 2237 | ui_delay(1L, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Bram Moolenaar | 24d92ce | 2008-09-14 13:58:34 +0000 | [diff] [blame] | 2240 | if (success == TRUE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2241 | return; |
Bram Moolenaar | 89417b9 | 2008-09-07 19:48:53 +0000 | [diff] [blame] | 2242 | |
| 2243 | /* don't do a retry with another type after timing out, otherwise we |
| 2244 | * hang for 15 seconds. */ |
| 2245 | if (timed_out) |
| 2246 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | /* Final fallback position - use the X CUT_BUFFER0 store */ |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 2250 | yank_cut_buffer0(dpy, cbd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2253 | static Boolean |
| 2254 | clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 2255 | Widget w UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2256 | Atom *sel_atom; |
| 2257 | Atom *target; |
| 2258 | Atom *type; |
| 2259 | XtPointer *value; |
| 2260 | long_u *length; |
| 2261 | int *format; |
| 2262 | { |
| 2263 | char_u *string; |
| 2264 | char_u *result; |
| 2265 | int motion_type; |
| 2266 | VimClipboard *cbd; |
| 2267 | int i; |
| 2268 | |
| 2269 | if (*sel_atom == clip_plus.sel_atom) |
| 2270 | cbd = &clip_plus; |
| 2271 | else |
| 2272 | cbd = &clip_star; |
| 2273 | |
| 2274 | if (!cbd->owned) |
| 2275 | return False; /* Shouldn't ever happen */ |
| 2276 | |
| 2277 | /* requestor wants to know what target types we support */ |
| 2278 | if (*target == targets_atom) |
| 2279 | { |
| 2280 | Atom *array; |
| 2281 | |
| 2282 | if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 6))) == NULL) |
| 2283 | return False; |
| 2284 | *value = (XtPointer)array; |
| 2285 | i = 0; |
| 2286 | array[i++] = XA_STRING; |
| 2287 | array[i++] = targets_atom; |
| 2288 | #ifdef FEAT_MBYTE |
| 2289 | array[i++] = vimenc_atom; |
| 2290 | #endif |
| 2291 | array[i++] = vim_atom; |
| 2292 | array[i++] = text_atom; |
| 2293 | array[i++] = compound_text_atom; |
| 2294 | *type = XA_ATOM; |
| 2295 | /* This used to be: *format = sizeof(Atom) * 8; but that caused |
| 2296 | * crashes on 64 bit machines. (Peter Derr) */ |
| 2297 | *format = 32; |
| 2298 | *length = i; |
| 2299 | return True; |
| 2300 | } |
| 2301 | |
| 2302 | if ( *target != XA_STRING |
| 2303 | #ifdef FEAT_MBYTE |
| 2304 | && *target != vimenc_atom |
| 2305 | #endif |
| 2306 | && *target != vim_atom |
| 2307 | && *target != text_atom |
| 2308 | && *target != compound_text_atom) |
| 2309 | return False; |
| 2310 | |
| 2311 | clip_get_selection(cbd); |
| 2312 | motion_type = clip_convert_selection(&string, length, cbd); |
| 2313 | if (motion_type < 0) |
| 2314 | return False; |
| 2315 | |
| 2316 | /* For our own format, the first byte contains the motion type */ |
| 2317 | if (*target == vim_atom) |
| 2318 | (*length)++; |
| 2319 | |
| 2320 | #ifdef FEAT_MBYTE |
| 2321 | /* Our own format with encoding: motion 'encoding' NUL text */ |
| 2322 | if (*target == vimenc_atom) |
| 2323 | *length += STRLEN(p_enc) + 2; |
| 2324 | #endif |
| 2325 | |
| 2326 | *value = XtMalloc((Cardinal)*length); |
| 2327 | result = (char_u *)*value; |
| 2328 | if (result == NULL) |
| 2329 | { |
| 2330 | vim_free(string); |
| 2331 | return False; |
| 2332 | } |
| 2333 | |
| 2334 | if (*target == XA_STRING) |
| 2335 | { |
| 2336 | mch_memmove(result, string, (size_t)(*length)); |
| 2337 | *type = XA_STRING; |
| 2338 | } |
| 2339 | else if (*target == compound_text_atom |
| 2340 | || *target == text_atom) |
| 2341 | { |
| 2342 | XTextProperty text_prop; |
| 2343 | char *string_nt = (char *)alloc((unsigned)*length + 1); |
| 2344 | |
| 2345 | /* create NUL terminated string which XmbTextListToTextProperty wants */ |
| 2346 | mch_memmove(string_nt, string, (size_t)*length); |
| 2347 | string_nt[*length] = NUL; |
| 2348 | XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt, 1, |
| 2349 | XCompoundTextStyle, &text_prop); |
| 2350 | vim_free(string_nt); |
| 2351 | XtFree(*value); /* replace with COMPOUND text */ |
| 2352 | *value = (XtPointer)(text_prop.value); /* from plain text */ |
| 2353 | *length = text_prop.nitems; |
| 2354 | *type = compound_text_atom; |
| 2355 | } |
| 2356 | |
| 2357 | #ifdef FEAT_MBYTE |
| 2358 | else if (*target == vimenc_atom) |
| 2359 | { |
| 2360 | int l = STRLEN(p_enc); |
| 2361 | |
| 2362 | result[0] = motion_type; |
| 2363 | STRCPY(result + 1, p_enc); |
| 2364 | mch_memmove(result + l + 2, string, (size_t)(*length - l - 2)); |
| 2365 | *type = vimenc_atom; |
| 2366 | } |
| 2367 | #endif |
| 2368 | |
| 2369 | else |
| 2370 | { |
| 2371 | result[0] = motion_type; |
| 2372 | mch_memmove(result + 1, string, (size_t)(*length - 1)); |
| 2373 | *type = vim_atom; |
| 2374 | } |
| 2375 | *format = 8; /* 8 bits per char */ |
| 2376 | vim_free(string); |
| 2377 | return True; |
| 2378 | } |
| 2379 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2380 | static void |
| 2381 | clip_x11_lose_ownership_cb(w, sel_atom) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 2382 | Widget w UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2383 | Atom *sel_atom; |
| 2384 | { |
| 2385 | if (*sel_atom == clip_plus.sel_atom) |
| 2386 | clip_lose_selection(&clip_plus); |
| 2387 | else |
| 2388 | clip_lose_selection(&clip_star); |
| 2389 | } |
| 2390 | |
| 2391 | void |
| 2392 | clip_x11_lose_selection(myShell, cbd) |
| 2393 | Widget myShell; |
| 2394 | VimClipboard *cbd; |
| 2395 | { |
| 2396 | XtDisownSelection(myShell, cbd->sel_atom, CurrentTime); |
| 2397 | } |
| 2398 | |
| 2399 | int |
| 2400 | clip_x11_own_selection(myShell, cbd) |
| 2401 | Widget myShell; |
| 2402 | VimClipboard *cbd; |
| 2403 | { |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 2404 | /* Get the time by a zero-length append, clip_x11_timestamp_cb will be |
| 2405 | * called with the current timestamp. */ |
| 2406 | if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell), cbd->sel_atom, |
| 2407 | timestamp_atom, 32, PropModeAppend, NULL, 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2408 | return FAIL; |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 2409 | /* Flush is required in a terminal as nothing else is doing it. */ |
| 2410 | XFlush(XtDisplay(myShell)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2411 | return OK; |
| 2412 | } |
| 2413 | |
| 2414 | /* |
| 2415 | * Send the current selection to the clipboard. Do nothing for X because we |
| 2416 | * will fill in the selection only when requested by another app. |
| 2417 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2418 | void |
| 2419 | clip_x11_set_selection(cbd) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 2420 | VimClipboard *cbd UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2421 | { |
| 2422 | } |
| 2423 | #endif |
| 2424 | |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 2425 | #if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \ |
| 2426 | || defined(FEAT_GUI_GTK) || defined(PROTO) |
| 2427 | /* |
| 2428 | * Get the contents of the X CUT_BUFFER0 and put it in "cbd". |
| 2429 | */ |
| 2430 | void |
| 2431 | yank_cut_buffer0(dpy, cbd) |
| 2432 | Display *dpy; |
| 2433 | VimClipboard *cbd; |
| 2434 | { |
| 2435 | int nbytes = 0; |
| 2436 | char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0); |
| 2437 | |
| 2438 | if (nbytes > 0) |
| 2439 | { |
| 2440 | #ifdef FEAT_MBYTE |
| 2441 | int done = FALSE; |
| 2442 | |
| 2443 | /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when |
| 2444 | * using a multi-byte encoding. Conversion between two 8-bit |
| 2445 | * character sets usually fails and the text might actually be in |
| 2446 | * 'enc' anyway. */ |
| 2447 | if (has_mbyte) |
| 2448 | { |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 2449 | char_u *conv_buf; |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 2450 | vimconv_T vc; |
| 2451 | |
| 2452 | vc.vc_type = CONV_NONE; |
| 2453 | if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK) |
| 2454 | { |
| 2455 | conv_buf = string_convert(&vc, buffer, &nbytes); |
| 2456 | if (conv_buf != NULL) |
| 2457 | { |
| 2458 | clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd); |
| 2459 | vim_free(conv_buf); |
| 2460 | done = TRUE; |
| 2461 | } |
| 2462 | convert_setup(&vc, NULL, NULL); |
| 2463 | } |
| 2464 | } |
| 2465 | if (!done) /* use the text without conversion */ |
| 2466 | #endif |
| 2467 | clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd); |
| 2468 | XFree((void *)buffer); |
| 2469 | if (p_verbose > 0) |
| 2470 | { |
| 2471 | verbose_enter(); |
| 2472 | verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection")); |
| 2473 | verbose_leave(); |
| 2474 | } |
| 2475 | } |
| 2476 | } |
| 2477 | #endif |
| 2478 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2479 | #if defined(FEAT_MOUSE) || defined(PROTO) |
| 2480 | |
| 2481 | /* |
| 2482 | * Move the cursor to the specified row and column on the screen. |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 2483 | * Change current window if necessary. Returns an integer with the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2484 | * CURSOR_MOVED bit set if the cursor has moved or unset otherwise. |
| 2485 | * |
| 2486 | * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column. |
| 2487 | * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column. |
| 2488 | * |
| 2489 | * If flags has MOUSE_FOCUS, then the current window will not be changed, and |
| 2490 | * if the mouse is outside the window then the text will scroll, or if the |
| 2491 | * mouse was previously on a status line, then the status line may be dragged. |
| 2492 | * |
| 2493 | * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the |
| 2494 | * cursor is moved unless the cursor was on a status line. |
| 2495 | * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or |
| 2496 | * IN_SEP_LINE depending on where the cursor was clicked. |
| 2497 | * |
| 2498 | * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless |
| 2499 | * the mouse is on the status line of the same window. |
| 2500 | * |
| 2501 | * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since |
| 2502 | * the last call. |
| 2503 | * |
| 2504 | * If flags has MOUSE_SETPOS, nothing is done, only the current position is |
| 2505 | * remembered. |
| 2506 | */ |
| 2507 | int |
| 2508 | jump_to_mouse(flags, inclusive, which_button) |
| 2509 | int flags; |
| 2510 | int *inclusive; /* used for inclusive operator, can be NULL */ |
| 2511 | int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */ |
| 2512 | { |
| 2513 | static int on_status_line = 0; /* #lines below bottom of window */ |
| 2514 | #ifdef FEAT_VERTSPLIT |
| 2515 | static int on_sep_line = 0; /* on separator right of window */ |
| 2516 | #endif |
| 2517 | static int prev_row = -1; |
| 2518 | static int prev_col = -1; |
| 2519 | static win_T *dragwin = NULL; /* window being dragged */ |
| 2520 | static int did_drag = FALSE; /* drag was noticed */ |
| 2521 | |
| 2522 | win_T *wp, *old_curwin; |
| 2523 | pos_T old_cursor; |
| 2524 | int count; |
| 2525 | int first; |
| 2526 | int row = mouse_row; |
| 2527 | int col = mouse_col; |
| 2528 | #ifdef FEAT_FOLDING |
| 2529 | int mouse_char; |
| 2530 | #endif |
| 2531 | |
| 2532 | mouse_past_bottom = FALSE; |
| 2533 | mouse_past_eol = FALSE; |
| 2534 | |
| 2535 | if (flags & MOUSE_RELEASED) |
| 2536 | { |
| 2537 | /* On button release we may change window focus if positioned on a |
| 2538 | * status line and no dragging happened. */ |
| 2539 | if (dragwin != NULL && !did_drag) |
| 2540 | flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE); |
| 2541 | dragwin = NULL; |
| 2542 | did_drag = FALSE; |
| 2543 | } |
| 2544 | |
| 2545 | if ((flags & MOUSE_DID_MOVE) |
| 2546 | && prev_row == mouse_row |
| 2547 | && prev_col == mouse_col) |
| 2548 | { |
| 2549 | retnomove: |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 2550 | /* before moving the cursor for a left click which is NOT in a status |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2551 | * line, stop Visual mode */ |
| 2552 | if (on_status_line) |
| 2553 | return IN_STATUS_LINE; |
| 2554 | #ifdef FEAT_VERTSPLIT |
| 2555 | if (on_sep_line) |
| 2556 | return IN_SEP_LINE; |
| 2557 | #endif |
| 2558 | #ifdef FEAT_VISUAL |
| 2559 | if (flags & MOUSE_MAY_STOP_VIS) |
| 2560 | { |
| 2561 | end_visual_mode(); |
| 2562 | redraw_curbuf_later(INVERTED); /* delete the inversion */ |
| 2563 | } |
| 2564 | #endif |
| 2565 | #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD) |
| 2566 | /* Continue a modeless selection in another window. */ |
| 2567 | if (cmdwin_type != 0 && row < W_WINROW(curwin)) |
| 2568 | return IN_OTHER_WIN; |
| 2569 | #endif |
| 2570 | return IN_BUFFER; |
| 2571 | } |
| 2572 | |
| 2573 | prev_row = mouse_row; |
| 2574 | prev_col = mouse_col; |
| 2575 | |
| 2576 | if (flags & MOUSE_SETPOS) |
| 2577 | goto retnomove; /* ugly goto... */ |
| 2578 | |
| 2579 | #ifdef FEAT_FOLDING |
| 2580 | /* Remember the character under the mouse, it might be a '-' or '+' in the |
| 2581 | * fold column. */ |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2582 | if (row >= 0 && row < Rows && col >= 0 && col <= Columns |
| 2583 | && ScreenLines != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2584 | mouse_char = ScreenLines[LineOffset[row] + col]; |
| 2585 | else |
| 2586 | mouse_char = ' '; |
| 2587 | #endif |
| 2588 | |
| 2589 | old_curwin = curwin; |
| 2590 | old_cursor = curwin->w_cursor; |
| 2591 | |
| 2592 | if (!(flags & MOUSE_FOCUS)) |
| 2593 | { |
| 2594 | if (row < 0 || col < 0) /* check if it makes sense */ |
| 2595 | return IN_UNKNOWN; |
| 2596 | |
| 2597 | #ifdef FEAT_WINDOWS |
| 2598 | /* find the window where the row is in */ |
| 2599 | wp = mouse_find_win(&row, &col); |
| 2600 | #else |
| 2601 | wp = firstwin; |
| 2602 | #endif |
| 2603 | dragwin = NULL; |
| 2604 | /* |
| 2605 | * winpos and height may change in win_enter()! |
| 2606 | */ |
| 2607 | if (row >= wp->w_height) /* In (or below) status line */ |
| 2608 | { |
| 2609 | on_status_line = row - wp->w_height + 1; |
| 2610 | dragwin = wp; |
| 2611 | } |
| 2612 | else |
| 2613 | on_status_line = 0; |
| 2614 | #ifdef FEAT_VERTSPLIT |
| 2615 | if (col >= wp->w_width) /* In separator line */ |
| 2616 | { |
| 2617 | on_sep_line = col - wp->w_width + 1; |
| 2618 | dragwin = wp; |
| 2619 | } |
| 2620 | else |
| 2621 | on_sep_line = 0; |
| 2622 | |
| 2623 | /* The rightmost character of the status line might be a vertical |
| 2624 | * separator character if there is no connecting window to the right. */ |
| 2625 | if (on_status_line && on_sep_line) |
| 2626 | { |
| 2627 | if (stl_connected(wp)) |
| 2628 | on_sep_line = 0; |
| 2629 | else |
| 2630 | on_status_line = 0; |
| 2631 | } |
| 2632 | #endif |
| 2633 | |
| 2634 | #ifdef FEAT_VISUAL |
| 2635 | /* Before jumping to another buffer, or moving the cursor for a left |
| 2636 | * click, stop Visual mode. */ |
| 2637 | if (VIsual_active |
| 2638 | && (wp->w_buffer != curwin->w_buffer |
| 2639 | || (!on_status_line |
| 2640 | # ifdef FEAT_VERTSPLIT |
| 2641 | && !on_sep_line |
| 2642 | # endif |
| 2643 | # ifdef FEAT_FOLDING |
| 2644 | && ( |
| 2645 | # ifdef FEAT_RIGHTLEFT |
| 2646 | wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc : |
| 2647 | # endif |
| 2648 | col >= wp->w_p_fdc |
| 2649 | # ifdef FEAT_CMDWIN |
| 2650 | + (cmdwin_type == 0 && wp == curwin ? 0 : 1) |
| 2651 | # endif |
| 2652 | ) |
| 2653 | # endif |
| 2654 | && (flags & MOUSE_MAY_STOP_VIS)))) |
| 2655 | { |
| 2656 | end_visual_mode(); |
| 2657 | redraw_curbuf_later(INVERTED); /* delete the inversion */ |
| 2658 | } |
| 2659 | #endif |
| 2660 | #ifdef FEAT_CMDWIN |
| 2661 | if (cmdwin_type != 0 && wp != curwin) |
| 2662 | { |
| 2663 | /* A click outside the command-line window: Use modeless |
Bram Moolenaar | f679a43 | 2010-03-02 18:16:09 +0100 | [diff] [blame] | 2664 | * selection if possible. Allow dragging the status lines. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2665 | # ifdef FEAT_VERTSPLIT |
| 2666 | on_sep_line = 0; |
| 2667 | # endif |
| 2668 | # ifdef FEAT_CLIPBOARD |
| 2669 | if (on_status_line) |
| 2670 | return IN_STATUS_LINE; |
| 2671 | return IN_OTHER_WIN; |
| 2672 | # else |
| 2673 | row = 0; |
| 2674 | col += wp->w_wincol; |
| 2675 | wp = curwin; |
| 2676 | # endif |
| 2677 | } |
| 2678 | #endif |
| 2679 | #ifdef FEAT_WINDOWS |
| 2680 | /* Only change window focus when not clicking on or dragging the |
| 2681 | * status line. Do change focus when releasing the mouse button |
| 2682 | * (MOUSE_FOCUS was set above if we dragged first). */ |
| 2683 | if (dragwin == NULL || (flags & MOUSE_RELEASED)) |
| 2684 | win_enter(wp, TRUE); /* can make wp invalid! */ |
| 2685 | # ifdef CHECK_DOUBLE_CLICK |
| 2686 | /* set topline, to be able to check for double click ourselves */ |
| 2687 | if (curwin != old_curwin) |
| 2688 | set_mouse_topline(curwin); |
| 2689 | # endif |
| 2690 | #endif |
| 2691 | if (on_status_line) /* In (or below) status line */ |
| 2692 | { |
| 2693 | /* Don't use start_arrow() if we're in the same window */ |
| 2694 | if (curwin == old_curwin) |
| 2695 | return IN_STATUS_LINE; |
| 2696 | else |
| 2697 | return IN_STATUS_LINE | CURSOR_MOVED; |
| 2698 | } |
| 2699 | #ifdef FEAT_VERTSPLIT |
| 2700 | if (on_sep_line) /* In (or below) status line */ |
| 2701 | { |
| 2702 | /* Don't use start_arrow() if we're in the same window */ |
| 2703 | if (curwin == old_curwin) |
| 2704 | return IN_SEP_LINE; |
| 2705 | else |
| 2706 | return IN_SEP_LINE | CURSOR_MOVED; |
| 2707 | } |
| 2708 | #endif |
| 2709 | |
| 2710 | curwin->w_cursor.lnum = curwin->w_topline; |
| 2711 | #ifdef FEAT_GUI |
| 2712 | /* remember topline, needed for double click */ |
| 2713 | gui_prev_topline = curwin->w_topline; |
| 2714 | # ifdef FEAT_DIFF |
| 2715 | gui_prev_topfill = curwin->w_topfill; |
| 2716 | # endif |
| 2717 | #endif |
| 2718 | } |
| 2719 | else if (on_status_line && which_button == MOUSE_LEFT) |
| 2720 | { |
| 2721 | #ifdef FEAT_WINDOWS |
| 2722 | if (dragwin != NULL) |
| 2723 | { |
| 2724 | /* Drag the status line */ |
| 2725 | count = row - dragwin->w_winrow - dragwin->w_height + 1 |
| 2726 | - on_status_line; |
| 2727 | win_drag_status_line(dragwin, count); |
| 2728 | did_drag |= count; |
| 2729 | } |
| 2730 | #endif |
| 2731 | return IN_STATUS_LINE; /* Cursor didn't move */ |
| 2732 | } |
| 2733 | #ifdef FEAT_VERTSPLIT |
| 2734 | else if (on_sep_line && which_button == MOUSE_LEFT) |
| 2735 | { |
| 2736 | if (dragwin != NULL) |
| 2737 | { |
| 2738 | /* Drag the separator column */ |
| 2739 | count = col - dragwin->w_wincol - dragwin->w_width + 1 |
| 2740 | - on_sep_line; |
| 2741 | win_drag_vsep_line(dragwin, count); |
| 2742 | did_drag |= count; |
| 2743 | } |
| 2744 | return IN_SEP_LINE; /* Cursor didn't move */ |
| 2745 | } |
| 2746 | #endif |
| 2747 | else /* keep_window_focus must be TRUE */ |
| 2748 | { |
| 2749 | #ifdef FEAT_VISUAL |
| 2750 | /* before moving the cursor for a left click, stop Visual mode */ |
| 2751 | if (flags & MOUSE_MAY_STOP_VIS) |
| 2752 | { |
| 2753 | end_visual_mode(); |
| 2754 | redraw_curbuf_later(INVERTED); /* delete the inversion */ |
| 2755 | } |
| 2756 | #endif |
| 2757 | |
| 2758 | #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD) |
| 2759 | /* Continue a modeless selection in another window. */ |
| 2760 | if (cmdwin_type != 0 && row < W_WINROW(curwin)) |
| 2761 | return IN_OTHER_WIN; |
| 2762 | #endif |
| 2763 | |
| 2764 | row -= W_WINROW(curwin); |
| 2765 | #ifdef FEAT_VERTSPLIT |
| 2766 | col -= W_WINCOL(curwin); |
| 2767 | #endif |
| 2768 | |
| 2769 | /* |
| 2770 | * When clicking beyond the end of the window, scroll the screen. |
| 2771 | * Scroll by however many rows outside the window we are. |
| 2772 | */ |
| 2773 | if (row < 0) |
| 2774 | { |
| 2775 | count = 0; |
| 2776 | for (first = TRUE; curwin->w_topline > 1; ) |
| 2777 | { |
| 2778 | #ifdef FEAT_DIFF |
| 2779 | if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)) |
| 2780 | ++count; |
| 2781 | else |
| 2782 | #endif |
| 2783 | count += plines(curwin->w_topline - 1); |
| 2784 | if (!first && count > -row) |
| 2785 | break; |
| 2786 | first = FALSE; |
| 2787 | #ifdef FEAT_FOLDING |
| 2788 | hasFolding(curwin->w_topline, &curwin->w_topline, NULL); |
| 2789 | #endif |
| 2790 | #ifdef FEAT_DIFF |
| 2791 | if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)) |
| 2792 | ++curwin->w_topfill; |
| 2793 | else |
| 2794 | #endif |
| 2795 | { |
| 2796 | --curwin->w_topline; |
| 2797 | #ifdef FEAT_DIFF |
| 2798 | curwin->w_topfill = 0; |
| 2799 | #endif |
| 2800 | } |
| 2801 | } |
| 2802 | #ifdef FEAT_DIFF |
| 2803 | check_topfill(curwin, FALSE); |
| 2804 | #endif |
| 2805 | curwin->w_valid &= |
| 2806 | ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP); |
| 2807 | redraw_later(VALID); |
| 2808 | row = 0; |
| 2809 | } |
| 2810 | else if (row >= curwin->w_height) |
| 2811 | { |
| 2812 | count = 0; |
| 2813 | for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; ) |
| 2814 | { |
| 2815 | #ifdef FEAT_DIFF |
| 2816 | if (curwin->w_topfill > 0) |
| 2817 | ++count; |
| 2818 | else |
| 2819 | #endif |
| 2820 | count += plines(curwin->w_topline); |
| 2821 | if (!first && count > row - curwin->w_height + 1) |
| 2822 | break; |
| 2823 | first = FALSE; |
| 2824 | #ifdef FEAT_FOLDING |
| 2825 | if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline) |
| 2826 | && curwin->w_topline == curbuf->b_ml.ml_line_count) |
| 2827 | break; |
| 2828 | #endif |
| 2829 | #ifdef FEAT_DIFF |
| 2830 | if (curwin->w_topfill > 0) |
| 2831 | --curwin->w_topfill; |
| 2832 | else |
| 2833 | #endif |
| 2834 | { |
| 2835 | ++curwin->w_topline; |
| 2836 | #ifdef FEAT_DIFF |
| 2837 | curwin->w_topfill = |
| 2838 | diff_check_fill(curwin, curwin->w_topline); |
| 2839 | #endif |
| 2840 | } |
| 2841 | } |
| 2842 | #ifdef FEAT_DIFF |
| 2843 | check_topfill(curwin, FALSE); |
| 2844 | #endif |
| 2845 | redraw_later(VALID); |
| 2846 | curwin->w_valid &= |
| 2847 | ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP); |
| 2848 | row = curwin->w_height - 1; |
| 2849 | } |
| 2850 | else if (row == 0) |
| 2851 | { |
| 2852 | /* When dragging the mouse, while the text has been scrolled up as |
| 2853 | * far as it goes, moving the mouse in the top line should scroll |
| 2854 | * the text down (done later when recomputing w_topline). */ |
Bram Moolenaar | 8cfdc0d | 2007-05-06 14:12:36 +0000 | [diff] [blame] | 2855 | if (mouse_dragging > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2856 | && curwin->w_cursor.lnum |
| 2857 | == curwin->w_buffer->b_ml.ml_line_count |
| 2858 | && curwin->w_cursor.lnum == curwin->w_topline) |
| 2859 | curwin->w_valid &= ~(VALID_TOPLINE); |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | #ifdef FEAT_FOLDING |
| 2864 | /* Check for position outside of the fold column. */ |
| 2865 | if ( |
| 2866 | # ifdef FEAT_RIGHTLEFT |
| 2867 | curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc : |
| 2868 | # endif |
| 2869 | col >= curwin->w_p_fdc |
| 2870 | # ifdef FEAT_CMDWIN |
| 2871 | + (cmdwin_type == 0 ? 0 : 1) |
| 2872 | # endif |
| 2873 | ) |
| 2874 | mouse_char = ' '; |
| 2875 | #endif |
| 2876 | |
| 2877 | /* compute the position in the buffer line from the posn on the screen */ |
| 2878 | if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum)) |
| 2879 | mouse_past_bottom = TRUE; |
| 2880 | |
| 2881 | #ifdef FEAT_VISUAL |
| 2882 | /* Start Visual mode before coladvance(), for when 'sel' != "old" */ |
| 2883 | if ((flags & MOUSE_MAY_VIS) && !VIsual_active) |
| 2884 | { |
| 2885 | check_visual_highlight(); |
| 2886 | VIsual = old_cursor; |
| 2887 | VIsual_active = TRUE; |
| 2888 | VIsual_reselect = TRUE; |
| 2889 | /* if 'selectmode' contains "mouse", start Select mode */ |
| 2890 | may_start_select('o'); |
| 2891 | setmouse(); |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 2892 | if (p_smd && msg_silent == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2893 | redraw_cmdline = TRUE; /* show visual mode later */ |
| 2894 | } |
| 2895 | #endif |
| 2896 | |
| 2897 | curwin->w_curswant = col; |
| 2898 | curwin->w_set_curswant = FALSE; /* May still have been TRUE */ |
| 2899 | if (coladvance(col) == FAIL) /* Mouse click beyond end of line */ |
| 2900 | { |
| 2901 | if (inclusive != NULL) |
| 2902 | *inclusive = TRUE; |
| 2903 | mouse_past_eol = TRUE; |
| 2904 | } |
| 2905 | else if (inclusive != NULL) |
| 2906 | *inclusive = FALSE; |
| 2907 | |
| 2908 | count = IN_BUFFER; |
| 2909 | if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum |
| 2910 | || curwin->w_cursor.col != old_cursor.col) |
| 2911 | count |= CURSOR_MOVED; /* Cursor has moved */ |
| 2912 | |
| 2913 | #ifdef FEAT_FOLDING |
| 2914 | if (mouse_char == '+') |
| 2915 | count |= MOUSE_FOLD_OPEN; |
| 2916 | else if (mouse_char != ' ') |
| 2917 | count |= MOUSE_FOLD_CLOSE; |
| 2918 | #endif |
| 2919 | |
| 2920 | return count; |
| 2921 | } |
| 2922 | |
| 2923 | /* |
| 2924 | * Compute the position in the buffer line from the posn on the screen in |
| 2925 | * window "win". |
| 2926 | * Returns TRUE if the position is below the last line. |
| 2927 | */ |
| 2928 | int |
| 2929 | mouse_comp_pos(win, rowp, colp, lnump) |
| 2930 | win_T *win; |
| 2931 | int *rowp; |
| 2932 | int *colp; |
| 2933 | linenr_T *lnump; |
| 2934 | { |
| 2935 | int col = *colp; |
| 2936 | int row = *rowp; |
| 2937 | linenr_T lnum; |
| 2938 | int retval = FALSE; |
| 2939 | int off; |
| 2940 | int count; |
| 2941 | |
| 2942 | #ifdef FEAT_RIGHTLEFT |
| 2943 | if (win->w_p_rl) |
| 2944 | col = W_WIDTH(win) - 1 - col; |
| 2945 | #endif |
| 2946 | |
| 2947 | lnum = win->w_topline; |
| 2948 | |
| 2949 | while (row > 0) |
| 2950 | { |
| 2951 | #ifdef FEAT_DIFF |
| 2952 | /* Don't include filler lines in "count" */ |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 2953 | if (win->w_p_diff |
| 2954 | # ifdef FEAT_FOLDING |
| 2955 | && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL) |
| 2956 | # endif |
| 2957 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2958 | { |
| 2959 | if (lnum == win->w_topline) |
| 2960 | row -= win->w_topfill; |
| 2961 | else |
| 2962 | row -= diff_check_fill(win, lnum); |
| 2963 | count = plines_win_nofill(win, lnum, TRUE); |
| 2964 | } |
| 2965 | else |
| 2966 | #endif |
| 2967 | count = plines_win(win, lnum, TRUE); |
| 2968 | if (count > row) |
| 2969 | break; /* Position is in this buffer line. */ |
| 2970 | #ifdef FEAT_FOLDING |
| 2971 | (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL); |
| 2972 | #endif |
| 2973 | if (lnum == win->w_buffer->b_ml.ml_line_count) |
| 2974 | { |
| 2975 | retval = TRUE; |
| 2976 | break; /* past end of file */ |
| 2977 | } |
| 2978 | row -= count; |
| 2979 | ++lnum; |
| 2980 | } |
| 2981 | |
| 2982 | if (!retval) |
| 2983 | { |
| 2984 | /* Compute the column without wrapping. */ |
| 2985 | off = win_col_off(win) - win_col_off2(win); |
| 2986 | if (col < off) |
| 2987 | col = off; |
| 2988 | col += row * (W_WIDTH(win) - off); |
| 2989 | /* add skip column (for long wrapping line) */ |
| 2990 | col += win->w_skipcol; |
| 2991 | } |
| 2992 | |
| 2993 | if (!win->w_p_wrap) |
| 2994 | col += win->w_leftcol; |
| 2995 | |
| 2996 | /* skip line number and fold column in front of the line */ |
| 2997 | col -= win_col_off(win); |
| 2998 | if (col < 0) |
| 2999 | { |
| 3000 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 3001 | netbeans_gutter_click(lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3002 | #endif |
| 3003 | col = 0; |
| 3004 | } |
| 3005 | |
| 3006 | *colp = col; |
| 3007 | *rowp = row; |
| 3008 | *lnump = lnum; |
| 3009 | return retval; |
| 3010 | } |
| 3011 | |
| 3012 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
| 3013 | /* |
| 3014 | * Find the window at screen position "*rowp" and "*colp". The positions are |
| 3015 | * updated to become relative to the top-left of the window. |
| 3016 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3017 | win_T * |
| 3018 | mouse_find_win(rowp, colp) |
| 3019 | int *rowp; |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 3020 | int *colp UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3021 | { |
| 3022 | frame_T *fp; |
| 3023 | |
| 3024 | fp = topframe; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3025 | *rowp -= firstwin->w_winrow; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3026 | for (;;) |
| 3027 | { |
| 3028 | if (fp->fr_layout == FR_LEAF) |
| 3029 | break; |
| 3030 | #ifdef FEAT_VERTSPLIT |
| 3031 | if (fp->fr_layout == FR_ROW) |
| 3032 | { |
| 3033 | for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next) |
| 3034 | { |
| 3035 | if (*colp < fp->fr_width) |
| 3036 | break; |
| 3037 | *colp -= fp->fr_width; |
| 3038 | } |
| 3039 | } |
| 3040 | #endif |
| 3041 | else /* fr_layout == FR_COL */ |
| 3042 | { |
| 3043 | for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next) |
| 3044 | { |
| 3045 | if (*rowp < fp->fr_height) |
| 3046 | break; |
| 3047 | *rowp -= fp->fr_height; |
| 3048 | } |
| 3049 | } |
| 3050 | } |
| 3051 | return fp->fr_win; |
| 3052 | } |
| 3053 | #endif |
| 3054 | |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 3055 | #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3056 | || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \ |
| 3057 | || defined(FEAT_GUI_PHOTON) || defined(PROTO) |
| 3058 | /* |
| 3059 | * Translate window coordinates to buffer position without any side effects |
| 3060 | */ |
| 3061 | int |
| 3062 | get_fpos_of_mouse(mpos) |
| 3063 | pos_T *mpos; |
| 3064 | { |
| 3065 | win_T *wp; |
| 3066 | int row = mouse_row; |
| 3067 | int col = mouse_col; |
| 3068 | |
| 3069 | if (row < 0 || col < 0) /* check if it makes sense */ |
| 3070 | return IN_UNKNOWN; |
| 3071 | |
| 3072 | #ifdef FEAT_WINDOWS |
| 3073 | /* find the window where the row is in */ |
| 3074 | wp = mouse_find_win(&row, &col); |
| 3075 | #else |
| 3076 | wp = firstwin; |
| 3077 | #endif |
| 3078 | /* |
| 3079 | * winpos and height may change in win_enter()! |
| 3080 | */ |
| 3081 | if (row >= wp->w_height) /* In (or below) status line */ |
| 3082 | return IN_STATUS_LINE; |
| 3083 | #ifdef FEAT_VERTSPLIT |
| 3084 | if (col >= wp->w_width) /* In vertical separator line */ |
| 3085 | return IN_SEP_LINE; |
| 3086 | #endif |
| 3087 | |
| 3088 | if (wp != curwin) |
| 3089 | return IN_UNKNOWN; |
| 3090 | |
| 3091 | /* compute the position in the buffer line from the posn on the screen */ |
| 3092 | if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum)) |
| 3093 | return IN_STATUS_LINE; /* past bottom */ |
| 3094 | |
| 3095 | mpos->col = vcol2col(wp, mpos->lnum, col); |
| 3096 | |
| 3097 | if (mpos->col > 0) |
| 3098 | --mpos->col; |
| 3099 | return IN_BUFFER; |
| 3100 | } |
| 3101 | |
| 3102 | /* |
| 3103 | * Convert a virtual (screen) column to a character column. |
| 3104 | * The first column is one. |
| 3105 | */ |
| 3106 | int |
| 3107 | vcol2col(wp, lnum, vcol) |
| 3108 | win_T *wp; |
| 3109 | linenr_T lnum; |
| 3110 | int vcol; |
| 3111 | { |
| 3112 | /* try to advance to the specified column */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3113 | int count = 0; |
| 3114 | char_u *ptr; |
Bram Moolenaar | 86c800a | 2009-09-11 14:48:27 +0000 | [diff] [blame] | 3115 | char_u *start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3116 | |
Bram Moolenaar | 86c800a | 2009-09-11 14:48:27 +0000 | [diff] [blame] | 3117 | start = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3118 | while (count <= vcol && *ptr != NUL) |
| 3119 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3120 | count += win_lbr_chartabsize(wp, ptr, count, NULL); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3121 | mb_ptr_adv(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3122 | } |
Bram Moolenaar | 86c800a | 2009-09-11 14:48:27 +0000 | [diff] [blame] | 3123 | return (int)(ptr - start); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3124 | } |
| 3125 | #endif |
| 3126 | |
| 3127 | #endif /* FEAT_MOUSE */ |
| 3128 | |
| 3129 | #if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO) |
| 3130 | /* |
| 3131 | * Called when focus changed. Used for the GUI or for systems where this can |
| 3132 | * be done in the console (Win32). |
| 3133 | */ |
| 3134 | void |
| 3135 | ui_focus_change(in_focus) |
| 3136 | int in_focus; /* TRUE if focus gained. */ |
| 3137 | { |
| 3138 | static time_t last_time = (time_t)0; |
| 3139 | int need_redraw = FALSE; |
| 3140 | |
| 3141 | /* When activated: Check if any file was modified outside of Vim. |
| 3142 | * Only do this when not done within the last two seconds (could get |
| 3143 | * several events in a row). */ |
| 3144 | if (in_focus && last_time + 2 < time(NULL)) |
| 3145 | { |
| 3146 | need_redraw = check_timestamps( |
| 3147 | # ifdef FEAT_GUI |
| 3148 | gui.in_use |
| 3149 | # else |
| 3150 | FALSE |
| 3151 | # endif |
| 3152 | ); |
| 3153 | last_time = time(NULL); |
| 3154 | } |
| 3155 | |
| 3156 | #ifdef FEAT_AUTOCMD |
| 3157 | /* |
| 3158 | * Fire the focus gained/lost autocommand. |
| 3159 | */ |
| 3160 | need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED |
| 3161 | : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf); |
| 3162 | #endif |
| 3163 | |
| 3164 | if (need_redraw) |
| 3165 | { |
| 3166 | /* Something was executed, make sure the cursor is put back where it |
| 3167 | * belongs. */ |
| 3168 | need_wait_return = FALSE; |
| 3169 | |
| 3170 | if (State & CMDLINE) |
| 3171 | redrawcmdline(); |
| 3172 | else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE |
| 3173 | || State == EXTERNCMD || State == CONFIRM || exmode_active) |
| 3174 | repeat_message(); |
| 3175 | else if ((State & NORMAL) || (State & INSERT)) |
| 3176 | { |
| 3177 | if (must_redraw != 0) |
| 3178 | update_screen(0); |
| 3179 | setcursor(); |
| 3180 | } |
| 3181 | cursor_on(); /* redrawing may have switched it off */ |
| 3182 | out_flush(); |
| 3183 | # ifdef FEAT_GUI |
| 3184 | if (gui.in_use) |
| 3185 | { |
| 3186 | gui_update_cursor(FALSE, TRUE); |
| 3187 | gui_update_scrollbars(FALSE); |
| 3188 | } |
| 3189 | # endif |
| 3190 | } |
| 3191 | #ifdef FEAT_TITLE |
| 3192 | /* File may have been changed from 'readonly' to 'noreadonly' */ |
| 3193 | if (need_maketitle) |
| 3194 | maketitle(); |
| 3195 | #endif |
| 3196 | } |
| 3197 | #endif |
| 3198 | |
| 3199 | #if defined(USE_IM_CONTROL) || defined(PROTO) |
| 3200 | /* |
| 3201 | * Save current Input Method status to specified place. |
| 3202 | */ |
| 3203 | void |
| 3204 | im_save_status(psave) |
| 3205 | long *psave; |
| 3206 | { |
| 3207 | /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always |
| 3208 | * disabled then (but might start later). |
| 3209 | * Also don't save when inside a mapping, vgetc_im_active has not been set |
| 3210 | * then. |
| 3211 | * And don't save when the keys were stuffed (e.g., for a "." command). |
| 3212 | * And don't save when the GUI is running but our window doesn't have |
| 3213 | * input focus (e.g., when a find dialog is open). */ |
| 3214 | if (!p_imdisable && KeyTyped && !KeyStuffed |
| 3215 | # ifdef FEAT_XIM |
| 3216 | && xic != NULL |
| 3217 | # endif |
| 3218 | # ifdef FEAT_GUI |
| 3219 | && (!gui.in_use || gui.in_focus) |
| 3220 | # endif |
| 3221 | ) |
| 3222 | { |
| 3223 | /* Do save when IM is on, or IM is off and saved status is on. */ |
| 3224 | if (vgetc_im_active) |
| 3225 | *psave = B_IMODE_IM; |
| 3226 | else if (*psave == B_IMODE_IM) |
| 3227 | *psave = B_IMODE_NONE; |
| 3228 | } |
| 3229 | } |
| 3230 | #endif |