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