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