From 38dca11cf8dd8dc69ac6ed89e0375ee4e86168a4 Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Sun, 29 Nov 2026 05:14:08 +0100 Subject: [PATCH] Sort channels and private chats in the tree --- README.md | 3 ++- irc9.c | 28 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 232a363..ec0084c 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,8 @@ Build yours by copying `irc9rc-example` to `irc9rc` next to the binary (or to ``` The left column is a tree: each server and its windows (channels and private -message windows). An unread window shows `(n)` next to its name. `*` marks a +message windows). Channels appear before private message windows, with +uppercase names sorting before lowercase names in each group. An unread window shows `(n)` next to its name. `*` marks a connected server. The selected server or window is shown bold and underlined. Highlights use the terminal's default background so transparency is preserved where supported. Channel topics wrap to the chat pane width and stay at the top while you scroll. diff --git a/irc9.c b/irc9.c index d6910b6..04ac8df 100644 --- a/irc9.c +++ b/irc9.c @@ -290,17 +290,25 @@ static Window *reply_window(Server *s) /* ------------------------------------------------------------------ */ /* windows and scrollback */ +static void window_insert(Server *s, Window *w) +{ + Window **link = &s->windows; + while (*link && ((*link)->type < w->type || + ((*link)->type == w->type && strcmp((*link)->name, w->name) <= 0))) + link = &(*link)->next; + w->next = *link; + *link = w; +} + static Window *window_new(Server *s, const char *name, Wintype type) { Window *w = xmalloc(sizeof *w); - Window **tail = &s->windows; memset(w, 0, sizeof *w); snprintf(w->name, sizeof w->name, "%s", name); w->type = type; w->srv = s; w->lines = xalloc_array(MAXSCROLL, sizeof *w->lines); - while (*tail) tail = &(*tail)->next; - *tail = w; + window_insert(s, w); return w; } @@ -1316,14 +1324,26 @@ static void handle_line(Server *s, char *line) if (strcasecmp(cmd, "NICK") == 0) { if (narg < 1) return; int in_channel = 0; - for (Window *w = s->windows; w; w = w->next) { + Window *renamed = NULL; + for (Window **link = &s->windows; *link; ) { + Window *w = *link; if (w->type == W_CHANNEL && member_find(w, nick)) { member_rename(w, nick, arg[0]); if (!silent) append_nickf(w, "* ", nick, " ", "is now known as %s", arg[0]); in_channel = 1; } else if (w->type == W_QUERY && strcasecmp(w->name, nick) == 0) { snprintf(w->name, sizeof w->name, "%s", arg[0]); + *link = w->next; + w->next = renamed; + renamed = w; + continue; } + link = &w->next; + } + while (renamed) { + Window *w = renamed; + renamed = w->next; + window_insert(s, w); } if (strcasecmp(nick, s->nick) == 0) snprintf(s->nick, sizeof s->nick, "%s", arg[0]);