forked from mintty/mintty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermmouse.c
1047 lines (968 loc) · 29.2 KB
/
termmouse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// termmouse.c (part of mintty)
// Copyright 2008-2023 Andy Koppe, 2017-2024 Thomas Wolff
// Based on code from PuTTY-0.60 by Simon Tatham and team.
// Licensed under the terms of the GNU General Public License v3 or later.
#include "termpriv.h"
#include "win.h"
#include "child.h"
#include "charset.h" // cs__utftowcs
#include "tek.h"
/*
* Fetch the character at a particular position in a line array.
* The reason this isn't just a simple array reference is that if the
* character we find is UCSWIDE, then we must look one space further
* to the left.
*/
static wchar
get_char(termline *line, int x)
{
wchar c = line->chars[x].chr;
if (c == UCSWIDE && x > 0)
c = line->chars[x - 1].chr;
return c;
}
static pos
sel_spread_word(pos p, bool forward)
{
pos ret_p = p;
termline *line = fetch_line(p.y);
static int level = 0;
static char scheme = 0;
if (!forward) {
level = 0;
scheme = 0;
}
//printf("sel_ %d: forward %d level %d\n", p.x, forward, level);
wchar prevc1 = 0;
wchar prevc2 = 0;
for (;;) {
wchar c = get_char(line, p.x);
// special colon and double-colon handling
if (forward && prevc1 == ':' && prevc2 != ':' && !strchr(":/", c)) {
// handle previous match of : if not matching :: or :/
return ret_p;
}
else if (forward && prevc1 == ':' && strchr(":/", c)) {
// handle match of :: or :/
ret_p = p;
}
prevc2 = prevc1;
prevc1 = c;
// scheme detection state machine
if (!forward) {
// http://abc.xy
//0ssss://
if (isalnum(c)) {
if (scheme == ':')
scheme = 's';
else if (scheme != 's')
scheme = 0;
}
else if (c == ':') {
if (scheme == '/')
scheme = ':';
else
scheme = 0;
}
else if (c == '/') {
scheme = '/';
}
else if (scheme == 's') // #1209 / #1208
break;
else
scheme = 0;
}
if (term.mouse_state != MS_OPENING && *cfg.word_chars_excl)
if (strchr(cfg.word_chars_excl, c))
break;
if (iswalnum(c))
ret_p = p;
else if (term.mouse_state != MS_OPENING && *cfg.word_chars) {
if (!strchr(cfg.word_chars, c))
break;
ret_p = p;
}
else if (strchr("_#%~+-", c))
ret_p = p;
else if (strchr(".$@/\\", c)) {
if (!forward)
ret_p = p;
}
// support URLs with parentheses (#1196)
// distinguish opening and closing parentheses to match proper nesting
else if (!term.mouse_state && strchr("([{", c)) {
level ++;
//printf("%d: %c forward %d level %d\n", p.x, c, forward, level);
if (forward)
ret_p = p;
}
else if (!term.mouse_state && strchr(")]}", c)) {
level --;
//printf("%d: %c forward %d level %d\n", p.x, c, forward, level);
if (forward && level < 0)
break;
if (forward)
ret_p = p;
}
// should we also consider *^`| as part of a URL?
// should we strip ?!.,;: at the end?
// what about #$%&*\^`|~ at the end?
else if (c == ' ' && p.x > 0 && get_char(line, p.x - 1) == '\\')
ret_p = p;
else if (c == (forward ? '=' : ':')) {
}
else if (strchr("&,;?!", c)) {
}
else if (forward && c == ':') {
// could set marker in case we match : but not :: or :/
// but this is better handled at the beginning of the for loop alone;
// note: this could be handled more easily here with some look-ahead
// but that is not possible as the URL may wrap into the next line
}
else {
//printf("%d: %c forward %d level %d BREAK\n", p.x, c, forward, level);
break;
}
if (forward) {
p.x++;
if (p.x >= term.cols - ((line->lattr & LATTR_WRAPPED2) != 0)) {
if (!(line->lattr & LATTR_WRAPPED))
break;
p.x = 0;
release_line(line);
line = fetch_line(++p.y);
}
}
else {
if (p.x <= 0) {
if (p.y <= -sblines())
break;
release_line(line);
line = fetch_line(--p.y);
if (!(line->lattr & LATTR_WRAPPED))
break;
p.x = term.cols - ((line->lattr & LATTR_WRAPPED2) != 0);
}
p.x--;
}
}
//printf("%d: return\n", ret_p.x);
release_line(line);
return ret_p;
}
/*
* Spread the selection outwards according to the selection mode.
*/
static pos
sel_spread_half(pos p, bool forward)
{
switch (term.mouse_state) {
when MS_SEL_CHAR: {
/*
* In this mode, every character is a separate unit, except
* for runs of spaces at the end of a non-wrapping line.
*/
termline *line = fetch_line(p.y);
if (!(line->lattr & LATTR_WRAPPED)) {
termchar *q = line->chars + term.cols;
while (q > line->chars && q[-1].chr == ' ' && !q[-1].cc_next)
q--;
if (q == line->chars + term.cols)
q--;
if (p.x >= q - line->chars)
p.x = forward ? term.cols - 1 : q - line->chars;
}
release_line(line);
}
when MS_SEL_WORD or MS_OPENING:
p = sel_spread_word(p, forward);
when MS_SEL_LINE:
if (forward) {
termline *line = fetch_line(p.y);
while (line->lattr & LATTR_WRAPPED) {
release_line(line);
line = fetch_line(++p.y);
p.x = 0;
}
int x = p.x;
p.x = term.cols - 1;
do {
if (get_char(line, x) != ' ')
p.x = x;
} while (++x < line->cols);
release_line(line);
}
else {
p.x = 0;
while (p.y > -sblines()) {
termline *line = fetch_line(p.y - 1);
bool wrapped = line->lattr & LATTR_WRAPPED;
release_line(line);
if (!wrapped)
break;
p.y--;
}
}
otherwise:
/* Shouldn't happen. */
break;
}
return p;
}
static void
sel_spread(void)
{
term.sel_start = sel_spread_half(term.sel_start, false);
term.sel_end = sel_spread_half(term.sel_end, true);
incpos(term.sel_end);
}
static bool
hover_spread_empty(void)
{
//printf("hover_spread_empty\n");
term.hover_start = sel_spread_word(term.hover_start, false);
term.hover_end = sel_spread_word(term.hover_end, true);
//printf("hover_spread_empty %d..%d\n", term.hover_start.x, term.hover_end.x);
bool eq = term.hover_start.y == term.hover_end.y && term.hover_start.x == term.hover_end.x;
incpos(term.hover_end);
return eq;
}
static void
sel_drag(pos selpoint)
{
//printf("sel_drag %d+%d/2 (anchor %d+%d/2)\n", selpoint.x, selpoint.r, term.sel_anchor.x, term.sel_anchor.r);
term.selected = true;
if (!term.sel_rect) {
/*
* For normal selection, we set (sel_start,sel_end) to
* (selpoint,sel_anchor) in some order.
*/
if (poslt(selpoint, term.sel_anchor)) {
term.sel_start = selpoint;
term.sel_end = term.sel_anchor;
if (cfg.elastic_mouse && !term.mouse_mode) {
if (selpoint.r) {
incpos(term.sel_start);
}
if (!term.sel_anchor.r) {
decpos(term.sel_end);
}
}
}
else {
term.sel_start = term.sel_anchor;
term.sel_end = selpoint;
if (cfg.elastic_mouse && !term.mouse_mode) {
if (term.sel_anchor.r) {
incpos(term.sel_start);
}
if (!selpoint.r) {
decpos(term.sel_end);
}
}
}
sel_spread();
}
else {
/*
* For rectangular selection, we may need to
* interchange x and y coordinates (if the user has
* dragged in the -x and +y directions, or vice versa).
*/
term.sel_start.x = min(term.sel_anchor.x, selpoint.x);
term.sel_end.x = 1 + max(term.sel_anchor.x, selpoint.x);
term.sel_start.y = min(term.sel_anchor.y, selpoint.y);
term.sel_end.y = max(term.sel_anchor.y, selpoint.y);
}
}
static void
sel_extend(pos selpoint)
{
//printf("sel_extend %d+%d/2 (anchor %d+%d/2)\n", selpoint.x, selpoint.r, term.sel_anchor.x, term.sel_anchor.r);
if (term.selected) {
if (!term.sel_rect) {
/*
* For normal selection, we extend by moving
* whichever end of the current selection is closer
* to the mouse.
*/
if (posdiff(selpoint, term.sel_start) <
posdiff(term.sel_end, term.sel_start) / 2) {
term.sel_anchor = term.sel_end;
decpos(term.sel_anchor);
}
else
term.sel_anchor = term.sel_start;
}
else {
/*
* For rectangular selection, we have a choice of
* _four_ places to put sel_anchor and selpoint: the
* four corners of the selection.
*/
term.sel_anchor.x =
selpoint.x * 2 < term.sel_start.x + term.sel_end.x
? term.sel_end.x - 1
: term.sel_start.x;
term.sel_anchor.y =
selpoint.y * 2 < term.sel_start.y + term.sel_end.y
? term.sel_end.y
: term.sel_start.y;
}
}
else
term.sel_anchor = selpoint;
sel_drag(selpoint);
}
typedef enum {
MA_CLICK = 0,
MA_MOVE = 1,
MA_WHEEL = 2,
MA_RELEASE = 3
} mouse_action; // values are significant, used for calculation!
static void
send_mouse_event(mouse_action a, mouse_button b, mod_keys mods, pos p)
{
if (term.mouse_mode == MM_LOCATOR) {
// handle DECSLE: select locator events
if ((a == MA_CLICK && term.locator_report_up)
|| (a == MA_RELEASE && term.locator_report_dn)) {
int pe = 0;
switch (b) {
when MBT_LEFT:
pe = a == MA_CLICK ? 2 : 3;
when MBT_MIDDLE:
pe = a == MA_CLICK ? 4 : 5;
when MBT_RIGHT:
pe = a == MA_CLICK ? 6 : 7;
when MBT_4:
pe = a == MA_CLICK ? 8 : 9;
otherwise:;
}
if (pe) {
int x, y, buttons;
win_get_locator_info(&x, &y, &buttons, term.locator_by_pixels);
child_printf("\e[%d;%d;%d;%d;0&w", pe, buttons, y, x);
term.locator_rectangle = false;
}
}
// handle DECEFR: enable filter rectangle
else if (a == MA_MOVE && term.locator_rectangle) {
/* Anytime the locator is detected outside of the filter
rectangle, an outside rectangle event is generated and the
rectangle is disabled.
*/
int x, y, buttons;
win_get_locator_info(&x, &y, &buttons, term.locator_by_pixels);
if (x < term.locator_left || x > term.locator_right
|| y < term.locator_top || y > term.locator_bottom) {
child_printf("\e[10;%d;%d;%d;0&w", buttons, y, x);
term.locator_rectangle = false;
}
}
return;
}
uint x = p.x + 1, y = p.y + 1;
if (a != MA_WHEEL) {
if (cfg.old_xbuttons)
switch (b) {
when MBT_4:
b = MBT_LEFT; mods |= MDK_ALT;
when MBT_5:
b = MBT_RIGHT; mods |= MDK_ALT;
otherwise:;
}
else
switch (b) {
when MBT_4:
b = 129;
when MBT_5:
b = 130;
otherwise:;
}
}
uint code = b ? b - 1 : 0x3;
if (a != MA_RELEASE)
code |= a * 0x20;
else if (term.mouse_enc != ME_XTERM_CSI && term.mouse_enc != ME_PIXEL_CSI)
code = 0x3;
code |= (mods & ~cfg.click_target_mod) * 0x4;
if (term.mouse_enc == ME_XTERM_CSI)
child_printf("\e[<%u;%u;%u%c", code, x, y, (a == MA_RELEASE ? 'm' : 'M'));
else if (term.mouse_enc == ME_PIXEL_CSI)
child_printf("\e[<%u;%u;%u%c", code, p.pix + 1, p.piy + 1, (a == MA_RELEASE ? 'm' : 'M'));
else if (term.mouse_enc == ME_URXVT_CSI)
child_printf("\e[%u;%u;%uM", code + 0x20, x, y);
else {
// Xterm's hacky but traditional character offset approach.
char buf[8] = "\e[M";
uint len = 3;
void encode_coord(uint c) {
c += 0x20;
if (term.mouse_enc != ME_UTF8)
buf[len++] = c < 0x100 ? c : 0;
else if (c < 0x80)
buf[len++] = c;
else if (c < 0x800) {
// In extended mouse mode, positions from 96 to 2015 are encoded as a
// two-byte UTF-8 sequence (as introduced in xterm #262.)
buf[len++] = 0xC0 + (c >> 6);
buf[len++] = 0x80 + (c & 0x3F);
}
else {
// Xterm reports out-of-range positions as a NUL byte.
buf[len++] = 0;
}
}
buf[len++] = code + 0x20;
encode_coord(x);
encode_coord(y);
child_write(buf, len);
}
}
static pos
box_pos(pos p)
{
p.y = min(max(0, p.y), term.rows - 1);
p.x = min(max(0, p.x), term.cols - 1);
// p.piy and p.pix already clipped in translate_pos()
return p;
}
static pos
get_selpoint(const pos p)
{
pos sp = { .y = p.y + term.disptop, .x = p.x, .r = p.r };
termline *line = fetch_line(sp.y);
// Adjust to presentational direction.
if (line->lattr & LATTR_PRESRTL) {
sp.x = term.cols - 1 - sp.x;
sp.r = !sp.r;
}
// Adjust to double-width line display.
if ((line->lattr & LATTR_MODE) != LATTR_NORM)
sp.x /= 2;
/*
* Transform x through the bidi algorithm to find the _logical_
* click point from the physical one.
*/
if (term_bidi_line(line, p.y) != null) {
#ifdef debug_bidi_cache
printf("mouse @ log %d -> vis %d\n", sp.x, term.post_bidi_cache[p.y].backward[sp.x]);
#endif
sp.x = term.post_bidi_cache[p.y].backward[sp.x];
}
// Back to previous cell if current one is second half of a wide char
if (line->chars[sp.x].chr == UCSWIDE)
sp.x--;
release_line(line);
return sp;
}
static void
send_keys(uint count, string code)
{
if (count) {
uint len = strlen(code);
char buf[len * count];
char *p = buf;
while (count--) { memcpy(p, code, len); p += len; }
child_write(buf, sizeof buf);
}
}
static bool
check_app_mouse(mod_keys *mods_p)
{
if (term.locator_1_enabled)
return true;
if (!term.mouse_mode || term.show_other_screen)
return false;
bool override = *mods_p & cfg.click_target_mod;
*mods_p &= ~cfg.click_target_mod;
return cfg.clicks_target_app ^ override;
}
bool
term_mouse_click(mouse_button b, mod_keys mods, pos p, int count)
{
compose_clear();
/* (#1169) was_hovering considers the case when hovering is not directly
triggered via state MS_OPENING but rather overrides state MS_SEL_CHAR,
in order to support its configuration to be applied without modifier
*/
bool was_hovering = term.hovering;
if (term.hovering) {
term.hovering = false;
win_update(true);
}
bool res = true;
if (tek_mode == TEKMODE_GIN) {
char c = '`';
switch (b) {
when MBT_LEFT: c = 'l';
when MBT_MIDDLE: c = 'm';
when MBT_RIGHT: c = 'r';
when MBT_4: c = 'p';
when MBT_5: c = 'q';
}
if (mods & MDK_SHIFT)
c ^= ' ';
c |= 0x80;
child_send(&c, 1);
tek_send_address();
}
else if (check_app_mouse(&mods)) {
if (term.mouse_mode == MM_X10)
mods = 0;
send_mouse_event(MA_CLICK, b, mods, box_pos(p));
term.mouse_state = (int)b;
}
else {
// generic transformation M4/M5 -> Alt+left/right;
// if any specific handling is designed for M4/M5, this needs to be tweaked
bool fake_alt = false;
switch (b) {
when MBT_4:
b = MBT_LEFT; mods |= MDK_ALT; fake_alt = true;
when MBT_5:
b = MBT_RIGHT; mods |= MDK_ALT; fake_alt = true;
otherwise:;
}
bool alt = mods & MDK_ALT;
bool shift_or_ctrl = mods & (MDK_SHIFT | MDK_CTRL);
int mca = cfg.middle_click_action;
int rca = cfg.right_click_action;
term.mouse_state = 0;
if (b == MBT_RIGHT && (rca == RC_MENU || shift_or_ctrl)) {
// disable Alt+mouse menu opening;
// the menu would often be closed soon by auto-repeat Alt, sending
// WM_CAPTURECHANGED, WM_UNINITMENUPOPUP, WM_MENUSELECT, WM_EXITMENULOOP
// trying to ignore WM_CAPTURECHANGED does not help
if (!alt || fake_alt)
win_popup_menu(mods);
else
res = false;
}
else if (b == MBT_MIDDLE && (mods & ~MDK_SHIFT) == MDK_CTRL) {
if (cfg.zoom_mouse)
win_zoom_font(0, mods & MDK_SHIFT);
else
res = false;
}
else if ((b == MBT_RIGHT && rca == RC_PASTE) ||
(b == MBT_MIDDLE && mca == MC_PASTE))
{
if (!alt)
term.mouse_state = shift_or_ctrl ? MS_COPYING : MS_PASTING;
else
res = false;
}
else if ((b == MBT_RIGHT && rca == RC_ENTER) ||
(b == MBT_MIDDLE && mca == MC_ENTER)) {
child_send("\r", 1);
}
else if (b == MBT_LEFT && mods == MDK_SHIFT && rca == RC_EXTEND) {
term.mouse_state = MS_PASTING;
}
else if (b == MBT_LEFT &&
((char)(mods & ~cfg.click_target_mod) == cfg.opening_mod || was_hovering)
)
{
if (count == cfg.opening_clicks) {
// Open word under cursor
p = get_selpoint(box_pos(p));
term.mouse_state = MS_OPENING;
term.selected = true;
term.sel_rect = false;
term.sel_start = term.sel_end = term.sel_anchor = p;
sel_spread();
win_update(true);
}
else
res = false;
}
else if (b == MBT_MIDDLE && mca == MC_VOID) {
// res = true; // MC_VOID explicitly ignores the click
}
else if ((mods & (MDK_CTRL | MDK_ALT)) != (MDK_CTRL | MDK_ALT)) {
// Only clicks for selecting and extending should get here.
p = get_selpoint(box_pos(p));
term.mouse_state = -count;
term.sel_rect = alt;
if (b != MBT_LEFT || shift_or_ctrl)
sel_extend(p);
else if (count == 1) {
term.selected = false;
term.sel_anchor = p;
}
else {
// Double or triple-click: select whole word or line
term.selected = true;
term.sel_rect = false;
term.sel_start = term.sel_end = term.sel_anchor = p;
sel_spread();
}
win_capture_mouse();
win_update(true);
}
else {
res = false;
}
}
return res;
}
static void
mouse_open(pos p)
{
termline *line = fetch_line(p.y + term.disptop);
int urli = line->chars[p.x].attr.link;
release_line(line);
char * url = geturl(urli);
if (url)
win_open(cs__utftowcs(url), true); // win_open frees its argument
else
term_open();
term.selected = false;
term.hovering = false;
win_update(true);
}
void
term_mouse_release(mouse_button b, mod_keys mods, pos p)
{
compose_clear();
int state = term.mouse_state;
//printf("term_mouse_release state %d button %d\n", state, b);
// "Clicks place cursor" implementation.
void place_cursor(int mode)
{
pos dest;
if (mode == 2002)
dest = get_selpoint(box_pos(p));
else
dest = term.selected ? term.sel_end : get_selpoint(box_pos(p));
//printf("place_cursor p %d x %d sel %d..%d\n", p.x, term.curs.x, term.sel_start.x, term.sel_end.x);
static bool moved_previously = false;
static pos last_dest;
pos orig;
if (mode == 2003)
orig = term.sel_start;
else
if (state == MS_SEL_CHAR)
orig = (pos){.y = term.curs.y, .x = term.curs.x};
else if (moved_previously)
orig = last_dest;
else
return;
bool forward = posle(orig, dest);
pos end = forward ? dest : orig;
p = forward ? orig : dest;
//printf("place_cursor %d %d..%d\n", mode, p.x, end.x);
uint count = 0;
while (p.y != end.y) {
termline *line = fetch_line(p.y);
if (!(line->lattr & LATTR_WRAPPED)) {
release_line(line);
moved_previously = false;
return;
}
int cols = term.cols - ((line->lattr & LATTR_WRAPPED2) != 0);
for (int x = p.x; x < cols; x++) {
if (line->chars[x].chr != UCSWIDE)
count++;
}
p.y++;
p.x = 0;
release_line(line);
}
termline *line = fetch_line(p.y);
for (int x = p.x; x < end.x; x++) {
if (line->chars[x].chr != UCSWIDE)
count++;
}
release_line(line);
//printf(forward ? "keys +%d\n" : "keys -%d\n", count);
if (mode == 2003) {
struct termios attr;
tcgetattr(0, &attr);
send_keys(count, (char[]){attr.c_cc[VERASE], 0});
}
else {
send_keys(count, term.app_cursor_keys ? (forward ? "\eOC" : "\eOD")
: (forward ? "\e[C" : "\e[D"));
}
moved_previously = true;
last_dest = dest;
}
term.mouse_state = 0;
switch (state) {
when MS_COPYING: term_copy();
when MS_OPENING: mouse_open(p);
when MS_PASTING: {
// Finish selection.
if (term.selected && cfg.copy_on_select)
term_copy();
// Flush any output held back during selection.
term_flush();
// Readline mouse mode: place cursor to mouse position before pasting
if (term.readline_mouse_2)
place_cursor(2002);
// Now the pasting.
win_paste();
}
when MS_SEL_CHAR or MS_SEL_WORD or MS_SEL_LINE: {
// Open hovered link, accepting configurable modifiers
if (state == MS_SEL_CHAR && !term.selected
&& ((char)(mods & ~cfg.click_target_mod) == cfg.opening_mod)
)
{
// support the case of hovering and link opening without modifiers
// if so configured (#1169)
mouse_open(p);
term.mouse_state = 0;
return;
}
// Finish selection.
if (term.selected && cfg.copy_on_select)
term_copy();
// Flush any output held back during selection.
term_flush();
// Guard "Clicks place cursor" implementation.
if (term.on_alt_screen || term.app_cursor_keys)
return;
// Readline mouse mode: place cursor to mouse position
// Should cfg.clicks_place_cursor override DECSET mode?
bool extenda = (b == MBT_RIGHT && cfg.right_click_action == RC_EXTEND)
|| (b == MBT_MIDDLE && cfg.middle_click_action == MC_EXTEND);
if (term.readline_mouse_1 && b == MBT_LEFT)
place_cursor(2001);
else if (term.readline_mouse_3 && extenda && state == MS_SEL_WORD) {
place_cursor(2001);
place_cursor(2003);
term.selected = false;
}
}
otherwise:
if (check_app_mouse(&mods)) {
if (term.mouse_mode >= MM_VT200)
send_mouse_event(MA_RELEASE, b, mods, box_pos(p));
}
}
}
static void
sel_scroll_cb(void)
{
if (term_selecting() && term.sel_scroll) {
term_scroll(0, term.sel_scroll);
sel_drag(get_selpoint(term.sel_pos));
win_update(true);
win_set_timer(sel_scroll_cb, 125);
}
}
void
term_mouse_move(mod_keys mods, pos p)
{
compose_clear();
//printf("mouse_move %d+%d/2\n", p.x, p.r);
pos bp = box_pos(p);
if (term_selecting()) {
//printf("term_mouse_move selecting\n");
if (p.y < 0 || p.y >= term.rows) {
if (!term.sel_scroll)
win_set_timer(sel_scroll_cb, 200);
term.sel_scroll = p.y < 0 ? p.y : p.y - term.rows + 1;
term.sel_pos = bp;
}
else {
term.sel_scroll = 0;
if (p.x < 0 && p.y + term.disptop > term.sel_anchor.y)
bp = (pos){.y = p.y - 1, .x = term.cols - 1, .r = p.r};
}
bool alt = mods & MDK_ALT;
term.sel_rect = alt;
sel_drag(get_selpoint(bp));
win_update(true);
}
else if (term.mouse_state == MS_OPENING && 0 == cfg.opening_mod) {
//printf("term_mouse_move opening mods %X\n", mods);
// assumption: don't need to check mouse button state in this workflow
// if hover links are configured to work without modifier,
// still enable text selection even over a link, after the mouse
// has moved from initial click;
// not this is in opposite to the next case, which deliberately
// catches this case to NOT switch to selection, in order to
// support hover action even after tiny mouse shaking (#1039)
term.mouse_state = MS_SEL_CHAR;
// here we could already establish selection mode properly,
// as it is done above in term_mouse_click, case
// "Only clicks for selecting and extending should get here." -
// we save that effort as it will be achieved anyway by the
// next tiny movement...
}
else if (term.mouse_state == MS_OPENING) {
//printf("term_mouse_move opening mods %X\n", mods);
// let's not clear link opening state when just moving the mouse (#1039)
// but only after hovering out of the link area (below)
#ifdef link_opening_only_if_unmoved
term.mouse_state = 0;
term.selected = false;
win_update(true);
#endif
}
else if (term.mouse_state > 0) {
//printf("term_mouse_move >0\n");
if (term.mouse_mode >= MM_BTN_EVENT)
send_mouse_event(MA_MOVE, (mouse_button)term.mouse_state, mods, bp);
}
else {
//printf("term_mouse_move any\n");
if (term.mouse_mode == MM_ANY_EVENT)
send_mouse_event(MA_MOVE, 0, mods, bp);
}
// hover indication
if (!check_app_mouse(&mods) && term.has_focus &&
((char)(mods & ~cfg.click_target_mod) == cfg.opening_mod)
)
{
//printf("term_mouse_move link\n");
p = get_selpoint(box_pos(p));
term.hover_start = term.hover_end = p;
if (!hover_spread_empty()) {
term.hovering = true;
termline *line = fetch_line(p.y);
term.hoverlink = line->chars[p.x].attr.link;
release_line(line);
win_update(true);
}
else if (term.hovering) {
term.hovering = false;
win_update(true);
}
//printf("->hovering %d (opening %d)\n", term.hovering, term.mouse_state == MS_OPENING);
// clear link opening state after hovering out of link area
if (!term.hovering && term.mouse_state == MS_OPENING)
term.mouse_state = 0;
}
}
void
term_mouse_wheel(bool horizontal, int delta, int lines_per_notch, mod_keys mods, pos p)
{
compose_clear();
if (term.hovering) {
term.hovering = false;
win_update(true);
}
enum { NOTCH_DELTA = 120 };
static int accu = 0;
accu += delta;
if (tek_mode == TEKMODE_GIN) {
int step = (mods & MDK_SHIFT) ? 40 : (mods & MDK_CTRL) ? 1 : 4;
if (horizontal ^ (mods & MDK_CTRL))
tek_move_by(0, step * delta / NOTCH_DELTA);
else
tek_move_by(step * delta / NOTCH_DELTA, 0);
}
else if (check_app_mouse(&mods)) {
if (strstr(cfg.suppress_wheel, "report"))
return;
// Send as mouse events, with one event per notch.
int notches = accu / NOTCH_DELTA;
if (notches) {
accu -= NOTCH_DELTA * notches;
mouse_button b = (notches < 0) + 1;
if (horizontal)
b = 5 - b;
notches = abs(notches);
do
send_mouse_event(MA_WHEEL, b, mods, box_pos(p));
while (--notches);
}
}
else if (horizontal) {
}
else if (cfg.zoom_mouse && (mods & ~MDK_SHIFT) == MDK_CTRL) {
if (strstr(cfg.suppress_wheel, "zoom"))
return;
if (cfg.zoom_mouse) {
int zoom = accu / NOTCH_DELTA;
if (zoom) {
accu -= NOTCH_DELTA * zoom;
win_zoom_font(zoom, mods & MDK_SHIFT);
}
}
}
else if ((mods & ~(MDK_SHIFT | MDK_CTRL)) == MDK_WIN) {
// yes, some copy/paste here for more clarity of the conditions
if (strstr(cfg.suppress_wheel, "zoom"))
return;
int zoom = accu / NOTCH_DELTA;
if (zoom) {
accu -= NOTCH_DELTA * zoom;
win_zoom_font(zoom, mods & MDK_SHIFT);
}
}
else if (!(mods & ~(MDK_SHIFT | MDK_CTRL | MDK_ALT))) {
// Determine number of lines per wheel notch. -1 means page-wise scrolling.
if (mods & MDK_SHIFT)
lines_per_notch = -1;
else if (mods & MDK_CTRL)
lines_per_notch = 1;
else if (cfg.lines_per_notch > 0)
lines_per_notch = min(cfg.lines_per_notch, term.rows - 1);
bool pages = lines_per_notch == -1;
int count_per_notch = pages ? 1 : lines_per_notch;
int count = count_per_notch * accu / NOTCH_DELTA;
if (count) {
accu -= count * NOTCH_DELTA / count_per_notch;
bool alt = mods & MDK_ALT;
bool scrollback = !term.on_alt_screen || term.show_other_screen;
// If Alt is pressed while looking at the primary screen, consume the Alt
// modifier and send events to the application instead.
if (scrollback && alt)
scrollback = alt = false;
if (scrollback) {
if (strstr(cfg.suppress_wheel, "scrollwin"))
return;
// For page-wise scrolling, scroll by one line less than window height.