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