Adjust the logic when deleting last buffer to better preserve the

selection: if selecting the element below the deleted one fails (because
as the last one), select the one above it instead. From Daniel Mueller,
GitHub issue 4043.
This commit is contained in:
nicm 2024-08-04 08:53:43 +00:00
parent ddd4e57c65
commit 7b6fbe7262
3 changed files with 18 additions and 7 deletions

View File

@ -261,19 +261,21 @@ mode_tree_up(struct mode_tree_data *mtd, int wrap)
}
}
void
int
mode_tree_down(struct mode_tree_data *mtd, int wrap)
{
if (mtd->current == mtd->line_size - 1) {
if (wrap) {
mtd->current = 0;
mtd->offset = 0;
}
} else
return (0);
} else {
mtd->current++;
if (mtd->current > mtd->offset + mtd->height - 1)
mtd->offset++;
}
return (1);
}
void *

2
tmux.h
View File

@ -3138,7 +3138,7 @@ int mode_tree_set_current(struct mode_tree_data *, uint64_t);
void mode_tree_each_tagged(struct mode_tree_data *, mode_tree_each_cb,
struct client *, key_code, int);
void mode_tree_up(struct mode_tree_data *, int);
void mode_tree_down(struct mode_tree_data *, int);
int mode_tree_down(struct mode_tree_data *, int);
struct mode_tree_data *mode_tree_start(struct window_pane *, struct args *,
mode_tree_build_cb, mode_tree_draw_cb, mode_tree_search_cb,
mode_tree_menu_cb, mode_tree_height_cb, mode_tree_key_cb, void *,

View File

@ -408,8 +408,17 @@ window_buffer_do_delete(void *modedata, void *itemdata,
struct window_buffer_itemdata *item = itemdata;
struct paste_buffer *pb;
if (item == mode_tree_get_current(data->data))
mode_tree_down(data->data, 0);
if (item == mode_tree_get_current(data->data) &&
!mode_tree_down(data->data, 0)) {
/*
*If we were unable to select the item further down we are at
* the end of the list. Move one element up instead, to make
* sure that we preserve a valid selection or we risk having
* the tree build logic reset it to the first item.
*/
mode_tree_up(data->data, 0);
}
if ((pb = paste_get_name(item->name)) != NULL)
paste_free(pb);
}
@ -508,7 +517,7 @@ window_buffer_key(struct window_mode_entry *wme, struct client *c,
struct window_buffer_itemdata *item;
int finished;
if (paste_get_top(NULL) == NULL) {
if (paste_is_empty()) {
finished = 1;
goto out;
}
@ -541,7 +550,7 @@ window_buffer_key(struct window_mode_entry *wme, struct client *c,
}
out:
if (finished || paste_get_top(NULL) == NULL)
if (finished || paste_is_empty())
window_pane_reset_mode(wp);
else {
mode_tree_draw(mtd);