blob: 5ebf125dcfe3437a1c26b90e6db755c8d672a0ad [file] [log] [blame]
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001/* 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
LemonBoyc27747e2022-05-07 12:25:40 +010016/*
17 * Horiziontal and vertical steps used when scrolling.
18 * When negative scroll by a whole page.
19 */
20static long mouse_hor_step = 6;
21static long mouse_vert_step = 3;
22
23 void
24mouse_set_vert_scroll_step(long step)
25{
26 mouse_vert_step = step;
27}
28
29 void
30mouse_set_hor_scroll_step(long step)
31{
32 mouse_hor_step = step;
33}
34
Bram Moolenaar85c35022019-11-22 22:21:59 +010035#ifdef CHECK_DOUBLE_CLICK
36/*
37 * Return the duration from t1 to t2 in milliseconds.
38 */
39 static long
40time_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 Moolenaarb20b9e12019-09-21 20:48:04 +020051/*
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
59get_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
86find_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
110find_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 Moolenaar910c3782019-09-22 14:11:50 +0200136#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100137 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200138 || defined(FEAT_GUI_PHOTON) \
Bram Moolenaar910c3782019-09-22 14:11:50 +0200139 || defined(FEAT_TERM_POPUP_MENU)
140# define USE_POPUP_SETPOS
141# define NEED_VCOL2COL
142
143/*
Yee Cheng Chin17822c52022-10-13 13:17:40 +0100144 * Translate window coordinates to buffer position without any side effects.
145 * Returns IN_BUFFER and sets "mpos->col" to the column when in buffer text.
146 * The column is one for the first column.
Bram Moolenaar910c3782019-09-22 14:11:50 +0200147 */
148 static int
149get_fpos_of_mouse(pos_T *mpos)
150{
151 win_T *wp;
152 int row = mouse_row;
153 int col = mouse_col;
154
155 if (row < 0 || col < 0) // check if it makes sense
156 return IN_UNKNOWN;
157
158 // find the window where the row is in
159 wp = mouse_find_win(&row, &col, FAIL_POPUP);
160 if (wp == NULL)
161 return IN_UNKNOWN;
162 // winpos and height may change in win_enter()!
163 if (row >= wp->w_height) // In (or below) status line
164 return IN_STATUS_LINE;
165 if (col >= wp->w_width) // In vertical separator line
166 return IN_SEP_LINE;
167
168 if (wp != curwin)
169 return IN_UNKNOWN;
170
171 // compute the position in the buffer line from the posn on the screen
172 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum, NULL))
173 return IN_STATUS_LINE; // past bottom
174
175 mpos->col = vcol2col(wp, mpos->lnum, col);
176
Bram Moolenaar910c3782019-09-22 14:11:50 +0200177 mpos->coladd = 0;
178 return IN_BUFFER;
179}
180#endif
181
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200182/*
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
219do_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
zeertzjq8e0ccb62022-11-01 18:35:27 +0000227 static int got_click = FALSE; // got a click some time back
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200228
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 Moolenaar24959102022-05-07 20:01:16 +0100272 else if (State == MODE_NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200273 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.
zeertzjq0f68e6c2022-04-05 13:17:01 +0100283 // 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 Moolenaarb20b9e12019-09-21 20:48:04 +0200287 {
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 Moolenaar05ad5ff2019-11-30 22:48:27 +0100316#ifdef FEAT_PROP_POPUP
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200317 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)
zeertzjq8e0ccb62022-11-01 18:35:27 +0000339 got_click = TRUE;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200340 else
341 {
zeertzjq8e0ccb62022-11-01 18:35:27 +0000342 if (!got_click) // didn't get click, ignore
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200343 return FALSE;
zeertzjq8e0ccb62022-11-01 18:35:27 +0000344 if (!is_drag) // release, reset got_click
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200345 {
zeertzjq8e0ccb62022-11-01 18:35:27 +0000346 got_click = FALSE;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200347 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 Moolenaar24959102022-05-07 20:01:16 +0100358 if (State & MODE_INSERT)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200359 stuffcharReadbuff(Ctrl_O);
360 if (count > 1)
361 stuffnumReadbuff(count);
362 stuffcharReadbuff(Ctrl_T);
zeertzjq8e0ccb62022-11-01 18:35:27 +0000363 got_click = FALSE; // ignore drag&release now
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200364 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 Moolenaar24959102022-05-07 20:01:16 +0100402 if (State == MODE_NORMAL)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200403 {
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 Moolenaar24959102022-05-07 20:01:16 +0100433 else if ((State & MODE_INSERT) == 0)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200434 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 Moolenaar24959102022-05-07 20:01:16 +0100441 if ((State & MODE_INSERT) || !mouse_has(MOUSE_NORMAL))
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200442 {
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 Moolenaarc3516f72020-09-08 22:45:35 +0200455 do_put(regname, NULL, BACKWARD, 1L,
456 fixindent | PUT_CURSEND);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200457
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
Bram Moolenaar80525752022-08-24 19:27:45 +0100474 if (TabPageIdxs != NULL) // only when initialized
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200475 {
Bram Moolenaar80525752022-08-24 19:27:45 +0100476 // Check for clicking in the tab page line.
477 if (mouse_row == 0 && firstwin->w_winrow > 0)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200478 {
Bram Moolenaar80525752022-08-24 19:27:45 +0100479 if (is_drag)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200480 {
Bram Moolenaar80525752022-08-24 19:27:45 +0100481 if (in_tab_line)
482 {
483 c1 = TabPageIdxs[mouse_col];
484 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
Martin Tournoij7904fa42022-10-04 16:28:45 +0100485 ? c1 - 1 : c1);
Bram Moolenaar80525752022-08-24 19:27:45 +0100486 }
487 return FALSE;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200488 }
Bram Moolenaar80525752022-08-24 19:27:45 +0100489
490 // click in a tab selects that tab page
Martin Tournoij7904fa42022-10-04 16:28:45 +0100491 if (is_click && cmdwin_type == 0 && mouse_col < Columns)
Bram Moolenaar80525752022-08-24 19:27:45 +0100492 {
493 in_tab_line = TRUE;
494 c1 = TabPageIdxs[mouse_col];
495 if (c1 >= 0)
496 {
497 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
498 {
499 // double click opens new page
500 end_visual_mode_keep_button();
501 tabpage_new();
502 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
503 }
504 else
505 {
506 // Go to specified tab page, or next one if not clicking
507 // on a label.
508 goto_tabpage(c1);
509
510 // It's like clicking on the status line of a window.
511 if (curwin != old_curwin)
512 end_visual_mode_keep_button();
513 }
514 }
515 else
516 {
517 tabpage_T *tp;
518
519 // Close the current or specified tab page.
520 if (c1 == -999)
521 tp = curtab;
522 else
523 tp = find_tabpage(-c1);
524 if (tp == curtab)
525 {
526 if (first_tabpage->tp_next != NULL)
527 tabpage_close(FALSE);
528 }
529 else if (tp != NULL)
530 tabpage_close_other(tp, FALSE);
531 }
532 }
533 return TRUE;
534 }
535 else if (is_drag && in_tab_line)
536 {
537 c1 = TabPageIdxs[mouse_col];
538 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200539 return FALSE;
540 }
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200541 }
542
543 // When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
544 // right button up -> pop-up menu
545 // shift-left button -> right button
546 // alt-left button -> alt-right button
547 if (mouse_model_popup())
548 {
549 if (which_button == MOUSE_RIGHT
550 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
551 {
Bram Moolenaar910c3782019-09-22 14:11:50 +0200552#ifdef USE_POPUP_SETPOS
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200553# ifdef FEAT_GUI
554 if (gui.in_use)
555 {
556# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200557 || defined(FEAT_GUI_PHOTON)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200558 if (!is_click)
559 // Ignore right button release events, only shows the popup
560 // menu on the button down event.
561 return FALSE;
562# endif
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100563# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200564 if (is_click || is_drag)
565 // Ignore right button down and drag mouse events. Windows
566 // only shows the popup menu on the button up event.
567 return FALSE;
568# endif
569 }
570# endif
571# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
572 else
573# endif
574# if defined(FEAT_TERM_POPUP_MENU)
575 if (!is_click)
576 // Ignore right button release events, only shows the popup
577 // menu on the button down event.
578 return FALSE;
579#endif
580
581 jump_flags = 0;
582 if (STRCMP(p_mousem, "popup_setpos") == 0)
583 {
584 // First set the cursor position before showing the popup
585 // menu.
586 if (VIsual_active)
587 {
588 pos_T m_pos;
589
590 // set MOUSE_MAY_STOP_VIS if we are outside the
591 // selection or the current window (might have false
592 // negative here)
593 if (mouse_row < curwin->w_winrow
594 || mouse_row
595 > (curwin->w_winrow + curwin->w_height))
596 jump_flags = MOUSE_MAY_STOP_VIS;
597 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
598 jump_flags = MOUSE_MAY_STOP_VIS;
599 else
600 {
Yee Cheng Chin17822c52022-10-13 13:17:40 +0100601 if (VIsual_mode == 'V')
602 {
603 if ((curwin->w_cursor.lnum <= VIsual.lnum
604 && (m_pos.lnum < curwin->w_cursor.lnum
605 || VIsual.lnum < m_pos.lnum))
606 || (VIsual.lnum < curwin->w_cursor.lnum
607 && (m_pos.lnum < VIsual.lnum
608 || curwin->w_cursor.lnum < m_pos.lnum)))
609 {
610 jump_flags = MOUSE_MAY_STOP_VIS;
611 }
612 }
613 else if ((LTOREQ_POS(curwin->w_cursor, VIsual)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200614 && (LT_POS(m_pos, curwin->w_cursor)
615 || LT_POS(VIsual, m_pos)))
616 || (LT_POS(VIsual, curwin->w_cursor)
617 && (LT_POS(m_pos, VIsual)
618 || LT_POS(curwin->w_cursor, m_pos))))
619 {
620 jump_flags = MOUSE_MAY_STOP_VIS;
621 }
622 else if (VIsual_mode == Ctrl_V)
623 {
624 getvcols(curwin, &curwin->w_cursor, &VIsual,
625 &leftcol, &rightcol);
626 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
627 if (m_pos.col < leftcol || m_pos.col > rightcol)
628 jump_flags = MOUSE_MAY_STOP_VIS;
629 }
630 }
631 }
632 else
633 jump_flags = MOUSE_MAY_STOP_VIS;
634 }
635 if (jump_flags)
636 {
637 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100638 update_curbuf(VIsual_active ? UPD_INVERTED : UPD_VALID);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200639 setcursor();
640 out_flush(); // Update before showing popup menu
641 }
642# ifdef FEAT_MENU
643 show_popupmenu();
zeertzjq8e0ccb62022-11-01 18:35:27 +0000644 got_click = FALSE; // ignore release events
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200645# endif
646 return (jump_flags & CURSOR_MOVED) != 0;
647#else
648 return FALSE;
649#endif
650 }
651 if (which_button == MOUSE_LEFT
652 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
653 {
654 which_button = MOUSE_RIGHT;
655 mod_mask &= ~MOD_MASK_SHIFT;
656 }
657 }
658
Bram Moolenaar24959102022-05-07 20:01:16 +0100659 if ((State & (MODE_NORMAL | MODE_INSERT))
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200660 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
661 {
662 if (which_button == MOUSE_LEFT)
663 {
664 if (is_click)
665 {
666 // stop Visual mode for a left click in a window, but not when
667 // on a status line
668 if (VIsual_active)
669 jump_flags |= MOUSE_MAY_STOP_VIS;
670 }
671 else if (mouse_has(MOUSE_VISUAL))
672 jump_flags |= MOUSE_MAY_VIS;
673 }
674 else if (which_button == MOUSE_RIGHT)
675 {
676 if (is_click && VIsual_active)
677 {
678 // Remember the start and end of visual before moving the
679 // cursor.
680 if (LT_POS(curwin->w_cursor, VIsual))
681 {
682 start_visual = curwin->w_cursor;
683 end_visual = VIsual;
684 }
685 else
686 {
687 start_visual = VIsual;
688 end_visual = curwin->w_cursor;
689 }
690 }
691 jump_flags |= MOUSE_FOCUS;
692 if (mouse_has(MOUSE_VISUAL))
693 jump_flags |= MOUSE_MAY_VIS;
694 }
695 }
696
697 // If an operator is pending, ignore all drags and releases until the
698 // next mouse click.
699 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
700 {
zeertzjq8e0ccb62022-11-01 18:35:27 +0000701 got_click = FALSE;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200702 oap->motion_type = MCHAR;
703 }
704
705 // When releasing the button let jump_to_mouse() know.
706 if (!is_click && !is_drag)
707 jump_flags |= MOUSE_RELEASED;
708
709 // JUMP!
710 jump_flags = jump_to_mouse(jump_flags,
711 oap == NULL ? NULL : &(oap->inclusive), which_button);
712
713#ifdef FEAT_MENU
714 // A click in the window toolbar has no side effects.
715 if (jump_flags & MOUSE_WINBAR)
716 return FALSE;
717#endif
718 moved = (jump_flags & CURSOR_MOVED);
719 in_status_line = (jump_flags & IN_STATUS_LINE);
720 in_sep_line = (jump_flags & IN_SEP_LINE);
721
722#ifdef FEAT_NETBEANS_INTG
723 if (isNetbeansBuffer(curbuf)
724 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
725 {
726 int key = KEY2TERMCAP1(c);
727
728 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
729 || key == (int)KE_RIGHTRELEASE)
730 netbeans_button_release(which_button);
731 }
732#endif
733
734 // When jumping to another window, clear a pending operator. That's a bit
735 // friendlier than beeping and not jumping to that window.
736 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
737 clearop(oap);
738
739#ifdef FEAT_FOLDING
740 if (mod_mask == 0
741 && !is_drag
742 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
743 && which_button == MOUSE_LEFT)
744 {
745 // open or close a fold at this line
746 if (jump_flags & MOUSE_FOLD_OPEN)
747 openFold(curwin->w_cursor.lnum, 1L);
748 else
749 closeFold(curwin->w_cursor.lnum, 1L);
750 // don't move the cursor if still in the same window
751 if (curwin == old_curwin)
752 curwin->w_cursor = save_cursor;
753 }
754#endif
755
Martin Tournoij7904fa42022-10-04 16:28:45 +0100756#if defined(FEAT_CLIPBOARD)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200757 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
758 {
759 clip_modeless(which_button, is_click, is_drag);
760 return FALSE;
761 }
762#endif
763
764 // Set global flag that we are extending the Visual area with mouse
765 // dragging; temporarily minimize 'scrolloff'.
766 if (VIsual_active && is_drag && get_scrolloff_value())
767 {
768 // In the very first line, allow scrolling one line
769 if (mouse_row == 0)
770 mouse_dragging = 2;
771 else
772 mouse_dragging = 1;
773 }
774
775 // When dragging the mouse above the window, scroll down.
776 if (is_drag && mouse_row < 0 && !in_status_line)
777 {
778 scroll_redraw(FALSE, 1L);
779 mouse_row = 0;
780 }
781
782 if (start_visual.lnum) // right click in visual mode
783 {
784 // When ALT is pressed make Visual mode blockwise.
785 if (mod_mask & MOD_MASK_ALT)
786 VIsual_mode = Ctrl_V;
787
788 // In Visual-block mode, divide the area in four, pick up the corner
789 // that is in the quarter that the cursor is in.
790 if (VIsual_mode == Ctrl_V)
791 {
792 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
793 if (curwin->w_curswant > (leftcol + rightcol) / 2)
794 end_visual.col = leftcol;
795 else
796 end_visual.col = rightcol;
797 if (curwin->w_cursor.lnum >=
798 (start_visual.lnum + end_visual.lnum) / 2)
799 end_visual.lnum = start_visual.lnum;
800
801 // move VIsual to the right column
802 start_visual = curwin->w_cursor; // save the cursor pos
803 curwin->w_cursor = end_visual;
804 coladvance(end_visual.col);
805 VIsual = curwin->w_cursor;
806 curwin->w_cursor = start_visual; // restore the cursor
807 }
808 else
809 {
810 // If the click is before the start of visual, change the start.
811 // If the click is after the end of visual, change the end. If
812 // the click is inside the visual, change the closest side.
813 if (LT_POS(curwin->w_cursor, start_visual))
814 VIsual = end_visual;
815 else if (LT_POS(end_visual, curwin->w_cursor))
816 VIsual = start_visual;
817 else
818 {
819 // In the same line, compare column number
820 if (end_visual.lnum == start_visual.lnum)
821 {
822 if (curwin->w_cursor.col - start_visual.col >
823 end_visual.col - curwin->w_cursor.col)
824 VIsual = start_visual;
825 else
826 VIsual = end_visual;
827 }
828
829 // In different lines, compare line number
830 else
831 {
832 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
833 (end_visual.lnum - curwin->w_cursor.lnum);
834
835 if (diff > 0) // closest to end
836 VIsual = start_visual;
837 else if (diff < 0) // closest to start
838 VIsual = end_visual;
839 else // in the middle line
840 {
841 if (curwin->w_cursor.col <
842 (start_visual.col + end_visual.col) / 2)
843 VIsual = end_visual;
844 else
845 VIsual = start_visual;
846 }
847 }
848 }
849 }
850 }
851 // If Visual mode started in insert mode, execute "CTRL-O"
Bram Moolenaar24959102022-05-07 20:01:16 +0100852 else if ((State & MODE_INSERT) && VIsual_active)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200853 stuffcharReadbuff(Ctrl_O);
854
855 // Middle mouse click: Put text before cursor.
856 if (which_button == MOUSE_MIDDLE)
857 {
858#ifdef FEAT_CLIPBOARD
859 if (clip_star.available && regname == 0)
860 regname = '*';
861#endif
862 if (yank_register_mline(regname))
863 {
864 if (mouse_past_bottom)
865 dir = FORWARD;
866 }
867 else if (mouse_past_eol)
868 dir = FORWARD;
869
870 if (fixindent)
871 {
872 c1 = (dir == BACKWARD) ? '[' : ']';
873 c2 = 'p';
874 }
875 else
876 {
877 c1 = (dir == FORWARD) ? 'p' : 'P';
878 c2 = NUL;
879 }
880 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
881
882 // Remember where the paste started, so in edit() Insstart can be set
883 // to this position
884 if (restart_edit != 0)
885 where_paste_started = curwin->w_cursor;
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200886 do_put(regname, NULL, dir, count, fixindent | PUT_CURSEND);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200887 }
888
889#if defined(FEAT_QUICKFIX)
890 // Ctrl-Mouse click or double click in a quickfix window jumps to the
891 // error under the mouse pointer.
892 else if (((mod_mask & MOD_MASK_CTRL)
893 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
894 && bt_quickfix(curbuf))
895 {
896 if (curwin->w_llist_ref == NULL) // quickfix window
897 do_cmdline_cmd((char_u *)".cc");
898 else // location list window
899 do_cmdline_cmd((char_u *)".ll");
zeertzjq8e0ccb62022-11-01 18:35:27 +0000900 got_click = FALSE; // ignore drag&release now
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200901 }
902#endif
903
904 // Ctrl-Mouse click (or double click in a help window) jumps to the tag
905 // under the mouse pointer.
906 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
907 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
908 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100909 if (State & MODE_INSERT)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200910 stuffcharReadbuff(Ctrl_O);
911 stuffcharReadbuff(Ctrl_RSB);
zeertzjq8e0ccb62022-11-01 18:35:27 +0000912 got_click = FALSE; // ignore drag&release now
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200913 }
914
915 // Shift-Mouse click searches for the next occurrence of the word under
916 // the mouse pointer
917 else if ((mod_mask & MOD_MASK_SHIFT))
918 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100919 if ((State & MODE_INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200920 stuffcharReadbuff(Ctrl_O);
921 if (which_button == MOUSE_LEFT)
922 stuffcharReadbuff('*');
923 else // MOUSE_RIGHT
924 stuffcharReadbuff('#');
925 }
926
927 // Handle double clicks, unless on status line
928 else if (in_status_line)
929 {
930#ifdef FEAT_MOUSESHAPE
931 if ((is_drag || is_click) && !drag_status_line)
932 {
933 drag_status_line = TRUE;
934 update_mouseshape(-1);
935 }
936#endif
937 }
938 else if (in_sep_line)
939 {
940#ifdef FEAT_MOUSESHAPE
941 if ((is_drag || is_click) && !drag_sep_line)
942 {
943 drag_sep_line = TRUE;
944 update_mouseshape(-1);
945 }
946#endif
947 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100948 else if ((mod_mask & MOD_MASK_MULTI_CLICK)
949 && (State & (MODE_NORMAL | MODE_INSERT))
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200950 && mouse_has(MOUSE_VISUAL))
951 {
952 if (is_click || !VIsual_active)
953 {
954 if (VIsual_active)
955 orig_cursor = VIsual;
956 else
957 {
958 check_visual_highlight();
959 VIsual = curwin->w_cursor;
960 orig_cursor = VIsual;
961 VIsual_active = TRUE;
962 VIsual_reselect = TRUE;
963 // start Select mode if 'selectmode' contains "mouse"
964 may_start_select('o');
965 setmouse();
966 }
967 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
968 {
969 // Double click with ALT pressed makes it blockwise.
970 if (mod_mask & MOD_MASK_ALT)
971 VIsual_mode = Ctrl_V;
972 else
973 VIsual_mode = 'v';
974 }
975 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
976 VIsual_mode = 'V';
977 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
978 VIsual_mode = Ctrl_V;
979#ifdef FEAT_CLIPBOARD
980 // Make sure the clipboard gets updated. Needed because start and
981 // end may still be the same, and the selection needs to be owned
982 clip_star.vmode = NUL;
983#endif
984 }
985 // A double click selects a word or a block.
986 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
987 {
988 pos_T *pos = NULL;
989 int gc;
990
991 if (is_click)
992 {
993 // If the character under the cursor (skipping white space) is
994 // not a word character, try finding a match and select a (),
995 // {}, [], #if/#endif, etc. block.
996 end_visual = curwin->w_cursor;
997 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
998 inc(&end_visual);
999 if (oap != NULL)
1000 oap->motion_type = MCHAR;
1001 if (oap != NULL
1002 && VIsual_mode == 'v'
1003 && !vim_iswordc(gchar_pos(&end_visual))
1004 && EQUAL_POS(curwin->w_cursor, VIsual)
1005 && (pos = findmatch(oap, NUL)) != NULL)
1006 {
1007 curwin->w_cursor = *pos;
1008 if (oap->motion_type == MLINE)
1009 VIsual_mode = 'V';
1010 else if (*p_sel == 'e')
1011 {
1012 if (LT_POS(curwin->w_cursor, VIsual))
1013 ++VIsual.col;
1014 else
1015 ++curwin->w_cursor.col;
1016 }
1017 }
1018 }
1019
1020 if (pos == NULL && (is_click || is_drag))
1021 {
1022 // When not found a match or when dragging: extend to include
1023 // a word.
1024 if (LT_POS(curwin->w_cursor, orig_cursor))
1025 {
1026 find_start_of_word(&curwin->w_cursor);
1027 find_end_of_word(&VIsual);
1028 }
1029 else
1030 {
1031 find_start_of_word(&VIsual);
1032 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
1033 curwin->w_cursor.col +=
1034 (*mb_ptr2len)(ml_get_cursor());
1035 find_end_of_word(&curwin->w_cursor);
1036 }
1037 }
1038 curwin->w_set_curswant = TRUE;
1039 }
1040 if (is_click)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001041 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001042 }
1043 else if (VIsual_active && !old_active)
1044 {
1045 if (mod_mask & MOD_MASK_ALT)
1046 VIsual_mode = Ctrl_V;
1047 else
1048 VIsual_mode = 'v';
1049 }
1050
1051 // If Visual mode changed show it later.
1052 if ((!VIsual_active && old_active && mode_displayed)
1053 || (VIsual_active && p_smd && msg_silent == 0
1054 && (!old_active || VIsual_mode != old_mode)))
1055 redraw_cmdline = TRUE;
1056
1057 return moved;
1058}
1059
1060 void
1061ins_mouse(int c)
1062{
1063 pos_T tpos;
1064 win_T *old_curwin = curwin;
1065
1066# ifdef FEAT_GUI
1067 // When GUI is active, also move/paste when 'mouse' is empty
1068 if (!gui.in_use)
1069# endif
1070 if (!mouse_has(MOUSE_INSERT))
1071 return;
1072
1073 undisplay_dollar();
1074 tpos = curwin->w_cursor;
1075 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
1076 {
1077 win_T *new_curwin = curwin;
1078
1079 if (curwin != old_curwin && win_valid(old_curwin))
1080 {
1081 // Mouse took us to another window. We need to go back to the
1082 // previous one to stop insert there properly.
1083 curwin = old_curwin;
1084 curbuf = curwin->w_buffer;
1085#ifdef FEAT_JOB_CHANNEL
1086 if (bt_prompt(curbuf))
1087 // Restart Insert mode when re-entering the prompt buffer.
1088 curbuf->b_prompt_insert = 'A';
1089#endif
1090 }
1091 start_arrow(curwin == old_curwin ? &tpos : NULL);
1092 if (curwin != new_curwin && win_valid(new_curwin))
1093 {
1094 curwin = new_curwin;
1095 curbuf = curwin->w_buffer;
1096 }
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001097 set_can_cindent(TRUE);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001098 }
1099
1100 // redraw status lines (in case another window became active)
1101 redraw_statuslines();
1102}
1103
Christopher Plewright44c22092022-11-15 17:43:36 +00001104/*
1105 * Implementation for scrolling in direction "dir", which is one of the MSCR_
1106 * values.
1107 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001108 void
1109ins_mousescroll(int dir)
1110{
Christopher Plewright44c22092022-11-15 17:43:36 +00001111 cmdarg_T cap;
1112 CLEAR_FIELD(cap);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001113
Christopher Plewright44c22092022-11-15 17:43:36 +00001114 oparg_T oa;
1115 clear_oparg(&oa);
1116 cap.oap = &oa;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001117
Christopher Plewright44c22092022-11-15 17:43:36 +00001118 cap.arg = dir;
1119 switch (dir)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001120 {
Christopher Plewright44c22092022-11-15 17:43:36 +00001121 case MSCR_UP:
1122 cap.cmdchar = K_MOUSEUP;
1123 break;
1124 case MSCR_DOWN:
1125 cap.cmdchar = K_MOUSEDOWN;
1126 break;
1127 case MSCR_LEFT:
1128 cap.cmdchar = K_MOUSELEFT;
1129 break;
1130 case MSCR_RIGHT:
1131 cap.cmdchar = K_MOUSERIGHT;
1132 break;
1133 default:
1134 siemsg("Invalid ins_mousescroll() argument: %d", dir);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001135 }
Christopher Plewright44c22092022-11-15 17:43:36 +00001136 do_mousescroll(MODE_INSERT, &cap);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001137}
1138
1139/*
1140 * Return TRUE if "c" is a mouse key.
1141 */
1142 int
1143is_mouse_key(int c)
1144{
1145 return c == K_LEFTMOUSE
1146 || c == K_LEFTMOUSE_NM
1147 || c == K_LEFTDRAG
1148 || c == K_LEFTRELEASE
1149 || c == K_LEFTRELEASE_NM
1150 || c == K_MOUSEMOVE
1151 || c == K_MIDDLEMOUSE
1152 || c == K_MIDDLEDRAG
1153 || c == K_MIDDLERELEASE
1154 || c == K_RIGHTMOUSE
1155 || c == K_RIGHTDRAG
1156 || c == K_RIGHTRELEASE
1157 || c == K_MOUSEDOWN
1158 || c == K_MOUSEUP
1159 || c == K_MOUSELEFT
1160 || c == K_MOUSERIGHT
1161 || c == K_X1MOUSE
1162 || c == K_X1DRAG
1163 || c == K_X1RELEASE
1164 || c == K_X2MOUSE
1165 || c == K_X2DRAG
1166 || c == K_X2RELEASE;
1167}
1168
1169static struct mousetable
1170{
1171 int pseudo_code; // Code for pseudo mouse event
1172 int button; // Which mouse button is it?
1173 int is_click; // Is it a mouse button click event?
1174 int is_drag; // Is it a mouse drag event?
1175} mouse_table[] =
1176{
1177 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
1178#ifdef FEAT_GUI
1179 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
1180#endif
1181 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
1182 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
1183#ifdef FEAT_GUI
1184 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
1185#endif
1186 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
1187 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
1188 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
1189 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
1190 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
1191 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
1192 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
1193 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
1194 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
1195 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
1196 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
1197 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
1198 // DRAG without CLICK
1199 {(int)KE_MOUSEMOVE, MOUSE_RELEASE, FALSE, TRUE},
1200 // RELEASE without CLICK
1201 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
1202 {0, 0, 0, 0},
1203};
1204
1205/*
1206 * Look up the given mouse code to return the relevant information in the other
1207 * arguments. Return which button is down or was released.
1208 */
1209 int
1210get_mouse_button(int code, int *is_click, int *is_drag)
1211{
1212 int i;
1213
1214 for (i = 0; mouse_table[i].pseudo_code; i++)
1215 if (code == mouse_table[i].pseudo_code)
1216 {
1217 *is_click = mouse_table[i].is_click;
1218 *is_drag = mouse_table[i].is_drag;
1219 return mouse_table[i].button;
1220 }
1221 return 0; // Shouldn't get here
1222}
1223
1224/*
1225 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
1226 * the given information about which mouse button is down, and whether the
1227 * mouse was clicked, dragged or released.
1228 */
1229 int
1230get_pseudo_mouse_code(
1231 int button, // eg MOUSE_LEFT
1232 int is_click,
1233 int is_drag)
1234{
1235 int i;
1236
1237 for (i = 0; mouse_table[i].pseudo_code; i++)
1238 if (button == mouse_table[i].button
1239 && is_click == mouse_table[i].is_click
1240 && is_drag == mouse_table[i].is_drag)
1241 {
1242#ifdef FEAT_GUI
1243 // Trick: a non mappable left click and release has mouse_col -1
1244 // or added MOUSE_COLOFF. Used for 'mousefocus' in
1245 // gui_mouse_moved()
1246 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
1247 {
1248 if (mouse_col < 0)
1249 mouse_col = 0;
1250 else
1251 mouse_col -= MOUSE_COLOFF;
1252 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
1253 return (int)KE_LEFTMOUSE_NM;
1254 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
1255 return (int)KE_LEFTRELEASE_NM;
1256 }
1257#endif
1258 return mouse_table[i].pseudo_code;
1259 }
1260 return (int)KE_IGNORE; // not recognized, ignore it
1261}
1262
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001263# define HMT_NORMAL 1
1264# define HMT_NETTERM 2
1265# define HMT_DEC 4
1266# define HMT_JSBTERM 8
1267# define HMT_PTERM 16
1268# define HMT_URXVT 32
1269# define HMT_GPM 64
1270# define HMT_SGR 128
1271# define HMT_SGR_REL 256
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001272static int has_mouse_termcode = 0;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001273
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001274 void
1275set_mouse_termcode(
1276 int n, // KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE
1277 char_u *s)
1278{
1279 char_u name[2];
1280
1281 name[0] = n;
1282 name[1] = KE_FILLER;
1283 add_termcode(name, s, FALSE);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001284# ifdef FEAT_MOUSE_JSB
1285 if (n == KS_JSBTERM_MOUSE)
1286 has_mouse_termcode |= HMT_JSBTERM;
1287 else
1288# endif
1289# ifdef FEAT_MOUSE_NET
1290 if (n == KS_NETTERM_MOUSE)
1291 has_mouse_termcode |= HMT_NETTERM;
1292 else
1293# endif
1294# ifdef FEAT_MOUSE_DEC
1295 if (n == KS_DEC_MOUSE)
1296 has_mouse_termcode |= HMT_DEC;
1297 else
1298# endif
1299# ifdef FEAT_MOUSE_PTERM
1300 if (n == KS_PTERM_MOUSE)
1301 has_mouse_termcode |= HMT_PTERM;
1302 else
1303# endif
1304# ifdef FEAT_MOUSE_URXVT
1305 if (n == KS_URXVT_MOUSE)
1306 has_mouse_termcode |= HMT_URXVT;
1307 else
1308# endif
1309# ifdef FEAT_MOUSE_GPM
1310 if (n == KS_GPM_MOUSE)
1311 has_mouse_termcode |= HMT_GPM;
1312 else
1313# endif
1314 if (n == KS_SGR_MOUSE)
1315 has_mouse_termcode |= HMT_SGR;
1316 else if (n == KS_SGR_MOUSE_RELEASE)
1317 has_mouse_termcode |= HMT_SGR_REL;
1318 else
1319 has_mouse_termcode |= HMT_NORMAL;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001320}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001321
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001322# if defined(UNIX) || defined(VMS) || defined(PROTO)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001323 void
1324del_mouse_termcode(
1325 int n) // KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE
1326{
1327 char_u name[2];
1328
1329 name[0] = n;
1330 name[1] = KE_FILLER;
1331 del_termcode(name);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001332# ifdef FEAT_MOUSE_JSB
1333 if (n == KS_JSBTERM_MOUSE)
1334 has_mouse_termcode &= ~HMT_JSBTERM;
1335 else
1336# endif
1337# ifdef FEAT_MOUSE_NET
1338 if (n == KS_NETTERM_MOUSE)
1339 has_mouse_termcode &= ~HMT_NETTERM;
1340 else
1341# endif
1342# ifdef FEAT_MOUSE_DEC
1343 if (n == KS_DEC_MOUSE)
1344 has_mouse_termcode &= ~HMT_DEC;
1345 else
1346# endif
1347# ifdef FEAT_MOUSE_PTERM
1348 if (n == KS_PTERM_MOUSE)
1349 has_mouse_termcode &= ~HMT_PTERM;
1350 else
1351# endif
1352# ifdef FEAT_MOUSE_URXVT
1353 if (n == KS_URXVT_MOUSE)
1354 has_mouse_termcode &= ~HMT_URXVT;
1355 else
1356# endif
1357# ifdef FEAT_MOUSE_GPM
1358 if (n == KS_GPM_MOUSE)
1359 has_mouse_termcode &= ~HMT_GPM;
1360 else
1361# endif
1362 if (n == KS_SGR_MOUSE)
1363 has_mouse_termcode &= ~HMT_SGR;
1364 else if (n == KS_SGR_MOUSE_RELEASE)
1365 has_mouse_termcode &= ~HMT_SGR_REL;
1366 else
1367 has_mouse_termcode &= ~HMT_NORMAL;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001368}
1369# endif
1370
1371/*
1372 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
1373 */
1374 void
1375setmouse(void)
1376{
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001377 int checkfor;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001378
1379# ifdef FEAT_MOUSESHAPE
1380 update_mouseshape(-1);
1381# endif
1382
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001383 // Should be outside proc, but may break MOUSESHAPE
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001384# ifdef FEAT_GUI
1385 // In the GUI the mouse is always enabled.
1386 if (gui.in_use)
1387 return;
1388# endif
1389 // be quick when mouse is off
1390 if (*p_mouse == NUL || has_mouse_termcode == 0)
1391 return;
1392
1393 // don't switch mouse on when not in raw mode (Ex mode)
1394 if (cur_tmode != TMODE_RAW)
1395 {
1396 mch_setmouse(FALSE);
1397 return;
1398 }
1399
1400 if (VIsual_active)
1401 checkfor = MOUSE_VISUAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001402 else if (State == MODE_HITRETURN || State == MODE_ASKMORE
1403 || State == MODE_SETWSIZE)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001404 checkfor = MOUSE_RETURN;
Bram Moolenaar24959102022-05-07 20:01:16 +01001405 else if (State & MODE_INSERT)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001406 checkfor = MOUSE_INSERT;
Bram Moolenaar24959102022-05-07 20:01:16 +01001407 else if (State & MODE_CMDLINE)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001408 checkfor = MOUSE_COMMAND;
Bram Moolenaar24959102022-05-07 20:01:16 +01001409 else if (State == MODE_CONFIRM || State == MODE_EXTERNCMD)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001410 checkfor = ' '; // don't use mouse for ":confirm" or ":!cmd"
1411 else
1412 checkfor = MOUSE_NORMAL; // assume normal mode
1413
1414 if (mouse_has(checkfor))
1415 mch_setmouse(TRUE);
1416 else
1417 mch_setmouse(FALSE);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001418}
1419
1420/*
1421 * Return TRUE if
1422 * - "c" is in 'mouse', or
1423 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
1424 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
1425 * normal editing mode (not at hit-return message).
1426 */
1427 int
1428mouse_has(int c)
1429{
1430 char_u *p;
1431
1432 for (p = p_mouse; *p; ++p)
1433 switch (*p)
1434 {
1435 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
1436 return TRUE;
1437 break;
1438 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
1439 return TRUE;
1440 break;
1441 default: if (c == *p) return TRUE; break;
1442 }
1443 return FALSE;
1444}
1445
1446/*
1447 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
1448 */
1449 int
1450mouse_model_popup(void)
1451{
1452 return (p_mousem[0] == 'p');
1453}
1454
zeertzjq8e0ccb62022-11-01 18:35:27 +00001455static win_T *dragwin = NULL; // window being dragged
1456
1457/*
1458 * Reset the window being dragged. To be called when switching tab page.
1459 */
1460 void
1461reset_dragwin(void)
1462{
1463 dragwin = NULL;
1464}
1465
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001466/*
1467 * Move the cursor to the specified row and column on the screen.
1468 * Change current window if necessary. Returns an integer with the
1469 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
1470 *
1471 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
1472 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
1473 *
1474 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
1475 * if the mouse is outside the window then the text will scroll, or if the
1476 * mouse was previously on a status line, then the status line may be dragged.
1477 *
1478 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
1479 * cursor is moved unless the cursor was on a status line.
1480 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
1481 * IN_SEP_LINE depending on where the cursor was clicked.
1482 *
1483 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
1484 * the mouse is on the status line of the same window.
1485 *
1486 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
1487 * the last call.
1488 *
1489 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
1490 * remembered.
1491 */
1492 int
1493jump_to_mouse(
1494 int flags,
1495 int *inclusive, // used for inclusive operator, can be NULL
1496 int which_button) // MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE
1497{
1498 static int on_status_line = 0; // #lines below bottom of window
1499 static int on_sep_line = 0; // on separator right of window
1500#ifdef FEAT_MENU
1501 static int in_winbar = FALSE;
1502#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001503#ifdef FEAT_PROP_POPUP
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001504 static int in_popup_win = FALSE;
1505 static win_T *click_in_popup_win = NULL;
1506#endif
1507 static int prev_row = -1;
1508 static int prev_col = -1;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001509 static int did_drag = FALSE; // drag was noticed
1510
1511 win_T *wp, *old_curwin;
1512 pos_T old_cursor;
1513 int count;
1514 int first;
1515 int row = mouse_row;
1516 int col = mouse_col;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001517 colnr_T col_from_screen = -1;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001518#ifdef FEAT_FOLDING
Bram Moolenaarb9081882022-07-09 04:56:24 +01001519 int mouse_char = ' ';
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001520#endif
1521
1522 mouse_past_bottom = FALSE;
1523 mouse_past_eol = FALSE;
1524
1525 if (flags & MOUSE_RELEASED)
1526 {
1527 // On button release we may change window focus if positioned on a
1528 // status line and no dragging happened.
1529 if (dragwin != NULL && !did_drag)
1530 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
1531 dragwin = NULL;
1532 did_drag = FALSE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001533#ifdef FEAT_PROP_POPUP
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001534 if (click_in_popup_win != NULL && popup_dragwin == NULL)
1535 popup_close_for_mouse_click(click_in_popup_win);
1536
1537 popup_dragwin = NULL;
1538 click_in_popup_win = NULL;
1539#endif
1540 }
1541
1542 if ((flags & MOUSE_DID_MOVE)
1543 && prev_row == mouse_row
1544 && prev_col == mouse_col)
1545 {
1546retnomove:
1547 // before moving the cursor for a left click which is NOT in a status
1548 // line, stop Visual mode
1549 if (on_status_line)
1550 return IN_STATUS_LINE;
1551 if (on_sep_line)
1552 return IN_SEP_LINE;
1553#ifdef FEAT_MENU
1554 if (in_winbar)
1555 {
1556 // A quick second click may arrive as a double-click, but we use it
1557 // as a second click in the WinBar.
1558 if ((mod_mask & MOD_MASK_MULTI_CLICK) && !(flags & MOUSE_RELEASED))
1559 {
1560 wp = mouse_find_win(&row, &col, FAIL_POPUP);
1561 if (wp == NULL)
1562 return IN_UNKNOWN;
1563 winbar_click(wp, col);
1564 }
1565 return IN_OTHER_WIN | MOUSE_WINBAR;
1566 }
1567#endif
1568 if (flags & MOUSE_MAY_STOP_VIS)
1569 {
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001570 end_visual_mode_keep_button();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001571 redraw_curbuf_later(UPD_INVERTED); // delete the inversion
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001572 }
Martin Tournoij7904fa42022-10-04 16:28:45 +01001573#if defined(FEAT_CLIPBOARD)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001574 // Continue a modeless selection in another window.
1575 if (cmdwin_type != 0 && row < curwin->w_winrow)
1576 return IN_OTHER_WIN;
1577#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001578#ifdef FEAT_PROP_POPUP
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001579 // Continue a modeless selection in a popup window or dragging it.
1580 if (in_popup_win)
1581 {
1582 click_in_popup_win = NULL; // don't close it on release
1583 if (popup_dragwin != NULL)
1584 {
1585 // dragging a popup window
1586 popup_drag(popup_dragwin);
1587 return IN_UNKNOWN;
1588 }
1589 return IN_OTHER_WIN;
1590 }
1591#endif
1592 return IN_BUFFER;
1593 }
1594
1595 prev_row = mouse_row;
1596 prev_col = mouse_col;
1597
1598 if (flags & MOUSE_SETPOS)
1599 goto retnomove; // ugly goto...
1600
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001601 old_curwin = curwin;
1602 old_cursor = curwin->w_cursor;
1603
1604 if (!(flags & MOUSE_FOCUS))
1605 {
1606 if (row < 0 || col < 0) // check if it makes sense
1607 return IN_UNKNOWN;
1608
1609 // find the window where the row is in and adjust "row" and "col" to be
1610 // relative to top-left of the window
1611 wp = mouse_find_win(&row, &col, FIND_POPUP);
1612 if (wp == NULL)
1613 return IN_UNKNOWN;
1614 dragwin = NULL;
1615
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001616#ifdef FEAT_PROP_POPUP
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001617 // Click in a popup window may start dragging or modeless selection,
1618 // but not much else.
1619 if (WIN_IS_POPUP(wp))
1620 {
1621 on_sep_line = 0;
Bram Moolenaarbfc57862021-11-26 15:57:40 +00001622 on_status_line = 0;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001623 in_popup_win = TRUE;
1624 if (which_button == MOUSE_LEFT && popup_close_if_on_X(wp, row, col))
1625 {
1626 return IN_UNKNOWN;
1627 }
Bram Moolenaar0b74d002021-11-29 17:38:02 +00001628 else if (((wp->w_popup_flags & (POPF_DRAG | POPF_RESIZE))
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001629 && popup_on_border(wp, row, col))
Bram Moolenaar0b74d002021-11-29 17:38:02 +00001630 || (wp->w_popup_flags & POPF_DRAGALL))
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001631 {
1632 popup_dragwin = wp;
1633 popup_start_drag(wp, row, col);
1634 return IN_UNKNOWN;
1635 }
1636 // Only close on release, otherwise it's not possible to drag or do
1637 // modeless selection.
1638 else if (wp->w_popup_close == POPCLOSE_CLICK
1639 && which_button == MOUSE_LEFT)
1640 {
1641 click_in_popup_win = wp;
1642 }
1643 else if (which_button == MOUSE_LEFT)
1644 // If the click is in the scrollbar, may scroll up/down.
1645 popup_handle_scrollbar_click(wp, row, col);
1646# ifdef FEAT_CLIPBOARD
1647 return IN_OTHER_WIN;
1648# else
1649 return IN_UNKNOWN;
1650# endif
1651 }
1652 in_popup_win = FALSE;
1653 popup_dragwin = NULL;
1654#endif
1655#ifdef FEAT_MENU
1656 if (row == -1)
1657 {
1658 // A click in the window toolbar does not enter another window or
1659 // change Visual highlighting.
1660 winbar_click(wp, col);
1661 in_winbar = TRUE;
1662 return IN_OTHER_WIN | MOUSE_WINBAR;
1663 }
1664 in_winbar = FALSE;
1665#endif
1666
1667 // winpos and height may change in win_enter()!
1668 if (row >= wp->w_height) // In (or below) status line
1669 {
1670 on_status_line = row - wp->w_height + 1;
1671 dragwin = wp;
1672 }
1673 else
1674 on_status_line = 0;
1675 if (col >= wp->w_width) // In separator line
1676 {
1677 on_sep_line = col - wp->w_width + 1;
1678 dragwin = wp;
1679 }
1680 else
1681 on_sep_line = 0;
1682
1683 // The rightmost character of the status line might be a vertical
1684 // separator character if there is no connecting window to the right.
1685 if (on_status_line && on_sep_line)
1686 {
1687 if (stl_connected(wp))
1688 on_sep_line = 0;
1689 else
1690 on_status_line = 0;
1691 }
1692
1693 // Before jumping to another buffer, or moving the cursor for a left
1694 // click, stop Visual mode.
1695 if (VIsual_active
1696 && (wp->w_buffer != curwin->w_buffer
1697 || (!on_status_line && !on_sep_line
1698#ifdef FEAT_FOLDING
1699 && (
1700# ifdef FEAT_RIGHTLEFT
1701 wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc :
1702# endif
Martin Tournoij7904fa42022-10-04 16:28:45 +01001703 col >= wp->w_p_fdc + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001704 )
1705#endif
1706 && (flags & MOUSE_MAY_STOP_VIS))))
1707 {
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001708 end_visual_mode_keep_button();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001709 redraw_curbuf_later(UPD_INVERTED); // delete the inversion
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001710 }
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001711 if (cmdwin_type != 0 && wp != curwin)
1712 {
1713 // A click outside the command-line window: Use modeless
1714 // selection if possible. Allow dragging the status lines.
1715 on_sep_line = 0;
1716# ifdef FEAT_CLIPBOARD
1717 if (on_status_line)
1718 return IN_STATUS_LINE;
1719 return IN_OTHER_WIN;
1720# else
1721 row = 0;
1722 col += wp->w_wincol;
1723 wp = curwin;
1724# endif
1725 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001726#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1727 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1728 // terminal in popup window: don't jump to another window
1729 return IN_OTHER_WIN;
1730#endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001731 // Only change window focus when not clicking on or dragging the
1732 // status line. Do change focus when releasing the mouse button
1733 // (MOUSE_FOCUS was set above if we dragged first).
1734 if (dragwin == NULL || (flags & MOUSE_RELEASED))
1735 win_enter(wp, TRUE); // can make wp invalid!
1736
1737 if (curwin != old_curwin)
1738 {
1739#ifdef CHECK_DOUBLE_CLICK
1740 // set topline, to be able to check for double click ourselves
1741 set_mouse_topline(curwin);
1742#endif
1743#ifdef FEAT_TERMINAL
1744 // when entering a terminal window may change state
1745 term_win_entered();
1746#endif
1747 }
1748 if (on_status_line) // In (or below) status line
1749 {
1750 // Don't use start_arrow() if we're in the same window
1751 if (curwin == old_curwin)
1752 return IN_STATUS_LINE;
1753 else
1754 return IN_STATUS_LINE | CURSOR_MOVED;
1755 }
1756 if (on_sep_line) // In (or below) status line
1757 {
1758 // Don't use start_arrow() if we're in the same window
1759 if (curwin == old_curwin)
1760 return IN_SEP_LINE;
1761 else
1762 return IN_SEP_LINE | CURSOR_MOVED;
1763 }
1764
1765 curwin->w_cursor.lnum = curwin->w_topline;
1766#ifdef FEAT_GUI
1767 // remember topline, needed for double click
1768 gui_prev_topline = curwin->w_topline;
1769# ifdef FEAT_DIFF
1770 gui_prev_topfill = curwin->w_topfill;
1771# endif
1772#endif
1773 }
1774 else if (on_status_line && which_button == MOUSE_LEFT)
1775 {
1776 if (dragwin != NULL)
1777 {
1778 // Drag the status line
zeertzjq6dab00a2022-05-20 13:45:59 +01001779 count = row - W_WINROW(dragwin) - dragwin->w_height + 1
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001780 - on_status_line;
1781 win_drag_status_line(dragwin, count);
1782 did_drag |= count;
1783 }
1784 return IN_STATUS_LINE; // Cursor didn't move
1785 }
1786 else if (on_sep_line && which_button == MOUSE_LEFT)
1787 {
1788 if (dragwin != NULL)
1789 {
1790 // Drag the separator column
1791 count = col - dragwin->w_wincol - dragwin->w_width + 1
1792 - on_sep_line;
1793 win_drag_vsep_line(dragwin, count);
1794 did_drag |= count;
1795 }
1796 return IN_SEP_LINE; // Cursor didn't move
1797 }
1798#ifdef FEAT_MENU
1799 else if (in_winbar)
1800 {
1801 // After a click on the window toolbar don't start Visual mode.
1802 return IN_OTHER_WIN | MOUSE_WINBAR;
1803 }
1804#endif
1805 else // keep_window_focus must be TRUE
1806 {
1807 // before moving the cursor for a left click, stop Visual mode
1808 if (flags & MOUSE_MAY_STOP_VIS)
1809 {
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001810 end_visual_mode_keep_button();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001811 redraw_curbuf_later(UPD_INVERTED); // delete the inversion
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001812 }
1813
Martin Tournoij7904fa42022-10-04 16:28:45 +01001814#if defined(FEAT_CLIPBOARD)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001815 // Continue a modeless selection in another window.
1816 if (cmdwin_type != 0 && row < curwin->w_winrow)
1817 return IN_OTHER_WIN;
1818#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001819#ifdef FEAT_PROP_POPUP
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001820 if (in_popup_win)
1821 {
1822 if (popup_dragwin != NULL)
1823 {
1824 // dragging a popup window
1825 popup_drag(popup_dragwin);
1826 return IN_UNKNOWN;
1827 }
1828 // continue a modeless selection in a popup window
1829 click_in_popup_win = NULL;
1830 return IN_OTHER_WIN;
1831 }
1832#endif
1833
1834 row -= W_WINROW(curwin);
1835 col -= curwin->w_wincol;
1836
1837 // When clicking beyond the end of the window, scroll the screen.
1838 // Scroll by however many rows outside the window we are.
1839 if (row < 0)
1840 {
1841 count = 0;
1842 for (first = TRUE; curwin->w_topline > 1; )
1843 {
1844#ifdef FEAT_DIFF
1845 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
1846 ++count;
1847 else
1848#endif
1849 count += plines(curwin->w_topline - 1);
1850 if (!first && count > -row)
1851 break;
1852 first = FALSE;
1853#ifdef FEAT_FOLDING
1854 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1855#endif
1856#ifdef FEAT_DIFF
1857 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
1858 ++curwin->w_topfill;
1859 else
1860#endif
1861 {
1862 --curwin->w_topline;
1863#ifdef FEAT_DIFF
1864 curwin->w_topfill = 0;
1865#endif
1866 }
1867 }
1868#ifdef FEAT_DIFF
1869 check_topfill(curwin, FALSE);
1870#endif
1871 curwin->w_valid &=
1872 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001873 redraw_later(UPD_VALID);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001874 row = 0;
1875 }
1876 else if (row >= curwin->w_height)
1877 {
1878 count = 0;
1879 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
1880 {
1881#ifdef FEAT_DIFF
1882 if (curwin->w_topfill > 0)
1883 ++count;
1884 else
1885#endif
1886 count += plines(curwin->w_topline);
1887 if (!first && count > row - curwin->w_height + 1)
1888 break;
1889 first = FALSE;
1890#ifdef FEAT_FOLDING
1891 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
1892 && curwin->w_topline == curbuf->b_ml.ml_line_count)
1893 break;
1894#endif
1895#ifdef FEAT_DIFF
1896 if (curwin->w_topfill > 0)
1897 --curwin->w_topfill;
1898 else
1899#endif
1900 {
1901 ++curwin->w_topline;
1902#ifdef FEAT_DIFF
1903 curwin->w_topfill =
1904 diff_check_fill(curwin, curwin->w_topline);
1905#endif
1906 }
1907 }
1908#ifdef FEAT_DIFF
1909 check_topfill(curwin, FALSE);
1910#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001911 redraw_later(UPD_VALID);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001912 curwin->w_valid &=
1913 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1914 row = curwin->w_height - 1;
1915 }
1916 else if (row == 0)
1917 {
1918 // When dragging the mouse, while the text has been scrolled up as
1919 // far as it goes, moving the mouse in the top line should scroll
1920 // the text down (done later when recomputing w_topline).
1921 if (mouse_dragging > 0
1922 && curwin->w_cursor.lnum
1923 == curwin->w_buffer->b_ml.ml_line_count
1924 && curwin->w_cursor.lnum == curwin->w_topline)
1925 curwin->w_valid &= ~(VALID_TOPLINE);
1926 }
1927 }
1928
Bram Moolenaarb9081882022-07-09 04:56:24 +01001929 if (prev_row >= 0 && prev_row < Rows && prev_col >= 0 && prev_col <= Columns
1930 && ScreenLines != NULL)
1931 {
1932 int off = LineOffset[prev_row] + prev_col;
1933
1934 // Only use ScreenCols[] after the window was redrawn. Mainly matters
1935 // for tests, a user would not click before redrawing.
Bram Moolenaar8f49e692022-08-09 14:19:40 +01001936 // Do not use when 'virtualedit' is active.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001937 if (curwin->w_redr_type <= UPD_VALID_NO_UPDATE && !virtual_active())
Bram Moolenaarb9081882022-07-09 04:56:24 +01001938 col_from_screen = ScreenCols[off];
1939#ifdef FEAT_FOLDING
1940 // Remember the character under the mouse, it might be a '-' or '+' in
1941 // the fold column.
1942 mouse_char = ScreenLines[off];
1943#endif
1944 }
1945
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001946#ifdef FEAT_FOLDING
1947 // Check for position outside of the fold column.
1948 if (
1949# ifdef FEAT_RIGHTLEFT
1950 curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc :
1951# endif
Martin Tournoij7904fa42022-10-04 16:28:45 +01001952 col >= curwin->w_p_fdc + (cmdwin_type == 0 ? 0 : 1)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001953 )
1954 mouse_char = ' ';
1955#endif
1956
1957 // compute the position in the buffer line from the posn on the screen
1958 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum, NULL))
1959 mouse_past_bottom = TRUE;
1960
1961 // Start Visual mode before coladvance(), for when 'sel' != "old"
1962 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
1963 {
1964 check_visual_highlight();
1965 VIsual = old_cursor;
1966 VIsual_active = TRUE;
1967 VIsual_reselect = TRUE;
1968 // if 'selectmode' contains "mouse", start Select mode
1969 may_start_select('o');
1970 setmouse();
1971 if (p_smd && msg_silent == 0)
1972 redraw_cmdline = TRUE; // show visual mode later
1973 }
1974
Bram Moolenaarb9081882022-07-09 04:56:24 +01001975 if (col_from_screen >= 0)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001976 {
Bram Moolenaarb9081882022-07-09 04:56:24 +01001977 // Use the column from ScreenCols[], it is accurate also after
1978 // concealed characters.
1979 curwin->w_cursor.col = col_from_screen;
1980 if (col_from_screen == MAXCOL)
1981 {
1982 curwin->w_curswant = col_from_screen;
1983 curwin->w_set_curswant = FALSE; // May still have been TRUE
1984 mouse_past_eol = TRUE;
1985 if (inclusive != NULL)
1986 *inclusive = TRUE;
1987 }
1988 else
1989 {
1990 curwin->w_set_curswant = TRUE;
1991 if (inclusive != NULL)
1992 *inclusive = FALSE;
1993 }
1994 check_cursor_col();
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001995 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01001996 else
1997 {
1998 curwin->w_curswant = col;
1999 curwin->w_set_curswant = FALSE; // May still have been TRUE
2000 if (coladvance(col) == FAIL) // Mouse click beyond end of line
2001 {
2002 if (inclusive != NULL)
2003 *inclusive = TRUE;
2004 mouse_past_eol = TRUE;
2005 }
2006 else if (inclusive != NULL)
2007 *inclusive = FALSE;
2008 }
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002009
2010 count = IN_BUFFER;
2011 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
2012 || curwin->w_cursor.col != old_cursor.col)
2013 count |= CURSOR_MOVED; // Cursor has moved
2014
2015# ifdef FEAT_FOLDING
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002016 if (mouse_char == curwin->w_fill_chars.foldclosed)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002017 count |= MOUSE_FOLD_OPEN;
2018 else if (mouse_char != ' ')
2019 count |= MOUSE_FOLD_CLOSE;
2020# endif
2021
2022 return count;
2023}
2024
2025/*
Christopher Plewright44c22092022-11-15 17:43:36 +00002026 * Make a horizontal scroll to "leftcol".
2027 * Return TRUE if the cursor moved, FALSE otherwise.
2028 */
2029 int
2030do_mousescroll_horiz(long_u leftcol)
2031{
2032 if (curwin->w_p_wrap)
2033 return FALSE; // no wrapping, no scrolling
2034
2035 if (curwin->w_leftcol == (colnr_T)leftcol)
2036 return FALSE; // already there
2037
2038 curwin->w_leftcol = (colnr_T)leftcol;
2039
2040 // When the line of the cursor is too short, move the cursor to the
2041 // longest visible line.
2042 if (
2043#ifdef FEAT_GUI
2044 (!gui.in_use || vim_strchr(p_go, GO_HORSCROLL) == NULL) &&
2045#endif
2046 !virtual_active()
2047 && (long)leftcol > scroll_line_len(curwin->w_cursor.lnum))
2048 {
2049 curwin->w_cursor.lnum = ui_find_longest_lnum();
2050 curwin->w_cursor.col = 0;
2051 }
2052
2053 return leftcol_changed();
2054}
2055
2056/*
LemonBoyc27747e2022-05-07 12:25:40 +01002057 * Mouse scroll wheel: Default action is to scroll mouse_vert_step lines (or
Christopher Plewright44c22092022-11-15 17:43:36 +00002058 * mouse_hor_step, depending on the scroll direction), or one page when Shift
2059 * or Ctrl is used.
2060 * Direction is indicated by "cap->arg":
2061 * K_MOUSEUP - MSCR_UP
2062 * K_MOUSEDOWN - MSCR_DOWN
2063 * K_MOUSELEFT - MSCR_LEFT
2064 * K_MOUSERIGHT - MSCR_RIGHT
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002065 */
2066 void
Christopher Plewright44c22092022-11-15 17:43:36 +00002067do_mousescroll(int mode, cmdarg_T *cap)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002068{
Christopher Plewright44c22092022-11-15 17:43:36 +00002069 win_T *old_curwin = curwin, *wp;
2070 int did_ins_scroll = FALSE;
2071 pos_T tpos = curwin->w_cursor;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002072
2073 if (mouse_row >= 0 && mouse_col >= 0)
2074 {
2075 int row, col;
2076
2077 row = mouse_row;
2078 col = mouse_col;
2079
2080 // find the window at the pointer coordinates
2081 wp = mouse_find_win(&row, &col, FIND_POPUP);
2082 if (wp == NULL)
2083 return;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002084#ifdef FEAT_PROP_POPUP
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002085 if (WIN_IS_POPUP(wp) && !wp->w_has_scrollbar)
2086 return;
2087#endif
2088 curwin = wp;
2089 curbuf = curwin->w_buffer;
2090 }
Christopher Plewright44c22092022-11-15 17:43:36 +00002091 if (mode == MODE_INSERT && curwin == old_curwin)
2092 undisplay_dollar();
2093
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002094# ifdef FEAT_TERMINAL
Christopher Plewright44c22092022-11-15 17:43:36 +00002095 if (term_use_loop())
2096 // This window is a terminal window, send the mouse event there.
2097 // Set "typed" to FALSE to avoid an endless loop.
2098 send_keys_to_term(curbuf->b_term, cap->cmdchar, mod_mask, FALSE);
2099 else
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002100# endif
Christopher Plewright44c22092022-11-15 17:43:36 +00002101 // For insert mode, don't scroll the window in which completion is being
2102 // done.
2103 if (mode == MODE_NORMAL || !pum_visible() || curwin != old_curwin)
2104 {
2105 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002106 {
Christopher Plewright44c22092022-11-15 17:43:36 +00002107 if (mouse_vert_step < 0
2108 || mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002109 {
Christopher Plewright44c22092022-11-15 17:43:36 +00002110 if (mode == MODE_INSERT)
2111 {
2112 long step = (long)(curwin->w_botline - curwin->w_topline);
2113 scroll_redraw(cap->arg, step);
2114 }
2115 else
2116 {
2117 did_ins_scroll = onepage(cap->arg ? FORWARD : BACKWARD, 1L);
2118 }
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002119 }
2120 else
Christopher Plewright44c22092022-11-15 17:43:36 +00002121 {
2122 if (mode == MODE_INSERT)
2123 {
2124 scroll_redraw(cap->arg, mouse_vert_step);
2125 }
2126 else
2127 {
2128 // Don't scroll more than half the window height.
2129 if (curwin->w_height < mouse_vert_step * 2)
2130 {
2131 cap->count1 = curwin->w_height / 2;
2132 if (cap->count1 == 0)
2133 cap->count1 = 1;
2134 }
2135 else
2136 {
2137 cap->count1 = mouse_vert_step;
2138 }
2139 cap->count0 = cap->count1;
2140 nv_scroll_line(cap);
2141 }
2142 }
2143
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002144#ifdef FEAT_PROP_POPUP
Christopher Plewright44c22092022-11-15 17:43:36 +00002145 if (WIN_IS_POPUP(curwin))
2146 popup_set_firstline(curwin);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002147#endif
Christopher Plewright44c22092022-11-15 17:43:36 +00002148 }
2149 else
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002150 {
Christopher Plewright44c22092022-11-15 17:43:36 +00002151 long step = (mouse_hor_step < 0
2152 || (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2153 ? curwin->w_width : mouse_hor_step;
2154 long leftcol = curwin->w_leftcol
2155 + (cap->arg == MSCR_RIGHT ? -step : step);
2156 if (leftcol < 0)
2157 leftcol = 0;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002158
Christopher Plewright44c22092022-11-15 17:43:36 +00002159 did_ins_scroll = do_mousescroll_horiz((long_u)leftcol);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002160 }
2161 }
Christopher Plewright44c22092022-11-15 17:43:36 +00002162
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002163# ifdef FEAT_SYN_HL
Christopher Plewright44c22092022-11-15 17:43:36 +00002164 if (mode == MODE_NORMAL && curwin != old_curwin && curwin->w_p_cul)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002165 redraw_for_cursorline(curwin);
2166# endif
LemonBoy66e13ae2022-04-21 22:52:11 +01002167 may_trigger_winscrolled();
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002168
2169 curwin->w_redr_status = TRUE;
2170
2171 curwin = old_curwin;
2172 curbuf = curwin->w_buffer;
Christopher Plewright44c22092022-11-15 17:43:36 +00002173
2174 if (mode == MODE_INSERT)
2175 {
2176 // The popup menu may overlay the window, need to redraw it.
2177 // TODO: Would be more efficient to only redraw the windows that are
2178 // overlapped by the popup menu.
2179 if (pum_visible() && did_ins_scroll)
2180 {
2181 redraw_all_later(UPD_NOT_VALID);
2182 ins_compl_show_pum();
2183 }
2184 if (!EQUAL_POS(curwin->w_cursor, tpos))
2185 {
2186 start_arrow(&tpos);
2187 set_can_cindent(TRUE);
2188 }
2189 }
2190}
2191
2192 void
2193nv_mousescroll(cmdarg_T *cap)
2194{
2195 do_mousescroll(MODE_NORMAL, cap);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002196}
2197
2198/*
2199 * Mouse clicks and drags.
2200 */
2201 void
2202nv_mouse(cmdarg_T *cap)
2203{
2204 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
2205}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002206
Bram Moolenaar85eee5b2021-06-03 20:34:57 +02002207static int held_button = MOUSE_RELEASE;
2208
2209 void
2210reset_held_button()
2211{
2212 held_button = MOUSE_RELEASE;
2213}
2214
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002215/*
2216 * Check if typebuf 'tp' contains a terminal mouse code and returns the
2217 * modifiers found in typebuf in 'modifiers'.
2218 */
2219 int
2220check_termcode_mouse(
2221 char_u *tp,
2222 int *slen,
2223 char_u *key_name,
2224 char_u *modifiers_start,
2225 int idx,
2226 int *modifiers)
2227{
2228 int j;
2229 char_u *p;
2230# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
2231 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
2232 char_u bytes[6];
2233 int num_bytes;
2234# endif
2235 int mouse_code = 0; // init for GCC
2236 int is_click, is_drag;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002237 int is_release, release_is_ambiguous;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002238 int wheel_code = 0;
2239 int current_button;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002240 static int orig_num_clicks = 1;
2241 static int orig_mouse_code = 0x0;
2242# ifdef CHECK_DOUBLE_CLICK
2243 static int orig_mouse_col = 0;
2244 static int orig_mouse_row = 0;
2245 static struct timeval orig_mouse_time = {0, 0};
2246 // time of previous mouse click
2247 struct timeval mouse_time; // time of current mouse click
2248 long timediff; // elapsed time in msec
2249# endif
2250
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002251 is_click = is_drag = is_release = release_is_ambiguous = FALSE;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002252
2253# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
2254 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
2255 if (key_name[0] == KS_MOUSE
2256# ifdef FEAT_MOUSE_GPM
2257 || key_name[0] == KS_GPM_MOUSE
2258# endif
2259 )
2260 {
2261 /*
Bram Moolenaar13c04632020-07-12 13:47:42 +02002262 * For xterm we get "<t_mouse>scr", where s == encoded button state:
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002263 * 0x20 = left button down
2264 * 0x21 = middle button down
2265 * 0x22 = right button down
2266 * 0x23 = any button release
2267 * 0x60 = button 4 down (scroll wheel down)
2268 * 0x61 = button 5 down (scroll wheel up)
2269 * add 0x04 for SHIFT
2270 * add 0x08 for ALT
2271 * add 0x10 for CTRL
2272 * add 0x20 for mouse drag (0x40 is drag with left button)
2273 * add 0x40 for mouse move (0x80 is move, 0x81 too)
2274 * 0x43 (drag + release) is also move
2275 * c == column + ' ' + 1 == column + 33
2276 * r == row + ' ' + 1 == row + 33
2277 *
Bram Moolenaar13c04632020-07-12 13:47:42 +02002278 * The coordinates are passed on through global variables. Ugly, but
2279 * this avoids trouble with mouse clicks at an unexpected moment and
2280 * allows for mapping them.
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002281 */
2282 for (;;)
2283 {
2284# ifdef FEAT_GUI
2285 if (gui.in_use)
2286 {
2287 // GUI uses more bits for columns > 223
2288 num_bytes = get_bytes_from_buf(tp + *slen, bytes, 5);
2289 if (num_bytes == -1) // not enough coordinates
2290 return -1;
2291 mouse_code = bytes[0];
2292 mouse_col = 128 * (bytes[1] - ' ' - 1)
2293 + bytes[2] - ' ' - 1;
2294 mouse_row = 128 * (bytes[3] - ' ' - 1)
2295 + bytes[4] - ' ' - 1;
2296 }
2297 else
2298# endif
2299 {
2300 num_bytes = get_bytes_from_buf(tp + *slen, bytes, 3);
2301 if (num_bytes == -1) // not enough coordinates
2302 return -1;
2303 mouse_code = bytes[0];
2304 mouse_col = bytes[1] - ' ' - 1;
2305 mouse_row = bytes[2] - ' ' - 1;
2306 }
2307 *slen += num_bytes;
2308
Bram Moolenaar13c04632020-07-12 13:47:42 +02002309 // If the following bytes is also a mouse code and it has the same
2310 // code, dump this one and get the next. This makes dragging a
2311 // whole lot faster.
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002312# ifdef FEAT_GUI
2313 if (gui.in_use)
2314 j = 3;
2315 else
2316# endif
2317 j = get_termcode_len(idx);
2318 if (STRNCMP(tp, tp + *slen, (size_t)j) == 0
2319 && tp[*slen + j] == mouse_code
2320 && tp[*slen + j + 1] != NUL
2321 && tp[*slen + j + 2] != NUL
2322# ifdef FEAT_GUI
2323 && (!gui.in_use
2324 || (tp[*slen + j + 3] != NUL
2325 && tp[*slen + j + 4] != NUL))
2326# endif
2327 )
2328 *slen += j;
2329 else
2330 break;
2331 }
2332 }
2333
2334 if (key_name[0] == KS_URXVT_MOUSE
2335 || key_name[0] == KS_SGR_MOUSE
2336 || key_name[0] == KS_SGR_MOUSE_RELEASE)
2337 {
2338 // URXVT 1015 mouse reporting mode:
Bram Moolenaar13c04632020-07-12 13:47:42 +02002339 // Almost identical to xterm mouse mode, except the values are decimal
2340 // instead of bytes.
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002341 //
2342 // \033[%d;%d;%dM
2343 // ^-- row
2344 // ^----- column
2345 // ^-------- code
2346 //
2347 // SGR 1006 mouse reporting mode:
Bram Moolenaar13c04632020-07-12 13:47:42 +02002348 // Almost identical to xterm mouse mode, except the values are decimal
2349 // instead of bytes.
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002350 //
2351 // \033[<%d;%d;%dM
2352 // ^-- row
2353 // ^----- column
2354 // ^-------- code
2355 //
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002356 // \033[<%d;%d;%dm : mouse release event
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002357 // ^-- row
2358 // ^----- column
2359 // ^-------- code
2360 p = modifiers_start;
2361 if (p == NULL)
2362 return -1;
2363
2364 mouse_code = getdigits(&p);
2365 if (*p++ != ';')
2366 return -1;
2367
2368 // when mouse reporting is SGR, add 32 to mouse code
2369 if (key_name[0] == KS_SGR_MOUSE
2370 || key_name[0] == KS_SGR_MOUSE_RELEASE)
2371 mouse_code += 32;
2372
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002373 mouse_col = getdigits(&p) - 1;
2374 if (*p++ != ';')
2375 return -1;
2376
2377 mouse_row = getdigits(&p) - 1;
2378
Bram Moolenaar13c04632020-07-12 13:47:42 +02002379 // The modifiers were the mouse coordinates, not the modifier keys
2380 // (alt/shift/ctrl/meta) state.
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002381 *modifiers = 0;
2382 }
2383
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002384 if (key_name[0] == KS_SGR_MOUSE
2385 || key_name[0] == KS_SGR_MOUSE_RELEASE)
2386 {
2387 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar13c04632020-07-12 13:47:42 +02002388 {
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002389 is_release = TRUE;
Bram Moolenaar13c04632020-07-12 13:47:42 +02002390 // This is used below to set held_button.
2391 mouse_code |= MOUSE_RELEASE;
2392 }
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002393 }
2394 else
2395 {
2396 release_is_ambiguous = TRUE;
2397 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
2398 is_release = TRUE;
2399 }
2400
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002401 if (key_name[0] == KS_MOUSE
2402# ifdef FEAT_MOUSE_GPM
2403 || key_name[0] == KS_GPM_MOUSE
2404# endif
2405# ifdef FEAT_MOUSE_URXVT
2406 || key_name[0] == KS_URXVT_MOUSE
2407# endif
2408 || key_name[0] == KS_SGR_MOUSE
2409 || key_name[0] == KS_SGR_MOUSE_RELEASE)
2410 {
2411# if !defined(MSWIN)
2412 /*
Bram Moolenaar13c04632020-07-12 13:47:42 +02002413 * Handle old style mouse events.
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002414 * Recognize the xterm mouse wheel, but not in the GUI, the
2415 * Linux console with GPM and the MS-DOS or Win32 console
2416 * (multi-clicks use >= 0x60).
2417 */
2418 if (mouse_code >= MOUSEWHEEL_LOW
2419# ifdef FEAT_GUI
2420 && !gui.in_use
2421# endif
2422# ifdef FEAT_MOUSE_GPM
2423 && key_name[0] != KS_GPM_MOUSE
2424# endif
2425 )
2426 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002427# if defined(UNIX)
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002428 if (use_xterm_mouse() > 1 && mouse_code >= 0x80)
2429 // mouse-move event, using MOUSE_DRAG works
2430 mouse_code = MOUSE_DRAG;
2431 else
2432# endif
2433 // Keep the mouse_code before it's changed, so that we
2434 // remember that it was a mouse wheel click.
2435 wheel_code = mouse_code;
2436 }
2437# ifdef FEAT_MOUSE_XTERM
2438 else if (held_button == MOUSE_RELEASE
2439# ifdef FEAT_GUI
2440 && !gui.in_use
2441# endif
2442 && (mouse_code == 0x23 || mouse_code == 0x24
2443 || mouse_code == 0x40 || mouse_code == 0x41))
2444 {
2445 // Apparently 0x23 and 0x24 are used by rxvt scroll wheel.
2446 // And 0x40 and 0x41 are used by some xterm emulator.
2447 wheel_code = mouse_code - (mouse_code >= 0x40 ? 0x40 : 0x23)
Bram Moolenaard6212b82022-08-03 15:48:33 +01002448 + MOUSEWHEEL_LOW;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002449 }
2450# endif
2451
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002452# if defined(UNIX)
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002453 else if (use_xterm_mouse() > 1)
2454 {
2455 if (mouse_code & MOUSE_DRAG_XTERM)
2456 mouse_code |= MOUSE_DRAG;
2457 }
2458# endif
2459# ifdef FEAT_XCLIPBOARD
2460 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
2461 {
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002462 if (is_release)
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002463 stop_xterm_trace();
2464 else
2465 start_xterm_trace(mouse_code);
2466 }
2467# endif
2468# endif
2469 }
2470# endif // !UNIX || FEAT_MOUSE_XTERM
2471# ifdef FEAT_MOUSE_NET
2472 if (key_name[0] == KS_NETTERM_MOUSE)
2473 {
2474 int mc, mr;
2475
2476 // expect a rather limited sequence like: balancing {
2477 // \033}6,45\r
2478 // '6' is the row, 45 is the column
2479 p = tp + *slen;
2480 mr = getdigits(&p);
2481 if (*p++ != ',')
2482 return -1;
2483 mc = getdigits(&p);
2484 if (*p++ != '\r')
2485 return -1;
2486
2487 mouse_col = mc - 1;
2488 mouse_row = mr - 1;
2489 mouse_code = MOUSE_LEFT;
2490 *slen += (int)(p - (tp + *slen));
2491 }
2492# endif // FEAT_MOUSE_NET
2493# ifdef FEAT_MOUSE_JSB
2494 if (key_name[0] == KS_JSBTERM_MOUSE)
2495 {
2496 int mult, val, iter, button, status;
2497
2498 /*
2499 * JSBTERM Input Model
2500 * \033[0~zw uniq escape sequence
2501 * (L-x) Left button pressed - not pressed x not reporting
2502 * (M-x) Middle button pressed - not pressed x not reporting
2503 * (R-x) Right button pressed - not pressed x not reporting
Bram Moolenaar13c04632020-07-12 13:47:42 +02002504 * (SDmdu) Single , Double click, m: mouse move, d: button down,
2505 * u: button up
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002506 * ### X cursor position padded to 3 digits
2507 * ### Y cursor position padded to 3 digits
2508 * (s-x) SHIFT key pressed - not pressed x not reporting
2509 * (c-x) CTRL key pressed - not pressed x not reporting
2510 * \033\\ terminating sequence
2511 */
2512 p = tp + *slen;
2513 button = mouse_code = 0;
2514 switch (*p++)
2515 {
2516 case 'L': button = 1; break;
2517 case '-': break;
2518 case 'x': break; // ignore sequence
2519 default: return -1; // Unknown Result
2520 }
2521 switch (*p++)
2522 {
2523 case 'M': button |= 2; break;
2524 case '-': break;
2525 case 'x': break; // ignore sequence
2526 default: return -1; // Unknown Result
2527 }
2528 switch (*p++)
2529 {
2530 case 'R': button |= 4; break;
2531 case '-': break;
2532 case 'x': break; // ignore sequence
2533 default: return -1; // Unknown Result
2534 }
2535 status = *p++;
2536 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
2537 mult /= 10, p++)
2538 if (*p >= '0' && *p <= '9')
2539 val += (*p - '0') * mult;
2540 else
2541 return -1;
2542 mouse_col = val;
2543 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
2544 mult /= 10, p++)
2545 if (*p >= '0' && *p <= '9')
2546 val += (*p - '0') * mult;
2547 else
2548 return -1;
2549 mouse_row = val;
2550 switch (*p++)
2551 {
2552 case 's': button |= 8; break; // SHIFT key Pressed
2553 case '-': break; // Not Pressed
2554 case 'x': break; // Not Reporting
2555 default: return -1; // Unknown Result
2556 }
2557 switch (*p++)
2558 {
2559 case 'c': button |= 16; break; // CTRL key Pressed
2560 case '-': break; // Not Pressed
2561 case 'x': break; // Not Reporting
2562 default: return -1; // Unknown Result
2563 }
2564 if (*p++ != '\033')
2565 return -1;
2566 if (*p++ != '\\')
2567 return -1;
2568 switch (status)
2569 {
2570 case 'D': // Double Click
2571 case 'S': // Single Click
2572 if (button & 1) mouse_code |= MOUSE_LEFT;
2573 if (button & 2) mouse_code |= MOUSE_MIDDLE;
2574 if (button & 4) mouse_code |= MOUSE_RIGHT;
2575 if (button & 8) mouse_code |= MOUSE_SHIFT;
2576 if (button & 16) mouse_code |= MOUSE_CTRL;
2577 break;
2578 case 'm': // Mouse move
2579 if (button & 1) mouse_code |= MOUSE_LEFT;
2580 if (button & 2) mouse_code |= MOUSE_MIDDLE;
2581 if (button & 4) mouse_code |= MOUSE_RIGHT;
2582 if (button & 8) mouse_code |= MOUSE_SHIFT;
2583 if (button & 16) mouse_code |= MOUSE_CTRL;
2584 if ((button & 7) != 0)
2585 {
2586 held_button = mouse_code;
2587 mouse_code |= MOUSE_DRAG;
2588 }
2589 is_drag = TRUE;
2590 showmode();
2591 break;
2592 case 'd': // Button Down
2593 if (button & 1) mouse_code |= MOUSE_LEFT;
2594 if (button & 2) mouse_code |= MOUSE_MIDDLE;
2595 if (button & 4) mouse_code |= MOUSE_RIGHT;
2596 if (button & 8) mouse_code |= MOUSE_SHIFT;
2597 if (button & 16) mouse_code |= MOUSE_CTRL;
2598 break;
2599 case 'u': // Button Up
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002600 is_release = TRUE;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002601 if (button & 1)
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002602 mouse_code |= MOUSE_LEFT;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002603 if (button & 2)
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002604 mouse_code |= MOUSE_MIDDLE;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002605 if (button & 4)
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002606 mouse_code |= MOUSE_RIGHT;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002607 if (button & 8)
2608 mouse_code |= MOUSE_SHIFT;
2609 if (button & 16)
2610 mouse_code |= MOUSE_CTRL;
2611 break;
2612 default: return -1; // Unknown Result
2613 }
2614
2615 *slen += (p - (tp + *slen));
2616 }
2617# endif // FEAT_MOUSE_JSB
2618# ifdef FEAT_MOUSE_DEC
2619 if (key_name[0] == KS_DEC_MOUSE)
2620 {
2621 /*
2622 * The DEC Locator Input Model
2623 * Netterm delivers the code sequence:
2624 * \033[2;4;24;80&w (left button down)
2625 * \033[3;0;24;80&w (left button up)
2626 * \033[6;1;24;80&w (right button down)
2627 * \033[7;0;24;80&w (right button up)
2628 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
2629 * Pe is the event code
2630 * Pb is the button code
2631 * Pr is the row coordinate
2632 * Pc is the column coordinate
2633 * Pp is the third coordinate (page number)
2634 * Pe, the event code indicates what event caused this report
2635 * The following event codes are defined:
Bram Moolenaar13c04632020-07-12 13:47:42 +02002636 * 0 - request, the terminal received an explicit request for a
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002637 * locator report, but the locator is unavailable
Bram Moolenaar13c04632020-07-12 13:47:42 +02002638 * 1 - request, the terminal received an explicit request for a
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002639 * locator report
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002640 * 2 - left button down
2641 * 3 - left button up
2642 * 4 - middle button down
2643 * 5 - middle button up
2644 * 6 - right button down
2645 * 7 - right button up
2646 * 8 - fourth button down
2647 * 9 - fourth button up
2648 * 10 - locator outside filter rectangle
Bram Moolenaar13c04632020-07-12 13:47:42 +02002649 * Pb, the button code, ASCII decimal 0-15 indicating which buttons are
2650 * down if any. The state of the four buttons on the locator
2651 * correspond to the low four bits of the decimal value, "1" means
2652 * button depressed
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002653 * 0 - no buttons down,
2654 * 1 - right,
2655 * 2 - middle,
2656 * 4 - left,
2657 * 8 - fourth
2658 * Pr is the row coordinate of the locator position in the page,
Bram Moolenaar13c04632020-07-12 13:47:42 +02002659 * encoded as an ASCII decimal value. If Pr is omitted, the locator
2660 * position is undefined (outside the terminal window for example).
2661 * Pc is the column coordinate of the locator position in the page,
2662 * encoded as an ASCII decimal value. If Pc is omitted, the locator
2663 * position is undefined (outside the terminal window for example).
2664 * Pp is the page coordinate of the locator position encoded as an
2665 * ASCII decimal value. The page coordinate may be omitted if the
2666 * locator is on page one (the default). We ignore it anyway.
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002667 */
2668 int Pe, Pb, Pr, Pc;
2669
2670 p = tp + *slen;
2671
2672 // get event status
2673 Pe = getdigits(&p);
2674 if (*p++ != ';')
2675 return -1;
2676
2677 // get button status
2678 Pb = getdigits(&p);
2679 if (*p++ != ';')
2680 return -1;
2681
2682 // get row status
2683 Pr = getdigits(&p);
2684 if (*p++ != ';')
2685 return -1;
2686
2687 // get column status
2688 Pc = getdigits(&p);
2689
2690 // the page parameter is optional
2691 if (*p == ';')
2692 {
2693 p++;
2694 (void)getdigits(&p);
2695 }
2696 if (*p++ != '&')
2697 return -1;
2698 if (*p++ != 'w')
2699 return -1;
2700
2701 mouse_code = 0;
2702 switch (Pe)
2703 {
2704 case 0: return -1; // position request while unavailable
2705 case 1: // a response to a locator position request includes
2706 // the status of all buttons
2707 Pb &= 7; // mask off and ignore fourth button
2708 if (Pb & 4)
2709 mouse_code = MOUSE_LEFT;
2710 if (Pb & 2)
2711 mouse_code = MOUSE_MIDDLE;
2712 if (Pb & 1)
2713 mouse_code = MOUSE_RIGHT;
2714 if (Pb)
2715 {
2716 held_button = mouse_code;
2717 mouse_code |= MOUSE_DRAG;
2718 WantQueryMouse = TRUE;
2719 }
2720 is_drag = TRUE;
2721 showmode();
2722 break;
2723 case 2: mouse_code = MOUSE_LEFT;
2724 WantQueryMouse = TRUE;
2725 break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002726 case 3: mouse_code = MOUSE_LEFT;
2727 is_release = TRUE;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002728 break;
2729 case 4: mouse_code = MOUSE_MIDDLE;
2730 WantQueryMouse = TRUE;
2731 break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002732 case 5: mouse_code = MOUSE_MIDDLE;
2733 is_release = TRUE;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002734 break;
2735 case 6: mouse_code = MOUSE_RIGHT;
2736 WantQueryMouse = TRUE;
2737 break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002738 case 7: mouse_code = MOUSE_RIGHT;
2739 is_release = TRUE;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002740 break;
2741 case 8: return -1; // fourth button down
2742 case 9: return -1; // fourth button up
2743 case 10: return -1; // mouse outside of filter rectangle
2744 default: return -1; // should never occur
2745 }
2746
2747 mouse_col = Pc - 1;
2748 mouse_row = Pr - 1;
2749
2750 *slen += (int)(p - (tp + *slen));
2751 }
2752# endif // FEAT_MOUSE_DEC
2753# ifdef FEAT_MOUSE_PTERM
2754 if (key_name[0] == KS_PTERM_MOUSE)
2755 {
2756 int button, num_clicks, action;
2757
2758 p = tp + *slen;
2759
2760 action = getdigits(&p);
2761 if (*p++ != ';')
2762 return -1;
2763
2764 mouse_row = getdigits(&p);
2765 if (*p++ != ';')
2766 return -1;
2767 mouse_col = getdigits(&p);
2768 if (*p++ != ';')
2769 return -1;
2770
2771 button = getdigits(&p);
2772 mouse_code = 0;
2773
2774 switch (button)
2775 {
2776 case 4: mouse_code = MOUSE_LEFT; break;
2777 case 1: mouse_code = MOUSE_RIGHT; break;
2778 case 2: mouse_code = MOUSE_MIDDLE; break;
2779 default: return -1;
2780 }
2781
2782 switch (action)
2783 {
2784 case 31: // Initial press
2785 if (*p++ != ';')
2786 return -1;
2787
2788 num_clicks = getdigits(&p); // Not used
2789 break;
2790
2791 case 32: // Release
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002792 is_release = TRUE;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002793 break;
2794
2795 case 33: // Drag
2796 held_button = mouse_code;
2797 mouse_code |= MOUSE_DRAG;
2798 break;
2799
2800 default:
2801 return -1;
2802 }
2803
2804 if (*p++ != 't')
2805 return -1;
2806
2807 *slen += (p - (tp + *slen));
2808 }
2809# endif // FEAT_MOUSE_PTERM
2810
2811 // Interpret the mouse code
2812 current_button = (mouse_code & MOUSE_CLICK_MASK);
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002813 if (is_release)
2814 current_button |= MOUSE_RELEASE;
2815
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002816 if (current_button == MOUSE_RELEASE
2817# ifdef FEAT_MOUSE_XTERM
2818 && wheel_code == 0
2819# endif
2820 )
2821 {
2822 /*
Bram Moolenaar13c04632020-07-12 13:47:42 +02002823 * If we get a mouse drag or release event when there is no mouse
2824 * button held down (held_button == MOUSE_RELEASE), produce a K_IGNORE
2825 * below.
2826 * (can happen when you hold down two buttons and then let them go, or
2827 * click in the menu bar, but not on a menu, and drag into the text).
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002828 */
2829 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
2830 is_drag = TRUE;
2831 current_button = held_button;
2832 }
Bram Moolenaard6212b82022-08-03 15:48:33 +01002833 else
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002834 {
Bram Moolenaard6212b82022-08-03 15:48:33 +01002835 if (wheel_code == 0)
2836 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002837# ifdef CHECK_DOUBLE_CLICK
2838# ifdef FEAT_MOUSE_GPM
2839 /*
Bram Moolenaar13c04632020-07-12 13:47:42 +02002840 * Only for Unix, when GUI not active, we handle multi-clicks here, but
2841 * not for GPM mouse events.
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002842 */
2843# ifdef FEAT_GUI
2844 if (key_name[0] != KS_GPM_MOUSE && !gui.in_use)
2845# else
2846 if (key_name[0] != KS_GPM_MOUSE)
2847# endif
2848# else
2849# ifdef FEAT_GUI
2850 if (!gui.in_use)
2851# endif
2852# endif
2853 {
2854 /*
2855 * Compute the time elapsed since the previous mouse click.
2856 */
2857 gettimeofday(&mouse_time, NULL);
2858 if (orig_mouse_time.tv_sec == 0)
2859 {
2860 /*
2861 * Avoid computing the difference between mouse_time
2862 * and orig_mouse_time for the first click, as the
2863 * difference would be huge and would cause
2864 * multiplication overflow.
2865 */
2866 timediff = p_mouset;
2867 }
2868 else
Bram Moolenaar85c35022019-11-22 22:21:59 +01002869 timediff = time_diff_ms(&orig_mouse_time, &mouse_time);
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002870 orig_mouse_time = mouse_time;
2871 if (mouse_code == orig_mouse_code
2872 && timediff < p_mouset
2873 && orig_num_clicks != 4
2874 && orig_mouse_col == mouse_col
2875 && orig_mouse_row == mouse_row
2876 && (is_mouse_topline(curwin)
2877 // Double click in tab pages line also works
2878 // when window contents changes.
2879 || (mouse_row == 0 && firstwin->w_winrow > 0))
2880 )
2881 ++orig_num_clicks;
2882 else
2883 orig_num_clicks = 1;
2884 orig_mouse_col = mouse_col;
2885 orig_mouse_row = mouse_row;
2886 set_mouse_topline(curwin);
2887 }
2888# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
2889 else
2890 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
2891# endif
2892# else
2893 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
2894# endif
2895 is_click = TRUE;
Bram Moolenaard6212b82022-08-03 15:48:33 +01002896 }
2897 orig_mouse_code = mouse_code;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002898 }
2899 if (!is_drag)
2900 held_button = mouse_code & MOUSE_CLICK_MASK;
2901
2902 /*
2903 * Translate the actual mouse event into a pseudo mouse event.
2904 * First work out what modifiers are to be used.
2905 */
2906 if (orig_mouse_code & MOUSE_SHIFT)
2907 *modifiers |= MOD_MASK_SHIFT;
2908 if (orig_mouse_code & MOUSE_CTRL)
2909 *modifiers |= MOD_MASK_CTRL;
2910 if (orig_mouse_code & MOUSE_ALT)
2911 *modifiers |= MOD_MASK_ALT;
2912 if (orig_num_clicks == 2)
2913 *modifiers |= MOD_MASK_2CLICK;
2914 else if (orig_num_clicks == 3)
2915 *modifiers |= MOD_MASK_3CLICK;
2916 else if (orig_num_clicks == 4)
2917 *modifiers |= MOD_MASK_4CLICK;
2918
Bram Moolenaar13c04632020-07-12 13:47:42 +02002919 // Work out our pseudo mouse event. Note that MOUSE_RELEASE gets added,
2920 // then it's not mouse up/down.
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002921 key_name[0] = KS_EXTRA;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002922 if (wheel_code != 0 && (!is_release || release_is_ambiguous))
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002923 {
2924 if (wheel_code & MOUSE_CTRL)
2925 *modifiers |= MOD_MASK_CTRL;
2926 if (wheel_code & MOUSE_ALT)
2927 *modifiers |= MOD_MASK_ALT;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002928
2929 if (wheel_code & 1 && wheel_code & 2)
2930 key_name[1] = (int)KE_MOUSELEFT;
2931 else if (wheel_code & 2)
2932 key_name[1] = (int)KE_MOUSERIGHT;
2933 else if (wheel_code & 1)
2934 key_name[1] = (int)KE_MOUSEUP;
2935 else
2936 key_name[1] = (int)KE_MOUSEDOWN;
2937
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002938 held_button = MOUSE_RELEASE;
2939 }
2940 else
Bram Moolenaar13c04632020-07-12 13:47:42 +02002941 key_name[1] = get_pseudo_mouse_code(current_button, is_click, is_drag);
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002942
Bram Moolenaar13c04632020-07-12 13:47:42 +02002943
2944 // Make sure the mouse position is valid. Some terminals may return weird
2945 // values.
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02002946 if (mouse_col >= Columns)
2947 mouse_col = Columns - 1;
2948 if (mouse_row >= Rows)
2949 mouse_row = Rows - 1;
2950
2951 return 0;
2952}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002953
2954// Functions also used for popup windows.
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002955
2956/*
2957 * Compute the buffer line position from the screen position "rowp" / "colp" in
2958 * window "win".
Bram Moolenaar452143c2020-07-15 17:38:21 +02002959 * "plines_cache" can be NULL (no cache) or an array with "Rows" entries that
2960 * caches the plines_win() result from a previous call. Entry is zero if not
2961 * computed yet. There must be no text or setting changes since the entry is
2962 * put in the cache.
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002963 * Returns TRUE if the position is below the last line.
2964 */
2965 int
2966mouse_comp_pos(
2967 win_T *win,
2968 int *rowp,
2969 int *colp,
2970 linenr_T *lnump,
2971 int *plines_cache)
2972{
2973 int col = *colp;
2974 int row = *rowp;
2975 linenr_T lnum;
2976 int retval = FALSE;
2977 int off;
2978 int count;
2979
2980#ifdef FEAT_RIGHTLEFT
2981 if (win->w_p_rl)
2982 col = win->w_width - 1 - col;
2983#endif
2984
2985 lnum = win->w_topline;
2986
2987 while (row > 0)
2988 {
2989 int cache_idx = lnum - win->w_topline;
2990
Bram Moolenaar452143c2020-07-15 17:38:21 +02002991 // Only "Rows" lines are cached, with folding we'll run out of entries
2992 // and use the slow way.
2993 if (plines_cache != NULL && cache_idx < Rows
2994 && plines_cache[cache_idx] > 0)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002995 count = plines_cache[cache_idx];
2996 else
2997 {
2998#ifdef FEAT_DIFF
2999 // Don't include filler lines in "count"
3000 if (win->w_p_diff
3001# ifdef FEAT_FOLDING
3002 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
3003# endif
3004 )
3005 {
3006 if (lnum == win->w_topline)
3007 row -= win->w_topfill;
3008 else
3009 row -= diff_check_fill(win, lnum);
3010 count = plines_win_nofill(win, lnum, TRUE);
3011 }
3012 else
3013#endif
3014 count = plines_win(win, lnum, TRUE);
Bram Moolenaar452143c2020-07-15 17:38:21 +02003015 if (plines_cache != NULL && cache_idx < Rows)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003016 plines_cache[cache_idx] = count;
3017 }
3018 if (count > row)
3019 break; // Position is in this buffer line.
3020#ifdef FEAT_FOLDING
3021 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
3022#endif
3023 if (lnum == win->w_buffer->b_ml.ml_line_count)
3024 {
3025 retval = TRUE;
3026 break; // past end of file
3027 }
3028 row -= count;
3029 ++lnum;
3030 }
3031
3032 if (!retval)
3033 {
3034 // Compute the column without wrapping.
3035 off = win_col_off(win) - win_col_off2(win);
3036 if (col < off)
3037 col = off;
3038 col += row * (win->w_width - off);
3039 // add skip column (for long wrapping line)
3040 col += win->w_skipcol;
3041 }
3042
3043 if (!win->w_p_wrap)
3044 col += win->w_leftcol;
3045
3046 // skip line number and fold column in front of the line
3047 col -= win_col_off(win);
Bram Moolenaardbfa7952020-11-02 20:04:22 +01003048 if (col <= 0)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003049 {
3050#ifdef FEAT_NETBEANS_INTG
Bram Moolenaardbfa7952020-11-02 20:04:22 +01003051 // if mouse is clicked on the gutter, then inform the netbeans server
3052 if (*colp < win_col_off(win))
3053 netbeans_gutter_click(lnum);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003054#endif
3055 col = 0;
3056 }
3057
3058 *colp = col;
3059 *rowp = row;
3060 *lnump = lnum;
3061 return retval;
3062}
3063
3064/*
3065 * Find the window at screen position "*rowp" and "*colp". The positions are
3066 * updated to become relative to the top-left of the window.
3067 * When "popup" is FAIL_POPUP and the position is in a popup window then NULL
3068 * is returned. When "popup" is IGNORE_POPUP then do not even check popup
3069 * windows.
3070 * Returns NULL when something is wrong.
3071 */
3072 win_T *
3073mouse_find_win(int *rowp, int *colp, mouse_find_T popup UNUSED)
3074{
3075 frame_T *fp;
3076 win_T *wp;
3077
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003078#ifdef FEAT_PROP_POPUP
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003079 win_T *pwp = NULL;
3080
3081 if (popup != IGNORE_POPUP)
3082 {
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003083 popup_reset_handled(POPUP_HANDLED_1);
3084 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_1)) != NULL)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003085 {
3086 if (*rowp >= wp->w_winrow && *rowp < wp->w_winrow + popup_height(wp)
3087 && *colp >= wp->w_wincol
3088 && *colp < wp->w_wincol + popup_width(wp))
3089 pwp = wp;
3090 }
3091 if (pwp != NULL)
3092 {
3093 if (popup == FAIL_POPUP)
3094 return NULL;
3095 *rowp -= pwp->w_winrow;
3096 *colp -= pwp->w_wincol;
3097 return pwp;
3098 }
3099 }
3100#endif
3101
3102 fp = topframe;
3103 *rowp -= firstwin->w_winrow;
3104 for (;;)
3105 {
3106 if (fp->fr_layout == FR_LEAF)
3107 break;
3108 if (fp->fr_layout == FR_ROW)
3109 {
3110 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3111 {
3112 if (*colp < fp->fr_width)
3113 break;
3114 *colp -= fp->fr_width;
3115 }
3116 }
3117 else // fr_layout == FR_COL
3118 {
3119 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3120 {
3121 if (*rowp < fp->fr_height)
3122 break;
3123 *rowp -= fp->fr_height;
3124 }
3125 }
3126 }
3127 // When using a timer that closes a window the window might not actually
3128 // exist.
3129 FOR_ALL_WINDOWS(wp)
3130 if (wp == fp->fr_win)
3131 {
3132#ifdef FEAT_MENU
3133 *rowp -= wp->w_winbar_height;
3134#endif
3135 return wp;
3136 }
3137 return NULL;
3138}
3139
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003140#if defined(NEED_VCOL2COL) || defined(FEAT_BEVAL) || defined(FEAT_PROP_POPUP) \
Bram Moolenaar424da7a2022-03-13 19:08:48 +00003141 || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003142/*
3143 * Convert a virtual (screen) column to a character column.
3144 * The first column is one.
3145 */
3146 int
3147vcol2col(win_T *wp, linenr_T lnum, int vcol)
3148{
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003149 char_u *line;
3150 chartabsize_T cts;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003151
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003152 // try to advance to the specified column
3153 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3154 init_chartabsize_arg(&cts, wp, lnum, 0, line, line);
3155 while (cts.cts_vcol < vcol && *cts.cts_ptr != NUL)
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003156 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003157 cts.cts_vcol += win_lbr_chartabsize(&cts, NULL);
3158 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003159 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003160 clear_chartabsize_arg(&cts);
3161
3162 return (int)(cts.cts_ptr - line);
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003163}
3164#endif
Bram Moolenaardb3a2052019-11-16 18:22:41 +01003165
3166#if defined(FEAT_EVAL) || defined(PROTO)
3167 void
3168f_getmousepos(typval_T *argvars UNUSED, typval_T *rettv)
3169{
3170 dict_T *d;
3171 win_T *wp;
3172 int row = mouse_row;
3173 int col = mouse_col;
3174 varnumber_T winid = 0;
3175 varnumber_T winrow = 0;
3176 varnumber_T wincol = 0;
Bram Moolenaar533870a2022-03-13 15:52:44 +00003177 linenr_T lnum = 0;
Bram Moolenaardb3a2052019-11-16 18:22:41 +01003178 varnumber_T column = 0;
3179
Bram Moolenaar93a10962022-06-16 11:42:09 +01003180 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01003181 return;
3182 d = rettv->vval.v_dict;
3183
3184 dict_add_number(d, "screenrow", (varnumber_T)mouse_row + 1);
3185 dict_add_number(d, "screencol", (varnumber_T)mouse_col + 1);
3186
3187 wp = mouse_find_win(&row, &col, FIND_POPUP);
3188 if (wp != NULL)
3189 {
3190 int top_off = 0;
3191 int left_off = 0;
3192 int height = wp->w_height + wp->w_status_height;
3193
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003194#ifdef FEAT_PROP_POPUP
Bram Moolenaardb3a2052019-11-16 18:22:41 +01003195 if (WIN_IS_POPUP(wp))
3196 {
3197 top_off = popup_top_extra(wp);
3198 left_off = popup_left_extra(wp);
3199 height = popup_height(wp);
3200 }
3201#endif
3202 if (row < height)
3203 {
3204 winid = wp->w_id;
3205 winrow = row + 1;
3206 wincol = col + 1;
3207 row -= top_off;
3208 col -= left_off;
3209 if (row >= 0 && row < wp->w_height && col >= 0 && col < wp->w_width)
3210 {
Sean Dewar10792fe2022-03-15 09:46:54 +00003211 (void)mouse_comp_pos(wp, &row, &col, &lnum, NULL);
3212 col = vcol2col(wp, lnum, col);
Bram Moolenaardb3a2052019-11-16 18:22:41 +01003213 column = col + 1;
3214 }
3215 }
3216 }
3217 dict_add_number(d, "winid", winid);
3218 dict_add_number(d, "winrow", winrow);
3219 dict_add_number(d, "wincol", wincol);
Bram Moolenaar533870a2022-03-13 15:52:44 +00003220 dict_add_number(d, "line", (varnumber_T)lnum);
Bram Moolenaardb3a2052019-11-16 18:22:41 +01003221 dict_add_number(d, "column", column);
3222}
3223#endif