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