Skip to content

Commit 9927d07

Browse files
authored
Merge pull request #5127 from RobertP-McDowell/master
Select multiple words/lines when double/triple clicking.
2 parents 4c4e62a + 1f4b5f1 commit 9927d07

4 files changed

Lines changed: 103 additions & 64 deletions

File tree

lib/tty/key.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -818,11 +818,9 @@ xmouse_get_event (Gpm_Event *ev, gboolean extended)
818818
else
819819
{
820820
ev->type = GPM_UP | (GPM_SINGLE << clicks);
821-
tv1 = g_get_monotonic_time ();
822821
}
823822
ev->buttons = 0;
824823
last_btn = 0;
825-
clicks = 0;
826824
}
827825
else
828826
{
@@ -834,14 +832,6 @@ xmouse_get_event (Gpm_Event *ev, gboolean extended)
834832
{
835833
gint64 tv2;
836834

837-
if (btn >= 32 && btn <= 34)
838-
{
839-
btn -= 32;
840-
ev->type = GPM_DRAG;
841-
}
842-
else
843-
ev->type = GPM_DOWN;
844-
845835
tv2 = g_get_monotonic_time ();
846836
if (tv1 != 0 && tv2 - tv1 < (gint64) double_click_speed * MC_USEC_PER_MSEC)
847837
{
@@ -851,6 +841,17 @@ xmouse_get_event (Gpm_Event *ev, gboolean extended)
851841
else
852842
clicks = 0;
853843

844+
if (btn >= 32 && btn <= 34)
845+
{
846+
btn -= 32;
847+
ev->type = GPM_DRAG;
848+
}
849+
else
850+
{
851+
ev->type = GPM_DOWN;
852+
tv1 = g_get_monotonic_time ();
853+
}
854+
854855
switch (btn)
855856
{
856857
case 0:
@@ -876,6 +877,7 @@ xmouse_get_event (Gpm_Event *ev, gboolean extended)
876877
ev->buttons = 0;
877878
break;
878879
}
880+
ev->type |= (GPM_SINGLE << clicks);
879881
last_btn = ev->buttons;
880882
}
881883
}

src/editor/edit.c

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,41 @@ my_type_of (int c)
932932
return r;
933933
}
934934

935+
/* --------------------------------------------------------------------------------------------- */
936+
/** get word at cursor for marking */
937+
938+
static void
939+
edit_get_current_word_extents (WEdit *edit, off_t *start, off_t *end)
940+
{
941+
long pos;
942+
943+
for (pos = edit->buffer.curs1; pos < edit->buffer.size; pos++)
944+
{
945+
const int check_char = edit_buffer_get_byte (&edit->buffer, pos);
946+
947+
if (is_break_char (check_char))
948+
{
949+
if (pos == edit->buffer.curs1) // always select at least one character.
950+
{
951+
*start = pos;
952+
*end = pos + 1;
953+
return;
954+
}
955+
break;
956+
}
957+
}
958+
*end = pos;
959+
960+
for (; pos != 0; pos--)
961+
{
962+
const int check_char = edit_buffer_get_byte (&edit->buffer, pos - 1);
963+
964+
if (is_break_char (check_char))
965+
break;
966+
}
967+
*start = pos;
968+
}
969+
935970
/* --------------------------------------------------------------------------------------------- */
936971

937972
static void
@@ -3078,8 +3113,8 @@ edit_set_markers (WEdit *edit, off_t m1, off_t m2, long c1, long c2)
30783113

30793114
/* --------------------------------------------------------------------------------------------- */
30803115
/**
3081-
if mark2 is -1 then marking is from mark1 to the cursor.
3082-
Otherwise its between the markers. This handles this.
3116+
if mark2 is -1 then marking is from mark1 to end_mark_curs (the cursor).
3117+
This also handles the logic for word/line highlighting.
30833118
Returns FALSE if no text is marked.
30843119
*/
30853120

@@ -3107,8 +3142,25 @@ eval_marks (WEdit *edit, off_t *start_mark, off_t *end_mark)
31073142
}
31083143
else
31093144
{
3110-
*start_mark = MIN (edit->mark1, end_mark_curs);
3111-
*end_mark = MAX (edit->mark1, end_mark_curs);
3145+
if (edit->word_highlight)
3146+
{
3147+
off_t word_start;
3148+
off_t word_end;
3149+
3150+
edit_get_current_word_extents (edit, &word_start, &word_end);
3151+
*start_mark = MIN (edit->mark1, word_start);
3152+
*end_mark = MAX (end_mark_curs, word_end);
3153+
}
3154+
else if (edit->line_highlight)
3155+
{
3156+
*start_mark = MIN (edit->mark1, edit_buffer_get_current_bol (&edit->buffer));
3157+
*end_mark = MAX (end_mark_curs, edit_buffer_get_current_eol (&edit->buffer));
3158+
}
3159+
else
3160+
{
3161+
*start_mark = MIN (edit->mark1, end_mark_curs);
3162+
*end_mark = MAX (edit->mark1, end_mark_curs);
3163+
}
31123164
edit->column2 = edit->curs_col + edit->over_col;
31133165
}
31143166

