-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathui.c
3301 lines (3017 loc) · 78.5 KB
/
ui.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
/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* ui.c: functions that handle the user interface.
* 1. Keyboard input stuff, and a bit of windowing stuff. These are called
* before the machine specific stuff (mch_*) so that we can call the GUI
* stuff instead if the GUI is running.
* 2. Clipboard stuff.
* 3. Input buffer stuff.
*/
#include "vim.h"
#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include "winclip.pro"
#endif
void
ui_write(s, len)
char_u *s;
int len;
{
#ifdef FEAT_GUI
if (gui.in_use && !gui.dying && !gui.starting)
{
gui_write(s, len);
if (p_wd)
gui_wait_for_chars(p_wd);
return;
}
#endif
#ifndef NO_CONSOLE
/* Don't output anything in silent mode ("ex -s") unless 'verbose' set */
if (!(silent_mode && p_verbose == 0))
{
#ifdef FEAT_MBYTE
char_u *tofree = NULL;
if (output_conv.vc_type != CONV_NONE)
{
/* Convert characters from 'encoding' to 'termencoding'. */
tofree = string_convert(&output_conv, s, &len);
if (tofree != NULL)
s = tofree;
}
#endif
mch_write(s, len);
#ifdef FEAT_MBYTE
if (output_conv.vc_type != CONV_NONE)
vim_free(tofree);
#endif
}
#endif
}
#if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(WIN3264)
/*
* When executing an external program, there may be some typed characters that
* are not consumed by it. Give them back to ui_inchar() and they are stored
* here for the next call.
*/
static char_u *ta_str = NULL;
static int ta_off; /* offset for next char to use when ta_str != NULL */
static int ta_len; /* length of ta_str when it's not NULL*/
void
ui_inchar_undo(s, len)
char_u *s;
int len;
{
char_u *new;
int newlen;
newlen = len;
if (ta_str != NULL)
newlen += ta_len - ta_off;
new = alloc(newlen);
if (new != NULL)
{
if (ta_str != NULL)
{
mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off));
mch_memmove(new + ta_len - ta_off, s, (size_t)len);
vim_free(ta_str);
}
else
mch_memmove(new, s, (size_t)len);
ta_str = new;
ta_len = newlen;
ta_off = 0;
}
}
#endif
/*
* ui_inchar(): low level input function.
* Get characters from the keyboard.
* Return the number of characters that are available.
* If "wtime" == 0 do not wait for characters.
* If "wtime" == -1 wait forever for characters.
* If "wtime" > 0 wait "wtime" milliseconds for a character.
*
* "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
* it. When typebuf.tb_change_cnt changes (e.g., when a message is received
* from a remote client) "buf" can no longer be used. "tb_change_cnt" is NULL
* otherwise.
*/
int
ui_inchar(buf, maxlen, wtime, tb_change_cnt)
char_u *buf;
int maxlen;
long wtime; /* don't use "time", MIPS cannot handle it */
int tb_change_cnt;
{
int retval = 0;
#if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS))
/*
* Use the typeahead if there is any.
*/
if (ta_str != NULL)
{
if (maxlen >= ta_len - ta_off)
{
mch_memmove(buf, ta_str + ta_off, (size_t)ta_len);
vim_free(ta_str);
ta_str = NULL;
return ta_len;
}
mch_memmove(buf, ta_str + ta_off, (size_t)maxlen);
ta_off += maxlen;
return maxlen;
}
#endif
#ifdef FEAT_PROFILE
if (do_profiling == PROF_YES && wtime != 0)
prof_inchar_enter();
#endif
#ifdef NO_CONSOLE_INPUT
/* Don't wait for character input when the window hasn't been opened yet.
* Do try reading, this works when redirecting stdin from a file.
* Must return something, otherwise we'll loop forever. If we run into
* this very often we probably got stuck, exit Vim. */
if (no_console_input())
{
static int count = 0;
# ifndef NO_CONSOLE
retval = mch_inchar(buf, maxlen, (wtime >= 0 && wtime < 10)
? 10L : wtime, tb_change_cnt);
if (retval > 0 || typebuf_changed(tb_change_cnt) || wtime >= 0)
goto theend;
# endif
if (wtime == -1 && ++count == 1000)
read_error_exit();
buf[0] = CAR;
retval = 1;
goto theend;
}
#endif
/* If we are going to wait for some time or block... */
if (wtime == -1 || wtime > 100L)
{
/* ... allow signals to kill us. */
(void)vim_handle_signal(SIGNAL_UNBLOCK);
/* ... there is no need for CTRL-C to interrupt something, don't let
* it set got_int when it was mapped. */
if (mapped_ctrl_c)
ctrl_c_interrupts = FALSE;
}
#ifdef FEAT_GUI
if (gui.in_use)
{
if (gui_wait_for_chars(wtime) && !typebuf_changed(tb_change_cnt))
retval = read_from_input_buf(buf, (long)maxlen);
}
#endif
#ifndef NO_CONSOLE
# ifdef FEAT_GUI
else
# endif
{
retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt);
}
#endif
if (wtime == -1 || wtime > 100L)
/* block SIGHUP et al. */
(void)vim_handle_signal(SIGNAL_BLOCK);
ctrl_c_interrupts = TRUE;
#ifdef NO_CONSOLE_INPUT
theend:
#endif
#ifdef FEAT_PROFILE
if (do_profiling == PROF_YES && wtime != 0)
prof_inchar_exit();
#endif
return retval;
}
/*
* return non-zero if a character is available
*/
int
ui_char_avail()
{
#ifdef FEAT_GUI
if (gui.in_use)
{
gui_mch_update();
return input_available();
}
#endif
#ifndef NO_CONSOLE
# ifdef NO_CONSOLE_INPUT
if (no_console_input())
return 0;
# endif
return mch_char_avail();
#else
return 0;
#endif
}
/*
* Delay for the given number of milliseconds. If ignoreinput is FALSE then we
* cancel the delay if a key is hit.
*/
void
ui_delay(msec, ignoreinput)
long msec;
int ignoreinput;
{
#ifdef FEAT_GUI
if (gui.in_use && !ignoreinput)
gui_wait_for_chars(msec);
else
#endif
{
#if defined(FEAT_GUI_MACVIM)
/* MacVim tries to be conservative with flushing, but when Vim takes a
* nap it really must flush (else timed error messages might not appear
* etc.). Note that gui_mch_wait_for_chars() already does force a
* flush, so only do it here. */
if (gui.in_use)
gui_macvim_force_flush();
#endif
mch_delay(msec, ignoreinput);
}
}
/*
* If the machine has job control, use it to suspend the program,
* otherwise fake it by starting a new shell.
* When running the GUI iconify the window.
*/
void
ui_suspend()
{
#ifdef FEAT_GUI
if (gui.in_use)
{
gui_mch_iconify();
return;
}
#endif
mch_suspend();
}
#if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
/*
* When the OS can't really suspend, call this function to start a shell.
* This is never called in the GUI.
*/
void
suspend_shell()
{
if (*p_sh == NUL)
EMSG(_(e_shellempty));
else
{
MSG_PUTS(_("new shell started\n"));
do_shell(NULL, 0);
}
}
#endif
/*
* Try to get the current Vim shell size. Put the result in Rows and Columns.
* Use the new sizes as defaults for 'columns' and 'lines'.
* Return OK when size could be determined, FAIL otherwise.
*/
int
ui_get_shellsize()
{
int retval;
#ifdef FEAT_GUI
if (gui.in_use
# ifdef FEAT_GUI_MACVIM
/* Avoid using terminal dimensions for GUI window. MacVim
* autosaves the dimensions of the first window. */
|| gui.starting
# endif
)
retval = gui_get_shellsize();
else
#endif
retval = mch_get_shellsize();
check_shellsize();
/* adjust the default for 'lines' and 'columns' */
if (retval == OK)
{
set_number_default("lines", Rows);
set_number_default("columns", Columns);
}
return retval;
}
/*
* Set the size of the Vim shell according to Rows and Columns, if possible.
* The gui_set_shellsize() or mch_set_shellsize() function will try to set the
* new size. If this is not possible, it will adjust Rows and Columns.
*/
void
ui_set_shellsize(mustset)
int mustset UNUSED; /* set by the user */
{
#ifdef FEAT_GUI
if (gui.in_use)
gui_set_shellsize(mustset, TRUE, RESIZE_BOTH);
else
#endif
mch_set_shellsize();
}
/*
* Called when Rows and/or Columns changed. Adjust scroll region and mouse
* region.
*/
void
ui_new_shellsize()
{
if (full_screen && !exiting)
{
#ifdef FEAT_GUI
if (gui.in_use)
gui_new_shellsize();
else
#endif
mch_new_shellsize();
}
}
void
ui_breakcheck()
{
#ifdef FEAT_GUI
if (gui.in_use)
gui_mch_update();
else
#endif
mch_breakcheck();
}
/*****************************************************************************
* Functions for copying and pasting text between applications.
* This is always included in a GUI version, but may also be included when the
* clipboard and mouse is available to a terminal version such as xterm.
* Note: there are some more functions in ops.c that handle selection stuff.
*
* Also note that the majority of functions here deal with the X 'primary'
* (visible - for Visual mode use) selection, and only that. There are no
* versions of these for the 'clipboard' selection, as Visual mode has no use
* for them.
*/
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
static void clip_copy_selection __ARGS((VimClipboard *clip));
/*
* Selection stuff using Visual mode, for cutting and pasting text to other
* windows.
*/
/*
* Call this to initialise the clipboard. Pass it FALSE if the clipboard code
* is included, but the clipboard can not be used, or TRUE if the clipboard can
* be used. Eg unix may call this with FALSE, then call it again with TRUE if
* the GUI starts.
*/
void
clip_init(can_use)
int can_use;
{
VimClipboard *cb;
cb = &clip_star;
for (;;)
{
cb->available = can_use;
cb->owned = FALSE;
cb->start.lnum = 0;
cb->start.col = 0;
cb->end.lnum = 0;
cb->end.col = 0;
cb->state = SELECT_CLEARED;
if (cb == &clip_plus)
break;
cb = &clip_plus;
}
}
/*
* Check whether the VIsual area has changed, and if so try to become the owner
* of the selection, and free any old converted selection we may still have
* lying around. If the VIsual mode has ended, make a copy of what was
* selected so we can still give it to others. Will probably have to make sure
* this is called whenever VIsual mode is ended.
*/
void
clip_update_selection(clip)
VimClipboard *clip;
{
pos_T start, end;
/* If visual mode is only due to a redo command ("."), then ignore it */
if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
{
if (lt(VIsual, curwin->w_cursor))
{
start = VIsual;
end = curwin->w_cursor;
#ifdef FEAT_MBYTE
if (has_mbyte)
end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
#endif
}
else
{
start = curwin->w_cursor;
end = VIsual;
}
if (!equalpos(clip->start, start)
|| !equalpos(clip->end, end)
|| clip->vmode != VIsual_mode)
{
clip_clear_selection(clip);
clip->start = start;
clip->end = end;
clip->vmode = VIsual_mode;
clip_free_selection(clip);
clip_own_selection(clip);
clip_gen_set_selection(clip);
}
}
}
void
clip_own_selection(cbd)
VimClipboard *cbd;
{
/*
* Also want to check somehow that we are reading from the keyboard rather
* than a mapping etc.
*/
#ifdef FEAT_X11
/* Always own the selection, we might have lost it without being
* notified, e.g. during a ":sh" command. */
if (cbd->available)
{
int was_owned = cbd->owned;
cbd->owned = (clip_gen_own_selection(cbd) == OK);
if (!was_owned && (cbd == &clip_star || cbd == &clip_plus))
{
/* May have to show a different kind of highlighting for the
* selected area. There is no specific redraw command for this,
* just redraw all windows on the current buffer. */
if (cbd->owned
&& (get_real_state() == VISUAL
|| get_real_state() == SELECTMODE)
&& (cbd == &clip_star ? clip_isautosel_star()
: clip_isautosel_plus())
&& hl_attr(HLF_V) != hl_attr(HLF_VNC))
redraw_curbuf_later(INVERTED_ALL);
}
}
#else
/* Only own the clipboard when we didn't own it yet. */
if (!cbd->owned && cbd->available)
cbd->owned = (clip_gen_own_selection(cbd) == OK);
#endif
}
void
clip_lose_selection(cbd)
VimClipboard *cbd;
{
#ifdef FEAT_X11
int was_owned = cbd->owned;
#endif
int visual_selection = FALSE;
if (cbd == &clip_star || cbd == &clip_plus)
visual_selection = TRUE;
clip_free_selection(cbd);
cbd->owned = FALSE;
if (visual_selection)
clip_clear_selection(cbd);
clip_gen_lose_selection(cbd);
#ifdef FEAT_X11
if (visual_selection)
{
/* May have to show a different kind of highlighting for the selected
* area. There is no specific redraw command for this, just redraw all
* windows on the current buffer. */
if (was_owned
&& (get_real_state() == VISUAL
|| get_real_state() == SELECTMODE)
&& (cbd == &clip_star ?
clip_isautosel_star() : clip_isautosel_plus())
&& hl_attr(HLF_V) != hl_attr(HLF_VNC))
{
update_curbuf(INVERTED_ALL);
setcursor();
cursor_on();
out_flush();
# ifdef FEAT_GUI
if (gui.in_use)
gui_update_cursor(TRUE, FALSE);
# endif
}
}
#endif
}
static void
clip_copy_selection(clip)
VimClipboard *clip;
{
if (VIsual_active && (State & NORMAL) && clip->available)
{
clip_update_selection(clip);
clip_free_selection(clip);
clip_own_selection(clip);
if (clip->owned)
clip_get_selection(clip);
clip_gen_set_selection(clip);
}
}
/*
* Called when Visual mode is ended: update the selection.
*/
void
clip_auto_select()
{
if (clip_isautosel_star())
clip_copy_selection(&clip_star);
if (clip_isautosel_plus())
clip_copy_selection(&clip_plus);
}
/*
* Return TRUE if automatic selection of Visual area is desired for the *
* register.
*/
int
clip_isautosel_star()
{
return (
#ifdef FEAT_GUI
gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
#endif
clip_autoselect_star);
}
/*
* Return TRUE if automatic selection of Visual area is desired for the +
* register.
*/
int
clip_isautosel_plus()
{
return (
#ifdef FEAT_GUI
gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
#endif
clip_autoselect_plus);
}
/*
* Stuff for general mouse selection, without using Visual mode.
*/
static int clip_compare_pos __ARGS((int row1, int col1, int row2, int col2));
static void clip_invert_area __ARGS((int, int, int, int, int how));
static void clip_invert_rectangle __ARGS((int row, int col, int height, int width, int invert));
static void clip_get_word_boundaries __ARGS((VimClipboard *, int, int));
static int clip_get_line_end __ARGS((int));
static void clip_update_modeless_selection __ARGS((VimClipboard *, int, int,
int, int));
/* flags for clip_invert_area() */
#define CLIP_CLEAR 1
#define CLIP_SET 2
#define CLIP_TOGGLE 3
/*
* Start, continue or end a modeless selection. Used when editing the
* command-line and in the cmdline window.
*/
void
clip_modeless(button, is_click, is_drag)
int button;
int is_click;
int is_drag;
{
int repeat;
repeat = ((clip_star.mode == SELECT_MODE_CHAR
|| clip_star.mode == SELECT_MODE_LINE)
&& (mod_mask & MOD_MASK_2CLICK))
|| (clip_star.mode == SELECT_MODE_WORD
&& (mod_mask & MOD_MASK_3CLICK));
if (is_click && button == MOUSE_RIGHT)
{
/* Right mouse button: If there was no selection, start one.
* Otherwise extend the existing selection. */
if (clip_star.state == SELECT_CLEARED)
clip_start_selection(mouse_col, mouse_row, FALSE);
clip_process_selection(button, mouse_col, mouse_row, repeat);
}
else if (is_click)
clip_start_selection(mouse_col, mouse_row, repeat);
else if (is_drag)
{
/* Don't try extending a selection if there isn't one. Happens when
* button-down is in the cmdline and them moving mouse upwards. */
if (clip_star.state != SELECT_CLEARED)
clip_process_selection(button, mouse_col, mouse_row, repeat);
}
else /* release */
clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
}
/*
* Compare two screen positions ala strcmp()
*/
static int
clip_compare_pos(row1, col1, row2, col2)
int row1;
int col1;
int row2;
int col2;
{
if (row1 > row2) return(1);
if (row1 < row2) return(-1);
if (col1 > col2) return(1);
if (col1 < col2) return(-1);
return(0);
}
/*
* Start the selection
*/
void
clip_start_selection(col, row, repeated_click)
int col;
int row;
int repeated_click;
{
VimClipboard *cb = &clip_star;
if (cb->state == SELECT_DONE)
clip_clear_selection(cb);
row = check_row(row);
col = check_col(col);
#ifdef FEAT_MBYTE
col = mb_fix_col(col, row);
#endif
cb->start.lnum = row;
cb->start.col = col;
cb->end = cb->start;
cb->origin_row = (short_u)cb->start.lnum;
cb->state = SELECT_IN_PROGRESS;
if (repeated_click)
{
if (++cb->mode > SELECT_MODE_LINE)
cb->mode = SELECT_MODE_CHAR;
}
else
cb->mode = SELECT_MODE_CHAR;
#ifdef FEAT_GUI
/* clear the cursor until the selection is made */
if (gui.in_use)
gui_undraw_cursor();
#endif
switch (cb->mode)
{
case SELECT_MODE_CHAR:
cb->origin_start_col = cb->start.col;
cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
break;
case SELECT_MODE_WORD:
clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
cb->origin_start_col = cb->word_start_col;
cb->origin_end_col = cb->word_end_col;
clip_invert_area((int)cb->start.lnum, cb->word_start_col,
(int)cb->end.lnum, cb->word_end_col, CLIP_SET);
cb->start.col = cb->word_start_col;
cb->end.col = cb->word_end_col;
break;
case SELECT_MODE_LINE:
clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
(int)Columns, CLIP_SET);
cb->start.col = 0;
cb->end.col = Columns;
break;
}
cb->prev = cb->start;
#ifdef DEBUG_SELECTION
printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
#endif
}
/*
* Continue processing the selection
*/
void
clip_process_selection(button, col, row, repeated_click)
int button;
int col;
int row;
int_u repeated_click;
{
VimClipboard *cb = &clip_star;
int diff;
int slen = 1; /* cursor shape width */
if (button == MOUSE_RELEASE)
{
/* Check to make sure we have something selected */
if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
{
#ifdef FEAT_GUI
if (gui.in_use)
gui_update_cursor(FALSE, FALSE);
#endif
cb->state = SELECT_CLEARED;
return;
}
#ifdef DEBUG_SELECTION
printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
cb->start.col, cb->end.lnum, cb->end.col);
#endif
if (clip_isautosel_star()
|| (
#ifdef FEAT_GUI
gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
#endif
clip_autoselectml))
clip_copy_modeless_selection(FALSE);
#ifdef FEAT_GUI
if (gui.in_use)
gui_update_cursor(FALSE, FALSE);
#endif
cb->state = SELECT_DONE;
return;
}
row = check_row(row);
col = check_col(col);
#ifdef FEAT_MBYTE
col = mb_fix_col(col, row);
#endif
if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
return;
/*
* When extending the selection with the right mouse button, swap the
* start and end if the position is before half the selection
*/
if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
{
/*
* If the click is before the start, or the click is inside the
* selection and the start is the closest side, set the origin to the
* end of the selection.
*/
if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
|| (clip_compare_pos(row, col,
(int)cb->end.lnum, cb->end.col) < 0
&& (((cb->start.lnum == cb->end.lnum
&& cb->end.col - col > col - cb->start.col))
|| ((diff = (cb->end.lnum - row) -
(row - cb->start.lnum)) > 0
|| (diff == 0 && col < (int)(cb->start.col +
cb->end.col) / 2)))))
{
cb->origin_row = (short_u)cb->end.lnum;
cb->origin_start_col = cb->end.col - 1;
cb->origin_end_col = cb->end.col;
}
else
{
cb->origin_row = (short_u)cb->start.lnum;
cb->origin_start_col = cb->start.col;
cb->origin_end_col = cb->start.col;
}
if (cb->mode == SELECT_MODE_WORD && !repeated_click)
cb->mode = SELECT_MODE_CHAR;
}
/* set state, for when using the right mouse button */
cb->state = SELECT_IN_PROGRESS;
#ifdef DEBUG_SELECTION
printf("Selection extending to (%d,%d)\n", row, col);
#endif
if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
cb->mode = SELECT_MODE_CHAR;
switch (cb->mode)
{
case SELECT_MODE_CHAR:
/* If we're on a different line, find where the line ends */
if (row != cb->prev.lnum)
cb->word_end_col = clip_get_line_end(row);
/* See if we are before or after the origin of the selection */
if (clip_compare_pos(row, col, cb->origin_row,
cb->origin_start_col) >= 0)
{
if (col >= (int)cb->word_end_col)
clip_update_modeless_selection(cb, cb->origin_row,
cb->origin_start_col, row, (int)Columns);
else
{
#ifdef FEAT_MBYTE
if (has_mbyte && mb_lefthalve(row, col))
slen = 2;
#endif
clip_update_modeless_selection(cb, cb->origin_row,
cb->origin_start_col, row, col + slen);
}
}
else
{
#ifdef FEAT_MBYTE
if (has_mbyte
&& mb_lefthalve(cb->origin_row, cb->origin_start_col))
slen = 2;
#endif
if (col >= (int)cb->word_end_col)
clip_update_modeless_selection(cb, row, cb->word_end_col,
cb->origin_row, cb->origin_start_col + slen);
else
clip_update_modeless_selection(cb, row, col,
cb->origin_row, cb->origin_start_col + slen);
}
break;
case SELECT_MODE_WORD:
/* If we are still within the same word, do nothing */
if (row == cb->prev.lnum && col >= (int)cb->word_start_col
&& col < (int)cb->word_end_col && !repeated_click)
return;
/* Get new word boundaries */
clip_get_word_boundaries(cb, row, col);
/* Handle being after the origin point of selection */
if (clip_compare_pos(row, col, cb->origin_row,
cb->origin_start_col) >= 0)
clip_update_modeless_selection(cb, cb->origin_row,
cb->origin_start_col, row, cb->word_end_col);
else
clip_update_modeless_selection(cb, row, cb->word_start_col,
cb->origin_row, cb->origin_end_col);
break;
case SELECT_MODE_LINE:
if (row == cb->prev.lnum && !repeated_click)
return;
if (clip_compare_pos(row, col, cb->origin_row,
cb->origin_start_col) >= 0)
clip_update_modeless_selection(cb, cb->origin_row, 0, row,
(int)Columns);
else
clip_update_modeless_selection(cb, row, 0, cb->origin_row,
(int)Columns);
break;
}
cb->prev.lnum = row;
cb->prev.col = col;
#ifdef DEBUG_SELECTION
printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
cb->start.col, cb->end.lnum, cb->end.col);
#endif
}
# if defined(FEAT_GUI) || defined(PROTO)
/*
* Redraw part of the selection if character at "row,col" is inside of it.
* Only used for the GUI.
*/
void
clip_may_redraw_selection(row, col, len)
int row, col;
int len;
{
int start = col;
int end = col + len;
if (clip_star.state != SELECT_CLEARED
&& row >= clip_star.start.lnum
&& row <= clip_star.end.lnum)
{
if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
start = clip_star.start.col;
if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
end = clip_star.end.col;
if (end > start)
clip_invert_area(row, start, row, end, 0);
}
}
# endif
/*
* Called from outside to clear selected region from the display
*/
void
clip_clear_selection(cbd)
VimClipboard *cbd;
{
if (cbd->state == SELECT_CLEARED)
return;
clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum,
cbd->end.col, CLIP_CLEAR);
cbd->state = SELECT_CLEARED;
}
/*
* Clear the selection if any lines from "row1" to "row2" are inside of it.
*/
void
clip_may_clear_selection(row1, row2)
int row1, row2;
{
if (clip_star.state == SELECT_DONE
&& row2 >= clip_star.start.lnum
&& row1 <= clip_star.end.lnum)
clip_clear_selection(&clip_star);
}