diff --git a/src/tsm/libtsm.h b/src/tsm/libtsm.h index 23eb472..661ff64 100644 --- a/src/tsm/libtsm.h +++ b/src/tsm/libtsm.h @@ -260,6 +260,9 @@ void tsm_screen_selection_start(struct tsm_screen *con, void tsm_screen_selection_target(struct tsm_screen *con, unsigned int posx, unsigned int posy); +void tsm_screen_selection_word(struct tsm_screen *con, + unsigned int posx, + unsigned int posy); int tsm_screen_selection_copy(struct tsm_screen *con, char **out); tsm_age_t tsm_screen_draw(struct tsm_screen *con, tsm_screen_draw_cb draw_cb, diff --git a/src/tsm/libtsm.sym b/src/tsm/libtsm.sym index 17b83c1..be7f246 100644 --- a/src/tsm/libtsm.sym +++ b/src/tsm/libtsm.sym @@ -132,3 +132,8 @@ global: tsm_screen_sb_get_line_count; tsm_screen_sb_get_line_pos; } LIBTSM_4; + +LIBTSM_4_3 { +global: + tsm_screen_selection_word; +} LIBTSM_4_1; diff --git a/src/tsm/tsm-selection.c b/src/tsm/tsm-selection.c index 57b07cd..26047c7 100644 --- a/src/tsm/tsm-selection.c +++ b/src/tsm/tsm-selection.c @@ -81,6 +81,44 @@ static void selection_set(struct tsm_screen *con, struct selection_pos *sel, sel->y = y; } +static void word_select(struct tsm_screen *con, + unsigned int posx, + unsigned int posy) +{ + int start, end; + struct line *line; + + selection_set(con, &con->sel_start, posx, posy); + + if (con->sel_start.line) + line = con->sel_start.line; + else + line = con->lines[con->sel_start.y]; + + if (!line || line->cells[posx].ch == ' ') + return; + + for (start = posx; start >= 0; start--) { + if (line->cells[start].ch == ' ') { + start++; + break; + } + } + if (start < 0) + start = 0; + + for (end = posx; end < line->size; end++) { + if (line->cells[end].ch == ' ' || line->cells[end].ch == '\n' || + line->cells[end].ch == '\0') { + end--; + break; + } + } + con->sel_start.x = start; + selection_set(con, &con->sel_end, end, posy); + con->sel_active = true; +} + SHL_EXPORT void tsm_screen_selection_reset(struct tsm_screen *con) { @@ -126,6 +164,21 @@ void tsm_screen_selection_target(struct tsm_screen *con, selection_set(con, &con->sel_end, posx, posy); } +SHL_EXPORT +void tsm_screen_selection_word(struct tsm_screen *con, + unsigned int posx, + unsigned int posy) +{ + if (!con) + return; + + screen_inc_age(con); + /* TODO: more sophisticated ageing */ + con->age = con->age_cnt; + + word_select(con, posx, posy); +} + /* calculates the line length from the beginning to the last non zero character */ static unsigned int calc_line_len(struct line *line) {