@@ -3154,18 +3206,30 @@ edit_mark_cmd (WEdit *edit, gboolean unmark)
31543206
edit_set_markers (edit, 0, 0, 0, 0);
31553207
edit->force |= REDRAW_PAGE;
31563208
}
3157-
else if (edit->mark2 >= 0)
3209+
else if (edit->mark2 >= 0) // mark highlighting just started.
31583210
{
3211+
off_t m1 = edit->buffer.curs1;
3212+
31593213
edit->end_mark_curs = -1;
3160-
edit_set_markers (edit, edit->buffer.curs1, -1, edit->curs_col + edit->over_col,
3214+
if (edit->word_highlight)
3215+
edit_get_current_word_extents (edit, &m1, &edit->end_mark_curs);
3216+
else if (edit->line_highlight)
3217+
{
3218+
m1 = edit_buffer_get_current_bol (&edit->buffer);
3219+
edit->end_mark_curs = edit_buffer_get_current_eol (&edit->buffer);
3220+
}
3221+
edit_set_markers (edit, m1, -1, edit->curs_col + edit->over_col,
31613222
edit->curs_col + edit->over_col);
31623223
edit->force |= REDRAW_PAGE;
31633224
}
3164-
else
3225+
else // mark highlighting just ended.
31653226
{
3166-
edit->end_mark_curs = edit->buffer.curs1;
3167-
edit_set_markers (edit, edit->mark1, edit->buffer.curs1, edit->column1,
3168-
edit->curs_col + edit->over_col);
3227+
off_t m1;
3228+
off_t m2;
3229+
3230+
eval_marks (edit, &m1, &m2);
3231+
edit->end_mark_curs = m2;
3232+
edit_set_markers (edit, m1, m2, edit->column1, edit->curs_col + edit->over_col);
31693233
}
31703234
}
31713235

@@ -3175,33 +3239,7 @@ edit_mark_cmd (WEdit *edit, gboolean unmark)
31753239
void
31763240
edit_mark_current_word_cmd (WEdit *edit)
31773241
{
3178-
long pos;
3179-
3180-
for (pos = edit->buffer.curs1; pos != 0; pos--)
3181-
{
3182-
int c1, c2;
3183-
3184-
c1 = edit_buffer_get_byte (&edit->buffer, pos);
3185-
c2 = edit_buffer_get_byte (&edit->buffer, pos - 1);
3186-
if (!isspace (c1) && isspace (c2))
3187-
break;
3188-
if ((my_type_of (c1) & my_type_of (c2)) == 0)
3189-
break;
3190-
}
3191-
edit->mark1 = pos;
3192-
3193-
for (; pos < edit->buffer.size; pos++)
3194-
{
3195-
int c1, c2;
3196-
3197-
c1 = edit_buffer_get_byte (&edit->buffer, pos);
3198-
c2 = edit_buffer_get_byte (&edit->buffer, pos + 1);
3199-
if (!isspace (c1) && isspace (c2))
3200-
break;
3201-
if ((my_type_of (c1) & my_type_of (c2)) == 0)
3202-
break;
3203-
}
3204-
edit->mark2 = MIN (pos + 1, edit->buffer.size);
3242+
edit_get_current_word_extents (edit, &edit->mark1, &edit->mark2);
32053243

32063244
edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
32073245
}

src/editor/editwidget.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,11 @@ edit_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
11041104
edit_update_curs_row (edit);
11051105
edit_update_curs_col (edit);
11061106

1107+
if (event->count == GPM_DOUBLE)
1108+
edit->word_highlight = TRUE;
1109+
else if (event->count == GPM_TRIPLE)
1110+
edit->line_highlight = TRUE;
1111+
11071112
if (edit->fullscreen == 0)
11081113
{
11091114
if (event->y == 0)
@@ -1131,11 +1136,15 @@ edit_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
11311136
}
11321137
}
11331138

1134-
MC_FALLTHROUGH; // to start/stop text selection
1139+
edit_update_cursor (edit, event);
1140+
edit_total_update (edit);
1141+
break;
11351142

11361143
case MSG_MOUSE_UP:
11371144
edit_update_cursor (edit, event);
11381145
edit_total_update (edit);
1146+
edit->word_highlight = FALSE;
1147+
edit->line_highlight = FALSE;
11391148
break;
11401149

11411150
case MSG_MOUSE_CLICK:
@@ -1149,18 +1158,6 @@ edit_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
11491158
// double click on top line (toggle fullscreen)
11501159
edit_toggle_fullscreen (edit);
11511160
}
1152-
else if (event->count == GPM_DOUBLE)
1153-
{
1154-
// double click
1155-
edit_mark_current_word_cmd (edit);
1156-
edit_total_update (edit);
1157-
}
1158-
else if (event->count == GPM_TRIPLE)
1159-
{
1160-
// triple click: works in GPM only, not in xterm
1161-
edit_mark_current_line_cmd (edit);
1162-
edit_total_update (edit);
1163-
}
11641161
break;
11651162

11661163
case MSG_MOUSE_DRAG:

src/editor/editwidget.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ struct WEdit
9999
unsigned int delete_file : 1; // New file, needs to be deleted unless modified
100100
unsigned int highlight : 1; // There is a selected block
101101
unsigned int column_highlight : 1;
102-
unsigned int fullscreen : 1; // Is window fullscreen or not
103-
long prev_col; /* recent column position of the cursor - used when moving
104-
up or down past lines that are shorter than the current line */
105-
long start_line; // line number of the top of the page
102+
unsigned int word_highlight : 1; // Highlight each word the cursor crosses
103+
unsigned int line_highlight : 1; // Highlight each line the cursor crosses
104+
unsigned int fullscreen : 1; // Is window fullscreen or not
105+
long prev_col; /* recent column position of the cursor - used when moving
106+
up or down past lines that are shorter than the current line */
107+
long start_line; // line number of the top of the page
106108

107109
// file info
108110
off_t mark1; // position of highlight start

0 commit comments

Comments
 (0)