blob: bf3973b021acd56800790947f52a254ed874cc87 [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
16#if defined(FEAT_MOUSE) || defined(PROTO)
17
Bram Moolenaarb20b9e12019-09-21 20:48:04 +020018/*
19 * Get class of a character for selection: same class means same word.
20 * 0: blank
21 * 1: punctuation groups
22 * 2: normal word character
23 * >2: multi-byte word character.
24 */
25 static int
26get_mouse_class(char_u *p)
27{
28 int c;
29
30 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
31 return mb_get_class(p);
32
33 c = *p;
34 if (c == ' ' || c == '\t')
35 return 0;
36
37 if (vim_iswordc(c))
38 return 2;
39
40 // There are a few special cases where we want certain combinations of
41 // characters to be considered as a single word. These are things like
42 // "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
43 // character is in its own class.
44 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
45 return 1;
46 return c;
47}
48
49/*
50 * Move "pos" back to the start of the word it's in.
51 */
52 static void
53find_start_of_word(pos_T *pos)
54{
55 char_u *line;
56 int cclass;
57 int col;
58
59 line = ml_get(pos->lnum);
60 cclass = get_mouse_class(line + pos->col);
61
62 while (pos->col > 0)
63 {
64 col = pos->col - 1;
65 col -= (*mb_head_off)(line, line + col);
66 if (get_mouse_class(line + col) != cclass)
67 break;
68 pos->col = col;
69 }
70}
71
72/*
73 * Move "pos" forward to the end of the word it's in.
74 * When 'selection' is "exclusive", the position is just after the word.
75 */
76 static void
77find_end_of_word(pos_T *pos)
78{
79 char_u *line;
80 int cclass;
81 int col;
82
83 line = ml_get(pos->lnum);
84 if (*p_sel == 'e' && pos->col > 0)
85 {
86 --pos->col;
87 pos->col -= (*mb_head_off)(line, line + pos->col);
88 }
89 cclass = get_mouse_class(line + pos->col);
90 while (line[pos->col] != NUL)
91 {
92 col = pos->col + (*mb_ptr2len)(line + pos->col);
93 if (get_mouse_class(line + col) != cclass)
94 {
95 if (*p_sel == 'e')
96 pos->col = col;
97 break;
98 }
99 pos->col = col;
100 }
101}
102
Bram Moolenaar910c3782019-09-22 14:11:50 +0200103#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
104 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
105 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
106 || defined(FEAT_TERM_POPUP_MENU)
107# define USE_POPUP_SETPOS
108# define NEED_VCOL2COL
109
110/*
111 * Translate window coordinates to buffer position without any side effects
112 */
113 static int
114get_fpos_of_mouse(pos_T *mpos)
115{
116 win_T *wp;
117 int row = mouse_row;
118 int col = mouse_col;
119
120 if (row < 0 || col < 0) // check if it makes sense
121 return IN_UNKNOWN;
122
123 // find the window where the row is in
124 wp = mouse_find_win(&row, &col, FAIL_POPUP);
125 if (wp == NULL)
126 return IN_UNKNOWN;
127 // winpos and height may change in win_enter()!
128 if (row >= wp->w_height) // In (or below) status line
129 return IN_STATUS_LINE;
130 if (col >= wp->w_width) // In vertical separator line
131 return IN_SEP_LINE;
132
133 if (wp != curwin)
134 return IN_UNKNOWN;
135
136 // compute the position in the buffer line from the posn on the screen
137 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum, NULL))
138 return IN_STATUS_LINE; // past bottom
139
140 mpos->col = vcol2col(wp, mpos->lnum, col);
141
142 if (mpos->col > 0)
143 --mpos->col;
144 mpos->coladd = 0;
145 return IN_BUFFER;
146}
147#endif
148
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200149/*
150 * Do the appropriate action for the current mouse click in the current mode.
151 * Not used for Command-line mode.
152 *
153 * Normal and Visual Mode:
154 * event modi- position visual change action
155 * fier cursor window
156 * left press - yes end yes
157 * left press C yes end yes "^]" (2)
158 * left press S yes end (popup: extend) yes "*" (2)
159 * left drag - yes start if moved no
160 * left relse - yes start if moved no
161 * middle press - yes if not active no put register
162 * middle press - yes if active no yank and put
163 * right press - yes start or extend yes
164 * right press S yes no change yes "#" (2)
165 * right drag - yes extend no
166 * right relse - yes extend no
167 *
168 * Insert or Replace Mode:
169 * event modi- position visual change action
170 * fier cursor window
171 * left press - yes (cannot be active) yes
172 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
173 * left press S yes (cannot be active) yes "CTRL-O*" (2)
174 * left drag - yes start or extend (1) no CTRL-O (1)
175 * left relse - yes start or extend (1) no CTRL-O (1)
176 * middle press - no (cannot be active) no put register
177 * right press - yes start or extend yes CTRL-O
178 * right press S yes (cannot be active) yes "CTRL-O#" (2)
179 *
180 * (1) only if mouse pointer moved since press
181 * (2) only if click is in same buffer
182 *
183 * Return TRUE if start_arrow() should be called for edit mode.
184 */
185 int
186do_mouse(
187 oparg_T *oap, // operator argument, can be NULL
188 int c, // K_LEFTMOUSE, etc
189 int dir, // Direction to 'put' if necessary
190 long count,
191 int fixindent) // PUT_FIXINDENT if fixing indent necessary
192{
193 static int do_always = FALSE; // ignore 'mouse' setting next time
194 static int got_click = FALSE; // got a click some time back
195
196 int which_button; // MOUSE_LEFT, _MIDDLE or _RIGHT
197 int is_click = FALSE; // If FALSE it's a drag or release event
198 int is_drag = FALSE; // If TRUE it's a drag event
199 int jump_flags = 0; // flags for jump_to_mouse()
200 pos_T start_visual;
201 int moved; // Has cursor moved?
202 int in_status_line; // mouse in status line
203 static int in_tab_line = FALSE; // mouse clicked in tab line
204 int in_sep_line; // mouse in vertical separator line
205 int c1, c2;
206#if defined(FEAT_FOLDING)
207 pos_T save_cursor;
208#endif
209 win_T *old_curwin = curwin;
210 static pos_T orig_cursor;
211 colnr_T leftcol, rightcol;
212 pos_T end_visual;
213 int diff;
214 int old_active = VIsual_active;
215 int old_mode = VIsual_mode;
216 int regname;
217
218#if defined(FEAT_FOLDING)
219 save_cursor = curwin->w_cursor;
220#endif
221
222 // When GUI is active, always recognize mouse events, otherwise:
223 // - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
224 // - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
225 // - For command line and insert mode 'mouse' is checked before calling
226 // do_mouse().
227 if (do_always)
228 do_always = FALSE;
229 else
230#ifdef FEAT_GUI
231 if (!gui.in_use)
232#endif
233 {
234 if (VIsual_active)
235 {
236 if (!mouse_has(MOUSE_VISUAL))
237 return FALSE;
238 }
239 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
240 return FALSE;
241 }
242
243 for (;;)
244 {
245 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
246 if (is_drag)
247 {
248 // If the next character is the same mouse event then use that
249 // one. Speeds up dragging the status line.
250 if (vpeekc() != NUL)
251 {
252 int nc;
253 int save_mouse_row = mouse_row;
254 int save_mouse_col = mouse_col;
255
256 // Need to get the character, peeking doesn't get the actual
257 // one.
258 nc = safe_vgetc();
259 if (c == nc)
260 continue;
261 vungetc(nc);
262 mouse_row = save_mouse_row;
263 mouse_col = save_mouse_col;
264 }
265 }
266 break;
267 }
268
269 if (c == K_MOUSEMOVE)
270 {
271 // Mouse moved without a button pressed.
272#ifdef FEAT_BEVAL_TERM
273 ui_may_remove_balloon();
274 if (p_bevalterm)
275 {
276 profile_setlimit(p_bdlay, &bevalexpr_due);
277 bevalexpr_due_set = TRUE;
278 }
279#endif
280#ifdef FEAT_TEXT_PROP
281 popup_handle_mouse_moved();
282#endif
283 return FALSE;
284 }
285
286#ifdef FEAT_MOUSESHAPE
287 // May have stopped dragging the status or separator line. The pointer is
288 // most likely still on the status or separator line.
289 if (!is_drag && drag_status_line)
290 {
291 drag_status_line = FALSE;
292 update_mouseshape(SHAPE_IDX_STATUS);
293 }
294 if (!is_drag && drag_sep_line)
295 {
296 drag_sep_line = FALSE;
297 update_mouseshape(SHAPE_IDX_VSEP);
298 }
299#endif
300
301 // Ignore drag and release events if we didn't get a click.
302 if (is_click)
303 got_click = TRUE;
304 else
305 {
306 if (!got_click) // didn't get click, ignore
307 return FALSE;
308 if (!is_drag) // release, reset got_click
309 {
310 got_click = FALSE;
311 if (in_tab_line)
312 {
313 in_tab_line = FALSE;
314 return FALSE;
315 }
316 }
317 }
318
319 // CTRL right mouse button does CTRL-T
320 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
321 {
322 if (State & INSERT)
323 stuffcharReadbuff(Ctrl_O);
324 if (count > 1)
325 stuffnumReadbuff(count);
326 stuffcharReadbuff(Ctrl_T);
327 got_click = FALSE; // ignore drag&release now
328 return FALSE;
329 }
330
331 // CTRL only works with left mouse button
332 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
333 return FALSE;
334
335 // When a modifier is down, ignore drag and release events, as well as
336 // multiple clicks and the middle mouse button.
337 // Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
338 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
339 | MOD_MASK_META))
340 && (!is_click
341 || (mod_mask & MOD_MASK_MULTI_CLICK)
342 || which_button == MOUSE_MIDDLE)
343 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
344 && mouse_model_popup()
345 && which_button == MOUSE_LEFT)
346 && !((mod_mask & MOD_MASK_ALT)
347 && !mouse_model_popup()
348 && which_button == MOUSE_RIGHT)
349 )
350 return FALSE;
351
352 // If the button press was used as the movement command for an operator
353 // (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
354 // drag/release events.
355 if (!is_click && which_button == MOUSE_MIDDLE)
356 return FALSE;
357
358 if (oap != NULL)
359 regname = oap->regname;
360 else
361 regname = 0;
362
363 // Middle mouse button does a 'put' of the selected text
364 if (which_button == MOUSE_MIDDLE)
365 {
366 if (State == NORMAL)
367 {
368 // If an operator was pending, we don't know what the user wanted
369 // to do. Go back to normal mode: Clear the operator and beep().
370 if (oap != NULL && oap->op_type != OP_NOP)
371 {
372 clearopbeep(oap);
373 return FALSE;
374 }
375
376 // If visual was active, yank the highlighted text and put it
377 // before the mouse pointer position.
378 // In Select mode replace the highlighted text with the clipboard.
379 if (VIsual_active)
380 {
381 if (VIsual_select)
382 {
383 stuffcharReadbuff(Ctrl_G);
384 stuffReadbuff((char_u *)"\"+p");
385 }
386 else
387 {
388 stuffcharReadbuff('y');
389 stuffcharReadbuff(K_MIDDLEMOUSE);
390 }
391 do_always = TRUE; // ignore 'mouse' setting next time
392 return FALSE;
393 }
394 // The rest is below jump_to_mouse()
395 }
396
397 else if ((State & INSERT) == 0)
398 return FALSE;
399
400 // Middle click in insert mode doesn't move the mouse, just insert the
401 // contents of a register. '.' register is special, can't insert that
402 // with do_put().
403 // Also paste at the cursor if the current mode isn't in 'mouse' (only
404 // happens for the GUI).
405 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
406 {
407 if (regname == '.')
408 insert_reg(regname, TRUE);
409 else
410 {
411#ifdef FEAT_CLIPBOARD
412 if (clip_star.available && regname == 0)
413 regname = '*';
414#endif
415 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
416 insert_reg(regname, TRUE);
417 else
418 {
419 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
420
421 // Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r
422 AppendCharToRedobuff(Ctrl_R);
423 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
424 AppendCharToRedobuff(regname == 0 ? '"' : regname);
425 }
426 }
427 return FALSE;
428 }
429 }
430
431 // When dragging or button-up stay in the same window.
432 if (!is_click)
433 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
434
435 start_visual.lnum = 0;
436
437 // Check for clicking in the tab page line.
438 if (mouse_row == 0 && firstwin->w_winrow > 0)
439 {
440 if (is_drag)
441 {
442 if (in_tab_line)
443 {
444 c1 = TabPageIdxs[mouse_col];
445 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
446 ? c1 - 1 : c1);
447 }
448 return FALSE;
449 }
450
451 // click in a tab selects that tab page
452 if (is_click
453# ifdef FEAT_CMDWIN
454 && cmdwin_type == 0
455# endif
456 && mouse_col < Columns)
457 {
458 in_tab_line = TRUE;
459 c1 = TabPageIdxs[mouse_col];
460 if (c1 >= 0)
461 {
462 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
463 {
464 // double click opens new page
465 end_visual_mode();
466 tabpage_new();
467 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
468 }
469 else
470 {
471 // Go to specified tab page, or next one if not clicking
472 // on a label.
473 goto_tabpage(c1);
474
475 // It's like clicking on the status line of a window.
476 if (curwin != old_curwin)
477 end_visual_mode();
478 }
479 }
480 else
481 {
482 tabpage_T *tp;
483
484 // Close the current or specified tab page.
485 if (c1 == -999)
486 tp = curtab;
487 else
488 tp = find_tabpage(-c1);
489 if (tp == curtab)
490 {
491 if (first_tabpage->tp_next != NULL)
492 tabpage_close(FALSE);
493 }
494 else if (tp != NULL)
495 tabpage_close_other(tp, FALSE);
496 }
497 }
498 return TRUE;
499 }
500 else if (is_drag && in_tab_line)
501 {
502 c1 = TabPageIdxs[mouse_col];
503 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
504 return FALSE;
505 }
506
507 // When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
508 // right button up -> pop-up menu
509 // shift-left button -> right button
510 // alt-left button -> alt-right button
511 if (mouse_model_popup())
512 {
513 if (which_button == MOUSE_RIGHT
514 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
515 {
Bram Moolenaar910c3782019-09-22 14:11:50 +0200516#ifdef USE_POPUP_SETPOS
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200517# ifdef FEAT_GUI
518 if (gui.in_use)
519 {
520# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
521 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
522 if (!is_click)
523 // Ignore right button release events, only shows the popup
524 // menu on the button down event.
525 return FALSE;
526# endif
527# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
528 if (is_click || is_drag)
529 // Ignore right button down and drag mouse events. Windows
530 // only shows the popup menu on the button up event.
531 return FALSE;
532# endif
533 }
534# endif
535# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
536 else
537# endif
538# if defined(FEAT_TERM_POPUP_MENU)
539 if (!is_click)
540 // Ignore right button release events, only shows the popup
541 // menu on the button down event.
542 return FALSE;
543#endif
544
545 jump_flags = 0;
546 if (STRCMP(p_mousem, "popup_setpos") == 0)
547 {
548 // First set the cursor position before showing the popup
549 // menu.
550 if (VIsual_active)
551 {
552 pos_T m_pos;
553
554 // set MOUSE_MAY_STOP_VIS if we are outside the
555 // selection or the current window (might have false
556 // negative here)
557 if (mouse_row < curwin->w_winrow
558 || mouse_row
559 > (curwin->w_winrow + curwin->w_height))
560 jump_flags = MOUSE_MAY_STOP_VIS;
561 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
562 jump_flags = MOUSE_MAY_STOP_VIS;
563 else
564 {
565 if ((LT_POS(curwin->w_cursor, VIsual)
566 && (LT_POS(m_pos, curwin->w_cursor)
567 || LT_POS(VIsual, m_pos)))
568 || (LT_POS(VIsual, curwin->w_cursor)
569 && (LT_POS(m_pos, VIsual)
570 || LT_POS(curwin->w_cursor, m_pos))))
571 {
572 jump_flags = MOUSE_MAY_STOP_VIS;
573 }
574 else if (VIsual_mode == Ctrl_V)
575 {
576 getvcols(curwin, &curwin->w_cursor, &VIsual,
577 &leftcol, &rightcol);
578 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
579 if (m_pos.col < leftcol || m_pos.col > rightcol)
580 jump_flags = MOUSE_MAY_STOP_VIS;
581 }
582 }
583 }
584 else
585 jump_flags = MOUSE_MAY_STOP_VIS;
586 }
587 if (jump_flags)
588 {
589 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
590 update_curbuf(VIsual_active ? INVERTED : VALID);
591 setcursor();
592 out_flush(); // Update before showing popup menu
593 }
594# ifdef FEAT_MENU
595 show_popupmenu();
596 got_click = FALSE; // ignore release events
597# endif
598 return (jump_flags & CURSOR_MOVED) != 0;
599#else
600 return FALSE;
601#endif
602 }
603 if (which_button == MOUSE_LEFT
604 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
605 {
606 which_button = MOUSE_RIGHT;
607 mod_mask &= ~MOD_MASK_SHIFT;
608 }
609 }
610
611 if ((State & (NORMAL | INSERT))
612 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
613 {
614 if (which_button == MOUSE_LEFT)
615 {
616 if (is_click)
617 {
618 // stop Visual mode for a left click in a window, but not when
619 // on a status line
620 if (VIsual_active)
621 jump_flags |= MOUSE_MAY_STOP_VIS;
622 }
623 else if (mouse_has(MOUSE_VISUAL))
624 jump_flags |= MOUSE_MAY_VIS;
625 }
626 else if (which_button == MOUSE_RIGHT)
627 {
628 if (is_click && VIsual_active)
629 {
630 // Remember the start and end of visual before moving the
631 // cursor.
632 if (LT_POS(curwin->w_cursor, VIsual))
633 {
634 start_visual = curwin->w_cursor;
635 end_visual = VIsual;
636 }
637 else
638 {
639 start_visual = VIsual;
640 end_visual = curwin->w_cursor;
641 }
642 }
643 jump_flags |= MOUSE_FOCUS;
644 if (mouse_has(MOUSE_VISUAL))
645 jump_flags |= MOUSE_MAY_VIS;
646 }
647 }
648
649 // If an operator is pending, ignore all drags and releases until the
650 // next mouse click.
651 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
652 {
653 got_click = FALSE;
654 oap->motion_type = MCHAR;
655 }
656
657 // When releasing the button let jump_to_mouse() know.
658 if (!is_click && !is_drag)
659 jump_flags |= MOUSE_RELEASED;
660
661 // JUMP!
662 jump_flags = jump_to_mouse(jump_flags,
663 oap == NULL ? NULL : &(oap->inclusive), which_button);
664
665#ifdef FEAT_MENU
666 // A click in the window toolbar has no side effects.
667 if (jump_flags & MOUSE_WINBAR)
668 return FALSE;
669#endif
670 moved = (jump_flags & CURSOR_MOVED);
671 in_status_line = (jump_flags & IN_STATUS_LINE);
672 in_sep_line = (jump_flags & IN_SEP_LINE);
673
674#ifdef FEAT_NETBEANS_INTG
675 if (isNetbeansBuffer(curbuf)
676 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
677 {
678 int key = KEY2TERMCAP1(c);
679
680 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
681 || key == (int)KE_RIGHTRELEASE)
682 netbeans_button_release(which_button);
683 }
684#endif
685
686 // When jumping to another window, clear a pending operator. That's a bit
687 // friendlier than beeping and not jumping to that window.
688 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
689 clearop(oap);
690
691#ifdef FEAT_FOLDING
692 if (mod_mask == 0
693 && !is_drag
694 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
695 && which_button == MOUSE_LEFT)
696 {
697 // open or close a fold at this line
698 if (jump_flags & MOUSE_FOLD_OPEN)
699 openFold(curwin->w_cursor.lnum, 1L);
700 else
701 closeFold(curwin->w_cursor.lnum, 1L);
702 // don't move the cursor if still in the same window
703 if (curwin == old_curwin)
704 curwin->w_cursor = save_cursor;
705 }
706#endif
707
708#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
709 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
710 {
711 clip_modeless(which_button, is_click, is_drag);
712 return FALSE;
713 }
714#endif
715
716 // Set global flag that we are extending the Visual area with mouse
717 // dragging; temporarily minimize 'scrolloff'.
718 if (VIsual_active && is_drag && get_scrolloff_value())
719 {
720 // In the very first line, allow scrolling one line
721 if (mouse_row == 0)
722 mouse_dragging = 2;
723 else
724 mouse_dragging = 1;
725 }
726
727 // When dragging the mouse above the window, scroll down.
728 if (is_drag && mouse_row < 0 && !in_status_line)
729 {
730 scroll_redraw(FALSE, 1L);
731 mouse_row = 0;
732 }
733
734 if (start_visual.lnum) // right click in visual mode
735 {
736 // When ALT is pressed make Visual mode blockwise.
737 if (mod_mask & MOD_MASK_ALT)
738 VIsual_mode = Ctrl_V;
739
740 // In Visual-block mode, divide the area in four, pick up the corner
741 // that is in the quarter that the cursor is in.
742 if (VIsual_mode == Ctrl_V)
743 {
744 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
745 if (curwin->w_curswant > (leftcol + rightcol) / 2)
746 end_visual.col = leftcol;
747 else
748 end_visual.col = rightcol;
749 if (curwin->w_cursor.lnum >=
750 (start_visual.lnum + end_visual.lnum) / 2)
751 end_visual.lnum = start_visual.lnum;
752
753 // move VIsual to the right column
754 start_visual = curwin->w_cursor; // save the cursor pos
755 curwin->w_cursor = end_visual;
756 coladvance(end_visual.col);
757 VIsual = curwin->w_cursor;
758 curwin->w_cursor = start_visual; // restore the cursor
759 }
760 else
761 {
762 // If the click is before the start of visual, change the start.
763 // If the click is after the end of visual, change the end. If
764 // the click is inside the visual, change the closest side.
765 if (LT_POS(curwin->w_cursor, start_visual))
766 VIsual = end_visual;
767 else if (LT_POS(end_visual, curwin->w_cursor))
768 VIsual = start_visual;
769 else
770 {
771 // In the same line, compare column number
772 if (end_visual.lnum == start_visual.lnum)
773 {
774 if (curwin->w_cursor.col - start_visual.col >
775 end_visual.col - curwin->w_cursor.col)
776 VIsual = start_visual;
777 else
778 VIsual = end_visual;
779 }
780
781 // In different lines, compare line number
782 else
783 {
784 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
785 (end_visual.lnum - curwin->w_cursor.lnum);
786
787 if (diff > 0) // closest to end
788 VIsual = start_visual;
789 else if (diff < 0) // closest to start
790 VIsual = end_visual;
791 else // in the middle line
792 {
793 if (curwin->w_cursor.col <
794 (start_visual.col + end_visual.col) / 2)
795 VIsual = end_visual;
796 else
797 VIsual = start_visual;
798 }
799 }
800 }
801 }
802 }
803 // If Visual mode started in insert mode, execute "CTRL-O"
804 else if ((State & INSERT) && VIsual_active)
805 stuffcharReadbuff(Ctrl_O);
806
807 // Middle mouse click: Put text before cursor.
808 if (which_button == MOUSE_MIDDLE)
809 {
810#ifdef FEAT_CLIPBOARD
811 if (clip_star.available && regname == 0)
812 regname = '*';
813#endif
814 if (yank_register_mline(regname))
815 {
816 if (mouse_past_bottom)
817 dir = FORWARD;
818 }
819 else if (mouse_past_eol)
820 dir = FORWARD;
821
822 if (fixindent)
823 {
824 c1 = (dir == BACKWARD) ? '[' : ']';
825 c2 = 'p';
826 }
827 else
828 {
829 c1 = (dir == FORWARD) ? 'p' : 'P';
830 c2 = NUL;
831 }
832 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
833
834 // Remember where the paste started, so in edit() Insstart can be set
835 // to this position
836 if (restart_edit != 0)
837 where_paste_started = curwin->w_cursor;
838 do_put(regname, dir, count, fixindent | PUT_CURSEND);
839 }
840
841#if defined(FEAT_QUICKFIX)
842 // Ctrl-Mouse click or double click in a quickfix window jumps to the
843 // error under the mouse pointer.
844 else if (((mod_mask & MOD_MASK_CTRL)
845 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
846 && bt_quickfix(curbuf))
847 {
848 if (curwin->w_llist_ref == NULL) // quickfix window
849 do_cmdline_cmd((char_u *)".cc");
850 else // location list window
851 do_cmdline_cmd((char_u *)".ll");
852 got_click = FALSE; // ignore drag&release now
853 }
854#endif
855
856 // Ctrl-Mouse click (or double click in a help window) jumps to the tag
857 // under the mouse pointer.
858 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
859 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
860 {
861 if (State & INSERT)
862 stuffcharReadbuff(Ctrl_O);
863 stuffcharReadbuff(Ctrl_RSB);
864 got_click = FALSE; // ignore drag&release now
865 }
866
867 // Shift-Mouse click searches for the next occurrence of the word under
868 // the mouse pointer
869 else if ((mod_mask & MOD_MASK_SHIFT))
870 {
871 if ((State & INSERT) || (VIsual_active && VIsual_select))
872 stuffcharReadbuff(Ctrl_O);
873 if (which_button == MOUSE_LEFT)
874 stuffcharReadbuff('*');
875 else // MOUSE_RIGHT
876 stuffcharReadbuff('#');
877 }
878
879 // Handle double clicks, unless on status line
880 else if (in_status_line)
881 {
882#ifdef FEAT_MOUSESHAPE
883 if ((is_drag || is_click) && !drag_status_line)
884 {
885 drag_status_line = TRUE;
886 update_mouseshape(-1);
887 }
888#endif
889 }
890 else if (in_sep_line)
891 {
892#ifdef FEAT_MOUSESHAPE
893 if ((is_drag || is_click) && !drag_sep_line)
894 {
895 drag_sep_line = TRUE;
896 update_mouseshape(-1);
897 }
898#endif
899 }
900 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
901 && mouse_has(MOUSE_VISUAL))
902 {
903 if (is_click || !VIsual_active)
904 {
905 if (VIsual_active)
906 orig_cursor = VIsual;
907 else
908 {
909 check_visual_highlight();
910 VIsual = curwin->w_cursor;
911 orig_cursor = VIsual;
912 VIsual_active = TRUE;
913 VIsual_reselect = TRUE;
914 // start Select mode if 'selectmode' contains "mouse"
915 may_start_select('o');
916 setmouse();
917 }
918 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
919 {
920 // Double click with ALT pressed makes it blockwise.
921 if (mod_mask & MOD_MASK_ALT)
922 VIsual_mode = Ctrl_V;
923 else
924 VIsual_mode = 'v';
925 }
926 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
927 VIsual_mode = 'V';
928 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
929 VIsual_mode = Ctrl_V;
930#ifdef FEAT_CLIPBOARD
931 // Make sure the clipboard gets updated. Needed because start and
932 // end may still be the same, and the selection needs to be owned
933 clip_star.vmode = NUL;
934#endif
935 }
936 // A double click selects a word or a block.
937 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
938 {
939 pos_T *pos = NULL;
940 int gc;
941
942 if (is_click)
943 {
944 // If the character under the cursor (skipping white space) is
945 // not a word character, try finding a match and select a (),
946 // {}, [], #if/#endif, etc. block.
947 end_visual = curwin->w_cursor;
948 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
949 inc(&end_visual);
950 if (oap != NULL)
951 oap->motion_type = MCHAR;
952 if (oap != NULL
953 && VIsual_mode == 'v'
954 && !vim_iswordc(gchar_pos(&end_visual))
955 && EQUAL_POS(curwin->w_cursor, VIsual)
956 && (pos = findmatch(oap, NUL)) != NULL)
957 {
958 curwin->w_cursor = *pos;
959 if (oap->motion_type == MLINE)
960 VIsual_mode = 'V';
961 else if (*p_sel == 'e')
962 {
963 if (LT_POS(curwin->w_cursor, VIsual))
964 ++VIsual.col;
965 else
966 ++curwin->w_cursor.col;
967 }
968 }
969 }
970
971 if (pos == NULL && (is_click || is_drag))
972 {
973 // When not found a match or when dragging: extend to include
974 // a word.
975 if (LT_POS(curwin->w_cursor, orig_cursor))
976 {
977 find_start_of_word(&curwin->w_cursor);
978 find_end_of_word(&VIsual);
979 }
980 else
981 {
982 find_start_of_word(&VIsual);
983 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
984 curwin->w_cursor.col +=
985 (*mb_ptr2len)(ml_get_cursor());
986 find_end_of_word(&curwin->w_cursor);
987 }
988 }
989 curwin->w_set_curswant = TRUE;
990 }
991 if (is_click)
992 redraw_curbuf_later(INVERTED); // update the inversion
993 }
994 else if (VIsual_active && !old_active)
995 {
996 if (mod_mask & MOD_MASK_ALT)
997 VIsual_mode = Ctrl_V;
998 else
999 VIsual_mode = 'v';
1000 }
1001
1002 // If Visual mode changed show it later.
1003 if ((!VIsual_active && old_active && mode_displayed)
1004 || (VIsual_active && p_smd && msg_silent == 0
1005 && (!old_active || VIsual_mode != old_mode)))
1006 redraw_cmdline = TRUE;
1007
1008 return moved;
1009}
1010
1011 void
1012ins_mouse(int c)
1013{
1014 pos_T tpos;
1015 win_T *old_curwin = curwin;
1016
1017# ifdef FEAT_GUI
1018 // When GUI is active, also move/paste when 'mouse' is empty
1019 if (!gui.in_use)
1020# endif
1021 if (!mouse_has(MOUSE_INSERT))
1022 return;
1023
1024 undisplay_dollar();
1025 tpos = curwin->w_cursor;
1026 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
1027 {
1028 win_T *new_curwin = curwin;
1029
1030 if (curwin != old_curwin && win_valid(old_curwin))
1031 {
1032 // Mouse took us to another window. We need to go back to the
1033 // previous one to stop insert there properly.
1034 curwin = old_curwin;
1035 curbuf = curwin->w_buffer;
1036#ifdef FEAT_JOB_CHANNEL
1037 if (bt_prompt(curbuf))
1038 // Restart Insert mode when re-entering the prompt buffer.
1039 curbuf->b_prompt_insert = 'A';
1040#endif
1041 }
1042 start_arrow(curwin == old_curwin ? &tpos : NULL);
1043 if (curwin != new_curwin && win_valid(new_curwin))
1044 {
1045 curwin = new_curwin;
1046 curbuf = curwin->w_buffer;
1047 }
1048# ifdef FEAT_CINDENT
1049 set_can_cindent(TRUE);
1050# endif
1051 }
1052
1053 // redraw status lines (in case another window became active)
1054 redraw_statuslines();
1055}
1056
1057 void
1058ins_mousescroll(int dir)
1059{
1060 pos_T tpos;
1061 win_T *old_curwin = curwin, *wp;
1062 int did_scroll = FALSE;
1063
1064 tpos = curwin->w_cursor;
1065
1066 if (mouse_row >= 0 && mouse_col >= 0)
1067 {
1068 int row, col;
1069
1070 row = mouse_row;
1071 col = mouse_col;
1072
1073 // find the window at the pointer coordinates
1074 wp = mouse_find_win(&row, &col, FIND_POPUP);
1075 if (wp == NULL)
1076 return;
1077 curwin = wp;
1078 curbuf = curwin->w_buffer;
1079 }
1080 if (curwin == old_curwin)
1081 undisplay_dollar();
1082
1083 // Don't scroll the window in which completion is being done.
1084 if (!pum_visible() || curwin != old_curwin)
1085 {
1086 if (dir == MSCR_DOWN || dir == MSCR_UP)
1087 {
1088 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
1089 scroll_redraw(dir,
1090 (long)(curwin->w_botline - curwin->w_topline));
1091 else
1092 scroll_redraw(dir, 3L);
1093# ifdef FEAT_TEXT_PROP
1094 if (WIN_IS_POPUP(curwin))
1095 popup_set_firstline(curwin);
1096# endif
1097 }
1098#ifdef FEAT_GUI
1099 else
1100 {
1101 int val, step = 6;
1102
1103 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
1104 step = curwin->w_width;
1105 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
1106 if (val < 0)
1107 val = 0;
1108 gui_do_horiz_scroll(val, TRUE);
1109 }
1110#endif
1111 did_scroll = TRUE;
1112 }
1113
1114 curwin->w_redr_status = TRUE;
1115
1116 curwin = old_curwin;
1117 curbuf = curwin->w_buffer;
1118
1119 // The popup menu may overlay the window, need to redraw it.
1120 // TODO: Would be more efficient to only redraw the windows that are
1121 // overlapped by the popup menu.
1122 if (pum_visible() && did_scroll)
1123 {
1124 redraw_all_later(NOT_VALID);
1125 ins_compl_show_pum();
1126 }
1127
1128 if (!EQUAL_POS(curwin->w_cursor, tpos))
1129 {
1130 start_arrow(&tpos);
1131# ifdef FEAT_CINDENT
1132 set_can_cindent(TRUE);
1133# endif
1134 }
1135}
1136
1137/*
1138 * Return TRUE if "c" is a mouse key.
1139 */
1140 int
1141is_mouse_key(int c)
1142{
1143 return c == K_LEFTMOUSE
1144 || c == K_LEFTMOUSE_NM
1145 || c == K_LEFTDRAG
1146 || c == K_LEFTRELEASE
1147 || c == K_LEFTRELEASE_NM
1148 || c == K_MOUSEMOVE
1149 || c == K_MIDDLEMOUSE
1150 || c == K_MIDDLEDRAG
1151 || c == K_MIDDLERELEASE
1152 || c == K_RIGHTMOUSE
1153 || c == K_RIGHTDRAG
1154 || c == K_RIGHTRELEASE
1155 || c == K_MOUSEDOWN
1156 || c == K_MOUSEUP
1157 || c == K_MOUSELEFT
1158 || c == K_MOUSERIGHT
1159 || c == K_X1MOUSE
1160 || c == K_X1DRAG
1161 || c == K_X1RELEASE
1162 || c == K_X2MOUSE
1163 || c == K_X2DRAG
1164 || c == K_X2RELEASE;
1165}
1166
1167static struct mousetable
1168{
1169 int pseudo_code; // Code for pseudo mouse event
1170 int button; // Which mouse button is it?
1171 int is_click; // Is it a mouse button click event?
1172 int is_drag; // Is it a mouse drag event?
1173} mouse_table[] =
1174{
1175 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
1176#ifdef FEAT_GUI
1177 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
1178#endif
1179 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
1180 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
1181#ifdef FEAT_GUI
1182 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
1183#endif
1184 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
1185 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
1186 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
1187 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
1188 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
1189 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
1190 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
1191 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
1192 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
1193 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
1194 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
1195 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
1196 // DRAG without CLICK
1197 {(int)KE_MOUSEMOVE, MOUSE_RELEASE, FALSE, TRUE},
1198 // RELEASE without CLICK
1199 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
1200 {0, 0, 0, 0},
1201};
1202
1203/*
1204 * Look up the given mouse code to return the relevant information in the other
1205 * arguments. Return which button is down or was released.
1206 */
1207 int
1208get_mouse_button(int code, int *is_click, int *is_drag)
1209{
1210 int i;
1211
1212 for (i = 0; mouse_table[i].pseudo_code; i++)
1213 if (code == mouse_table[i].pseudo_code)
1214 {
1215 *is_click = mouse_table[i].is_click;
1216 *is_drag = mouse_table[i].is_drag;
1217 return mouse_table[i].button;
1218 }
1219 return 0; // Shouldn't get here
1220}
1221
1222/*
1223 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
1224 * the given information about which mouse button is down, and whether the
1225 * mouse was clicked, dragged or released.
1226 */
1227 int
1228get_pseudo_mouse_code(
1229 int button, // eg MOUSE_LEFT
1230 int is_click,
1231 int is_drag)
1232{
1233 int i;
1234
1235 for (i = 0; mouse_table[i].pseudo_code; i++)
1236 if (button == mouse_table[i].button
1237 && is_click == mouse_table[i].is_click
1238 && is_drag == mouse_table[i].is_drag)
1239 {
1240#ifdef FEAT_GUI
1241 // Trick: a non mappable left click and release has mouse_col -1
1242 // or added MOUSE_COLOFF. Used for 'mousefocus' in
1243 // gui_mouse_moved()
1244 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
1245 {
1246 if (mouse_col < 0)
1247 mouse_col = 0;
1248 else
1249 mouse_col -= MOUSE_COLOFF;
1250 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
1251 return (int)KE_LEFTMOUSE_NM;
1252 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
1253 return (int)KE_LEFTRELEASE_NM;
1254 }
1255#endif
1256 return mouse_table[i].pseudo_code;
1257 }
1258 return (int)KE_IGNORE; // not recognized, ignore it
1259}
1260
1261# ifdef FEAT_MOUSE_TTY
1262# define HMT_NORMAL 1
1263# define HMT_NETTERM 2
1264# define HMT_DEC 4
1265# define HMT_JSBTERM 8
1266# define HMT_PTERM 16
1267# define HMT_URXVT 32
1268# define HMT_GPM 64
1269# define HMT_SGR 128
1270# define HMT_SGR_REL 256
1271static int has_mouse_termcode = 0;
1272# endif
1273
1274# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
1275 void
1276set_mouse_termcode(
1277 int n, // KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE
1278 char_u *s)
1279{
1280 char_u name[2];
1281
1282 name[0] = n;
1283 name[1] = KE_FILLER;
1284 add_termcode(name, s, FALSE);
1285# ifdef FEAT_MOUSE_TTY
1286# ifdef FEAT_MOUSE_JSB
1287 if (n == KS_JSBTERM_MOUSE)
1288 has_mouse_termcode |= HMT_JSBTERM;
1289 else
1290# endif
1291# ifdef FEAT_MOUSE_NET
1292 if (n == KS_NETTERM_MOUSE)
1293 has_mouse_termcode |= HMT_NETTERM;
1294 else
1295# endif
1296# ifdef FEAT_MOUSE_DEC
1297 if (n == KS_DEC_MOUSE)
1298 has_mouse_termcode |= HMT_DEC;
1299 else
1300# endif
1301# ifdef FEAT_MOUSE_PTERM
1302 if (n == KS_PTERM_MOUSE)
1303 has_mouse_termcode |= HMT_PTERM;
1304 else
1305# endif
1306# ifdef FEAT_MOUSE_URXVT
1307 if (n == KS_URXVT_MOUSE)
1308 has_mouse_termcode |= HMT_URXVT;
1309 else
1310# endif
1311# ifdef FEAT_MOUSE_GPM
1312 if (n == KS_GPM_MOUSE)
1313 has_mouse_termcode |= HMT_GPM;
1314 else
1315# endif
1316 if (n == KS_SGR_MOUSE)
1317 has_mouse_termcode |= HMT_SGR;
1318 else if (n == KS_SGR_MOUSE_RELEASE)
1319 has_mouse_termcode |= HMT_SGR_REL;
1320 else
1321 has_mouse_termcode |= HMT_NORMAL;
1322# endif
1323}
1324# endif
1325
1326# if ((defined(UNIX) || defined(VMS)) \
1327 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
1328 void
1329del_mouse_termcode(
1330 int n) // KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE
1331{
1332 char_u name[2];
1333
1334 name[0] = n;
1335 name[1] = KE_FILLER;
1336 del_termcode(name);
1337# ifdef FEAT_MOUSE_TTY
1338# ifdef FEAT_MOUSE_JSB
1339 if (n == KS_JSBTERM_MOUSE)
1340 has_mouse_termcode &= ~HMT_JSBTERM;
1341 else
1342# endif
1343# ifdef FEAT_MOUSE_NET
1344 if (n == KS_NETTERM_MOUSE)
1345 has_mouse_termcode &= ~HMT_NETTERM;
1346 else
1347# endif
1348# ifdef FEAT_MOUSE_DEC
1349 if (n == KS_DEC_MOUSE)
1350 has_mouse_termcode &= ~HMT_DEC;
1351 else
1352# endif
1353# ifdef FEAT_MOUSE_PTERM
1354 if (n == KS_PTERM_MOUSE)
1355 has_mouse_termcode &= ~HMT_PTERM;
1356 else
1357# endif
1358# ifdef FEAT_MOUSE_URXVT
1359 if (n == KS_URXVT_MOUSE)
1360 has_mouse_termcode &= ~HMT_URXVT;
1361 else
1362# endif
1363# ifdef FEAT_MOUSE_GPM
1364 if (n == KS_GPM_MOUSE)
1365 has_mouse_termcode &= ~HMT_GPM;
1366 else
1367# endif
1368 if (n == KS_SGR_MOUSE)
1369 has_mouse_termcode &= ~HMT_SGR;
1370 else if (n == KS_SGR_MOUSE_RELEASE)
1371 has_mouse_termcode &= ~HMT_SGR_REL;
1372 else
1373 has_mouse_termcode &= ~HMT_NORMAL;
1374# endif
1375}
1376# endif
1377
1378/*
1379 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
1380 */
1381 void
1382setmouse(void)
1383{
1384# ifdef FEAT_MOUSE_TTY
1385 int checkfor;
1386# endif
1387
1388# ifdef FEAT_MOUSESHAPE
1389 update_mouseshape(-1);
1390# endif
1391
1392# ifdef FEAT_MOUSE_TTY // Should be outside proc, but may break MOUSESHAPE
1393# ifdef FEAT_GUI
1394 // In the GUI the mouse is always enabled.
1395 if (gui.in_use)
1396 return;
1397# endif
1398 // be quick when mouse is off
1399 if (*p_mouse == NUL || has_mouse_termcode == 0)
1400 return;
1401
1402 // don't switch mouse on when not in raw mode (Ex mode)
1403 if (cur_tmode != TMODE_RAW)
1404 {
1405 mch_setmouse(FALSE);
1406 return;
1407 }
1408
1409 if (VIsual_active)
1410 checkfor = MOUSE_VISUAL;
1411 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
1412 checkfor = MOUSE_RETURN;
1413 else if (State & INSERT)
1414 checkfor = MOUSE_INSERT;
1415 else if (State & CMDLINE)
1416 checkfor = MOUSE_COMMAND;
1417 else if (State == CONFIRM || State == EXTERNCMD)
1418 checkfor = ' '; // don't use mouse for ":confirm" or ":!cmd"
1419 else
1420 checkfor = MOUSE_NORMAL; // assume normal mode
1421
1422 if (mouse_has(checkfor))
1423 mch_setmouse(TRUE);
1424 else
1425 mch_setmouse(FALSE);
1426# endif
1427}
1428
1429/*
1430 * Return TRUE if
1431 * - "c" is in 'mouse', or
1432 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
1433 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
1434 * normal editing mode (not at hit-return message).
1435 */
1436 int
1437mouse_has(int c)
1438{
1439 char_u *p;
1440
1441 for (p = p_mouse; *p; ++p)
1442 switch (*p)
1443 {
1444 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
1445 return TRUE;
1446 break;
1447 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
1448 return TRUE;
1449 break;
1450 default: if (c == *p) return TRUE; break;
1451 }
1452 return FALSE;
1453}
1454
1455/*
1456 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
1457 */
1458 int
1459mouse_model_popup(void)
1460{
1461 return (p_mousem[0] == 'p');
1462}
1463
1464/*
1465 * Move the cursor to the specified row and column on the screen.
1466 * Change current window if necessary. Returns an integer with the
1467 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
1468 *
1469 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
1470 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
1471 *
1472 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
1473 * if the mouse is outside the window then the text will scroll, or if the
1474 * mouse was previously on a status line, then the status line may be dragged.
1475 *
1476 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
1477 * cursor is moved unless the cursor was on a status line.
1478 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
1479 * IN_SEP_LINE depending on where the cursor was clicked.
1480 *
1481 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
1482 * the mouse is on the status line of the same window.
1483 *
1484 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
1485 * the last call.
1486 *
1487 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
1488 * remembered.
1489 */
1490 int
1491jump_to_mouse(
1492 int flags,
1493 int *inclusive, // used for inclusive operator, can be NULL
1494 int which_button) // MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE
1495{
1496 static int on_status_line = 0; // #lines below bottom of window
1497 static int on_sep_line = 0; // on separator right of window
1498#ifdef FEAT_MENU
1499 static int in_winbar = FALSE;
1500#endif
1501#ifdef FEAT_TEXT_PROP
1502 static int in_popup_win = FALSE;
1503 static win_T *click_in_popup_win = NULL;
1504#endif
1505 static int prev_row = -1;
1506 static int prev_col = -1;
1507 static win_T *dragwin = NULL; // window being dragged
1508 static int did_drag = FALSE; // drag was noticed
1509
1510 win_T *wp, *old_curwin;
1511 pos_T old_cursor;
1512 int count;
1513 int first;
1514 int row = mouse_row;
1515 int col = mouse_col;
1516#ifdef FEAT_FOLDING
1517 int mouse_char;
1518#endif
1519
1520 mouse_past_bottom = FALSE;
1521 mouse_past_eol = FALSE;
1522
1523 if (flags & MOUSE_RELEASED)
1524 {
1525 // On button release we may change window focus if positioned on a
1526 // status line and no dragging happened.
1527 if (dragwin != NULL && !did_drag)
1528 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
1529 dragwin = NULL;
1530 did_drag = FALSE;
1531#ifdef FEAT_TEXT_PROP
1532 if (click_in_popup_win != NULL && popup_dragwin == NULL)
1533 popup_close_for_mouse_click(click_in_popup_win);
1534
1535 popup_dragwin = NULL;
1536 click_in_popup_win = NULL;
1537#endif
1538 }
1539
1540 if ((flags & MOUSE_DID_MOVE)
1541 && prev_row == mouse_row
1542 && prev_col == mouse_col)
1543 {
1544retnomove:
1545 // before moving the cursor for a left click which is NOT in a status
1546 // line, stop Visual mode
1547 if (on_status_line)
1548 return IN_STATUS_LINE;
1549 if (on_sep_line)
1550 return IN_SEP_LINE;
1551#ifdef FEAT_MENU
1552 if (in_winbar)
1553 {
1554 // A quick second click may arrive as a double-click, but we use it
1555 // as a second click in the WinBar.
1556 if ((mod_mask & MOD_MASK_MULTI_CLICK) && !(flags & MOUSE_RELEASED))
1557 {
1558 wp = mouse_find_win(&row, &col, FAIL_POPUP);
1559 if (wp == NULL)
1560 return IN_UNKNOWN;
1561 winbar_click(wp, col);
1562 }
1563 return IN_OTHER_WIN | MOUSE_WINBAR;
1564 }
1565#endif
1566 if (flags & MOUSE_MAY_STOP_VIS)
1567 {
1568 end_visual_mode();
1569 redraw_curbuf_later(INVERTED); // delete the inversion
1570 }
1571#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
1572 // Continue a modeless selection in another window.
1573 if (cmdwin_type != 0 && row < curwin->w_winrow)
1574 return IN_OTHER_WIN;
1575#endif
1576#ifdef FEAT_TEXT_PROP
1577 // Continue a modeless selection in a popup window or dragging it.
1578 if (in_popup_win)
1579 {
1580 click_in_popup_win = NULL; // don't close it on release
1581 if (popup_dragwin != NULL)
1582 {
1583 // dragging a popup window
1584 popup_drag(popup_dragwin);
1585 return IN_UNKNOWN;
1586 }
1587 return IN_OTHER_WIN;
1588 }
1589#endif
1590 return IN_BUFFER;
1591 }
1592
1593 prev_row = mouse_row;
1594 prev_col = mouse_col;
1595
1596 if (flags & MOUSE_SETPOS)
1597 goto retnomove; // ugly goto...
1598
1599#ifdef FEAT_FOLDING
1600 // Remember the character under the mouse, it might be a '-' or '+' in the
1601 // fold column.
1602 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
1603 && ScreenLines != NULL)
1604 mouse_char = ScreenLines[LineOffset[row] + col];
1605 else
1606 mouse_char = ' ';
1607#endif
1608
1609 old_curwin = curwin;
1610 old_cursor = curwin->w_cursor;
1611
1612 if (!(flags & MOUSE_FOCUS))
1613 {
1614 if (row < 0 || col < 0) // check if it makes sense
1615 return IN_UNKNOWN;
1616
1617 // find the window where the row is in and adjust "row" and "col" to be
1618 // relative to top-left of the window
1619 wp = mouse_find_win(&row, &col, FIND_POPUP);
1620 if (wp == NULL)
1621 return IN_UNKNOWN;
1622 dragwin = NULL;
1623
1624#ifdef FEAT_TEXT_PROP
1625 // Click in a popup window may start dragging or modeless selection,
1626 // but not much else.
1627 if (WIN_IS_POPUP(wp))
1628 {
1629 on_sep_line = 0;
1630 in_popup_win = TRUE;
1631 if (which_button == MOUSE_LEFT && popup_close_if_on_X(wp, row, col))
1632 {
1633 return IN_UNKNOWN;
1634 }
1635 else if ((wp->w_popup_flags & (POPF_DRAG | POPF_RESIZE))
1636 && popup_on_border(wp, row, col))
1637 {
1638 popup_dragwin = wp;
1639 popup_start_drag(wp, row, col);
1640 return IN_UNKNOWN;
1641 }
1642 // Only close on release, otherwise it's not possible to drag or do
1643 // modeless selection.
1644 else if (wp->w_popup_close == POPCLOSE_CLICK
1645 && which_button == MOUSE_LEFT)
1646 {
1647 click_in_popup_win = wp;
1648 }
1649 else if (which_button == MOUSE_LEFT)
1650 // If the click is in the scrollbar, may scroll up/down.
1651 popup_handle_scrollbar_click(wp, row, col);
1652# ifdef FEAT_CLIPBOARD
1653 return IN_OTHER_WIN;
1654# else
1655 return IN_UNKNOWN;
1656# endif
1657 }
1658 in_popup_win = FALSE;
1659 popup_dragwin = NULL;
1660#endif
1661#ifdef FEAT_MENU
1662 if (row == -1)
1663 {
1664 // A click in the window toolbar does not enter another window or
1665 // change Visual highlighting.
1666 winbar_click(wp, col);
1667 in_winbar = TRUE;
1668 return IN_OTHER_WIN | MOUSE_WINBAR;
1669 }
1670 in_winbar = FALSE;
1671#endif
1672
1673 // winpos and height may change in win_enter()!
1674 if (row >= wp->w_height) // In (or below) status line
1675 {
1676 on_status_line = row - wp->w_height + 1;
1677 dragwin = wp;
1678 }
1679 else
1680 on_status_line = 0;
1681 if (col >= wp->w_width) // In separator line
1682 {
1683 on_sep_line = col - wp->w_width + 1;
1684 dragwin = wp;
1685 }
1686 else
1687 on_sep_line = 0;
1688
1689 // The rightmost character of the status line might be a vertical
1690 // separator character if there is no connecting window to the right.
1691 if (on_status_line && on_sep_line)
1692 {
1693 if (stl_connected(wp))
1694 on_sep_line = 0;
1695 else
1696 on_status_line = 0;
1697 }
1698
1699 // Before jumping to another buffer, or moving the cursor for a left
1700 // click, stop Visual mode.
1701 if (VIsual_active
1702 && (wp->w_buffer != curwin->w_buffer
1703 || (!on_status_line && !on_sep_line
1704#ifdef FEAT_FOLDING
1705 && (
1706# ifdef FEAT_RIGHTLEFT
1707 wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc :
1708# endif
1709 col >= wp->w_p_fdc
1710# ifdef FEAT_CMDWIN
1711 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
1712# endif
1713 )
1714#endif
1715 && (flags & MOUSE_MAY_STOP_VIS))))
1716 {
1717 end_visual_mode();
1718 redraw_curbuf_later(INVERTED); // delete the inversion
1719 }
1720#ifdef FEAT_CMDWIN
1721 if (cmdwin_type != 0 && wp != curwin)
1722 {
1723 // A click outside the command-line window: Use modeless
1724 // selection if possible. Allow dragging the status lines.
1725 on_sep_line = 0;
1726# ifdef FEAT_CLIPBOARD
1727 if (on_status_line)
1728 return IN_STATUS_LINE;
1729 return IN_OTHER_WIN;
1730# else
1731 row = 0;
1732 col += wp->w_wincol;
1733 wp = curwin;
1734# endif
1735 }
1736#endif
1737 // Only change window focus when not clicking on or dragging the
1738 // status line. Do change focus when releasing the mouse button
1739 // (MOUSE_FOCUS was set above if we dragged first).
1740 if (dragwin == NULL || (flags & MOUSE_RELEASED))
1741 win_enter(wp, TRUE); // can make wp invalid!
1742
1743 if (curwin != old_curwin)
1744 {
1745#ifdef CHECK_DOUBLE_CLICK
1746 // set topline, to be able to check for double click ourselves
1747 set_mouse_topline(curwin);
1748#endif
1749#ifdef FEAT_TERMINAL
1750 // when entering a terminal window may change state
1751 term_win_entered();
1752#endif
1753 }
1754 if (on_status_line) // In (or below) status line
1755 {
1756 // Don't use start_arrow() if we're in the same window
1757 if (curwin == old_curwin)
1758 return IN_STATUS_LINE;
1759 else
1760 return IN_STATUS_LINE | CURSOR_MOVED;
1761 }
1762 if (on_sep_line) // In (or below) status line
1763 {
1764 // Don't use start_arrow() if we're in the same window
1765 if (curwin == old_curwin)
1766 return IN_SEP_LINE;
1767 else
1768 return IN_SEP_LINE | CURSOR_MOVED;
1769 }
1770
1771 curwin->w_cursor.lnum = curwin->w_topline;
1772#ifdef FEAT_GUI
1773 // remember topline, needed for double click
1774 gui_prev_topline = curwin->w_topline;
1775# ifdef FEAT_DIFF
1776 gui_prev_topfill = curwin->w_topfill;
1777# endif
1778#endif
1779 }
1780 else if (on_status_line && which_button == MOUSE_LEFT)
1781 {
1782 if (dragwin != NULL)
1783 {
1784 // Drag the status line
1785 count = row - dragwin->w_winrow - dragwin->w_height + 1
1786 - on_status_line;
1787 win_drag_status_line(dragwin, count);
1788 did_drag |= count;
1789 }
1790 return IN_STATUS_LINE; // Cursor didn't move
1791 }
1792 else if (on_sep_line && which_button == MOUSE_LEFT)
1793 {
1794 if (dragwin != NULL)
1795 {
1796 // Drag the separator column
1797 count = col - dragwin->w_wincol - dragwin->w_width + 1
1798 - on_sep_line;
1799 win_drag_vsep_line(dragwin, count);
1800 did_drag |= count;
1801 }
1802 return IN_SEP_LINE; // Cursor didn't move
1803 }
1804#ifdef FEAT_MENU
1805 else if (in_winbar)
1806 {
1807 // After a click on the window toolbar don't start Visual mode.
1808 return IN_OTHER_WIN | MOUSE_WINBAR;
1809 }
1810#endif
1811 else // keep_window_focus must be TRUE
1812 {
1813 // before moving the cursor for a left click, stop Visual mode
1814 if (flags & MOUSE_MAY_STOP_VIS)
1815 {
1816 end_visual_mode();
1817 redraw_curbuf_later(INVERTED); // delete the inversion
1818 }
1819
1820#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
1821 // Continue a modeless selection in another window.
1822 if (cmdwin_type != 0 && row < curwin->w_winrow)
1823 return IN_OTHER_WIN;
1824#endif
1825#ifdef FEAT_TEXT_PROP
1826 if (in_popup_win)
1827 {
1828 if (popup_dragwin != NULL)
1829 {
1830 // dragging a popup window
1831 popup_drag(popup_dragwin);
1832 return IN_UNKNOWN;
1833 }
1834 // continue a modeless selection in a popup window
1835 click_in_popup_win = NULL;
1836 return IN_OTHER_WIN;
1837 }
1838#endif
1839
1840 row -= W_WINROW(curwin);
1841 col -= curwin->w_wincol;
1842
1843 // When clicking beyond the end of the window, scroll the screen.
1844 // Scroll by however many rows outside the window we are.
1845 if (row < 0)
1846 {
1847 count = 0;
1848 for (first = TRUE; curwin->w_topline > 1; )
1849 {
1850#ifdef FEAT_DIFF
1851 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
1852 ++count;
1853 else
1854#endif
1855 count += plines(curwin->w_topline - 1);
1856 if (!first && count > -row)
1857 break;
1858 first = FALSE;
1859#ifdef FEAT_FOLDING
1860 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1861#endif
1862#ifdef FEAT_DIFF
1863 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
1864 ++curwin->w_topfill;
1865 else
1866#endif
1867 {
1868 --curwin->w_topline;
1869#ifdef FEAT_DIFF
1870 curwin->w_topfill = 0;
1871#endif
1872 }
1873 }
1874#ifdef FEAT_DIFF
1875 check_topfill(curwin, FALSE);
1876#endif
1877 curwin->w_valid &=
1878 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1879 redraw_later(VALID);
1880 row = 0;
1881 }
1882 else if (row >= curwin->w_height)
1883 {
1884 count = 0;
1885 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
1886 {
1887#ifdef FEAT_DIFF
1888 if (curwin->w_topfill > 0)
1889 ++count;
1890 else
1891#endif
1892 count += plines(curwin->w_topline);
1893 if (!first && count > row - curwin->w_height + 1)
1894 break;
1895 first = FALSE;
1896#ifdef FEAT_FOLDING
1897 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
1898 && curwin->w_topline == curbuf->b_ml.ml_line_count)
1899 break;
1900#endif
1901#ifdef FEAT_DIFF
1902 if (curwin->w_topfill > 0)
1903 --curwin->w_topfill;
1904 else
1905#endif
1906 {
1907 ++curwin->w_topline;
1908#ifdef FEAT_DIFF
1909 curwin->w_topfill =
1910 diff_check_fill(curwin, curwin->w_topline);
1911#endif
1912 }
1913 }
1914#ifdef FEAT_DIFF
1915 check_topfill(curwin, FALSE);
1916#endif
1917 redraw_later(VALID);
1918 curwin->w_valid &=
1919 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1920 row = curwin->w_height - 1;
1921 }
1922 else if (row == 0)
1923 {
1924 // When dragging the mouse, while the text has been scrolled up as
1925 // far as it goes, moving the mouse in the top line should scroll
1926 // the text down (done later when recomputing w_topline).
1927 if (mouse_dragging > 0
1928 && curwin->w_cursor.lnum
1929 == curwin->w_buffer->b_ml.ml_line_count
1930 && curwin->w_cursor.lnum == curwin->w_topline)
1931 curwin->w_valid &= ~(VALID_TOPLINE);
1932 }
1933 }
1934
1935#ifdef FEAT_FOLDING
1936 // Check for position outside of the fold column.
1937 if (
1938# ifdef FEAT_RIGHTLEFT
1939 curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc :
1940# endif
1941 col >= curwin->w_p_fdc
1942# ifdef FEAT_CMDWIN
1943 + (cmdwin_type == 0 ? 0 : 1)
1944# endif
1945 )
1946 mouse_char = ' ';
1947#endif
1948
1949 // compute the position in the buffer line from the posn on the screen
1950 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum, NULL))
1951 mouse_past_bottom = TRUE;
1952
1953 // Start Visual mode before coladvance(), for when 'sel' != "old"
1954 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
1955 {
1956 check_visual_highlight();
1957 VIsual = old_cursor;
1958 VIsual_active = TRUE;
1959 VIsual_reselect = TRUE;
1960 // if 'selectmode' contains "mouse", start Select mode
1961 may_start_select('o');
1962 setmouse();
1963 if (p_smd && msg_silent == 0)
1964 redraw_cmdline = TRUE; // show visual mode later
1965 }
1966
1967 curwin->w_curswant = col;
1968 curwin->w_set_curswant = FALSE; // May still have been TRUE
1969 if (coladvance(col) == FAIL) // Mouse click beyond end of line
1970 {
1971 if (inclusive != NULL)
1972 *inclusive = TRUE;
1973 mouse_past_eol = TRUE;
1974 }
1975 else if (inclusive != NULL)
1976 *inclusive = FALSE;
1977
1978 count = IN_BUFFER;
1979 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
1980 || curwin->w_cursor.col != old_cursor.col)
1981 count |= CURSOR_MOVED; // Cursor has moved
1982
1983# ifdef FEAT_FOLDING
1984 if (mouse_char == '+')
1985 count |= MOUSE_FOLD_OPEN;
1986 else if (mouse_char != ' ')
1987 count |= MOUSE_FOLD_CLOSE;
1988# endif
1989
1990 return count;
1991}
1992
1993/*
1994 * Mouse scroll wheel: Default action is to scroll three lines, or one page
1995 * when Shift or Ctrl is used.
1996 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
1997 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
1998 */
1999 void
2000nv_mousescroll(cmdarg_T *cap)
2001{
2002 win_T *old_curwin = curwin, *wp;
2003
2004 if (mouse_row >= 0 && mouse_col >= 0)
2005 {
2006 int row, col;
2007
2008 row = mouse_row;
2009 col = mouse_col;
2010
2011 // find the window at the pointer coordinates
2012 wp = mouse_find_win(&row, &col, FIND_POPUP);
2013 if (wp == NULL)
2014 return;
2015#ifdef FEAT_TEXT_PROP
2016 if (WIN_IS_POPUP(wp) && !wp->w_has_scrollbar)
2017 return;
2018#endif
2019 curwin = wp;
2020 curbuf = curwin->w_buffer;
2021 }
2022
2023 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
2024 {
2025# ifdef FEAT_TERMINAL
2026 if (term_use_loop())
2027 // This window is a terminal window, send the mouse event there.
2028 // Set "typed" to FALSE to avoid an endless loop.
2029 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
2030 else
2031# endif
2032 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
2033 {
2034 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
2035 }
2036 else
2037 {
2038 // Don't scroll more than half the window height.
2039 if (curwin->w_height < 6)
2040 {
2041 cap->count1 = curwin->w_height / 2;
2042 if (cap->count1 == 0)
2043 cap->count1 = 1;
2044 }
2045 else
2046 cap->count1 = 3;
2047 cap->count0 = cap->count1;
2048 nv_scroll_line(cap);
2049 }
2050#ifdef FEAT_TEXT_PROP
2051 if (WIN_IS_POPUP(curwin))
2052 popup_set_firstline(curwin);
2053#endif
2054 }
2055# ifdef FEAT_GUI
2056 else
2057 {
2058 // Horizontal scroll - only allowed when 'wrap' is disabled
2059 if (!curwin->w_p_wrap)
2060 {
2061 int val, step = 6;
2062
2063 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
2064 step = curwin->w_width;
2065 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
2066 if (val < 0)
2067 val = 0;
2068
2069 gui_do_horiz_scroll(val, TRUE);
2070 }
2071 }
2072# endif
2073# ifdef FEAT_SYN_HL
2074 if (curwin != old_curwin && curwin->w_p_cul)
2075 redraw_for_cursorline(curwin);
2076# endif
2077
2078 curwin->w_redr_status = TRUE;
2079
2080 curwin = old_curwin;
2081 curbuf = curwin->w_buffer;
2082}
2083
2084/*
2085 * Mouse clicks and drags.
2086 */
2087 void
2088nv_mouse(cmdarg_T *cap)
2089{
2090 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
2091}
2092#endif // FEAT_MOUSE
2093
2094// Functions also used for popup windows.
2095#if defined(FEAT_MOUSE) || defined(FEAT_TEXT_PROP) || defined(PROTO)
2096
2097/*
2098 * Compute the buffer line position from the screen position "rowp" / "colp" in
2099 * window "win".
2100 * "plines_cache" can be NULL (no cache) or an array with "win->w_height"
2101 * entries that caches the plines_win() result from a previous call. Entry is
2102 * zero if not computed yet. There must be no text or setting changes since
2103 * the entry is put in the cache.
2104 * Returns TRUE if the position is below the last line.
2105 */
2106 int
2107mouse_comp_pos(
2108 win_T *win,
2109 int *rowp,
2110 int *colp,
2111 linenr_T *lnump,
2112 int *plines_cache)
2113{
2114 int col = *colp;
2115 int row = *rowp;
2116 linenr_T lnum;
2117 int retval = FALSE;
2118 int off;
2119 int count;
2120
2121#ifdef FEAT_RIGHTLEFT
2122 if (win->w_p_rl)
2123 col = win->w_width - 1 - col;
2124#endif
2125
2126 lnum = win->w_topline;
2127
2128 while (row > 0)
2129 {
2130 int cache_idx = lnum - win->w_topline;
2131
2132 if (plines_cache != NULL && plines_cache[cache_idx] > 0)
2133 count = plines_cache[cache_idx];
2134 else
2135 {
2136#ifdef FEAT_DIFF
2137 // Don't include filler lines in "count"
2138 if (win->w_p_diff
2139# ifdef FEAT_FOLDING
2140 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
2141# endif
2142 )
2143 {
2144 if (lnum == win->w_topline)
2145 row -= win->w_topfill;
2146 else
2147 row -= diff_check_fill(win, lnum);
2148 count = plines_win_nofill(win, lnum, TRUE);
2149 }
2150 else
2151#endif
2152 count = plines_win(win, lnum, TRUE);
2153 if (plines_cache != NULL)
2154 plines_cache[cache_idx] = count;
2155 }
2156 if (count > row)
2157 break; // Position is in this buffer line.
2158#ifdef FEAT_FOLDING
2159 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
2160#endif
2161 if (lnum == win->w_buffer->b_ml.ml_line_count)
2162 {
2163 retval = TRUE;
2164 break; // past end of file
2165 }
2166 row -= count;
2167 ++lnum;
2168 }
2169
2170 if (!retval)
2171 {
2172 // Compute the column without wrapping.
2173 off = win_col_off(win) - win_col_off2(win);
2174 if (col < off)
2175 col = off;
2176 col += row * (win->w_width - off);
2177 // add skip column (for long wrapping line)
2178 col += win->w_skipcol;
2179 }
2180
2181 if (!win->w_p_wrap)
2182 col += win->w_leftcol;
2183
2184 // skip line number and fold column in front of the line
2185 col -= win_col_off(win);
2186 if (col < 0)
2187 {
2188#ifdef FEAT_NETBEANS_INTG
2189 netbeans_gutter_click(lnum);
2190#endif
2191 col = 0;
2192 }
2193
2194 *colp = col;
2195 *rowp = row;
2196 *lnump = lnum;
2197 return retval;
2198}
2199
2200/*
2201 * Find the window at screen position "*rowp" and "*colp". The positions are
2202 * updated to become relative to the top-left of the window.
2203 * When "popup" is FAIL_POPUP and the position is in a popup window then NULL
2204 * is returned. When "popup" is IGNORE_POPUP then do not even check popup
2205 * windows.
2206 * Returns NULL when something is wrong.
2207 */
2208 win_T *
2209mouse_find_win(int *rowp, int *colp, mouse_find_T popup UNUSED)
2210{
2211 frame_T *fp;
2212 win_T *wp;
2213
2214#ifdef FEAT_TEXT_PROP
2215 win_T *pwp = NULL;
2216
2217 if (popup != IGNORE_POPUP)
2218 {
2219 popup_reset_handled();
2220 while ((wp = find_next_popup(TRUE)) != NULL)
2221 {
2222 if (*rowp >= wp->w_winrow && *rowp < wp->w_winrow + popup_height(wp)
2223 && *colp >= wp->w_wincol
2224 && *colp < wp->w_wincol + popup_width(wp))
2225 pwp = wp;
2226 }
2227 if (pwp != NULL)
2228 {
2229 if (popup == FAIL_POPUP)
2230 return NULL;
2231 *rowp -= pwp->w_winrow;
2232 *colp -= pwp->w_wincol;
2233 return pwp;
2234 }
2235 }
2236#endif
2237
2238 fp = topframe;
2239 *rowp -= firstwin->w_winrow;
2240 for (;;)
2241 {
2242 if (fp->fr_layout == FR_LEAF)
2243 break;
2244 if (fp->fr_layout == FR_ROW)
2245 {
2246 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
2247 {
2248 if (*colp < fp->fr_width)
2249 break;
2250 *colp -= fp->fr_width;
2251 }
2252 }
2253 else // fr_layout == FR_COL
2254 {
2255 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
2256 {
2257 if (*rowp < fp->fr_height)
2258 break;
2259 *rowp -= fp->fr_height;
2260 }
2261 }
2262 }
2263 // When using a timer that closes a window the window might not actually
2264 // exist.
2265 FOR_ALL_WINDOWS(wp)
2266 if (wp == fp->fr_win)
2267 {
2268#ifdef FEAT_MENU
2269 *rowp -= wp->w_winbar_height;
2270#endif
2271 return wp;
2272 }
2273 return NULL;
2274}
2275
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002276#if defined(NEED_VCOL2COL) || defined(FEAT_BEVAL) || defined(FEAT_TEXT_PROP) \
2277 || defined(PROTO)
2278/*
2279 * Convert a virtual (screen) column to a character column.
2280 * The first column is one.
2281 */
2282 int
2283vcol2col(win_T *wp, linenr_T lnum, int vcol)
2284{
2285 // try to advance to the specified column
2286 int count = 0;
2287 char_u *ptr;
2288 char_u *line;
2289
2290 line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
2291 while (count < vcol && *ptr != NUL)
2292 {
2293 count += win_lbr_chartabsize(wp, line, ptr, count, NULL);
2294 MB_PTR_ADV(ptr);
2295 }
2296 return (int)(ptr - line);
2297}
2298#endif
2299
2300#else // FEAT_MOUSE
2301
2302/*
2303 * Dummy implementation of setmouse() to avoid lots of #ifdefs.
2304 */
2305 void
2306setmouse(void)
2307{
2308}
2309
2310#endif // FEAT_MOUSE