selection: break out pivot point adjustment for character-wise selections

This commit is contained in:
Daniel Eklöf 2021-01-06 10:54:53 +01:00
parent 3a9172342f
commit f236dc7701
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

View file

@ -549,46 +549,16 @@ selection_modify(struct terminal *term, struct coord start, struct coord end)
render_refresh(term); render_refresh(term);
} }
void static void
selection_update(struct terminal *term, int col, int row) set_pivot_point_for_block_and_char_wise(struct terminal *term,
struct coord start,
enum selection_direction new_direction)
{ {
if (term->selection.start.row < 0)
return;
if (!term->selection.ongoing)
return;
LOG_DBG("selection updated: start = %d,%d, end = %d,%d -> %d, %d",
term->selection.start.row, term->selection.start.col,
term->selection.end.row, term->selection.end.col,
row, col);
assert(term->grid->view + row != -1);
struct coord new_start = term->selection.start;
struct coord new_end = {col, term->grid->view + row};
/* Adjust start point if the selection has changed 'direction' */
if (!(new_end.row == new_start.row && new_end.col == new_start.col)) {
enum selection_direction new_direction = term->selection.direction;
struct coord *pivot_start = &term->selection.pivot.start; struct coord *pivot_start = &term->selection.pivot.start;
struct coord *pivot_end = &term->selection.pivot.end; struct coord *pivot_end = &term->selection.pivot.end;
if (new_end.row < pivot_start->row || *pivot_start = start;
(new_end.row == pivot_start->row && new_end.col < pivot_start->col))
{
/* New end point is before the start point */
new_direction = SELECTION_LEFT;
} else {
/* The new end point is after the start point */
new_direction = SELECTION_RIGHT;
}
if (term->selection.direction != new_direction) {
if (term->selection.direction == SELECTION_UNDIR &&
pivot_end->row < 0)
{
/* First, make sure start isnt in the middle of a /* First, make sure start isnt in the middle of a
* multi-column character */ * multi-column character */
while (true) { while (true) {
@ -653,6 +623,66 @@ selection_update(struct terminal *term, int col, int row)
cells[pivot_start->col].wc != CELL_MULT_COL_SPACER); cells[pivot_start->col].wc != CELL_MULT_COL_SPACER);
assert(term->grid->rows[pivot_end->row & (term->grid->num_rows - 1)]-> assert(term->grid->rows[pivot_end->row & (term->grid->num_rows - 1)]->
cells[pivot_end->col].wc != CELL_MULT_COL_SPACER); cells[pivot_end->col].wc != CELL_MULT_COL_SPACER);
}
void
selection_update(struct terminal *term, int col, int row)
{
if (term->selection.start.row < 0)
return;
if (!term->selection.ongoing)
return;
LOG_DBG("selection updated: start = %d,%d, end = %d,%d -> %d, %d",
term->selection.start.row, term->selection.start.col,
term->selection.end.row, term->selection.end.col,
row, col);
assert(term->grid->view + row != -1);
struct coord new_start = term->selection.start;
struct coord new_end = {col, term->grid->view + row};
/* Adjust start point if the selection has changed 'direction' */
if (!(new_end.row == new_start.row && new_end.col == new_start.col)) {
enum selection_direction new_direction = term->selection.direction;
struct coord *pivot_start = &term->selection.pivot.start;
struct coord *pivot_end = &term->selection.pivot.end;
if (term->selection.kind == SELECTION_BLOCK) {
if (new_end.col > pivot_start->col)
new_direction = SELECTION_RIGHT;
else
new_direction = SELECTION_LEFT;
if (term->selection.direction == SELECTION_UNDIR)
set_pivot_point_for_block_and_char_wise(term, *pivot_start, new_direction);
if (new_direction == SELECTION_LEFT)
new_start = *pivot_end;
else
new_start = *pivot_start;
term->selection.direction = new_direction;
} else {
if (new_end.row < pivot_start->row ||
(new_end.row == pivot_start->row &&
new_end.col < pivot_start->col))
{
/* New end point is before the start point */
new_direction = SELECTION_LEFT;
} else {
/* The new end point is after the start point */
new_direction = SELECTION_RIGHT;
}
if (term->selection.direction != new_direction) {
if (term->selection.direction == SELECTION_UNDIR &&
pivot_end->row < 0)
{
set_pivot_point_for_block_and_char_wise(
term, *pivot_start, new_direction);
} }
if (new_direction == SELECTION_LEFT) { if (new_direction == SELECTION_LEFT) {
@ -664,6 +694,7 @@ selection_update(struct terminal *term, int col, int row)
term->selection.direction = new_direction; term->selection.direction = new_direction;
} }
} }
}
switch (term->selection.kind) { switch (term->selection.kind) {
case SELECTION_CHAR_WISE: case SELECTION_CHAR_WISE: