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