mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-07-06 00:06:08 -04:00
selection: break out pivot point adjustment for character-wise selections
This commit is contained in:
parent
3a9172342f
commit
f236dc7701
1 changed files with 113 additions and 82 deletions
101
selection.c
101
selection.c
|
|
@ -549,46 +549,16 @@ selection_modify(struct terminal *term, struct coord start, struct coord end)
|
|||
render_refresh(term);
|
||||
}
|
||||
|
||||
void
|
||||
selection_update(struct terminal *term, int col, int row)
|
||||
static void
|
||||
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_end = &term->selection.pivot.end;
|
||||
|
||||
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;
|
||||
}
|
||||
*pivot_start = start;
|
||||
|
||||
if (term->selection.direction != new_direction) {
|
||||
if (term->selection.direction == SELECTION_UNDIR &&
|
||||
pivot_end->row < 0)
|
||||
{
|
||||
/* First, make sure ‘start’ isn’t in the middle of a
|
||||
* multi-column character */
|
||||
while (true) {
|
||||
|
|
@ -653,6 +623,66 @@ selection_update(struct terminal *term, int col, int row)
|
|||
cells[pivot_start->col].wc != CELL_MULT_COL_SPACER);
|
||||
assert(term->grid->rows[pivot_end->row & (term->grid->num_rows - 1)]->
|
||||
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) {
|
||||
|
|
@ -664,6 +694,7 @@ selection_update(struct terminal *term, int col, int row)
|
|||
term->selection.direction = new_direction;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch (term->selection.kind) {
|
||||
case SELECTION_CHAR_WISE:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue