Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 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 | * mouse.c: mouse handling functions |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 16 | /* |
| 17 | * Horiziontal and vertical steps used when scrolling. |
| 18 | * When negative scroll by a whole page. |
| 19 | */ |
| 20 | static long mouse_hor_step = 6; |
| 21 | static long mouse_vert_step = 3; |
| 22 | |
| 23 | void |
| 24 | mouse_set_vert_scroll_step(long step) |
| 25 | { |
| 26 | mouse_vert_step = step; |
| 27 | } |
| 28 | |
| 29 | void |
| 30 | mouse_set_hor_scroll_step(long step) |
| 31 | { |
| 32 | mouse_hor_step = step; |
| 33 | } |
| 34 | |
Bram Moolenaar | 85c3502 | 2019-11-22 22:21:59 +0100 | [diff] [blame] | 35 | #ifdef CHECK_DOUBLE_CLICK |
| 36 | /* |
| 37 | * Return the duration from t1 to t2 in milliseconds. |
| 38 | */ |
| 39 | static long |
| 40 | time_diff_ms(struct timeval *t1, struct timeval *t2) |
| 41 | { |
| 42 | // This handles wrapping of tv_usec correctly without any special case. |
| 43 | // Example of 2 pairs (tv_sec, tv_usec) with a duration of 5 ms: |
| 44 | // t1 = (1, 998000) t2 = (2, 3000) gives: |
| 45 | // (2 - 1) * 1000 + (3000 - 998000) / 1000 -> 5 ms. |
| 46 | return (t2->tv_sec - t1->tv_sec) * 1000 |
| 47 | + (t2->tv_usec - t1->tv_usec) / 1000; |
| 48 | } |
| 49 | #endif |
| 50 | |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 51 | /* |
| 52 | * Get class of a character for selection: same class means same word. |
| 53 | * 0: blank |
| 54 | * 1: punctuation groups |
| 55 | * 2: normal word character |
| 56 | * >2: multi-byte word character. |
| 57 | */ |
| 58 | static int |
| 59 | get_mouse_class(char_u *p) |
| 60 | { |
| 61 | int c; |
| 62 | |
| 63 | if (has_mbyte && MB_BYTE2LEN(p[0]) > 1) |
| 64 | return mb_get_class(p); |
| 65 | |
| 66 | c = *p; |
| 67 | if (c == ' ' || c == '\t') |
| 68 | return 0; |
| 69 | |
| 70 | if (vim_iswordc(c)) |
| 71 | return 2; |
| 72 | |
| 73 | // There are a few special cases where we want certain combinations of |
| 74 | // characters to be considered as a single word. These are things like |
| 75 | // "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each |
| 76 | // character is in its own class. |
| 77 | if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL) |
| 78 | return 1; |
| 79 | return c; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Move "pos" back to the start of the word it's in. |
| 84 | */ |
| 85 | static void |
| 86 | find_start_of_word(pos_T *pos) |
| 87 | { |
| 88 | char_u *line; |
| 89 | int cclass; |
| 90 | int col; |
| 91 | |
| 92 | line = ml_get(pos->lnum); |
| 93 | cclass = get_mouse_class(line + pos->col); |
| 94 | |
| 95 | while (pos->col > 0) |
| 96 | { |
| 97 | col = pos->col - 1; |
| 98 | col -= (*mb_head_off)(line, line + col); |
| 99 | if (get_mouse_class(line + col) != cclass) |
| 100 | break; |
| 101 | pos->col = col; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Move "pos" forward to the end of the word it's in. |
| 107 | * When 'selection' is "exclusive", the position is just after the word. |
| 108 | */ |
| 109 | static void |
| 110 | find_end_of_word(pos_T *pos) |
| 111 | { |
| 112 | char_u *line; |
| 113 | int cclass; |
| 114 | int col; |
| 115 | |
| 116 | line = ml_get(pos->lnum); |
| 117 | if (*p_sel == 'e' && pos->col > 0) |
| 118 | { |
| 119 | --pos->col; |
| 120 | pos->col -= (*mb_head_off)(line, line + pos->col); |
| 121 | } |
| 122 | cclass = get_mouse_class(line + pos->col); |
| 123 | while (line[pos->col] != NUL) |
| 124 | { |
| 125 | col = pos->col + (*mb_ptr2len)(line + pos->col); |
| 126 | if (get_mouse_class(line + col) != cclass) |
| 127 | { |
| 128 | if (*p_sel == 'e') |
| 129 | pos->col = col; |
| 130 | break; |
| 131 | } |
| 132 | pos->col = col; |
| 133 | } |
| 134 | } |
| 135 | |
Bram Moolenaar | 910c378 | 2019-09-22 14:11:50 +0200 | [diff] [blame] | 136 | #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \ |
Bram Moolenaar | 0b962e5 | 2022-04-03 18:02:37 +0100 | [diff] [blame] | 137 | || defined(FEAT_GUI_MSWIN) \ |
Bram Moolenaar | 097148e | 2020-08-11 21:58:20 +0200 | [diff] [blame] | 138 | || defined(FEAT_GUI_PHOTON) \ |
Bram Moolenaar | 910c378 | 2019-09-22 14:11:50 +0200 | [diff] [blame] | 139 | || defined(FEAT_TERM_POPUP_MENU) |
| 140 | # define USE_POPUP_SETPOS |
| 141 | # define NEED_VCOL2COL |
| 142 | |
| 143 | /* |
| 144 | * Translate window coordinates to buffer position without any side effects |
| 145 | */ |
| 146 | static int |
| 147 | get_fpos_of_mouse(pos_T *mpos) |
| 148 | { |
| 149 | win_T *wp; |
| 150 | int row = mouse_row; |
| 151 | int col = mouse_col; |
| 152 | |
| 153 | if (row < 0 || col < 0) // check if it makes sense |
| 154 | return IN_UNKNOWN; |
| 155 | |
| 156 | // find the window where the row is in |
| 157 | wp = mouse_find_win(&row, &col, FAIL_POPUP); |
| 158 | if (wp == NULL) |
| 159 | return IN_UNKNOWN; |
| 160 | // winpos and height may change in win_enter()! |
| 161 | if (row >= wp->w_height) // In (or below) status line |
| 162 | return IN_STATUS_LINE; |
| 163 | if (col >= wp->w_width) // In vertical separator line |
| 164 | return IN_SEP_LINE; |
| 165 | |
| 166 | if (wp != curwin) |
| 167 | return IN_UNKNOWN; |
| 168 | |
| 169 | // compute the position in the buffer line from the posn on the screen |
| 170 | if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum, NULL)) |
| 171 | return IN_STATUS_LINE; // past bottom |
| 172 | |
| 173 | mpos->col = vcol2col(wp, mpos->lnum, col); |
| 174 | |
| 175 | if (mpos->col > 0) |
| 176 | --mpos->col; |
| 177 | mpos->coladd = 0; |
| 178 | return IN_BUFFER; |
| 179 | } |
| 180 | #endif |
| 181 | |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 182 | /* |
| 183 | * Do the appropriate action for the current mouse click in the current mode. |
| 184 | * Not used for Command-line mode. |
| 185 | * |
| 186 | * Normal and Visual Mode: |
| 187 | * event modi- position visual change action |
| 188 | * fier cursor window |
| 189 | * left press - yes end yes |
| 190 | * left press C yes end yes "^]" (2) |
| 191 | * left press S yes end (popup: extend) yes "*" (2) |
| 192 | * left drag - yes start if moved no |
| 193 | * left relse - yes start if moved no |
| 194 | * middle press - yes if not active no put register |
| 195 | * middle press - yes if active no yank and put |
| 196 | * right press - yes start or extend yes |
| 197 | * right press S yes no change yes "#" (2) |
| 198 | * right drag - yes extend no |
| 199 | * right relse - yes extend no |
| 200 | * |
| 201 | * Insert or Replace Mode: |
| 202 | * event modi- position visual change action |
| 203 | * fier cursor window |
| 204 | * left press - yes (cannot be active) yes |
| 205 | * left press C yes (cannot be active) yes "CTRL-O^]" (2) |
| 206 | * left press S yes (cannot be active) yes "CTRL-O*" (2) |
| 207 | * left drag - yes start or extend (1) no CTRL-O (1) |
| 208 | * left relse - yes start or extend (1) no CTRL-O (1) |
| 209 | * middle press - no (cannot be active) no put register |
| 210 | * right press - yes start or extend yes CTRL-O |
| 211 | * right press S yes (cannot be active) yes "CTRL-O#" (2) |
| 212 | * |
| 213 | * (1) only if mouse pointer moved since press |
| 214 | * (2) only if click is in same buffer |
| 215 | * |
| 216 | * Return TRUE if start_arrow() should be called for edit mode. |
| 217 | */ |
| 218 | int |
| 219 | do_mouse( |
| 220 | oparg_T *oap, // operator argument, can be NULL |
| 221 | int c, // K_LEFTMOUSE, etc |
| 222 | int dir, // Direction to 'put' if necessary |
| 223 | long count, |
| 224 | int fixindent) // PUT_FIXINDENT if fixing indent necessary |
| 225 | { |
| 226 | static int do_always = FALSE; // ignore 'mouse' setting next time |
| 227 | static int got_click = FALSE; // got a click some time back |
| 228 | |
| 229 | int which_button; // MOUSE_LEFT, _MIDDLE or _RIGHT |
| 230 | int is_click = FALSE; // If FALSE it's a drag or release event |
| 231 | int is_drag = FALSE; // If TRUE it's a drag event |
| 232 | int jump_flags = 0; // flags for jump_to_mouse() |
| 233 | pos_T start_visual; |
| 234 | int moved; // Has cursor moved? |
| 235 | int in_status_line; // mouse in status line |
| 236 | static int in_tab_line = FALSE; // mouse clicked in tab line |
| 237 | int in_sep_line; // mouse in vertical separator line |
| 238 | int c1, c2; |
| 239 | #if defined(FEAT_FOLDING) |
| 240 | pos_T save_cursor; |
| 241 | #endif |
| 242 | win_T *old_curwin = curwin; |
| 243 | static pos_T orig_cursor; |
| 244 | colnr_T leftcol, rightcol; |
| 245 | pos_T end_visual; |
| 246 | int diff; |
| 247 | int old_active = VIsual_active; |
| 248 | int old_mode = VIsual_mode; |
| 249 | int regname; |
| 250 | |
| 251 | #if defined(FEAT_FOLDING) |
| 252 | save_cursor = curwin->w_cursor; |
| 253 | #endif |
| 254 | |
| 255 | // When GUI is active, always recognize mouse events, otherwise: |
| 256 | // - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'. |
| 257 | // - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'. |
| 258 | // - For command line and insert mode 'mouse' is checked before calling |
| 259 | // do_mouse(). |
| 260 | if (do_always) |
| 261 | do_always = FALSE; |
| 262 | else |
| 263 | #ifdef FEAT_GUI |
| 264 | if (!gui.in_use) |
| 265 | #endif |
| 266 | { |
| 267 | if (VIsual_active) |
| 268 | { |
| 269 | if (!mouse_has(MOUSE_VISUAL)) |
| 270 | return FALSE; |
| 271 | } |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 272 | else if (State == MODE_NORMAL && !mouse_has(MOUSE_NORMAL)) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 273 | return FALSE; |
| 274 | } |
| 275 | |
| 276 | for (;;) |
| 277 | { |
| 278 | which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag); |
| 279 | if (is_drag) |
| 280 | { |
| 281 | // If the next character is the same mouse event then use that |
| 282 | // one. Speeds up dragging the status line. |
zeertzjq | 0f68e6c | 2022-04-05 13:17:01 +0100 | [diff] [blame] | 283 | // Note: Since characters added to the stuff buffer in the code |
| 284 | // below need to come before the next character, do not do this |
| 285 | // when the current character was stuffed. |
| 286 | if (!KeyStuffed && vpeekc() != NUL) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 287 | { |
| 288 | int nc; |
| 289 | int save_mouse_row = mouse_row; |
| 290 | int save_mouse_col = mouse_col; |
| 291 | |
| 292 | // Need to get the character, peeking doesn't get the actual |
| 293 | // one. |
| 294 | nc = safe_vgetc(); |
| 295 | if (c == nc) |
| 296 | continue; |
| 297 | vungetc(nc); |
| 298 | mouse_row = save_mouse_row; |
| 299 | mouse_col = save_mouse_col; |
| 300 | } |
| 301 | } |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | if (c == K_MOUSEMOVE) |
| 306 | { |
| 307 | // Mouse moved without a button pressed. |
| 308 | #ifdef FEAT_BEVAL_TERM |
| 309 | ui_may_remove_balloon(); |
| 310 | if (p_bevalterm) |
| 311 | { |
| 312 | profile_setlimit(p_bdlay, &bevalexpr_due); |
| 313 | bevalexpr_due_set = TRUE; |
| 314 | } |
| 315 | #endif |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 316 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 317 | popup_handle_mouse_moved(); |
| 318 | #endif |
| 319 | return FALSE; |
| 320 | } |
| 321 | |
| 322 | #ifdef FEAT_MOUSESHAPE |
| 323 | // May have stopped dragging the status or separator line. The pointer is |
| 324 | // most likely still on the status or separator line. |
| 325 | if (!is_drag && drag_status_line) |
| 326 | { |
| 327 | drag_status_line = FALSE; |
| 328 | update_mouseshape(SHAPE_IDX_STATUS); |
| 329 | } |
| 330 | if (!is_drag && drag_sep_line) |
| 331 | { |
| 332 | drag_sep_line = FALSE; |
| 333 | update_mouseshape(SHAPE_IDX_VSEP); |
| 334 | } |
| 335 | #endif |
| 336 | |
| 337 | // Ignore drag and release events if we didn't get a click. |
| 338 | if (is_click) |
| 339 | got_click = TRUE; |
| 340 | else |
| 341 | { |
| 342 | if (!got_click) // didn't get click, ignore |
| 343 | return FALSE; |
| 344 | if (!is_drag) // release, reset got_click |
| 345 | { |
| 346 | got_click = FALSE; |
| 347 | if (in_tab_line) |
| 348 | { |
| 349 | in_tab_line = FALSE; |
| 350 | return FALSE; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // CTRL right mouse button does CTRL-T |
| 356 | if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT) |
| 357 | { |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 358 | if (State & MODE_INSERT) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 359 | stuffcharReadbuff(Ctrl_O); |
| 360 | if (count > 1) |
| 361 | stuffnumReadbuff(count); |
| 362 | stuffcharReadbuff(Ctrl_T); |
| 363 | got_click = FALSE; // ignore drag&release now |
| 364 | return FALSE; |
| 365 | } |
| 366 | |
| 367 | // CTRL only works with left mouse button |
| 368 | if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT) |
| 369 | return FALSE; |
| 370 | |
| 371 | // When a modifier is down, ignore drag and release events, as well as |
| 372 | // multiple clicks and the middle mouse button. |
| 373 | // Accept shift-leftmouse drags when 'mousemodel' is "popup.*". |
| 374 | if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT |
| 375 | | MOD_MASK_META)) |
| 376 | && (!is_click |
| 377 | || (mod_mask & MOD_MASK_MULTI_CLICK) |
| 378 | || which_button == MOUSE_MIDDLE) |
| 379 | && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)) |
| 380 | && mouse_model_popup() |
| 381 | && which_button == MOUSE_LEFT) |
| 382 | && !((mod_mask & MOD_MASK_ALT) |
| 383 | && !mouse_model_popup() |
| 384 | && which_button == MOUSE_RIGHT) |
| 385 | ) |
| 386 | return FALSE; |
| 387 | |
| 388 | // If the button press was used as the movement command for an operator |
| 389 | // (eg "d<MOUSE>"), or it is the middle button that is held down, ignore |
| 390 | // drag/release events. |
| 391 | if (!is_click && which_button == MOUSE_MIDDLE) |
| 392 | return FALSE; |
| 393 | |
| 394 | if (oap != NULL) |
| 395 | regname = oap->regname; |
| 396 | else |
| 397 | regname = 0; |
| 398 | |
| 399 | // Middle mouse button does a 'put' of the selected text |
| 400 | if (which_button == MOUSE_MIDDLE) |
| 401 | { |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 402 | if (State == MODE_NORMAL) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 403 | { |
| 404 | // If an operator was pending, we don't know what the user wanted |
| 405 | // to do. Go back to normal mode: Clear the operator and beep(). |
| 406 | if (oap != NULL && oap->op_type != OP_NOP) |
| 407 | { |
| 408 | clearopbeep(oap); |
| 409 | return FALSE; |
| 410 | } |
| 411 | |
| 412 | // If visual was active, yank the highlighted text and put it |
| 413 | // before the mouse pointer position. |
| 414 | // In Select mode replace the highlighted text with the clipboard. |
| 415 | if (VIsual_active) |
| 416 | { |
| 417 | if (VIsual_select) |
| 418 | { |
| 419 | stuffcharReadbuff(Ctrl_G); |
| 420 | stuffReadbuff((char_u *)"\"+p"); |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | stuffcharReadbuff('y'); |
| 425 | stuffcharReadbuff(K_MIDDLEMOUSE); |
| 426 | } |
| 427 | do_always = TRUE; // ignore 'mouse' setting next time |
| 428 | return FALSE; |
| 429 | } |
| 430 | // The rest is below jump_to_mouse() |
| 431 | } |
| 432 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 433 | else if ((State & MODE_INSERT) == 0) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 434 | return FALSE; |
| 435 | |
| 436 | // Middle click in insert mode doesn't move the mouse, just insert the |
| 437 | // contents of a register. '.' register is special, can't insert that |
| 438 | // with do_put(). |
| 439 | // Also paste at the cursor if the current mode isn't in 'mouse' (only |
| 440 | // happens for the GUI). |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 441 | if ((State & MODE_INSERT) || !mouse_has(MOUSE_NORMAL)) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 442 | { |
| 443 | if (regname == '.') |
| 444 | insert_reg(regname, TRUE); |
| 445 | else |
| 446 | { |
| 447 | #ifdef FEAT_CLIPBOARD |
| 448 | if (clip_star.available && regname == 0) |
| 449 | regname = '*'; |
| 450 | #endif |
| 451 | if ((State & REPLACE_FLAG) && !yank_register_mline(regname)) |
| 452 | insert_reg(regname, TRUE); |
| 453 | else |
| 454 | { |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 455 | do_put(regname, NULL, BACKWARD, 1L, |
| 456 | fixindent | PUT_CURSEND); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 457 | |
| 458 | // Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r |
| 459 | AppendCharToRedobuff(Ctrl_R); |
| 460 | AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O); |
| 461 | AppendCharToRedobuff(regname == 0 ? '"' : regname); |
| 462 | } |
| 463 | } |
| 464 | return FALSE; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // When dragging or button-up stay in the same window. |
| 469 | if (!is_click) |
| 470 | jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE; |
| 471 | |
| 472 | start_visual.lnum = 0; |
| 473 | |
| 474 | // Check for clicking in the tab page line. |
| 475 | if (mouse_row == 0 && firstwin->w_winrow > 0) |
| 476 | { |
| 477 | if (is_drag) |
| 478 | { |
| 479 | if (in_tab_line) |
| 480 | { |
| 481 | c1 = TabPageIdxs[mouse_col]; |
| 482 | tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab) |
| 483 | ? c1 - 1 : c1); |
| 484 | } |
| 485 | return FALSE; |
| 486 | } |
| 487 | |
| 488 | // click in a tab selects that tab page |
| 489 | if (is_click |
| 490 | # ifdef FEAT_CMDWIN |
| 491 | && cmdwin_type == 0 |
| 492 | # endif |
| 493 | && mouse_col < Columns) |
| 494 | { |
| 495 | in_tab_line = TRUE; |
| 496 | c1 = TabPageIdxs[mouse_col]; |
| 497 | if (c1 >= 0) |
| 498 | { |
| 499 | if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK) |
| 500 | { |
| 501 | // double click opens new page |
Bram Moolenaar | 4f3c57f | 2021-06-03 22:11:08 +0200 | [diff] [blame] | 502 | end_visual_mode_keep_button(); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 503 | tabpage_new(); |
| 504 | tabpage_move(c1 == 0 ? 9999 : c1 - 1); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | // Go to specified tab page, or next one if not clicking |
| 509 | // on a label. |
| 510 | goto_tabpage(c1); |
| 511 | |
| 512 | // It's like clicking on the status line of a window. |
| 513 | if (curwin != old_curwin) |
Bram Moolenaar | 4f3c57f | 2021-06-03 22:11:08 +0200 | [diff] [blame] | 514 | end_visual_mode_keep_button(); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | tabpage_T *tp; |
| 520 | |
| 521 | // Close the current or specified tab page. |
| 522 | if (c1 == -999) |
| 523 | tp = curtab; |
| 524 | else |
| 525 | tp = find_tabpage(-c1); |
| 526 | if (tp == curtab) |
| 527 | { |
| 528 | if (first_tabpage->tp_next != NULL) |
| 529 | tabpage_close(FALSE); |
| 530 | } |
| 531 | else if (tp != NULL) |
| 532 | tabpage_close_other(tp, FALSE); |
| 533 | } |
| 534 | } |
| 535 | return TRUE; |
| 536 | } |
| 537 | else if (is_drag && in_tab_line) |
| 538 | { |
| 539 | c1 = TabPageIdxs[mouse_col]; |
| 540 | tabpage_move(c1 <= 0 ? 9999 : c1 - 1); |
| 541 | return FALSE; |
| 542 | } |
| 543 | |
| 544 | // When 'mousemodel' is "popup" or "popup_setpos", translate mouse events: |
| 545 | // right button up -> pop-up menu |
| 546 | // shift-left button -> right button |
| 547 | // alt-left button -> alt-right button |
| 548 | if (mouse_model_popup()) |
| 549 | { |
| 550 | if (which_button == MOUSE_RIGHT |
| 551 | && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))) |
| 552 | { |
Bram Moolenaar | 910c378 | 2019-09-22 14:11:50 +0200 | [diff] [blame] | 553 | #ifdef USE_POPUP_SETPOS |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 554 | # ifdef FEAT_GUI |
| 555 | if (gui.in_use) |
| 556 | { |
| 557 | # if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \ |
Bram Moolenaar | 097148e | 2020-08-11 21:58:20 +0200 | [diff] [blame] | 558 | || defined(FEAT_GUI_PHOTON) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 559 | if (!is_click) |
| 560 | // Ignore right button release events, only shows the popup |
| 561 | // menu on the button down event. |
| 562 | return FALSE; |
| 563 | # endif |
Bram Moolenaar | 0b962e5 | 2022-04-03 18:02:37 +0100 | [diff] [blame] | 564 | # if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 565 | if (is_click || is_drag) |
| 566 | // Ignore right button down and drag mouse events. Windows |
| 567 | // only shows the popup menu on the button up event. |
| 568 | return FALSE; |
| 569 | # endif |
| 570 | } |
| 571 | # endif |
| 572 | # if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU) |
| 573 | else |
| 574 | # endif |
| 575 | # if defined(FEAT_TERM_POPUP_MENU) |
| 576 | if (!is_click) |
| 577 | // Ignore right button release events, only shows the popup |
| 578 | // menu on the button down event. |
| 579 | return FALSE; |
| 580 | #endif |
| 581 | |
| 582 | jump_flags = 0; |
| 583 | if (STRCMP(p_mousem, "popup_setpos") == 0) |
| 584 | { |
| 585 | // First set the cursor position before showing the popup |
| 586 | // menu. |
| 587 | if (VIsual_active) |
| 588 | { |
| 589 | pos_T m_pos; |
| 590 | |
| 591 | // set MOUSE_MAY_STOP_VIS if we are outside the |
| 592 | // selection or the current window (might have false |
| 593 | // negative here) |
| 594 | if (mouse_row < curwin->w_winrow |
| 595 | || mouse_row |
| 596 | > (curwin->w_winrow + curwin->w_height)) |
| 597 | jump_flags = MOUSE_MAY_STOP_VIS; |
| 598 | else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER) |
| 599 | jump_flags = MOUSE_MAY_STOP_VIS; |
| 600 | else |
| 601 | { |
| 602 | if ((LT_POS(curwin->w_cursor, VIsual) |
| 603 | && (LT_POS(m_pos, curwin->w_cursor) |
| 604 | || LT_POS(VIsual, m_pos))) |
| 605 | || (LT_POS(VIsual, curwin->w_cursor) |
| 606 | && (LT_POS(m_pos, VIsual) |
| 607 | || LT_POS(curwin->w_cursor, m_pos)))) |
| 608 | { |
| 609 | jump_flags = MOUSE_MAY_STOP_VIS; |
| 610 | } |
| 611 | else if (VIsual_mode == Ctrl_V) |
| 612 | { |
| 613 | getvcols(curwin, &curwin->w_cursor, &VIsual, |
| 614 | &leftcol, &rightcol); |
| 615 | getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL); |
| 616 | if (m_pos.col < leftcol || m_pos.col > rightcol) |
| 617 | jump_flags = MOUSE_MAY_STOP_VIS; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | else |
| 622 | jump_flags = MOUSE_MAY_STOP_VIS; |
| 623 | } |
| 624 | if (jump_flags) |
| 625 | { |
| 626 | jump_flags = jump_to_mouse(jump_flags, NULL, which_button); |
| 627 | update_curbuf(VIsual_active ? INVERTED : VALID); |
| 628 | setcursor(); |
| 629 | out_flush(); // Update before showing popup menu |
| 630 | } |
| 631 | # ifdef FEAT_MENU |
| 632 | show_popupmenu(); |
| 633 | got_click = FALSE; // ignore release events |
| 634 | # endif |
| 635 | return (jump_flags & CURSOR_MOVED) != 0; |
| 636 | #else |
| 637 | return FALSE; |
| 638 | #endif |
| 639 | } |
| 640 | if (which_button == MOUSE_LEFT |
| 641 | && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))) |
| 642 | { |
| 643 | which_button = MOUSE_RIGHT; |
| 644 | mod_mask &= ~MOD_MASK_SHIFT; |
| 645 | } |
| 646 | } |
| 647 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 648 | if ((State & (MODE_NORMAL | MODE_INSERT)) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 649 | && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))) |
| 650 | { |
| 651 | if (which_button == MOUSE_LEFT) |
| 652 | { |
| 653 | if (is_click) |
| 654 | { |
| 655 | // stop Visual mode for a left click in a window, but not when |
| 656 | // on a status line |
| 657 | if (VIsual_active) |
| 658 | jump_flags |= MOUSE_MAY_STOP_VIS; |
| 659 | } |
| 660 | else if (mouse_has(MOUSE_VISUAL)) |
| 661 | jump_flags |= MOUSE_MAY_VIS; |
| 662 | } |
| 663 | else if (which_button == MOUSE_RIGHT) |
| 664 | { |
| 665 | if (is_click && VIsual_active) |
| 666 | { |
| 667 | // Remember the start and end of visual before moving the |
| 668 | // cursor. |
| 669 | if (LT_POS(curwin->w_cursor, VIsual)) |
| 670 | { |
| 671 | start_visual = curwin->w_cursor; |
| 672 | end_visual = VIsual; |
| 673 | } |
| 674 | else |
| 675 | { |
| 676 | start_visual = VIsual; |
| 677 | end_visual = curwin->w_cursor; |
| 678 | } |
| 679 | } |
| 680 | jump_flags |= MOUSE_FOCUS; |
| 681 | if (mouse_has(MOUSE_VISUAL)) |
| 682 | jump_flags |= MOUSE_MAY_VIS; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | // If an operator is pending, ignore all drags and releases until the |
| 687 | // next mouse click. |
| 688 | if (!is_drag && oap != NULL && oap->op_type != OP_NOP) |
| 689 | { |
| 690 | got_click = FALSE; |
| 691 | oap->motion_type = MCHAR; |
| 692 | } |
| 693 | |
| 694 | // When releasing the button let jump_to_mouse() know. |
| 695 | if (!is_click && !is_drag) |
| 696 | jump_flags |= MOUSE_RELEASED; |
| 697 | |
| 698 | // JUMP! |
| 699 | jump_flags = jump_to_mouse(jump_flags, |
| 700 | oap == NULL ? NULL : &(oap->inclusive), which_button); |
| 701 | |
| 702 | #ifdef FEAT_MENU |
| 703 | // A click in the window toolbar has no side effects. |
| 704 | if (jump_flags & MOUSE_WINBAR) |
| 705 | return FALSE; |
| 706 | #endif |
| 707 | moved = (jump_flags & CURSOR_MOVED); |
| 708 | in_status_line = (jump_flags & IN_STATUS_LINE); |
| 709 | in_sep_line = (jump_flags & IN_SEP_LINE); |
| 710 | |
| 711 | #ifdef FEAT_NETBEANS_INTG |
| 712 | if (isNetbeansBuffer(curbuf) |
| 713 | && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE))) |
| 714 | { |
| 715 | int key = KEY2TERMCAP1(c); |
| 716 | |
| 717 | if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE |
| 718 | || key == (int)KE_RIGHTRELEASE) |
| 719 | netbeans_button_release(which_button); |
| 720 | } |
| 721 | #endif |
| 722 | |
| 723 | // When jumping to another window, clear a pending operator. That's a bit |
| 724 | // friendlier than beeping and not jumping to that window. |
| 725 | if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP) |
| 726 | clearop(oap); |
| 727 | |
| 728 | #ifdef FEAT_FOLDING |
| 729 | if (mod_mask == 0 |
| 730 | && !is_drag |
| 731 | && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN)) |
| 732 | && which_button == MOUSE_LEFT) |
| 733 | { |
| 734 | // open or close a fold at this line |
| 735 | if (jump_flags & MOUSE_FOLD_OPEN) |
| 736 | openFold(curwin->w_cursor.lnum, 1L); |
| 737 | else |
| 738 | closeFold(curwin->w_cursor.lnum, 1L); |
| 739 | // don't move the cursor if still in the same window |
| 740 | if (curwin == old_curwin) |
| 741 | curwin->w_cursor = save_cursor; |
| 742 | } |
| 743 | #endif |
| 744 | |
| 745 | #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN) |
| 746 | if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available) |
| 747 | { |
| 748 | clip_modeless(which_button, is_click, is_drag); |
| 749 | return FALSE; |
| 750 | } |
| 751 | #endif |
| 752 | |
| 753 | // Set global flag that we are extending the Visual area with mouse |
| 754 | // dragging; temporarily minimize 'scrolloff'. |
| 755 | if (VIsual_active && is_drag && get_scrolloff_value()) |
| 756 | { |
| 757 | // In the very first line, allow scrolling one line |
| 758 | if (mouse_row == 0) |
| 759 | mouse_dragging = 2; |
| 760 | else |
| 761 | mouse_dragging = 1; |
| 762 | } |
| 763 | |
| 764 | // When dragging the mouse above the window, scroll down. |
| 765 | if (is_drag && mouse_row < 0 && !in_status_line) |
| 766 | { |
| 767 | scroll_redraw(FALSE, 1L); |
| 768 | mouse_row = 0; |
| 769 | } |
| 770 | |
| 771 | if (start_visual.lnum) // right click in visual mode |
| 772 | { |
| 773 | // When ALT is pressed make Visual mode blockwise. |
| 774 | if (mod_mask & MOD_MASK_ALT) |
| 775 | VIsual_mode = Ctrl_V; |
| 776 | |
| 777 | // In Visual-block mode, divide the area in four, pick up the corner |
| 778 | // that is in the quarter that the cursor is in. |
| 779 | if (VIsual_mode == Ctrl_V) |
| 780 | { |
| 781 | getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol); |
| 782 | if (curwin->w_curswant > (leftcol + rightcol) / 2) |
| 783 | end_visual.col = leftcol; |
| 784 | else |
| 785 | end_visual.col = rightcol; |
| 786 | if (curwin->w_cursor.lnum >= |
| 787 | (start_visual.lnum + end_visual.lnum) / 2) |
| 788 | end_visual.lnum = start_visual.lnum; |
| 789 | |
| 790 | // move VIsual to the right column |
| 791 | start_visual = curwin->w_cursor; // save the cursor pos |
| 792 | curwin->w_cursor = end_visual; |
| 793 | coladvance(end_visual.col); |
| 794 | VIsual = curwin->w_cursor; |
| 795 | curwin->w_cursor = start_visual; // restore the cursor |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | // If the click is before the start of visual, change the start. |
| 800 | // If the click is after the end of visual, change the end. If |
| 801 | // the click is inside the visual, change the closest side. |
| 802 | if (LT_POS(curwin->w_cursor, start_visual)) |
| 803 | VIsual = end_visual; |
| 804 | else if (LT_POS(end_visual, curwin->w_cursor)) |
| 805 | VIsual = start_visual; |
| 806 | else |
| 807 | { |
| 808 | // In the same line, compare column number |
| 809 | if (end_visual.lnum == start_visual.lnum) |
| 810 | { |
| 811 | if (curwin->w_cursor.col - start_visual.col > |
| 812 | end_visual.col - curwin->w_cursor.col) |
| 813 | VIsual = start_visual; |
| 814 | else |
| 815 | VIsual = end_visual; |
| 816 | } |
| 817 | |
| 818 | // In different lines, compare line number |
| 819 | else |
| 820 | { |
| 821 | diff = (curwin->w_cursor.lnum - start_visual.lnum) - |
| 822 | (end_visual.lnum - curwin->w_cursor.lnum); |
| 823 | |
| 824 | if (diff > 0) // closest to end |
| 825 | VIsual = start_visual; |
| 826 | else if (diff < 0) // closest to start |
| 827 | VIsual = end_visual; |
| 828 | else // in the middle line |
| 829 | { |
| 830 | if (curwin->w_cursor.col < |
| 831 | (start_visual.col + end_visual.col) / 2) |
| 832 | VIsual = end_visual; |
| 833 | else |
| 834 | VIsual = start_visual; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | // If Visual mode started in insert mode, execute "CTRL-O" |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 841 | else if ((State & MODE_INSERT) && VIsual_active) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 842 | stuffcharReadbuff(Ctrl_O); |
| 843 | |
| 844 | // Middle mouse click: Put text before cursor. |
| 845 | if (which_button == MOUSE_MIDDLE) |
| 846 | { |
| 847 | #ifdef FEAT_CLIPBOARD |
| 848 | if (clip_star.available && regname == 0) |
| 849 | regname = '*'; |
| 850 | #endif |
| 851 | if (yank_register_mline(regname)) |
| 852 | { |
| 853 | if (mouse_past_bottom) |
| 854 | dir = FORWARD; |
| 855 | } |
| 856 | else if (mouse_past_eol) |
| 857 | dir = FORWARD; |
| 858 | |
| 859 | if (fixindent) |
| 860 | { |
| 861 | c1 = (dir == BACKWARD) ? '[' : ']'; |
| 862 | c2 = 'p'; |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | c1 = (dir == FORWARD) ? 'p' : 'P'; |
| 867 | c2 = NUL; |
| 868 | } |
| 869 | prep_redo(regname, count, NUL, c1, NUL, c2, NUL); |
| 870 | |
| 871 | // Remember where the paste started, so in edit() Insstart can be set |
| 872 | // to this position |
| 873 | if (restart_edit != 0) |
| 874 | where_paste_started = curwin->w_cursor; |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 875 | do_put(regname, NULL, dir, count, fixindent | PUT_CURSEND); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | #if defined(FEAT_QUICKFIX) |
| 879 | // Ctrl-Mouse click or double click in a quickfix window jumps to the |
| 880 | // error under the mouse pointer. |
| 881 | else if (((mod_mask & MOD_MASK_CTRL) |
| 882 | || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK) |
| 883 | && bt_quickfix(curbuf)) |
| 884 | { |
| 885 | if (curwin->w_llist_ref == NULL) // quickfix window |
| 886 | do_cmdline_cmd((char_u *)".cc"); |
| 887 | else // location list window |
| 888 | do_cmdline_cmd((char_u *)".ll"); |
| 889 | got_click = FALSE; // ignore drag&release now |
| 890 | } |
| 891 | #endif |
| 892 | |
| 893 | // Ctrl-Mouse click (or double click in a help window) jumps to the tag |
| 894 | // under the mouse pointer. |
| 895 | else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help |
| 896 | && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)) |
| 897 | { |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 898 | if (State & MODE_INSERT) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 899 | stuffcharReadbuff(Ctrl_O); |
| 900 | stuffcharReadbuff(Ctrl_RSB); |
| 901 | got_click = FALSE; // ignore drag&release now |
| 902 | } |
| 903 | |
| 904 | // Shift-Mouse click searches for the next occurrence of the word under |
| 905 | // the mouse pointer |
| 906 | else if ((mod_mask & MOD_MASK_SHIFT)) |
| 907 | { |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 908 | if ((State & MODE_INSERT) || (VIsual_active && VIsual_select)) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 909 | stuffcharReadbuff(Ctrl_O); |
| 910 | if (which_button == MOUSE_LEFT) |
| 911 | stuffcharReadbuff('*'); |
| 912 | else // MOUSE_RIGHT |
| 913 | stuffcharReadbuff('#'); |
| 914 | } |
| 915 | |
| 916 | // Handle double clicks, unless on status line |
| 917 | else if (in_status_line) |
| 918 | { |
| 919 | #ifdef FEAT_MOUSESHAPE |
| 920 | if ((is_drag || is_click) && !drag_status_line) |
| 921 | { |
| 922 | drag_status_line = TRUE; |
| 923 | update_mouseshape(-1); |
| 924 | } |
| 925 | #endif |
| 926 | } |
| 927 | else if (in_sep_line) |
| 928 | { |
| 929 | #ifdef FEAT_MOUSESHAPE |
| 930 | if ((is_drag || is_click) && !drag_sep_line) |
| 931 | { |
| 932 | drag_sep_line = TRUE; |
| 933 | update_mouseshape(-1); |
| 934 | } |
| 935 | #endif |
| 936 | } |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 937 | else if ((mod_mask & MOD_MASK_MULTI_CLICK) |
| 938 | && (State & (MODE_NORMAL | MODE_INSERT)) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 939 | && mouse_has(MOUSE_VISUAL)) |
| 940 | { |
| 941 | if (is_click || !VIsual_active) |
| 942 | { |
| 943 | if (VIsual_active) |
| 944 | orig_cursor = VIsual; |
| 945 | else |
| 946 | { |
| 947 | check_visual_highlight(); |
| 948 | VIsual = curwin->w_cursor; |
| 949 | orig_cursor = VIsual; |
| 950 | VIsual_active = TRUE; |
| 951 | VIsual_reselect = TRUE; |
| 952 | // start Select mode if 'selectmode' contains "mouse" |
| 953 | may_start_select('o'); |
| 954 | setmouse(); |
| 955 | } |
| 956 | if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK) |
| 957 | { |
| 958 | // Double click with ALT pressed makes it blockwise. |
| 959 | if (mod_mask & MOD_MASK_ALT) |
| 960 | VIsual_mode = Ctrl_V; |
| 961 | else |
| 962 | VIsual_mode = 'v'; |
| 963 | } |
| 964 | else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK) |
| 965 | VIsual_mode = 'V'; |
| 966 | else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK) |
| 967 | VIsual_mode = Ctrl_V; |
| 968 | #ifdef FEAT_CLIPBOARD |
| 969 | // Make sure the clipboard gets updated. Needed because start and |
| 970 | // end may still be the same, and the selection needs to be owned |
| 971 | clip_star.vmode = NUL; |
| 972 | #endif |
| 973 | } |
| 974 | // A double click selects a word or a block. |
| 975 | if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK) |
| 976 | { |
| 977 | pos_T *pos = NULL; |
| 978 | int gc; |
| 979 | |
| 980 | if (is_click) |
| 981 | { |
| 982 | // If the character under the cursor (skipping white space) is |
| 983 | // not a word character, try finding a match and select a (), |
| 984 | // {}, [], #if/#endif, etc. block. |
| 985 | end_visual = curwin->w_cursor; |
| 986 | while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc)) |
| 987 | inc(&end_visual); |
| 988 | if (oap != NULL) |
| 989 | oap->motion_type = MCHAR; |
| 990 | if (oap != NULL |
| 991 | && VIsual_mode == 'v' |
| 992 | && !vim_iswordc(gchar_pos(&end_visual)) |
| 993 | && EQUAL_POS(curwin->w_cursor, VIsual) |
| 994 | && (pos = findmatch(oap, NUL)) != NULL) |
| 995 | { |
| 996 | curwin->w_cursor = *pos; |
| 997 | if (oap->motion_type == MLINE) |
| 998 | VIsual_mode = 'V'; |
| 999 | else if (*p_sel == 'e') |
| 1000 | { |
| 1001 | if (LT_POS(curwin->w_cursor, VIsual)) |
| 1002 | ++VIsual.col; |
| 1003 | else |
| 1004 | ++curwin->w_cursor.col; |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | if (pos == NULL && (is_click || is_drag)) |
| 1010 | { |
| 1011 | // When not found a match or when dragging: extend to include |
| 1012 | // a word. |
| 1013 | if (LT_POS(curwin->w_cursor, orig_cursor)) |
| 1014 | { |
| 1015 | find_start_of_word(&curwin->w_cursor); |
| 1016 | find_end_of_word(&VIsual); |
| 1017 | } |
| 1018 | else |
| 1019 | { |
| 1020 | find_start_of_word(&VIsual); |
| 1021 | if (*p_sel == 'e' && *ml_get_cursor() != NUL) |
| 1022 | curwin->w_cursor.col += |
| 1023 | (*mb_ptr2len)(ml_get_cursor()); |
| 1024 | find_end_of_word(&curwin->w_cursor); |
| 1025 | } |
| 1026 | } |
| 1027 | curwin->w_set_curswant = TRUE; |
| 1028 | } |
| 1029 | if (is_click) |
| 1030 | redraw_curbuf_later(INVERTED); // update the inversion |
| 1031 | } |
| 1032 | else if (VIsual_active && !old_active) |
| 1033 | { |
| 1034 | if (mod_mask & MOD_MASK_ALT) |
| 1035 | VIsual_mode = Ctrl_V; |
| 1036 | else |
| 1037 | VIsual_mode = 'v'; |
| 1038 | } |
| 1039 | |
| 1040 | // If Visual mode changed show it later. |
| 1041 | if ((!VIsual_active && old_active && mode_displayed) |
| 1042 | || (VIsual_active && p_smd && msg_silent == 0 |
| 1043 | && (!old_active || VIsual_mode != old_mode))) |
| 1044 | redraw_cmdline = TRUE; |
| 1045 | |
| 1046 | return moved; |
| 1047 | } |
| 1048 | |
| 1049 | void |
| 1050 | ins_mouse(int c) |
| 1051 | { |
| 1052 | pos_T tpos; |
| 1053 | win_T *old_curwin = curwin; |
| 1054 | |
| 1055 | # ifdef FEAT_GUI |
| 1056 | // When GUI is active, also move/paste when 'mouse' is empty |
| 1057 | if (!gui.in_use) |
| 1058 | # endif |
| 1059 | if (!mouse_has(MOUSE_INSERT)) |
| 1060 | return; |
| 1061 | |
| 1062 | undisplay_dollar(); |
| 1063 | tpos = curwin->w_cursor; |
| 1064 | if (do_mouse(NULL, c, BACKWARD, 1L, 0)) |
| 1065 | { |
| 1066 | win_T *new_curwin = curwin; |
| 1067 | |
| 1068 | if (curwin != old_curwin && win_valid(old_curwin)) |
| 1069 | { |
| 1070 | // Mouse took us to another window. We need to go back to the |
| 1071 | // previous one to stop insert there properly. |
| 1072 | curwin = old_curwin; |
| 1073 | curbuf = curwin->w_buffer; |
| 1074 | #ifdef FEAT_JOB_CHANNEL |
| 1075 | if (bt_prompt(curbuf)) |
| 1076 | // Restart Insert mode when re-entering the prompt buffer. |
| 1077 | curbuf->b_prompt_insert = 'A'; |
| 1078 | #endif |
| 1079 | } |
| 1080 | start_arrow(curwin == old_curwin ? &tpos : NULL); |
| 1081 | if (curwin != new_curwin && win_valid(new_curwin)) |
| 1082 | { |
| 1083 | curwin = new_curwin; |
| 1084 | curbuf = curwin->w_buffer; |
| 1085 | } |
| 1086 | # ifdef FEAT_CINDENT |
| 1087 | set_can_cindent(TRUE); |
| 1088 | # endif |
| 1089 | } |
| 1090 | |
| 1091 | // redraw status lines (in case another window became active) |
| 1092 | redraw_statuslines(); |
| 1093 | } |
| 1094 | |
| 1095 | void |
| 1096 | ins_mousescroll(int dir) |
| 1097 | { |
| 1098 | pos_T tpos; |
| 1099 | win_T *old_curwin = curwin, *wp; |
| 1100 | int did_scroll = FALSE; |
| 1101 | |
| 1102 | tpos = curwin->w_cursor; |
| 1103 | |
| 1104 | if (mouse_row >= 0 && mouse_col >= 0) |
| 1105 | { |
| 1106 | int row, col; |
| 1107 | |
| 1108 | row = mouse_row; |
| 1109 | col = mouse_col; |
| 1110 | |
| 1111 | // find the window at the pointer coordinates |
| 1112 | wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 1113 | if (wp == NULL) |
| 1114 | return; |
| 1115 | curwin = wp; |
| 1116 | curbuf = curwin->w_buffer; |
| 1117 | } |
| 1118 | if (curwin == old_curwin) |
| 1119 | undisplay_dollar(); |
| 1120 | |
| 1121 | // Don't scroll the window in which completion is being done. |
| 1122 | if (!pum_visible() || curwin != old_curwin) |
| 1123 | { |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 1124 | long step; |
| 1125 | |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1126 | if (dir == MSCR_DOWN || dir == MSCR_UP) |
| 1127 | { |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 1128 | if (mouse_vert_step < 0 |
| 1129 | || mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 1130 | step = (long)(curwin->w_botline - curwin->w_topline); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1131 | else |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 1132 | step = mouse_vert_step; |
| 1133 | scroll_redraw(dir, step); |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 1134 | # ifdef FEAT_PROP_POPUP |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1135 | if (WIN_IS_POPUP(curwin)) |
| 1136 | popup_set_firstline(curwin); |
| 1137 | # endif |
| 1138 | } |
| 1139 | #ifdef FEAT_GUI |
| 1140 | else |
| 1141 | { |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 1142 | int val; |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1143 | |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 1144 | if (mouse_hor_step < 0 |
| 1145 | || mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1146 | step = curwin->w_width; |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 1147 | else |
| 1148 | step = mouse_hor_step; |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1149 | val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step); |
| 1150 | if (val < 0) |
| 1151 | val = 0; |
| 1152 | gui_do_horiz_scroll(val, TRUE); |
| 1153 | } |
| 1154 | #endif |
| 1155 | did_scroll = TRUE; |
LemonBoy | 66e13ae | 2022-04-21 22:52:11 +0100 | [diff] [blame] | 1156 | may_trigger_winscrolled(); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | curwin->w_redr_status = TRUE; |
| 1160 | |
| 1161 | curwin = old_curwin; |
| 1162 | curbuf = curwin->w_buffer; |
| 1163 | |
| 1164 | // The popup menu may overlay the window, need to redraw it. |
| 1165 | // TODO: Would be more efficient to only redraw the windows that are |
| 1166 | // overlapped by the popup menu. |
| 1167 | if (pum_visible() && did_scroll) |
| 1168 | { |
| 1169 | redraw_all_later(NOT_VALID); |
| 1170 | ins_compl_show_pum(); |
| 1171 | } |
| 1172 | |
| 1173 | if (!EQUAL_POS(curwin->w_cursor, tpos)) |
| 1174 | { |
| 1175 | start_arrow(&tpos); |
| 1176 | # ifdef FEAT_CINDENT |
| 1177 | set_can_cindent(TRUE); |
| 1178 | # endif |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | * Return TRUE if "c" is a mouse key. |
| 1184 | */ |
| 1185 | int |
| 1186 | is_mouse_key(int c) |
| 1187 | { |
| 1188 | return c == K_LEFTMOUSE |
| 1189 | || c == K_LEFTMOUSE_NM |
| 1190 | || c == K_LEFTDRAG |
| 1191 | || c == K_LEFTRELEASE |
| 1192 | || c == K_LEFTRELEASE_NM |
| 1193 | || c == K_MOUSEMOVE |
| 1194 | || c == K_MIDDLEMOUSE |
| 1195 | || c == K_MIDDLEDRAG |
| 1196 | || c == K_MIDDLERELEASE |
| 1197 | || c == K_RIGHTMOUSE |
| 1198 | || c == K_RIGHTDRAG |
| 1199 | || c == K_RIGHTRELEASE |
| 1200 | || c == K_MOUSEDOWN |
| 1201 | || c == K_MOUSEUP |
| 1202 | || c == K_MOUSELEFT |
| 1203 | || c == K_MOUSERIGHT |
| 1204 | || c == K_X1MOUSE |
| 1205 | || c == K_X1DRAG |
| 1206 | || c == K_X1RELEASE |
| 1207 | || c == K_X2MOUSE |
| 1208 | || c == K_X2DRAG |
| 1209 | || c == K_X2RELEASE; |
| 1210 | } |
| 1211 | |
| 1212 | static struct mousetable |
| 1213 | { |
| 1214 | int pseudo_code; // Code for pseudo mouse event |
| 1215 | int button; // Which mouse button is it? |
| 1216 | int is_click; // Is it a mouse button click event? |
| 1217 | int is_drag; // Is it a mouse drag event? |
| 1218 | } mouse_table[] = |
| 1219 | { |
| 1220 | {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE}, |
| 1221 | #ifdef FEAT_GUI |
| 1222 | {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE}, |
| 1223 | #endif |
| 1224 | {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE}, |
| 1225 | {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE}, |
| 1226 | #ifdef FEAT_GUI |
| 1227 | {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE}, |
| 1228 | #endif |
| 1229 | {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE}, |
| 1230 | {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE}, |
| 1231 | {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE}, |
| 1232 | {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE}, |
| 1233 | {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE}, |
| 1234 | {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE}, |
| 1235 | {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE}, |
| 1236 | {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE}, |
| 1237 | {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE}, |
| 1238 | {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE}, |
| 1239 | {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE}, |
| 1240 | {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE}, |
| 1241 | // DRAG without CLICK |
| 1242 | {(int)KE_MOUSEMOVE, MOUSE_RELEASE, FALSE, TRUE}, |
| 1243 | // RELEASE without CLICK |
| 1244 | {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE}, |
| 1245 | {0, 0, 0, 0}, |
| 1246 | }; |
| 1247 | |
| 1248 | /* |
| 1249 | * Look up the given mouse code to return the relevant information in the other |
| 1250 | * arguments. Return which button is down or was released. |
| 1251 | */ |
| 1252 | int |
| 1253 | get_mouse_button(int code, int *is_click, int *is_drag) |
| 1254 | { |
| 1255 | int i; |
| 1256 | |
| 1257 | for (i = 0; mouse_table[i].pseudo_code; i++) |
| 1258 | if (code == mouse_table[i].pseudo_code) |
| 1259 | { |
| 1260 | *is_click = mouse_table[i].is_click; |
| 1261 | *is_drag = mouse_table[i].is_drag; |
| 1262 | return mouse_table[i].button; |
| 1263 | } |
| 1264 | return 0; // Shouldn't get here |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on |
| 1269 | * the given information about which mouse button is down, and whether the |
| 1270 | * mouse was clicked, dragged or released. |
| 1271 | */ |
| 1272 | int |
| 1273 | get_pseudo_mouse_code( |
| 1274 | int button, // eg MOUSE_LEFT |
| 1275 | int is_click, |
| 1276 | int is_drag) |
| 1277 | { |
| 1278 | int i; |
| 1279 | |
| 1280 | for (i = 0; mouse_table[i].pseudo_code; i++) |
| 1281 | if (button == mouse_table[i].button |
| 1282 | && is_click == mouse_table[i].is_click |
| 1283 | && is_drag == mouse_table[i].is_drag) |
| 1284 | { |
| 1285 | #ifdef FEAT_GUI |
| 1286 | // Trick: a non mappable left click and release has mouse_col -1 |
| 1287 | // or added MOUSE_COLOFF. Used for 'mousefocus' in |
| 1288 | // gui_mouse_moved() |
| 1289 | if (mouse_col < 0 || mouse_col > MOUSE_COLOFF) |
| 1290 | { |
| 1291 | if (mouse_col < 0) |
| 1292 | mouse_col = 0; |
| 1293 | else |
| 1294 | mouse_col -= MOUSE_COLOFF; |
| 1295 | if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE) |
| 1296 | return (int)KE_LEFTMOUSE_NM; |
| 1297 | if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE) |
| 1298 | return (int)KE_LEFTRELEASE_NM; |
| 1299 | } |
| 1300 | #endif |
| 1301 | return mouse_table[i].pseudo_code; |
| 1302 | } |
| 1303 | return (int)KE_IGNORE; // not recognized, ignore it |
| 1304 | } |
| 1305 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 1306 | # define HMT_NORMAL 1 |
| 1307 | # define HMT_NETTERM 2 |
| 1308 | # define HMT_DEC 4 |
| 1309 | # define HMT_JSBTERM 8 |
| 1310 | # define HMT_PTERM 16 |
| 1311 | # define HMT_URXVT 32 |
| 1312 | # define HMT_GPM 64 |
| 1313 | # define HMT_SGR 128 |
| 1314 | # define HMT_SGR_REL 256 |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1315 | static int has_mouse_termcode = 0; |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1316 | |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1317 | void |
| 1318 | set_mouse_termcode( |
| 1319 | int n, // KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE |
| 1320 | char_u *s) |
| 1321 | { |
| 1322 | char_u name[2]; |
| 1323 | |
| 1324 | name[0] = n; |
| 1325 | name[1] = KE_FILLER; |
| 1326 | add_termcode(name, s, FALSE); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1327 | # ifdef FEAT_MOUSE_JSB |
| 1328 | if (n == KS_JSBTERM_MOUSE) |
| 1329 | has_mouse_termcode |= HMT_JSBTERM; |
| 1330 | else |
| 1331 | # endif |
| 1332 | # ifdef FEAT_MOUSE_NET |
| 1333 | if (n == KS_NETTERM_MOUSE) |
| 1334 | has_mouse_termcode |= HMT_NETTERM; |
| 1335 | else |
| 1336 | # endif |
| 1337 | # ifdef FEAT_MOUSE_DEC |
| 1338 | if (n == KS_DEC_MOUSE) |
| 1339 | has_mouse_termcode |= HMT_DEC; |
| 1340 | else |
| 1341 | # endif |
| 1342 | # ifdef FEAT_MOUSE_PTERM |
| 1343 | if (n == KS_PTERM_MOUSE) |
| 1344 | has_mouse_termcode |= HMT_PTERM; |
| 1345 | else |
| 1346 | # endif |
| 1347 | # ifdef FEAT_MOUSE_URXVT |
| 1348 | if (n == KS_URXVT_MOUSE) |
| 1349 | has_mouse_termcode |= HMT_URXVT; |
| 1350 | else |
| 1351 | # endif |
| 1352 | # ifdef FEAT_MOUSE_GPM |
| 1353 | if (n == KS_GPM_MOUSE) |
| 1354 | has_mouse_termcode |= HMT_GPM; |
| 1355 | else |
| 1356 | # endif |
| 1357 | if (n == KS_SGR_MOUSE) |
| 1358 | has_mouse_termcode |= HMT_SGR; |
| 1359 | else if (n == KS_SGR_MOUSE_RELEASE) |
| 1360 | has_mouse_termcode |= HMT_SGR_REL; |
| 1361 | else |
| 1362 | has_mouse_termcode |= HMT_NORMAL; |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1363 | } |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1364 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 1365 | # if defined(UNIX) || defined(VMS) || defined(PROTO) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1366 | void |
| 1367 | del_mouse_termcode( |
| 1368 | int n) // KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE |
| 1369 | { |
| 1370 | char_u name[2]; |
| 1371 | |
| 1372 | name[0] = n; |
| 1373 | name[1] = KE_FILLER; |
| 1374 | del_termcode(name); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1375 | # ifdef FEAT_MOUSE_JSB |
| 1376 | if (n == KS_JSBTERM_MOUSE) |
| 1377 | has_mouse_termcode &= ~HMT_JSBTERM; |
| 1378 | else |
| 1379 | # endif |
| 1380 | # ifdef FEAT_MOUSE_NET |
| 1381 | if (n == KS_NETTERM_MOUSE) |
| 1382 | has_mouse_termcode &= ~HMT_NETTERM; |
| 1383 | else |
| 1384 | # endif |
| 1385 | # ifdef FEAT_MOUSE_DEC |
| 1386 | if (n == KS_DEC_MOUSE) |
| 1387 | has_mouse_termcode &= ~HMT_DEC; |
| 1388 | else |
| 1389 | # endif |
| 1390 | # ifdef FEAT_MOUSE_PTERM |
| 1391 | if (n == KS_PTERM_MOUSE) |
| 1392 | has_mouse_termcode &= ~HMT_PTERM; |
| 1393 | else |
| 1394 | # endif |
| 1395 | # ifdef FEAT_MOUSE_URXVT |
| 1396 | if (n == KS_URXVT_MOUSE) |
| 1397 | has_mouse_termcode &= ~HMT_URXVT; |
| 1398 | else |
| 1399 | # endif |
| 1400 | # ifdef FEAT_MOUSE_GPM |
| 1401 | if (n == KS_GPM_MOUSE) |
| 1402 | has_mouse_termcode &= ~HMT_GPM; |
| 1403 | else |
| 1404 | # endif |
| 1405 | if (n == KS_SGR_MOUSE) |
| 1406 | has_mouse_termcode &= ~HMT_SGR; |
| 1407 | else if (n == KS_SGR_MOUSE_RELEASE) |
| 1408 | has_mouse_termcode &= ~HMT_SGR_REL; |
| 1409 | else |
| 1410 | has_mouse_termcode &= ~HMT_NORMAL; |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1411 | } |
| 1412 | # endif |
| 1413 | |
| 1414 | /* |
| 1415 | * setmouse() - switch mouse on/off depending on current mode and 'mouse' |
| 1416 | */ |
| 1417 | void |
| 1418 | setmouse(void) |
| 1419 | { |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1420 | int checkfor; |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1421 | |
| 1422 | # ifdef FEAT_MOUSESHAPE |
| 1423 | update_mouseshape(-1); |
| 1424 | # endif |
| 1425 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 1426 | // Should be outside proc, but may break MOUSESHAPE |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1427 | # ifdef FEAT_GUI |
| 1428 | // In the GUI the mouse is always enabled. |
| 1429 | if (gui.in_use) |
| 1430 | return; |
| 1431 | # endif |
| 1432 | // be quick when mouse is off |
| 1433 | if (*p_mouse == NUL || has_mouse_termcode == 0) |
| 1434 | return; |
| 1435 | |
| 1436 | // don't switch mouse on when not in raw mode (Ex mode) |
| 1437 | if (cur_tmode != TMODE_RAW) |
| 1438 | { |
| 1439 | mch_setmouse(FALSE); |
| 1440 | return; |
| 1441 | } |
| 1442 | |
| 1443 | if (VIsual_active) |
| 1444 | checkfor = MOUSE_VISUAL; |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1445 | else if (State == MODE_HITRETURN || State == MODE_ASKMORE |
| 1446 | || State == MODE_SETWSIZE) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1447 | checkfor = MOUSE_RETURN; |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1448 | else if (State & MODE_INSERT) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1449 | checkfor = MOUSE_INSERT; |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1450 | else if (State & MODE_CMDLINE) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1451 | checkfor = MOUSE_COMMAND; |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1452 | else if (State == MODE_CONFIRM || State == MODE_EXTERNCMD) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1453 | checkfor = ' '; // don't use mouse for ":confirm" or ":!cmd" |
| 1454 | else |
| 1455 | checkfor = MOUSE_NORMAL; // assume normal mode |
| 1456 | |
| 1457 | if (mouse_has(checkfor)) |
| 1458 | mch_setmouse(TRUE); |
| 1459 | else |
| 1460 | mch_setmouse(FALSE); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * Return TRUE if |
| 1465 | * - "c" is in 'mouse', or |
| 1466 | * - 'a' is in 'mouse' and "c" is in MOUSE_A, or |
| 1467 | * - the current buffer is a help file and 'h' is in 'mouse' and we are in a |
| 1468 | * normal editing mode (not at hit-return message). |
| 1469 | */ |
| 1470 | int |
| 1471 | mouse_has(int c) |
| 1472 | { |
| 1473 | char_u *p; |
| 1474 | |
| 1475 | for (p = p_mouse; *p; ++p) |
| 1476 | switch (*p) |
| 1477 | { |
| 1478 | case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL) |
| 1479 | return TRUE; |
| 1480 | break; |
| 1481 | case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help) |
| 1482 | return TRUE; |
| 1483 | break; |
| 1484 | default: if (c == *p) return TRUE; break; |
| 1485 | } |
| 1486 | return FALSE; |
| 1487 | } |
| 1488 | |
| 1489 | /* |
| 1490 | * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos". |
| 1491 | */ |
| 1492 | int |
| 1493 | mouse_model_popup(void) |
| 1494 | { |
| 1495 | return (p_mousem[0] == 'p'); |
| 1496 | } |
| 1497 | |
| 1498 | /* |
| 1499 | * Move the cursor to the specified row and column on the screen. |
| 1500 | * Change current window if necessary. Returns an integer with the |
| 1501 | * CURSOR_MOVED bit set if the cursor has moved or unset otherwise. |
| 1502 | * |
| 1503 | * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column. |
| 1504 | * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column. |
| 1505 | * |
| 1506 | * If flags has MOUSE_FOCUS, then the current window will not be changed, and |
| 1507 | * if the mouse is outside the window then the text will scroll, or if the |
| 1508 | * mouse was previously on a status line, then the status line may be dragged. |
| 1509 | * |
| 1510 | * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the |
| 1511 | * cursor is moved unless the cursor was on a status line. |
| 1512 | * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or |
| 1513 | * IN_SEP_LINE depending on where the cursor was clicked. |
| 1514 | * |
| 1515 | * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless |
| 1516 | * the mouse is on the status line of the same window. |
| 1517 | * |
| 1518 | * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since |
| 1519 | * the last call. |
| 1520 | * |
| 1521 | * If flags has MOUSE_SETPOS, nothing is done, only the current position is |
| 1522 | * remembered. |
| 1523 | */ |
| 1524 | int |
| 1525 | jump_to_mouse( |
| 1526 | int flags, |
| 1527 | int *inclusive, // used for inclusive operator, can be NULL |
| 1528 | int which_button) // MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE |
| 1529 | { |
| 1530 | static int on_status_line = 0; // #lines below bottom of window |
| 1531 | static int on_sep_line = 0; // on separator right of window |
| 1532 | #ifdef FEAT_MENU |
| 1533 | static int in_winbar = FALSE; |
| 1534 | #endif |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 1535 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1536 | static int in_popup_win = FALSE; |
| 1537 | static win_T *click_in_popup_win = NULL; |
| 1538 | #endif |
| 1539 | static int prev_row = -1; |
| 1540 | static int prev_col = -1; |
| 1541 | static win_T *dragwin = NULL; // window being dragged |
| 1542 | static int did_drag = FALSE; // drag was noticed |
| 1543 | |
| 1544 | win_T *wp, *old_curwin; |
| 1545 | pos_T old_cursor; |
| 1546 | int count; |
| 1547 | int first; |
| 1548 | int row = mouse_row; |
| 1549 | int col = mouse_col; |
| 1550 | #ifdef FEAT_FOLDING |
| 1551 | int mouse_char; |
| 1552 | #endif |
| 1553 | |
| 1554 | mouse_past_bottom = FALSE; |
| 1555 | mouse_past_eol = FALSE; |
| 1556 | |
| 1557 | if (flags & MOUSE_RELEASED) |
| 1558 | { |
| 1559 | // On button release we may change window focus if positioned on a |
| 1560 | // status line and no dragging happened. |
| 1561 | if (dragwin != NULL && !did_drag) |
| 1562 | flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE); |
| 1563 | dragwin = NULL; |
| 1564 | did_drag = FALSE; |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 1565 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1566 | if (click_in_popup_win != NULL && popup_dragwin == NULL) |
| 1567 | popup_close_for_mouse_click(click_in_popup_win); |
| 1568 | |
| 1569 | popup_dragwin = NULL; |
| 1570 | click_in_popup_win = NULL; |
| 1571 | #endif |
| 1572 | } |
| 1573 | |
| 1574 | if ((flags & MOUSE_DID_MOVE) |
| 1575 | && prev_row == mouse_row |
| 1576 | && prev_col == mouse_col) |
| 1577 | { |
| 1578 | retnomove: |
| 1579 | // before moving the cursor for a left click which is NOT in a status |
| 1580 | // line, stop Visual mode |
| 1581 | if (on_status_line) |
| 1582 | return IN_STATUS_LINE; |
| 1583 | if (on_sep_line) |
| 1584 | return IN_SEP_LINE; |
| 1585 | #ifdef FEAT_MENU |
| 1586 | if (in_winbar) |
| 1587 | { |
| 1588 | // A quick second click may arrive as a double-click, but we use it |
| 1589 | // as a second click in the WinBar. |
| 1590 | if ((mod_mask & MOD_MASK_MULTI_CLICK) && !(flags & MOUSE_RELEASED)) |
| 1591 | { |
| 1592 | wp = mouse_find_win(&row, &col, FAIL_POPUP); |
| 1593 | if (wp == NULL) |
| 1594 | return IN_UNKNOWN; |
| 1595 | winbar_click(wp, col); |
| 1596 | } |
| 1597 | return IN_OTHER_WIN | MOUSE_WINBAR; |
| 1598 | } |
| 1599 | #endif |
| 1600 | if (flags & MOUSE_MAY_STOP_VIS) |
| 1601 | { |
Bram Moolenaar | 4f3c57f | 2021-06-03 22:11:08 +0200 | [diff] [blame] | 1602 | end_visual_mode_keep_button(); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1603 | redraw_curbuf_later(INVERTED); // delete the inversion |
| 1604 | } |
| 1605 | #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD) |
| 1606 | // Continue a modeless selection in another window. |
| 1607 | if (cmdwin_type != 0 && row < curwin->w_winrow) |
| 1608 | return IN_OTHER_WIN; |
| 1609 | #endif |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 1610 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1611 | // Continue a modeless selection in a popup window or dragging it. |
| 1612 | if (in_popup_win) |
| 1613 | { |
| 1614 | click_in_popup_win = NULL; // don't close it on release |
| 1615 | if (popup_dragwin != NULL) |
| 1616 | { |
| 1617 | // dragging a popup window |
| 1618 | popup_drag(popup_dragwin); |
| 1619 | return IN_UNKNOWN; |
| 1620 | } |
| 1621 | return IN_OTHER_WIN; |
| 1622 | } |
| 1623 | #endif |
| 1624 | return IN_BUFFER; |
| 1625 | } |
| 1626 | |
| 1627 | prev_row = mouse_row; |
| 1628 | prev_col = mouse_col; |
| 1629 | |
| 1630 | if (flags & MOUSE_SETPOS) |
| 1631 | goto retnomove; // ugly goto... |
| 1632 | |
| 1633 | #ifdef FEAT_FOLDING |
| 1634 | // Remember the character under the mouse, it might be a '-' or '+' in the |
| 1635 | // fold column. |
| 1636 | if (row >= 0 && row < Rows && col >= 0 && col <= Columns |
| 1637 | && ScreenLines != NULL) |
| 1638 | mouse_char = ScreenLines[LineOffset[row] + col]; |
| 1639 | else |
| 1640 | mouse_char = ' '; |
| 1641 | #endif |
| 1642 | |
| 1643 | old_curwin = curwin; |
| 1644 | old_cursor = curwin->w_cursor; |
| 1645 | |
| 1646 | if (!(flags & MOUSE_FOCUS)) |
| 1647 | { |
| 1648 | if (row < 0 || col < 0) // check if it makes sense |
| 1649 | return IN_UNKNOWN; |
| 1650 | |
| 1651 | // find the window where the row is in and adjust "row" and "col" to be |
| 1652 | // relative to top-left of the window |
| 1653 | wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 1654 | if (wp == NULL) |
| 1655 | return IN_UNKNOWN; |
| 1656 | dragwin = NULL; |
| 1657 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 1658 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1659 | // Click in a popup window may start dragging or modeless selection, |
| 1660 | // but not much else. |
| 1661 | if (WIN_IS_POPUP(wp)) |
| 1662 | { |
| 1663 | on_sep_line = 0; |
Bram Moolenaar | bfc5786 | 2021-11-26 15:57:40 +0000 | [diff] [blame] | 1664 | on_status_line = 0; |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1665 | in_popup_win = TRUE; |
| 1666 | if (which_button == MOUSE_LEFT && popup_close_if_on_X(wp, row, col)) |
| 1667 | { |
| 1668 | return IN_UNKNOWN; |
| 1669 | } |
Bram Moolenaar | 0b74d00 | 2021-11-29 17:38:02 +0000 | [diff] [blame] | 1670 | else if (((wp->w_popup_flags & (POPF_DRAG | POPF_RESIZE)) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1671 | && popup_on_border(wp, row, col)) |
Bram Moolenaar | 0b74d00 | 2021-11-29 17:38:02 +0000 | [diff] [blame] | 1672 | || (wp->w_popup_flags & POPF_DRAGALL)) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1673 | { |
| 1674 | popup_dragwin = wp; |
| 1675 | popup_start_drag(wp, row, col); |
| 1676 | return IN_UNKNOWN; |
| 1677 | } |
| 1678 | // Only close on release, otherwise it's not possible to drag or do |
| 1679 | // modeless selection. |
| 1680 | else if (wp->w_popup_close == POPCLOSE_CLICK |
| 1681 | && which_button == MOUSE_LEFT) |
| 1682 | { |
| 1683 | click_in_popup_win = wp; |
| 1684 | } |
| 1685 | else if (which_button == MOUSE_LEFT) |
| 1686 | // If the click is in the scrollbar, may scroll up/down. |
| 1687 | popup_handle_scrollbar_click(wp, row, col); |
| 1688 | # ifdef FEAT_CLIPBOARD |
| 1689 | return IN_OTHER_WIN; |
| 1690 | # else |
| 1691 | return IN_UNKNOWN; |
| 1692 | # endif |
| 1693 | } |
| 1694 | in_popup_win = FALSE; |
| 1695 | popup_dragwin = NULL; |
| 1696 | #endif |
| 1697 | #ifdef FEAT_MENU |
| 1698 | if (row == -1) |
| 1699 | { |
| 1700 | // A click in the window toolbar does not enter another window or |
| 1701 | // change Visual highlighting. |
| 1702 | winbar_click(wp, col); |
| 1703 | in_winbar = TRUE; |
| 1704 | return IN_OTHER_WIN | MOUSE_WINBAR; |
| 1705 | } |
| 1706 | in_winbar = FALSE; |
| 1707 | #endif |
| 1708 | |
| 1709 | // winpos and height may change in win_enter()! |
| 1710 | if (row >= wp->w_height) // In (or below) status line |
| 1711 | { |
| 1712 | on_status_line = row - wp->w_height + 1; |
| 1713 | dragwin = wp; |
| 1714 | } |
| 1715 | else |
| 1716 | on_status_line = 0; |
| 1717 | if (col >= wp->w_width) // In separator line |
| 1718 | { |
| 1719 | on_sep_line = col - wp->w_width + 1; |
| 1720 | dragwin = wp; |
| 1721 | } |
| 1722 | else |
| 1723 | on_sep_line = 0; |
| 1724 | |
| 1725 | // The rightmost character of the status line might be a vertical |
| 1726 | // separator character if there is no connecting window to the right. |
| 1727 | if (on_status_line && on_sep_line) |
| 1728 | { |
| 1729 | if (stl_connected(wp)) |
| 1730 | on_sep_line = 0; |
| 1731 | else |
| 1732 | on_status_line = 0; |
| 1733 | } |
| 1734 | |
| 1735 | // Before jumping to another buffer, or moving the cursor for a left |
| 1736 | // click, stop Visual mode. |
| 1737 | if (VIsual_active |
| 1738 | && (wp->w_buffer != curwin->w_buffer |
| 1739 | || (!on_status_line && !on_sep_line |
| 1740 | #ifdef FEAT_FOLDING |
| 1741 | && ( |
| 1742 | # ifdef FEAT_RIGHTLEFT |
| 1743 | wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc : |
| 1744 | # endif |
| 1745 | col >= wp->w_p_fdc |
| 1746 | # ifdef FEAT_CMDWIN |
| 1747 | + (cmdwin_type == 0 && wp == curwin ? 0 : 1) |
| 1748 | # endif |
| 1749 | ) |
| 1750 | #endif |
| 1751 | && (flags & MOUSE_MAY_STOP_VIS)))) |
| 1752 | { |
Bram Moolenaar | 4f3c57f | 2021-06-03 22:11:08 +0200 | [diff] [blame] | 1753 | end_visual_mode_keep_button(); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1754 | redraw_curbuf_later(INVERTED); // delete the inversion |
| 1755 | } |
| 1756 | #ifdef FEAT_CMDWIN |
| 1757 | if (cmdwin_type != 0 && wp != curwin) |
| 1758 | { |
| 1759 | // A click outside the command-line window: Use modeless |
| 1760 | // selection if possible. Allow dragging the status lines. |
| 1761 | on_sep_line = 0; |
| 1762 | # ifdef FEAT_CLIPBOARD |
| 1763 | if (on_status_line) |
| 1764 | return IN_STATUS_LINE; |
| 1765 | return IN_OTHER_WIN; |
| 1766 | # else |
| 1767 | row = 0; |
| 1768 | col += wp->w_wincol; |
| 1769 | wp = curwin; |
| 1770 | # endif |
| 1771 | } |
| 1772 | #endif |
Bram Moolenaar | 219c7d0 | 2020-02-01 21:57:29 +0100 | [diff] [blame] | 1773 | #if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL) |
| 1774 | if (popup_is_popup(curwin) && curbuf->b_term != NULL) |
| 1775 | // terminal in popup window: don't jump to another window |
| 1776 | return IN_OTHER_WIN; |
| 1777 | #endif |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1778 | // Only change window focus when not clicking on or dragging the |
| 1779 | // status line. Do change focus when releasing the mouse button |
| 1780 | // (MOUSE_FOCUS was set above if we dragged first). |
| 1781 | if (dragwin == NULL || (flags & MOUSE_RELEASED)) |
| 1782 | win_enter(wp, TRUE); // can make wp invalid! |
| 1783 | |
| 1784 | if (curwin != old_curwin) |
| 1785 | { |
| 1786 | #ifdef CHECK_DOUBLE_CLICK |
| 1787 | // set topline, to be able to check for double click ourselves |
| 1788 | set_mouse_topline(curwin); |
| 1789 | #endif |
| 1790 | #ifdef FEAT_TERMINAL |
| 1791 | // when entering a terminal window may change state |
| 1792 | term_win_entered(); |
| 1793 | #endif |
| 1794 | } |
| 1795 | if (on_status_line) // In (or below) status line |
| 1796 | { |
| 1797 | // Don't use start_arrow() if we're in the same window |
| 1798 | if (curwin == old_curwin) |
| 1799 | return IN_STATUS_LINE; |
| 1800 | else |
| 1801 | return IN_STATUS_LINE | CURSOR_MOVED; |
| 1802 | } |
| 1803 | if (on_sep_line) // In (or below) status line |
| 1804 | { |
| 1805 | // Don't use start_arrow() if we're in the same window |
| 1806 | if (curwin == old_curwin) |
| 1807 | return IN_SEP_LINE; |
| 1808 | else |
| 1809 | return IN_SEP_LINE | CURSOR_MOVED; |
| 1810 | } |
| 1811 | |
| 1812 | curwin->w_cursor.lnum = curwin->w_topline; |
| 1813 | #ifdef FEAT_GUI |
| 1814 | // remember topline, needed for double click |
| 1815 | gui_prev_topline = curwin->w_topline; |
| 1816 | # ifdef FEAT_DIFF |
| 1817 | gui_prev_topfill = curwin->w_topfill; |
| 1818 | # endif |
| 1819 | #endif |
| 1820 | } |
| 1821 | else if (on_status_line && which_button == MOUSE_LEFT) |
| 1822 | { |
| 1823 | if (dragwin != NULL) |
| 1824 | { |
| 1825 | // Drag the status line |
| 1826 | count = row - dragwin->w_winrow - dragwin->w_height + 1 |
| 1827 | - on_status_line; |
| 1828 | win_drag_status_line(dragwin, count); |
| 1829 | did_drag |= count; |
| 1830 | } |
| 1831 | return IN_STATUS_LINE; // Cursor didn't move |
| 1832 | } |
| 1833 | else if (on_sep_line && which_button == MOUSE_LEFT) |
| 1834 | { |
| 1835 | if (dragwin != NULL) |
| 1836 | { |
| 1837 | // Drag the separator column |
| 1838 | count = col - dragwin->w_wincol - dragwin->w_width + 1 |
| 1839 | - on_sep_line; |
| 1840 | win_drag_vsep_line(dragwin, count); |
| 1841 | did_drag |= count; |
| 1842 | } |
| 1843 | return IN_SEP_LINE; // Cursor didn't move |
| 1844 | } |
| 1845 | #ifdef FEAT_MENU |
| 1846 | else if (in_winbar) |
| 1847 | { |
| 1848 | // After a click on the window toolbar don't start Visual mode. |
| 1849 | return IN_OTHER_WIN | MOUSE_WINBAR; |
| 1850 | } |
| 1851 | #endif |
| 1852 | else // keep_window_focus must be TRUE |
| 1853 | { |
| 1854 | // before moving the cursor for a left click, stop Visual mode |
| 1855 | if (flags & MOUSE_MAY_STOP_VIS) |
| 1856 | { |
Bram Moolenaar | 4f3c57f | 2021-06-03 22:11:08 +0200 | [diff] [blame] | 1857 | end_visual_mode_keep_button(); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1858 | redraw_curbuf_later(INVERTED); // delete the inversion |
| 1859 | } |
| 1860 | |
| 1861 | #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD) |
| 1862 | // Continue a modeless selection in another window. |
| 1863 | if (cmdwin_type != 0 && row < curwin->w_winrow) |
| 1864 | return IN_OTHER_WIN; |
| 1865 | #endif |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 1866 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 1867 | if (in_popup_win) |
| 1868 | { |
| 1869 | if (popup_dragwin != NULL) |
| 1870 | { |
| 1871 | // dragging a popup window |
| 1872 | popup_drag(popup_dragwin); |
| 1873 | return IN_UNKNOWN; |
| 1874 | } |
| 1875 | // continue a modeless selection in a popup window |
| 1876 | click_in_popup_win = NULL; |
| 1877 | return IN_OTHER_WIN; |
| 1878 | } |
| 1879 | #endif |
| 1880 | |
| 1881 | row -= W_WINROW(curwin); |
| 1882 | col -= curwin->w_wincol; |
| 1883 | |
| 1884 | // When clicking beyond the end of the window, scroll the screen. |
| 1885 | // Scroll by however many rows outside the window we are. |
| 1886 | if (row < 0) |
| 1887 | { |
| 1888 | count = 0; |
| 1889 | for (first = TRUE; curwin->w_topline > 1; ) |
| 1890 | { |
| 1891 | #ifdef FEAT_DIFF |
| 1892 | if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)) |
| 1893 | ++count; |
| 1894 | else |
| 1895 | #endif |
| 1896 | count += plines(curwin->w_topline - 1); |
| 1897 | if (!first && count > -row) |
| 1898 | break; |
| 1899 | first = FALSE; |
| 1900 | #ifdef FEAT_FOLDING |
| 1901 | (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL); |
| 1902 | #endif |
| 1903 | #ifdef FEAT_DIFF |
| 1904 | if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)) |
| 1905 | ++curwin->w_topfill; |
| 1906 | else |
| 1907 | #endif |
| 1908 | { |
| 1909 | --curwin->w_topline; |
| 1910 | #ifdef FEAT_DIFF |
| 1911 | curwin->w_topfill = 0; |
| 1912 | #endif |
| 1913 | } |
| 1914 | } |
| 1915 | #ifdef FEAT_DIFF |
| 1916 | check_topfill(curwin, FALSE); |
| 1917 | #endif |
| 1918 | curwin->w_valid &= |
| 1919 | ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP); |
| 1920 | redraw_later(VALID); |
| 1921 | row = 0; |
| 1922 | } |
| 1923 | else if (row >= curwin->w_height) |
| 1924 | { |
| 1925 | count = 0; |
| 1926 | for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; ) |
| 1927 | { |
| 1928 | #ifdef FEAT_DIFF |
| 1929 | if (curwin->w_topfill > 0) |
| 1930 | ++count; |
| 1931 | else |
| 1932 | #endif |
| 1933 | count += plines(curwin->w_topline); |
| 1934 | if (!first && count > row - curwin->w_height + 1) |
| 1935 | break; |
| 1936 | first = FALSE; |
| 1937 | #ifdef FEAT_FOLDING |
| 1938 | if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline) |
| 1939 | && curwin->w_topline == curbuf->b_ml.ml_line_count) |
| 1940 | break; |
| 1941 | #endif |
| 1942 | #ifdef FEAT_DIFF |
| 1943 | if (curwin->w_topfill > 0) |
| 1944 | --curwin->w_topfill; |
| 1945 | else |
| 1946 | #endif |
| 1947 | { |
| 1948 | ++curwin->w_topline; |
| 1949 | #ifdef FEAT_DIFF |
| 1950 | curwin->w_topfill = |
| 1951 | diff_check_fill(curwin, curwin->w_topline); |
| 1952 | #endif |
| 1953 | } |
| 1954 | } |
| 1955 | #ifdef FEAT_DIFF |
| 1956 | check_topfill(curwin, FALSE); |
| 1957 | #endif |
| 1958 | redraw_later(VALID); |
| 1959 | curwin->w_valid &= |
| 1960 | ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP); |
| 1961 | row = curwin->w_height - 1; |
| 1962 | } |
| 1963 | else if (row == 0) |
| 1964 | { |
| 1965 | // When dragging the mouse, while the text has been scrolled up as |
| 1966 | // far as it goes, moving the mouse in the top line should scroll |
| 1967 | // the text down (done later when recomputing w_topline). |
| 1968 | if (mouse_dragging > 0 |
| 1969 | && curwin->w_cursor.lnum |
| 1970 | == curwin->w_buffer->b_ml.ml_line_count |
| 1971 | && curwin->w_cursor.lnum == curwin->w_topline) |
| 1972 | curwin->w_valid &= ~(VALID_TOPLINE); |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | #ifdef FEAT_FOLDING |
| 1977 | // Check for position outside of the fold column. |
| 1978 | if ( |
| 1979 | # ifdef FEAT_RIGHTLEFT |
| 1980 | curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc : |
| 1981 | # endif |
| 1982 | col >= curwin->w_p_fdc |
| 1983 | # ifdef FEAT_CMDWIN |
| 1984 | + (cmdwin_type == 0 ? 0 : 1) |
| 1985 | # endif |
| 1986 | ) |
| 1987 | mouse_char = ' '; |
| 1988 | #endif |
| 1989 | |
| 1990 | // compute the position in the buffer line from the posn on the screen |
| 1991 | if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum, NULL)) |
| 1992 | mouse_past_bottom = TRUE; |
| 1993 | |
| 1994 | // Start Visual mode before coladvance(), for when 'sel' != "old" |
| 1995 | if ((flags & MOUSE_MAY_VIS) && !VIsual_active) |
| 1996 | { |
| 1997 | check_visual_highlight(); |
| 1998 | VIsual = old_cursor; |
| 1999 | VIsual_active = TRUE; |
| 2000 | VIsual_reselect = TRUE; |
| 2001 | // if 'selectmode' contains "mouse", start Select mode |
| 2002 | may_start_select('o'); |
| 2003 | setmouse(); |
| 2004 | if (p_smd && msg_silent == 0) |
| 2005 | redraw_cmdline = TRUE; // show visual mode later |
| 2006 | } |
| 2007 | |
| 2008 | curwin->w_curswant = col; |
| 2009 | curwin->w_set_curswant = FALSE; // May still have been TRUE |
| 2010 | if (coladvance(col) == FAIL) // Mouse click beyond end of line |
| 2011 | { |
| 2012 | if (inclusive != NULL) |
| 2013 | *inclusive = TRUE; |
| 2014 | mouse_past_eol = TRUE; |
| 2015 | } |
| 2016 | else if (inclusive != NULL) |
| 2017 | *inclusive = FALSE; |
| 2018 | |
| 2019 | count = IN_BUFFER; |
| 2020 | if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum |
| 2021 | || curwin->w_cursor.col != old_cursor.col) |
| 2022 | count |= CURSOR_MOVED; // Cursor has moved |
| 2023 | |
| 2024 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 3aca5a6 | 2021-02-17 13:14:07 +0100 | [diff] [blame] | 2025 | if (mouse_char == fill_foldclosed) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2026 | count |= MOUSE_FOLD_OPEN; |
| 2027 | else if (mouse_char != ' ') |
| 2028 | count |= MOUSE_FOLD_CLOSE; |
| 2029 | # endif |
| 2030 | |
| 2031 | return count; |
| 2032 | } |
| 2033 | |
| 2034 | /* |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 2035 | * Mouse scroll wheel: Default action is to scroll mouse_vert_step lines (or |
| 2036 | * mouse_hor_step, depending on the scroll direction), or one page when Shift or |
| 2037 | * Ctrl is used. |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2038 | * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or |
| 2039 | * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2) |
| 2040 | */ |
| 2041 | void |
| 2042 | nv_mousescroll(cmdarg_T *cap) |
| 2043 | { |
| 2044 | win_T *old_curwin = curwin, *wp; |
| 2045 | |
| 2046 | if (mouse_row >= 0 && mouse_col >= 0) |
| 2047 | { |
| 2048 | int row, col; |
| 2049 | |
| 2050 | row = mouse_row; |
| 2051 | col = mouse_col; |
| 2052 | |
| 2053 | // find the window at the pointer coordinates |
| 2054 | wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 2055 | if (wp == NULL) |
| 2056 | return; |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 2057 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2058 | if (WIN_IS_POPUP(wp) && !wp->w_has_scrollbar) |
| 2059 | return; |
| 2060 | #endif |
| 2061 | curwin = wp; |
| 2062 | curbuf = curwin->w_buffer; |
| 2063 | } |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2064 | if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN) |
| 2065 | { |
| 2066 | # ifdef FEAT_TERMINAL |
| 2067 | if (term_use_loop()) |
| 2068 | // This window is a terminal window, send the mouse event there. |
| 2069 | // Set "typed" to FALSE to avoid an endless loop. |
Bram Moolenaar | 1e814bc | 2019-11-03 21:19:41 +0100 | [diff] [blame] | 2070 | send_keys_to_term(curbuf->b_term, cap->cmdchar, mod_mask, FALSE); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2071 | else |
| 2072 | # endif |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 2073 | if (mouse_vert_step < 0 || mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2074 | { |
| 2075 | (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L); |
| 2076 | } |
| 2077 | else |
| 2078 | { |
| 2079 | // Don't scroll more than half the window height. |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 2080 | if (curwin->w_height < mouse_vert_step * 2) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2081 | { |
| 2082 | cap->count1 = curwin->w_height / 2; |
| 2083 | if (cap->count1 == 0) |
| 2084 | cap->count1 = 1; |
| 2085 | } |
| 2086 | else |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 2087 | cap->count1 = mouse_vert_step; |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2088 | cap->count0 = cap->count1; |
| 2089 | nv_scroll_line(cap); |
| 2090 | } |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 2091 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2092 | if (WIN_IS_POPUP(curwin)) |
| 2093 | popup_set_firstline(curwin); |
| 2094 | #endif |
| 2095 | } |
| 2096 | # ifdef FEAT_GUI |
| 2097 | else |
| 2098 | { |
| 2099 | // Horizontal scroll - only allowed when 'wrap' is disabled |
| 2100 | if (!curwin->w_p_wrap) |
| 2101 | { |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 2102 | int val, step; |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2103 | |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 2104 | if (mouse_hor_step < 0 |
| 2105 | || mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2106 | step = curwin->w_width; |
LemonBoy | c27747e | 2022-05-07 12:25:40 +0100 | [diff] [blame] | 2107 | else |
| 2108 | step = mouse_hor_step; |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2109 | val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step); |
| 2110 | if (val < 0) |
| 2111 | val = 0; |
| 2112 | |
| 2113 | gui_do_horiz_scroll(val, TRUE); |
| 2114 | } |
| 2115 | } |
| 2116 | # endif |
| 2117 | # ifdef FEAT_SYN_HL |
| 2118 | if (curwin != old_curwin && curwin->w_p_cul) |
| 2119 | redraw_for_cursorline(curwin); |
| 2120 | # endif |
LemonBoy | 66e13ae | 2022-04-21 22:52:11 +0100 | [diff] [blame] | 2121 | may_trigger_winscrolled(); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2122 | |
| 2123 | curwin->w_redr_status = TRUE; |
| 2124 | |
| 2125 | curwin = old_curwin; |
| 2126 | curbuf = curwin->w_buffer; |
| 2127 | } |
| 2128 | |
| 2129 | /* |
| 2130 | * Mouse clicks and drags. |
| 2131 | */ |
| 2132 | void |
| 2133 | nv_mouse(cmdarg_T *cap) |
| 2134 | { |
| 2135 | (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0); |
| 2136 | } |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2137 | |
Bram Moolenaar | 85eee5b | 2021-06-03 20:34:57 +0200 | [diff] [blame] | 2138 | static int held_button = MOUSE_RELEASE; |
| 2139 | |
| 2140 | void |
| 2141 | reset_held_button() |
| 2142 | { |
| 2143 | held_button = MOUSE_RELEASE; |
| 2144 | } |
| 2145 | |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2146 | /* |
| 2147 | * Check if typebuf 'tp' contains a terminal mouse code and returns the |
| 2148 | * modifiers found in typebuf in 'modifiers'. |
| 2149 | */ |
| 2150 | int |
| 2151 | check_termcode_mouse( |
| 2152 | char_u *tp, |
| 2153 | int *slen, |
| 2154 | char_u *key_name, |
| 2155 | char_u *modifiers_start, |
| 2156 | int idx, |
| 2157 | int *modifiers) |
| 2158 | { |
| 2159 | int j; |
| 2160 | char_u *p; |
| 2161 | # if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \ |
| 2162 | || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE) |
| 2163 | char_u bytes[6]; |
| 2164 | int num_bytes; |
| 2165 | # endif |
| 2166 | int mouse_code = 0; // init for GCC |
| 2167 | int is_click, is_drag; |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2168 | int is_release, release_is_ambiguous; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2169 | int wheel_code = 0; |
| 2170 | int current_button; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2171 | static int orig_num_clicks = 1; |
| 2172 | static int orig_mouse_code = 0x0; |
| 2173 | # ifdef CHECK_DOUBLE_CLICK |
| 2174 | static int orig_mouse_col = 0; |
| 2175 | static int orig_mouse_row = 0; |
| 2176 | static struct timeval orig_mouse_time = {0, 0}; |
| 2177 | // time of previous mouse click |
| 2178 | struct timeval mouse_time; // time of current mouse click |
| 2179 | long timediff; // elapsed time in msec |
| 2180 | # endif |
| 2181 | |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2182 | is_click = is_drag = is_release = release_is_ambiguous = FALSE; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2183 | |
| 2184 | # if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \ |
| 2185 | || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE) |
| 2186 | if (key_name[0] == KS_MOUSE |
| 2187 | # ifdef FEAT_MOUSE_GPM |
| 2188 | || key_name[0] == KS_GPM_MOUSE |
| 2189 | # endif |
| 2190 | ) |
| 2191 | { |
| 2192 | /* |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2193 | * For xterm we get "<t_mouse>scr", where s == encoded button state: |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2194 | * 0x20 = left button down |
| 2195 | * 0x21 = middle button down |
| 2196 | * 0x22 = right button down |
| 2197 | * 0x23 = any button release |
| 2198 | * 0x60 = button 4 down (scroll wheel down) |
| 2199 | * 0x61 = button 5 down (scroll wheel up) |
| 2200 | * add 0x04 for SHIFT |
| 2201 | * add 0x08 for ALT |
| 2202 | * add 0x10 for CTRL |
| 2203 | * add 0x20 for mouse drag (0x40 is drag with left button) |
| 2204 | * add 0x40 for mouse move (0x80 is move, 0x81 too) |
| 2205 | * 0x43 (drag + release) is also move |
| 2206 | * c == column + ' ' + 1 == column + 33 |
| 2207 | * r == row + ' ' + 1 == row + 33 |
| 2208 | * |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2209 | * The coordinates are passed on through global variables. Ugly, but |
| 2210 | * this avoids trouble with mouse clicks at an unexpected moment and |
| 2211 | * allows for mapping them. |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2212 | */ |
| 2213 | for (;;) |
| 2214 | { |
| 2215 | # ifdef FEAT_GUI |
| 2216 | if (gui.in_use) |
| 2217 | { |
| 2218 | // GUI uses more bits for columns > 223 |
| 2219 | num_bytes = get_bytes_from_buf(tp + *slen, bytes, 5); |
| 2220 | if (num_bytes == -1) // not enough coordinates |
| 2221 | return -1; |
| 2222 | mouse_code = bytes[0]; |
| 2223 | mouse_col = 128 * (bytes[1] - ' ' - 1) |
| 2224 | + bytes[2] - ' ' - 1; |
| 2225 | mouse_row = 128 * (bytes[3] - ' ' - 1) |
| 2226 | + bytes[4] - ' ' - 1; |
| 2227 | } |
| 2228 | else |
| 2229 | # endif |
| 2230 | { |
| 2231 | num_bytes = get_bytes_from_buf(tp + *slen, bytes, 3); |
| 2232 | if (num_bytes == -1) // not enough coordinates |
| 2233 | return -1; |
| 2234 | mouse_code = bytes[0]; |
| 2235 | mouse_col = bytes[1] - ' ' - 1; |
| 2236 | mouse_row = bytes[2] - ' ' - 1; |
| 2237 | } |
| 2238 | *slen += num_bytes; |
| 2239 | |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2240 | // If the following bytes is also a mouse code and it has the same |
| 2241 | // code, dump this one and get the next. This makes dragging a |
| 2242 | // whole lot faster. |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2243 | # ifdef FEAT_GUI |
| 2244 | if (gui.in_use) |
| 2245 | j = 3; |
| 2246 | else |
| 2247 | # endif |
| 2248 | j = get_termcode_len(idx); |
| 2249 | if (STRNCMP(tp, tp + *slen, (size_t)j) == 0 |
| 2250 | && tp[*slen + j] == mouse_code |
| 2251 | && tp[*slen + j + 1] != NUL |
| 2252 | && tp[*slen + j + 2] != NUL |
| 2253 | # ifdef FEAT_GUI |
| 2254 | && (!gui.in_use |
| 2255 | || (tp[*slen + j + 3] != NUL |
| 2256 | && tp[*slen + j + 4] != NUL)) |
| 2257 | # endif |
| 2258 | ) |
| 2259 | *slen += j; |
| 2260 | else |
| 2261 | break; |
| 2262 | } |
| 2263 | } |
| 2264 | |
| 2265 | if (key_name[0] == KS_URXVT_MOUSE |
| 2266 | || key_name[0] == KS_SGR_MOUSE |
| 2267 | || key_name[0] == KS_SGR_MOUSE_RELEASE) |
| 2268 | { |
| 2269 | // URXVT 1015 mouse reporting mode: |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2270 | // Almost identical to xterm mouse mode, except the values are decimal |
| 2271 | // instead of bytes. |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2272 | // |
| 2273 | // \033[%d;%d;%dM |
| 2274 | // ^-- row |
| 2275 | // ^----- column |
| 2276 | // ^-------- code |
| 2277 | // |
| 2278 | // SGR 1006 mouse reporting mode: |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2279 | // Almost identical to xterm mouse mode, except the values are decimal |
| 2280 | // instead of bytes. |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2281 | // |
| 2282 | // \033[<%d;%d;%dM |
| 2283 | // ^-- row |
| 2284 | // ^----- column |
| 2285 | // ^-------- code |
| 2286 | // |
| 2287 | // \033[<%d;%d;%dm : mouse release event |
| 2288 | // ^-- row |
| 2289 | // ^----- column |
| 2290 | // ^-------- code |
| 2291 | p = modifiers_start; |
| 2292 | if (p == NULL) |
| 2293 | return -1; |
| 2294 | |
| 2295 | mouse_code = getdigits(&p); |
| 2296 | if (*p++ != ';') |
| 2297 | return -1; |
| 2298 | |
| 2299 | // when mouse reporting is SGR, add 32 to mouse code |
| 2300 | if (key_name[0] == KS_SGR_MOUSE |
| 2301 | || key_name[0] == KS_SGR_MOUSE_RELEASE) |
| 2302 | mouse_code += 32; |
| 2303 | |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2304 | mouse_col = getdigits(&p) - 1; |
| 2305 | if (*p++ != ';') |
| 2306 | return -1; |
| 2307 | |
| 2308 | mouse_row = getdigits(&p) - 1; |
| 2309 | |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2310 | // The modifiers were the mouse coordinates, not the modifier keys |
| 2311 | // (alt/shift/ctrl/meta) state. |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2312 | *modifiers = 0; |
| 2313 | } |
| 2314 | |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2315 | if (key_name[0] == KS_SGR_MOUSE |
| 2316 | || key_name[0] == KS_SGR_MOUSE_RELEASE) |
| 2317 | { |
| 2318 | if (key_name[0] == KS_SGR_MOUSE_RELEASE) |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2319 | { |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2320 | is_release = TRUE; |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2321 | // This is used below to set held_button. |
| 2322 | mouse_code |= MOUSE_RELEASE; |
| 2323 | } |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2324 | } |
| 2325 | else |
| 2326 | { |
| 2327 | release_is_ambiguous = TRUE; |
| 2328 | if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE) |
| 2329 | is_release = TRUE; |
| 2330 | } |
| 2331 | |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2332 | if (key_name[0] == KS_MOUSE |
| 2333 | # ifdef FEAT_MOUSE_GPM |
| 2334 | || key_name[0] == KS_GPM_MOUSE |
| 2335 | # endif |
| 2336 | # ifdef FEAT_MOUSE_URXVT |
| 2337 | || key_name[0] == KS_URXVT_MOUSE |
| 2338 | # endif |
| 2339 | || key_name[0] == KS_SGR_MOUSE |
| 2340 | || key_name[0] == KS_SGR_MOUSE_RELEASE) |
| 2341 | { |
| 2342 | # if !defined(MSWIN) |
| 2343 | /* |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2344 | * Handle old style mouse events. |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2345 | * Recognize the xterm mouse wheel, but not in the GUI, the |
| 2346 | * Linux console with GPM and the MS-DOS or Win32 console |
| 2347 | * (multi-clicks use >= 0x60). |
| 2348 | */ |
| 2349 | if (mouse_code >= MOUSEWHEEL_LOW |
| 2350 | # ifdef FEAT_GUI |
| 2351 | && !gui.in_use |
| 2352 | # endif |
| 2353 | # ifdef FEAT_MOUSE_GPM |
| 2354 | && key_name[0] != KS_GPM_MOUSE |
| 2355 | # endif |
| 2356 | ) |
| 2357 | { |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 2358 | # if defined(UNIX) |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2359 | if (use_xterm_mouse() > 1 && mouse_code >= 0x80) |
| 2360 | // mouse-move event, using MOUSE_DRAG works |
| 2361 | mouse_code = MOUSE_DRAG; |
| 2362 | else |
| 2363 | # endif |
| 2364 | // Keep the mouse_code before it's changed, so that we |
| 2365 | // remember that it was a mouse wheel click. |
| 2366 | wheel_code = mouse_code; |
| 2367 | } |
| 2368 | # ifdef FEAT_MOUSE_XTERM |
| 2369 | else if (held_button == MOUSE_RELEASE |
| 2370 | # ifdef FEAT_GUI |
| 2371 | && !gui.in_use |
| 2372 | # endif |
| 2373 | && (mouse_code == 0x23 || mouse_code == 0x24 |
| 2374 | || mouse_code == 0x40 || mouse_code == 0x41)) |
| 2375 | { |
| 2376 | // Apparently 0x23 and 0x24 are used by rxvt scroll wheel. |
| 2377 | // And 0x40 and 0x41 are used by some xterm emulator. |
| 2378 | wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23) |
| 2379 | + MOUSEWHEEL_LOW; |
| 2380 | } |
| 2381 | # endif |
| 2382 | |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 2383 | # if defined(UNIX) |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2384 | else if (use_xterm_mouse() > 1) |
| 2385 | { |
| 2386 | if (mouse_code & MOUSE_DRAG_XTERM) |
| 2387 | mouse_code |= MOUSE_DRAG; |
| 2388 | } |
| 2389 | # endif |
| 2390 | # ifdef FEAT_XCLIPBOARD |
| 2391 | else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK)) |
| 2392 | { |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2393 | if (is_release) |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2394 | stop_xterm_trace(); |
| 2395 | else |
| 2396 | start_xterm_trace(mouse_code); |
| 2397 | } |
| 2398 | # endif |
| 2399 | # endif |
| 2400 | } |
| 2401 | # endif // !UNIX || FEAT_MOUSE_XTERM |
| 2402 | # ifdef FEAT_MOUSE_NET |
| 2403 | if (key_name[0] == KS_NETTERM_MOUSE) |
| 2404 | { |
| 2405 | int mc, mr; |
| 2406 | |
| 2407 | // expect a rather limited sequence like: balancing { |
| 2408 | // \033}6,45\r |
| 2409 | // '6' is the row, 45 is the column |
| 2410 | p = tp + *slen; |
| 2411 | mr = getdigits(&p); |
| 2412 | if (*p++ != ',') |
| 2413 | return -1; |
| 2414 | mc = getdigits(&p); |
| 2415 | if (*p++ != '\r') |
| 2416 | return -1; |
| 2417 | |
| 2418 | mouse_col = mc - 1; |
| 2419 | mouse_row = mr - 1; |
| 2420 | mouse_code = MOUSE_LEFT; |
| 2421 | *slen += (int)(p - (tp + *slen)); |
| 2422 | } |
| 2423 | # endif // FEAT_MOUSE_NET |
| 2424 | # ifdef FEAT_MOUSE_JSB |
| 2425 | if (key_name[0] == KS_JSBTERM_MOUSE) |
| 2426 | { |
| 2427 | int mult, val, iter, button, status; |
| 2428 | |
| 2429 | /* |
| 2430 | * JSBTERM Input Model |
| 2431 | * \033[0~zw uniq escape sequence |
| 2432 | * (L-x) Left button pressed - not pressed x not reporting |
| 2433 | * (M-x) Middle button pressed - not pressed x not reporting |
| 2434 | * (R-x) Right button pressed - not pressed x not reporting |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2435 | * (SDmdu) Single , Double click, m: mouse move, d: button down, |
| 2436 | * u: button up |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2437 | * ### X cursor position padded to 3 digits |
| 2438 | * ### Y cursor position padded to 3 digits |
| 2439 | * (s-x) SHIFT key pressed - not pressed x not reporting |
| 2440 | * (c-x) CTRL key pressed - not pressed x not reporting |
| 2441 | * \033\\ terminating sequence |
| 2442 | */ |
| 2443 | p = tp + *slen; |
| 2444 | button = mouse_code = 0; |
| 2445 | switch (*p++) |
| 2446 | { |
| 2447 | case 'L': button = 1; break; |
| 2448 | case '-': break; |
| 2449 | case 'x': break; // ignore sequence |
| 2450 | default: return -1; // Unknown Result |
| 2451 | } |
| 2452 | switch (*p++) |
| 2453 | { |
| 2454 | case 'M': button |= 2; break; |
| 2455 | case '-': break; |
| 2456 | case 'x': break; // ignore sequence |
| 2457 | default: return -1; // Unknown Result |
| 2458 | } |
| 2459 | switch (*p++) |
| 2460 | { |
| 2461 | case 'R': button |= 4; break; |
| 2462 | case '-': break; |
| 2463 | case 'x': break; // ignore sequence |
| 2464 | default: return -1; // Unknown Result |
| 2465 | } |
| 2466 | status = *p++; |
| 2467 | for (val = 0, mult = 100, iter = 0; iter < 3; iter++, |
| 2468 | mult /= 10, p++) |
| 2469 | if (*p >= '0' && *p <= '9') |
| 2470 | val += (*p - '0') * mult; |
| 2471 | else |
| 2472 | return -1; |
| 2473 | mouse_col = val; |
| 2474 | for (val = 0, mult = 100, iter = 0; iter < 3; iter++, |
| 2475 | mult /= 10, p++) |
| 2476 | if (*p >= '0' && *p <= '9') |
| 2477 | val += (*p - '0') * mult; |
| 2478 | else |
| 2479 | return -1; |
| 2480 | mouse_row = val; |
| 2481 | switch (*p++) |
| 2482 | { |
| 2483 | case 's': button |= 8; break; // SHIFT key Pressed |
| 2484 | case '-': break; // Not Pressed |
| 2485 | case 'x': break; // Not Reporting |
| 2486 | default: return -1; // Unknown Result |
| 2487 | } |
| 2488 | switch (*p++) |
| 2489 | { |
| 2490 | case 'c': button |= 16; break; // CTRL key Pressed |
| 2491 | case '-': break; // Not Pressed |
| 2492 | case 'x': break; // Not Reporting |
| 2493 | default: return -1; // Unknown Result |
| 2494 | } |
| 2495 | if (*p++ != '\033') |
| 2496 | return -1; |
| 2497 | if (*p++ != '\\') |
| 2498 | return -1; |
| 2499 | switch (status) |
| 2500 | { |
| 2501 | case 'D': // Double Click |
| 2502 | case 'S': // Single Click |
| 2503 | if (button & 1) mouse_code |= MOUSE_LEFT; |
| 2504 | if (button & 2) mouse_code |= MOUSE_MIDDLE; |
| 2505 | if (button & 4) mouse_code |= MOUSE_RIGHT; |
| 2506 | if (button & 8) mouse_code |= MOUSE_SHIFT; |
| 2507 | if (button & 16) mouse_code |= MOUSE_CTRL; |
| 2508 | break; |
| 2509 | case 'm': // Mouse move |
| 2510 | if (button & 1) mouse_code |= MOUSE_LEFT; |
| 2511 | if (button & 2) mouse_code |= MOUSE_MIDDLE; |
| 2512 | if (button & 4) mouse_code |= MOUSE_RIGHT; |
| 2513 | if (button & 8) mouse_code |= MOUSE_SHIFT; |
| 2514 | if (button & 16) mouse_code |= MOUSE_CTRL; |
| 2515 | if ((button & 7) != 0) |
| 2516 | { |
| 2517 | held_button = mouse_code; |
| 2518 | mouse_code |= MOUSE_DRAG; |
| 2519 | } |
| 2520 | is_drag = TRUE; |
| 2521 | showmode(); |
| 2522 | break; |
| 2523 | case 'd': // Button Down |
| 2524 | if (button & 1) mouse_code |= MOUSE_LEFT; |
| 2525 | if (button & 2) mouse_code |= MOUSE_MIDDLE; |
| 2526 | if (button & 4) mouse_code |= MOUSE_RIGHT; |
| 2527 | if (button & 8) mouse_code |= MOUSE_SHIFT; |
| 2528 | if (button & 16) mouse_code |= MOUSE_CTRL; |
| 2529 | break; |
| 2530 | case 'u': // Button Up |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2531 | is_release = TRUE; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2532 | if (button & 1) |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2533 | mouse_code |= MOUSE_LEFT; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2534 | if (button & 2) |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2535 | mouse_code |= MOUSE_MIDDLE; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2536 | if (button & 4) |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2537 | mouse_code |= MOUSE_RIGHT; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2538 | if (button & 8) |
| 2539 | mouse_code |= MOUSE_SHIFT; |
| 2540 | if (button & 16) |
| 2541 | mouse_code |= MOUSE_CTRL; |
| 2542 | break; |
| 2543 | default: return -1; // Unknown Result |
| 2544 | } |
| 2545 | |
| 2546 | *slen += (p - (tp + *slen)); |
| 2547 | } |
| 2548 | # endif // FEAT_MOUSE_JSB |
| 2549 | # ifdef FEAT_MOUSE_DEC |
| 2550 | if (key_name[0] == KS_DEC_MOUSE) |
| 2551 | { |
| 2552 | /* |
| 2553 | * The DEC Locator Input Model |
| 2554 | * Netterm delivers the code sequence: |
| 2555 | * \033[2;4;24;80&w (left button down) |
| 2556 | * \033[3;0;24;80&w (left button up) |
| 2557 | * \033[6;1;24;80&w (right button down) |
| 2558 | * \033[7;0;24;80&w (right button up) |
| 2559 | * CSI Pe ; Pb ; Pr ; Pc ; Pp & w |
| 2560 | * Pe is the event code |
| 2561 | * Pb is the button code |
| 2562 | * Pr is the row coordinate |
| 2563 | * Pc is the column coordinate |
| 2564 | * Pp is the third coordinate (page number) |
| 2565 | * Pe, the event code indicates what event caused this report |
| 2566 | * The following event codes are defined: |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2567 | * 0 - request, the terminal received an explicit request for a |
| 2568 | * locator report, but the locator is unavailable |
| 2569 | * 1 - request, the terminal received an explicit request for a |
| 2570 | * locator report |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2571 | * 2 - left button down |
| 2572 | * 3 - left button up |
| 2573 | * 4 - middle button down |
| 2574 | * 5 - middle button up |
| 2575 | * 6 - right button down |
| 2576 | * 7 - right button up |
| 2577 | * 8 - fourth button down |
| 2578 | * 9 - fourth button up |
| 2579 | * 10 - locator outside filter rectangle |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2580 | * Pb, the button code, ASCII decimal 0-15 indicating which buttons are |
| 2581 | * down if any. The state of the four buttons on the locator |
| 2582 | * correspond to the low four bits of the decimal value, "1" means |
| 2583 | * button depressed |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2584 | * 0 - no buttons down, |
| 2585 | * 1 - right, |
| 2586 | * 2 - middle, |
| 2587 | * 4 - left, |
| 2588 | * 8 - fourth |
| 2589 | * Pr is the row coordinate of the locator position in the page, |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2590 | * encoded as an ASCII decimal value. If Pr is omitted, the locator |
| 2591 | * position is undefined (outside the terminal window for example). |
| 2592 | * Pc is the column coordinate of the locator position in the page, |
| 2593 | * encoded as an ASCII decimal value. If Pc is omitted, the locator |
| 2594 | * position is undefined (outside the terminal window for example). |
| 2595 | * Pp is the page coordinate of the locator position encoded as an |
| 2596 | * ASCII decimal value. The page coordinate may be omitted if the |
| 2597 | * locator is on page one (the default). We ignore it anyway. |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2598 | */ |
| 2599 | int Pe, Pb, Pr, Pc; |
| 2600 | |
| 2601 | p = tp + *slen; |
| 2602 | |
| 2603 | // get event status |
| 2604 | Pe = getdigits(&p); |
| 2605 | if (*p++ != ';') |
| 2606 | return -1; |
| 2607 | |
| 2608 | // get button status |
| 2609 | Pb = getdigits(&p); |
| 2610 | if (*p++ != ';') |
| 2611 | return -1; |
| 2612 | |
| 2613 | // get row status |
| 2614 | Pr = getdigits(&p); |
| 2615 | if (*p++ != ';') |
| 2616 | return -1; |
| 2617 | |
| 2618 | // get column status |
| 2619 | Pc = getdigits(&p); |
| 2620 | |
| 2621 | // the page parameter is optional |
| 2622 | if (*p == ';') |
| 2623 | { |
| 2624 | p++; |
| 2625 | (void)getdigits(&p); |
| 2626 | } |
| 2627 | if (*p++ != '&') |
| 2628 | return -1; |
| 2629 | if (*p++ != 'w') |
| 2630 | return -1; |
| 2631 | |
| 2632 | mouse_code = 0; |
| 2633 | switch (Pe) |
| 2634 | { |
| 2635 | case 0: return -1; // position request while unavailable |
| 2636 | case 1: // a response to a locator position request includes |
| 2637 | // the status of all buttons |
| 2638 | Pb &= 7; // mask off and ignore fourth button |
| 2639 | if (Pb & 4) |
| 2640 | mouse_code = MOUSE_LEFT; |
| 2641 | if (Pb & 2) |
| 2642 | mouse_code = MOUSE_MIDDLE; |
| 2643 | if (Pb & 1) |
| 2644 | mouse_code = MOUSE_RIGHT; |
| 2645 | if (Pb) |
| 2646 | { |
| 2647 | held_button = mouse_code; |
| 2648 | mouse_code |= MOUSE_DRAG; |
| 2649 | WantQueryMouse = TRUE; |
| 2650 | } |
| 2651 | is_drag = TRUE; |
| 2652 | showmode(); |
| 2653 | break; |
| 2654 | case 2: mouse_code = MOUSE_LEFT; |
| 2655 | WantQueryMouse = TRUE; |
| 2656 | break; |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2657 | case 3: mouse_code = MOUSE_LEFT; |
| 2658 | is_release = TRUE; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2659 | break; |
| 2660 | case 4: mouse_code = MOUSE_MIDDLE; |
| 2661 | WantQueryMouse = TRUE; |
| 2662 | break; |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2663 | case 5: mouse_code = MOUSE_MIDDLE; |
| 2664 | is_release = TRUE; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2665 | break; |
| 2666 | case 6: mouse_code = MOUSE_RIGHT; |
| 2667 | WantQueryMouse = TRUE; |
| 2668 | break; |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2669 | case 7: mouse_code = MOUSE_RIGHT; |
| 2670 | is_release = TRUE; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2671 | break; |
| 2672 | case 8: return -1; // fourth button down |
| 2673 | case 9: return -1; // fourth button up |
| 2674 | case 10: return -1; // mouse outside of filter rectangle |
| 2675 | default: return -1; // should never occur |
| 2676 | } |
| 2677 | |
| 2678 | mouse_col = Pc - 1; |
| 2679 | mouse_row = Pr - 1; |
| 2680 | |
| 2681 | *slen += (int)(p - (tp + *slen)); |
| 2682 | } |
| 2683 | # endif // FEAT_MOUSE_DEC |
| 2684 | # ifdef FEAT_MOUSE_PTERM |
| 2685 | if (key_name[0] == KS_PTERM_MOUSE) |
| 2686 | { |
| 2687 | int button, num_clicks, action; |
| 2688 | |
| 2689 | p = tp + *slen; |
| 2690 | |
| 2691 | action = getdigits(&p); |
| 2692 | if (*p++ != ';') |
| 2693 | return -1; |
| 2694 | |
| 2695 | mouse_row = getdigits(&p); |
| 2696 | if (*p++ != ';') |
| 2697 | return -1; |
| 2698 | mouse_col = getdigits(&p); |
| 2699 | if (*p++ != ';') |
| 2700 | return -1; |
| 2701 | |
| 2702 | button = getdigits(&p); |
| 2703 | mouse_code = 0; |
| 2704 | |
| 2705 | switch (button) |
| 2706 | { |
| 2707 | case 4: mouse_code = MOUSE_LEFT; break; |
| 2708 | case 1: mouse_code = MOUSE_RIGHT; break; |
| 2709 | case 2: mouse_code = MOUSE_MIDDLE; break; |
| 2710 | default: return -1; |
| 2711 | } |
| 2712 | |
| 2713 | switch (action) |
| 2714 | { |
| 2715 | case 31: // Initial press |
| 2716 | if (*p++ != ';') |
| 2717 | return -1; |
| 2718 | |
| 2719 | num_clicks = getdigits(&p); // Not used |
| 2720 | break; |
| 2721 | |
| 2722 | case 32: // Release |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2723 | is_release = TRUE; |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2724 | break; |
| 2725 | |
| 2726 | case 33: // Drag |
| 2727 | held_button = mouse_code; |
| 2728 | mouse_code |= MOUSE_DRAG; |
| 2729 | break; |
| 2730 | |
| 2731 | default: |
| 2732 | return -1; |
| 2733 | } |
| 2734 | |
| 2735 | if (*p++ != 't') |
| 2736 | return -1; |
| 2737 | |
| 2738 | *slen += (p - (tp + *slen)); |
| 2739 | } |
| 2740 | # endif // FEAT_MOUSE_PTERM |
| 2741 | |
| 2742 | // Interpret the mouse code |
| 2743 | current_button = (mouse_code & MOUSE_CLICK_MASK); |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2744 | if (is_release) |
| 2745 | current_button |= MOUSE_RELEASE; |
| 2746 | |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2747 | if (current_button == MOUSE_RELEASE |
| 2748 | # ifdef FEAT_MOUSE_XTERM |
| 2749 | && wheel_code == 0 |
| 2750 | # endif |
| 2751 | ) |
| 2752 | { |
| 2753 | /* |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2754 | * If we get a mouse drag or release event when there is no mouse |
| 2755 | * button held down (held_button == MOUSE_RELEASE), produce a K_IGNORE |
| 2756 | * below. |
| 2757 | * (can happen when you hold down two buttons and then let them go, or |
| 2758 | * click in the menu bar, but not on a menu, and drag into the text). |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2759 | */ |
| 2760 | if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG) |
| 2761 | is_drag = TRUE; |
| 2762 | current_button = held_button; |
| 2763 | } |
| 2764 | else if (wheel_code == 0) |
| 2765 | { |
| 2766 | # ifdef CHECK_DOUBLE_CLICK |
| 2767 | # ifdef FEAT_MOUSE_GPM |
| 2768 | /* |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2769 | * Only for Unix, when GUI not active, we handle multi-clicks here, but |
| 2770 | * not for GPM mouse events. |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2771 | */ |
| 2772 | # ifdef FEAT_GUI |
| 2773 | if (key_name[0] != KS_GPM_MOUSE && !gui.in_use) |
| 2774 | # else |
| 2775 | if (key_name[0] != KS_GPM_MOUSE) |
| 2776 | # endif |
| 2777 | # else |
| 2778 | # ifdef FEAT_GUI |
| 2779 | if (!gui.in_use) |
| 2780 | # endif |
| 2781 | # endif |
| 2782 | { |
| 2783 | /* |
| 2784 | * Compute the time elapsed since the previous mouse click. |
| 2785 | */ |
| 2786 | gettimeofday(&mouse_time, NULL); |
| 2787 | if (orig_mouse_time.tv_sec == 0) |
| 2788 | { |
| 2789 | /* |
| 2790 | * Avoid computing the difference between mouse_time |
| 2791 | * and orig_mouse_time for the first click, as the |
| 2792 | * difference would be huge and would cause |
| 2793 | * multiplication overflow. |
| 2794 | */ |
| 2795 | timediff = p_mouset; |
| 2796 | } |
| 2797 | else |
Bram Moolenaar | 85c3502 | 2019-11-22 22:21:59 +0100 | [diff] [blame] | 2798 | timediff = time_diff_ms(&orig_mouse_time, &mouse_time); |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2799 | orig_mouse_time = mouse_time; |
| 2800 | if (mouse_code == orig_mouse_code |
| 2801 | && timediff < p_mouset |
| 2802 | && orig_num_clicks != 4 |
| 2803 | && orig_mouse_col == mouse_col |
| 2804 | && orig_mouse_row == mouse_row |
| 2805 | && (is_mouse_topline(curwin) |
| 2806 | // Double click in tab pages line also works |
| 2807 | // when window contents changes. |
| 2808 | || (mouse_row == 0 && firstwin->w_winrow > 0)) |
| 2809 | ) |
| 2810 | ++orig_num_clicks; |
| 2811 | else |
| 2812 | orig_num_clicks = 1; |
| 2813 | orig_mouse_col = mouse_col; |
| 2814 | orig_mouse_row = mouse_row; |
| 2815 | set_mouse_topline(curwin); |
| 2816 | } |
| 2817 | # if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM) |
| 2818 | else |
| 2819 | orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code); |
| 2820 | # endif |
| 2821 | # else |
| 2822 | orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code); |
| 2823 | # endif |
| 2824 | is_click = TRUE; |
| 2825 | orig_mouse_code = mouse_code; |
| 2826 | } |
| 2827 | if (!is_drag) |
| 2828 | held_button = mouse_code & MOUSE_CLICK_MASK; |
| 2829 | |
| 2830 | /* |
| 2831 | * Translate the actual mouse event into a pseudo mouse event. |
| 2832 | * First work out what modifiers are to be used. |
| 2833 | */ |
| 2834 | if (orig_mouse_code & MOUSE_SHIFT) |
| 2835 | *modifiers |= MOD_MASK_SHIFT; |
| 2836 | if (orig_mouse_code & MOUSE_CTRL) |
| 2837 | *modifiers |= MOD_MASK_CTRL; |
| 2838 | if (orig_mouse_code & MOUSE_ALT) |
| 2839 | *modifiers |= MOD_MASK_ALT; |
| 2840 | if (orig_num_clicks == 2) |
| 2841 | *modifiers |= MOD_MASK_2CLICK; |
| 2842 | else if (orig_num_clicks == 3) |
| 2843 | *modifiers |= MOD_MASK_3CLICK; |
| 2844 | else if (orig_num_clicks == 4) |
| 2845 | *modifiers |= MOD_MASK_4CLICK; |
| 2846 | |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2847 | // Work out our pseudo mouse event. Note that MOUSE_RELEASE gets added, |
| 2848 | // then it's not mouse up/down. |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2849 | key_name[0] = KS_EXTRA; |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2850 | if (wheel_code != 0 && (!is_release || release_is_ambiguous)) |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2851 | { |
| 2852 | if (wheel_code & MOUSE_CTRL) |
| 2853 | *modifiers |= MOD_MASK_CTRL; |
| 2854 | if (wheel_code & MOUSE_ALT) |
| 2855 | *modifiers |= MOD_MASK_ALT; |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 2856 | |
| 2857 | if (wheel_code & 1 && wheel_code & 2) |
| 2858 | key_name[1] = (int)KE_MOUSELEFT; |
| 2859 | else if (wheel_code & 2) |
| 2860 | key_name[1] = (int)KE_MOUSERIGHT; |
| 2861 | else if (wheel_code & 1) |
| 2862 | key_name[1] = (int)KE_MOUSEUP; |
| 2863 | else |
| 2864 | key_name[1] = (int)KE_MOUSEDOWN; |
| 2865 | |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2866 | held_button = MOUSE_RELEASE; |
| 2867 | } |
| 2868 | else |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2869 | key_name[1] = get_pseudo_mouse_code(current_button, is_click, is_drag); |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2870 | |
Bram Moolenaar | 13c0463 | 2020-07-12 13:47:42 +0200 | [diff] [blame] | 2871 | |
| 2872 | // Make sure the mouse position is valid. Some terminals may return weird |
| 2873 | // values. |
Bram Moolenaar | b8ff5c2 | 2019-09-23 21:16:54 +0200 | [diff] [blame] | 2874 | if (mouse_col >= Columns) |
| 2875 | mouse_col = Columns - 1; |
| 2876 | if (mouse_row >= Rows) |
| 2877 | mouse_row = Rows - 1; |
| 2878 | |
| 2879 | return 0; |
| 2880 | } |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2881 | |
| 2882 | // Functions also used for popup windows. |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2883 | |
| 2884 | /* |
| 2885 | * Compute the buffer line position from the screen position "rowp" / "colp" in |
| 2886 | * window "win". |
Bram Moolenaar | 452143c | 2020-07-15 17:38:21 +0200 | [diff] [blame] | 2887 | * "plines_cache" can be NULL (no cache) or an array with "Rows" entries that |
| 2888 | * caches the plines_win() result from a previous call. Entry is zero if not |
| 2889 | * computed yet. There must be no text or setting changes since the entry is |
| 2890 | * put in the cache. |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2891 | * Returns TRUE if the position is below the last line. |
| 2892 | */ |
| 2893 | int |
| 2894 | mouse_comp_pos( |
| 2895 | win_T *win, |
| 2896 | int *rowp, |
| 2897 | int *colp, |
| 2898 | linenr_T *lnump, |
| 2899 | int *plines_cache) |
| 2900 | { |
| 2901 | int col = *colp; |
| 2902 | int row = *rowp; |
| 2903 | linenr_T lnum; |
| 2904 | int retval = FALSE; |
| 2905 | int off; |
| 2906 | int count; |
| 2907 | |
| 2908 | #ifdef FEAT_RIGHTLEFT |
| 2909 | if (win->w_p_rl) |
| 2910 | col = win->w_width - 1 - col; |
| 2911 | #endif |
| 2912 | |
| 2913 | lnum = win->w_topline; |
| 2914 | |
| 2915 | while (row > 0) |
| 2916 | { |
| 2917 | int cache_idx = lnum - win->w_topline; |
| 2918 | |
Bram Moolenaar | 452143c | 2020-07-15 17:38:21 +0200 | [diff] [blame] | 2919 | // Only "Rows" lines are cached, with folding we'll run out of entries |
| 2920 | // and use the slow way. |
| 2921 | if (plines_cache != NULL && cache_idx < Rows |
| 2922 | && plines_cache[cache_idx] > 0) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2923 | count = plines_cache[cache_idx]; |
| 2924 | else |
| 2925 | { |
| 2926 | #ifdef FEAT_DIFF |
| 2927 | // Don't include filler lines in "count" |
| 2928 | if (win->w_p_diff |
| 2929 | # ifdef FEAT_FOLDING |
| 2930 | && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL) |
| 2931 | # endif |
| 2932 | ) |
| 2933 | { |
| 2934 | if (lnum == win->w_topline) |
| 2935 | row -= win->w_topfill; |
| 2936 | else |
| 2937 | row -= diff_check_fill(win, lnum); |
| 2938 | count = plines_win_nofill(win, lnum, TRUE); |
| 2939 | } |
| 2940 | else |
| 2941 | #endif |
| 2942 | count = plines_win(win, lnum, TRUE); |
Bram Moolenaar | 452143c | 2020-07-15 17:38:21 +0200 | [diff] [blame] | 2943 | if (plines_cache != NULL && cache_idx < Rows) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2944 | plines_cache[cache_idx] = count; |
| 2945 | } |
| 2946 | if (count > row) |
| 2947 | break; // Position is in this buffer line. |
| 2948 | #ifdef FEAT_FOLDING |
| 2949 | (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL); |
| 2950 | #endif |
| 2951 | if (lnum == win->w_buffer->b_ml.ml_line_count) |
| 2952 | { |
| 2953 | retval = TRUE; |
| 2954 | break; // past end of file |
| 2955 | } |
| 2956 | row -= count; |
| 2957 | ++lnum; |
| 2958 | } |
| 2959 | |
| 2960 | if (!retval) |
| 2961 | { |
| 2962 | // Compute the column without wrapping. |
| 2963 | off = win_col_off(win) - win_col_off2(win); |
| 2964 | if (col < off) |
| 2965 | col = off; |
| 2966 | col += row * (win->w_width - off); |
| 2967 | // add skip column (for long wrapping line) |
| 2968 | col += win->w_skipcol; |
| 2969 | } |
| 2970 | |
| 2971 | if (!win->w_p_wrap) |
| 2972 | col += win->w_leftcol; |
| 2973 | |
| 2974 | // skip line number and fold column in front of the line |
| 2975 | col -= win_col_off(win); |
Bram Moolenaar | dbfa795 | 2020-11-02 20:04:22 +0100 | [diff] [blame] | 2976 | if (col <= 0) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2977 | { |
| 2978 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | dbfa795 | 2020-11-02 20:04:22 +0100 | [diff] [blame] | 2979 | // if mouse is clicked on the gutter, then inform the netbeans server |
| 2980 | if (*colp < win_col_off(win)) |
| 2981 | netbeans_gutter_click(lnum); |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 2982 | #endif |
| 2983 | col = 0; |
| 2984 | } |
| 2985 | |
| 2986 | *colp = col; |
| 2987 | *rowp = row; |
| 2988 | *lnump = lnum; |
| 2989 | return retval; |
| 2990 | } |
| 2991 | |
| 2992 | /* |
| 2993 | * Find the window at screen position "*rowp" and "*colp". The positions are |
| 2994 | * updated to become relative to the top-left of the window. |
| 2995 | * When "popup" is FAIL_POPUP and the position is in a popup window then NULL |
| 2996 | * is returned. When "popup" is IGNORE_POPUP then do not even check popup |
| 2997 | * windows. |
| 2998 | * Returns NULL when something is wrong. |
| 2999 | */ |
| 3000 | win_T * |
| 3001 | mouse_find_win(int *rowp, int *colp, mouse_find_T popup UNUSED) |
| 3002 | { |
| 3003 | frame_T *fp; |
| 3004 | win_T *wp; |
| 3005 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 3006 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 3007 | win_T *pwp = NULL; |
| 3008 | |
| 3009 | if (popup != IGNORE_POPUP) |
| 3010 | { |
Bram Moolenaar | afe45b6 | 2019-11-13 22:35:19 +0100 | [diff] [blame] | 3011 | popup_reset_handled(POPUP_HANDLED_1); |
| 3012 | while ((wp = find_next_popup(TRUE, POPUP_HANDLED_1)) != NULL) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 3013 | { |
| 3014 | if (*rowp >= wp->w_winrow && *rowp < wp->w_winrow + popup_height(wp) |
| 3015 | && *colp >= wp->w_wincol |
| 3016 | && *colp < wp->w_wincol + popup_width(wp)) |
| 3017 | pwp = wp; |
| 3018 | } |
| 3019 | if (pwp != NULL) |
| 3020 | { |
| 3021 | if (popup == FAIL_POPUP) |
| 3022 | return NULL; |
| 3023 | *rowp -= pwp->w_winrow; |
| 3024 | *colp -= pwp->w_wincol; |
| 3025 | return pwp; |
| 3026 | } |
| 3027 | } |
| 3028 | #endif |
| 3029 | |
| 3030 | fp = topframe; |
| 3031 | *rowp -= firstwin->w_winrow; |
| 3032 | for (;;) |
| 3033 | { |
| 3034 | if (fp->fr_layout == FR_LEAF) |
| 3035 | break; |
| 3036 | if (fp->fr_layout == FR_ROW) |
| 3037 | { |
| 3038 | for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next) |
| 3039 | { |
| 3040 | if (*colp < fp->fr_width) |
| 3041 | break; |
| 3042 | *colp -= fp->fr_width; |
| 3043 | } |
| 3044 | } |
| 3045 | else // fr_layout == FR_COL |
| 3046 | { |
| 3047 | for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next) |
| 3048 | { |
| 3049 | if (*rowp < fp->fr_height) |
| 3050 | break; |
| 3051 | *rowp -= fp->fr_height; |
| 3052 | } |
| 3053 | } |
| 3054 | } |
| 3055 | // When using a timer that closes a window the window might not actually |
| 3056 | // exist. |
| 3057 | FOR_ALL_WINDOWS(wp) |
| 3058 | if (wp == fp->fr_win) |
| 3059 | { |
| 3060 | #ifdef FEAT_MENU |
| 3061 | *rowp -= wp->w_winbar_height; |
| 3062 | #endif |
| 3063 | return wp; |
| 3064 | } |
| 3065 | return NULL; |
| 3066 | } |
| 3067 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 3068 | #if defined(NEED_VCOL2COL) || defined(FEAT_BEVAL) || defined(FEAT_PROP_POPUP) \ |
Bram Moolenaar | 424da7a | 2022-03-13 19:08:48 +0000 | [diff] [blame] | 3069 | || defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | b20b9e1 | 2019-09-21 20:48:04 +0200 | [diff] [blame] | 3070 | /* |
| 3071 | * Convert a virtual (screen) column to a character column. |
| 3072 | * The first column is one. |
| 3073 | */ |
| 3074 | int |
| 3075 | vcol2col(win_T *wp, linenr_T lnum, int vcol) |
| 3076 | { |
| 3077 | // try to advance to the specified column |
| 3078 | int count = 0; |
| 3079 | char_u *ptr; |
| 3080 | char_u *line; |
| 3081 | |
| 3082 | line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE); |
| 3083 | while (count < vcol && *ptr != NUL) |
| 3084 | { |
| 3085 | count += win_lbr_chartabsize(wp, line, ptr, count, NULL); |
| 3086 | MB_PTR_ADV(ptr); |
| 3087 | } |
| 3088 | return (int)(ptr - line); |
| 3089 | } |
| 3090 | #endif |
Bram Moolenaar | db3a205 | 2019-11-16 18:22:41 +0100 | [diff] [blame] | 3091 | |
| 3092 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3093 | void |
| 3094 | f_getmousepos(typval_T *argvars UNUSED, typval_T *rettv) |
| 3095 | { |
| 3096 | dict_T *d; |
| 3097 | win_T *wp; |
| 3098 | int row = mouse_row; |
| 3099 | int col = mouse_col; |
| 3100 | varnumber_T winid = 0; |
| 3101 | varnumber_T winrow = 0; |
| 3102 | varnumber_T wincol = 0; |
Bram Moolenaar | 533870a | 2022-03-13 15:52:44 +0000 | [diff] [blame] | 3103 | linenr_T lnum = 0; |
Bram Moolenaar | db3a205 | 2019-11-16 18:22:41 +0100 | [diff] [blame] | 3104 | varnumber_T column = 0; |
| 3105 | |
| 3106 | if (rettv_dict_alloc(rettv) != OK) |
| 3107 | return; |
| 3108 | d = rettv->vval.v_dict; |
| 3109 | |
| 3110 | dict_add_number(d, "screenrow", (varnumber_T)mouse_row + 1); |
| 3111 | dict_add_number(d, "screencol", (varnumber_T)mouse_col + 1); |
| 3112 | |
| 3113 | wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 3114 | if (wp != NULL) |
| 3115 | { |
| 3116 | int top_off = 0; |
| 3117 | int left_off = 0; |
| 3118 | int height = wp->w_height + wp->w_status_height; |
| 3119 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 3120 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | db3a205 | 2019-11-16 18:22:41 +0100 | [diff] [blame] | 3121 | if (WIN_IS_POPUP(wp)) |
| 3122 | { |
| 3123 | top_off = popup_top_extra(wp); |
| 3124 | left_off = popup_left_extra(wp); |
| 3125 | height = popup_height(wp); |
| 3126 | } |
| 3127 | #endif |
| 3128 | if (row < height) |
| 3129 | { |
| 3130 | winid = wp->w_id; |
| 3131 | winrow = row + 1; |
| 3132 | wincol = col + 1; |
| 3133 | row -= top_off; |
| 3134 | col -= left_off; |
| 3135 | if (row >= 0 && row < wp->w_height && col >= 0 && col < wp->w_width) |
| 3136 | { |
Sean Dewar | 10792fe | 2022-03-15 09:46:54 +0000 | [diff] [blame] | 3137 | (void)mouse_comp_pos(wp, &row, &col, &lnum, NULL); |
| 3138 | col = vcol2col(wp, lnum, col); |
Bram Moolenaar | db3a205 | 2019-11-16 18:22:41 +0100 | [diff] [blame] | 3139 | column = col + 1; |
| 3140 | } |
| 3141 | } |
| 3142 | } |
| 3143 | dict_add_number(d, "winid", winid); |
| 3144 | dict_add_number(d, "winrow", winrow); |
| 3145 | dict_add_number(d, "wincol", wincol); |
Bram Moolenaar | 533870a | 2022-03-13 15:52:44 +0000 | [diff] [blame] | 3146 | dict_add_number(d, "line", (varnumber_T)lnum); |
Bram Moolenaar | db3a205 | 2019-11-16 18:22:41 +0100 | [diff] [blame] | 3147 | dict_add_number(d, "column", column); |
| 3148 | } |
| 3149 | #endif |