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