summaryrefslogtreecommitdiff
path: root/usr.bin/tmux/window-buffer.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2019-08-16 11:49:13 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2019-08-16 11:49:13 +0000
commit9fa3141ac9e568e83364b991fadc715db37a90c0 (patch)
tree98fe7031d3d51f2c1c19934c12ff237a74411abf /usr.bin/tmux/window-buffer.c
parent0887a7c432979654683c94005bde76863609593d (diff)
Add a flag to reverse sort in the various choose modes, from Benjamin
Poirier in GitHub issue 1875.
Diffstat (limited to 'usr.bin/tmux/window-buffer.c')
-rw-r--r--usr.bin/tmux/window-buffer.c72
1 files changed, 24 insertions, 48 deletions
diff --git a/usr.bin/tmux/window-buffer.c b/usr.bin/tmux/window-buffer.c
index 2927029f997..a29bec4d8af 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.21 2019/05/29 20:05:15 nicm Exp $ */
+/* $OpenBSD: window-buffer.c,v 1.22 2019/08/16 11:49:12 nicm Exp $ */
/*
* Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -75,6 +75,7 @@ static const char *window_buffer_sort_list[] = {
"name",
"size"
};
+static struct mode_tree_sort_criteria *window_buffer_sort;
struct window_buffer_itemdata {
const char *name;
@@ -113,43 +114,29 @@ window_buffer_free_item(struct window_buffer_itemdata *item)
}
static int
-window_buffer_cmp_name(const void *a0, const void *b0)
+window_buffer_cmp(const void *a0, const void *b0)
{
- const struct window_buffer_itemdata *const *a = a0;
- const struct window_buffer_itemdata *const *b = b0;
-
- return (strcmp((*a)->name, (*b)->name));
-}
-
-static int
-window_buffer_cmp_time(const void *a0, const void *b0)
-{
- const struct window_buffer_itemdata *const *a = a0;
- const struct window_buffer_itemdata *const *b = b0;
-
- if ((*a)->order > (*b)->order)
- return (-1);
- if ((*a)->order < (*b)->order)
- return (1);
- return (strcmp((*a)->name, (*b)->name));
-}
-
-static int
-window_buffer_cmp_size(const void *a0, const void *b0)
-{
- const struct window_buffer_itemdata *const *a = a0;
- const struct window_buffer_itemdata *const *b = b0;
-
- if ((*a)->size > (*b)->size)
- return (-1);
- if ((*a)->size < (*b)->size)
- return (1);
- return (strcmp((*a)->name, (*b)->name));
+ const struct window_buffer_itemdata *const *a = a0;
+ const struct window_buffer_itemdata *const *b = b0;
+ int result = 0;
+
+ if (window_buffer_sort->field == WINDOW_BUFFER_BY_TIME)
+ result = (*b)->order - (*a)->order;
+ else if (window_buffer_sort->field == WINDOW_BUFFER_BY_SIZE)
+ result = (*b)->size - (*a)->size;
+
+ /* Use WINDOW_BUFFER_BY_NAME as default order and tie breaker. */
+ if (result == 0)
+ result = strcmp((*a)->name, (*b)->name);
+
+ if (window_buffer_sort->reversed)
+ result = -result;
+ return (result);
}
static void
-window_buffer_build(void *modedata, u_int sort_type, __unused uint64_t *tag,
- const char *filter)
+window_buffer_build(void *modedata, struct mode_tree_sort_criteria *sort_crit,
+ __unused uint64_t *tag, const char *filter)
{
struct window_buffer_modedata *data = modedata;
struct window_buffer_itemdata *item;
@@ -175,20 +162,9 @@ window_buffer_build(void *modedata, u_int sort_type, __unused uint64_t *tag,
item->order = paste_buffer_order(pb);
}
- switch (sort_type) {
- case WINDOW_BUFFER_BY_NAME:
- qsort(data->item_list, data->item_size, sizeof *data->item_list,
- window_buffer_cmp_name);
- break;
- case WINDOW_BUFFER_BY_TIME:
- qsort(data->item_list, data->item_size, sizeof *data->item_list,
- window_buffer_cmp_time);
- break;
- case WINDOW_BUFFER_BY_SIZE:
- qsort(data->item_list, data->item_size, sizeof *data->item_list,
- window_buffer_cmp_size);
- break;
- }
+ window_buffer_sort = sort_crit;
+ qsort(data->item_list, data->item_size, sizeof *data->item_list,
+ window_buffer_cmp);
if (cmd_find_valid_state(&data->fs)) {
s = data->fs.s;