From d32785554e5217899691b82c33b50ad710f1ce7e Mon Sep 17 00:00:00 2001 From: nicm Date: Sun, 4 Aug 2024 08:53:43 +0000 Subject: [PATCH] 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. --- usr.bin/tmux/mode-tree.c | 8 +++++--- usr.bin/tmux/tmux.h | 4 ++-- usr.bin/tmux/window-buffer.c | 15 ++++++++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/usr.bin/tmux/mode-tree.c b/usr.bin/tmux/mode-tree.c index 49fe2b7cfdf..8c62602428b 100644 --- a/usr.bin/tmux/mode-tree.c +++ b/usr.bin/tmux/mode-tree.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mode-tree.c,v 1.67 2024/05/24 12:39:06 nicm Exp $ */ +/* $OpenBSD: mode-tree.c,v 1.68 2024/08/04 08:53:43 nicm Exp $ */ /* * Copyright (c) 2017 Nicholas Marriott @@ -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 * diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index cc6c014b5a7..a1ffc50b32f 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.1219 2024/07/12 11:21:18 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.1220 2024/08/04 08:53:43 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -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 *, diff --git a/usr.bin/tmux/window-buffer.c b/usr.bin/tmux/window-buffer.c index 28dcef5ec84..185faf9ebc8 100644 --- a/usr.bin/tmux/window-buffer.c +++ b/usr.bin/tmux/window-buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window-buffer.c,v 1.39 2024/08/04 08:39:38 nicm Exp $ */ +/* $OpenBSD: window-buffer.c,v 1.40 2024/08/04 08:53:43 nicm Exp $ */ /* * Copyright (c) 2017 Nicholas Marriott @@ -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); } -- 2.20.1