From c14317f3da8028b76ec57b7145775ed5efa638fa Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Sat, 28 Nov 2026 00:09:00 +0100 Subject: [PATCH] Add Ctrl-T copy view for chat windows --- README.md | 1 + irc9.c | 124 +++++++++++++++++++++++++++++++----------------------- 2 files changed, 73 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 0db62d6..f59f99a 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,7 @@ MODE, TOPIC, and INVITE events are shown in the relevant window. | `ctrl-n` / `ctrl-p` | next / previous window (tree order, wraps around) | | `PageUp` / `PageDown` | scroll the current window | | `ctrl-l` | clear the screen (history stays scrollable) | +| `ctrl-t` | toggle copy view, which hides the tree, chat timestamps, and sender names | | `ctrl-s` | save the current window’s scrollback to a new plain-text file | | Up / Down | command history | | Left / Right, Home / End | edit the input line | diff --git a/irc9.c b/irc9.c index 6c1d641..5c5c1f6 100644 --- a/irc9.c +++ b/irc9.c @@ -66,6 +66,11 @@ typedef struct Member { struct Member *next; } Member; +typedef struct ScrollLine { + char *text; + size_t body_offset; +} ScrollLine; + typedef struct Window { char name[CHANLEN]; char hostmask[128]; @@ -73,7 +78,7 @@ typedef struct Window { Wintype type; struct Server *srv; struct Window *next; - char **lines; + ScrollLine *lines; int nlines; int unread; int scroll; @@ -156,6 +161,7 @@ static struct termios saved_terminal; static int terminal_active; static int terminal_color; static int colored_nicks; +static int copy_view; static long accent_color = -1; enum { @@ -320,7 +326,7 @@ static void focus_window(Window *w) static void window_clear(Window *w) { if (!w) return; - for (int i = 0; i < w->nlines; i++) free(w->lines[i]); + for (int i = 0; i < w->nlines; i++) free(w->lines[i].text); w->nlines = 0; w->scroll = 0; w->unread = 0; @@ -334,7 +340,7 @@ static void window_close(Server *s, Window *w) while (*p && *p != w) p = &(*p)->next; if (!*p) return; *p = w->next; - for (int i = 0; i < w->nlines; i++) free(w->lines[i]); + for (int i = 0; i < w->nlines; i++) free(w->lines[i].text); free(w->lines); for (Member *m = w->members; m; ) { Member *mn = m->next; free(m); m = mn; @@ -384,10 +390,10 @@ static void set_channel_topic(Window *w, const char *topic) dirty = 1; } -static void win_append_raw(Window *w, const char *text, int keep_formatting) +static void win_append_raw(Window *w, const char *text, int keep_formatting, size_t body_offset) { if (w->nlines == MAXSCROLL) { - free(w->lines[0]); + free(w->lines[0].text); memmove(w->lines, w->lines + 1, (MAXSCROLL - 1) * sizeof *w->lines); w->nlines--; } @@ -397,8 +403,9 @@ static void win_append_raw(Window *w, const char *text, int keep_formatting) size_t sz = size_add(size_add(strlen(stamp), strlen(clean)), 1); line = xmalloc(sz); snprintf(line, sz, "%s%s", stamp, clean); + if (body_offset > strlen(clean)) body_offset = strlen(clean); + w->lines[w->nlines++] = (ScrollLine){line, strlen(stamp) + body_offset}; free(clean); - w->lines[w->nlines++] = line; if (w->scroll > 0) w->scroll++; if (w != curwin) w->unread++; dirty = 1; @@ -411,7 +418,7 @@ static void appendf(Window *w, const char *fmt, ...) va_start(ap, fmt); vsnprintf(buf, sizeof buf, fmt, ap); va_end(ap); - win_append_raw(w, buf, 0); + win_append_raw(w, buf, 0, 0); } /* Keep green reserved for our nick and assign others a stable palette color. */ @@ -426,7 +433,7 @@ static int nick_color(const Server *s, const char *nick, size_t len) } static void append_nickf(Window *w, const char *before, const char *identity, - const char *fmt, ...) + const char *separator, const char *fmt, ...) { char after[MAXLINE * 2]; va_list ap; @@ -434,23 +441,25 @@ static void append_nickf(Window *w, const char *before, const char *identity, vsnprintf(after, sizeof after, fmt, ap); va_end(ap); - if (!colored_nicks || !terminal_color || w->type == W_SERVER) { - appendf(w, "%s%s%s", before, identity, after); - return; - } - - size_t len = strcspn(identity, "!@"); char *plain = strip_formatting ? strip_ircfmt(after) : NULL; - const char *suffix = plain ? plain : after; - size_t size = size_add(size_add(strlen(before), strlen(identity)), - size_add(strlen(suffix), 5)); - char *line = xmalloc(size); - snprintf(line, size, "%s\003%02d%.*s\003%s%s", - before, nick_color(w->srv, identity, len), (int)len, identity, - identity + len, suffix); + const char *body = plain ? plain : after; + size_t prefix = strlen(before) + strlen(identity) + strlen(separator); + if (colored_nicks && terminal_color && w->type != W_SERVER) { + size_t len = strcspn(identity, "!@"); + prefix += 4; /* Three bytes to set the IRC color, one to reset it. */ + size_t size = size_add(prefix, size_add(strlen(body), 1)); + char *line = xmalloc(size); + snprintf(line, size, "%s\003%02d%.*s\003%s%s%s", + before, nick_color(w->srv, identity, len), (int)len, identity, + identity + len, separator, body); + win_append_raw(w, line, 1, prefix); + free(line); + } else { + char line[MAXLINE * 2]; + snprintf(line, sizeof line, "%s%s%s%s", before, identity, separator, body); + win_append_raw(w, line, 1, prefix); + } free(plain); - win_append_raw(w, line, 1); - free(line); } static void switch_window(int dir) @@ -594,7 +603,7 @@ static void show_names(Window *w) len += add; } line[len] = 0; - win_append_raw(w, line, colored_nicks); + win_append_raw(w, line, colored_nicks, 0); } /* ------------------------------------------------------------------ */ @@ -1107,9 +1116,9 @@ static int handle_ctcp(Server *s, const char *nick, char *text, const char *arg = argp ? argp : ""; if (is_reply) { - append_nickf(w, "* ", nick, " CTCP %s: %s", cmd, arg); + append_nickf(w, "* ", nick, " ", "CTCP %s: %s", cmd, arg); } else if (strcasecmp(cmd, "ACTION") == 0) { - append_nickf(w, "* ", nick, " %s", arg); + append_nickf(w, "* ", nick, " ", "%s", arg); } else if (strcasecmp(cmd, "VERSION") == 0) { sendf(s, "NOTICE %s :\001VERSION irc9\001", nick); } else if (strcasecmp(cmd, "PING") == 0) { @@ -1121,7 +1130,7 @@ static int handle_ctcp(Server *s, const char *nick, char *text, } else if (strcasecmp(cmd, "USERINFO") == 0) { sendf(s, "NOTICE %s :\001USERINFO %s\001", nick, g_real); } else { - append_nickf(w, "* ", nick, " CTCP %s", cmd); + append_nickf(w, "* ", nick, " ", "CTCP %s", cmd); } return 1; } @@ -1210,8 +1219,8 @@ static void handle_line(Server *s, char *line) if (w->type == W_QUERY && hostmask && strchr(hostmask, '!')) snprintf(w->hostmask, sizeof w->hostmask, "%s", hostmask); if (narg > 1 && handle_ctcp(s, nick, arg[1], w, notice)) return; - if (notice) append_nickf(w, "-", *nick ? nick : "server", "- %s", text); - else append_nickf(w, "<", *nick ? nick : "server", "> %s", text); + if (notice) append_nickf(w, "-", *nick ? nick : "server", "- ", "%s", text); + else append_nickf(w, "<", *nick ? nick : "server", "> ", "%s", text); return; } @@ -1219,7 +1228,7 @@ static void handle_line(Server *s, char *line) if (narg < 1) return; Window *w = window_get(s, arg[0], W_CHANNEL); member_add(w, nick); - if (!silent) append_nickf(w, "* ", hostmask ? hostmask : "someone", " joined %s", arg[0]); + if (!silent) append_nickf(w, "* ", hostmask ? hostmask : "someone", " ", "joined %s", arg[0]); return; } @@ -1235,7 +1244,7 @@ static void handle_line(Server *s, char *line) w = window_get(s, arg[0], W_CHANNEL); member_remove(w, nick); } - append_nickf(w, "* ", hostmask ? hostmask : "someone", " left %s%s", arg[0], why); + append_nickf(w, "* ", hostmask ? hostmask : "someone", " ", "left %s%s", arg[0], why); return; } @@ -1245,7 +1254,7 @@ static void handle_line(Server *s, char *line) int shown = 0; for (Window *w = s->windows; w; w = w->next) if (w->type == W_CHANNEL && member_find(w, nick)) { - if (!silent) append_nickf(w, "* ", hostmask ? hostmask : nick, " quit%s", why); + if (!silent) append_nickf(w, "* ", hostmask ? hostmask : nick, " ", "quit%s", why); member_remove(w, nick); shown = 1; } @@ -1259,7 +1268,7 @@ static void handle_line(Server *s, char *line) for (Window *w = s->windows; w; w = w->next) { if (w->type == W_CHANNEL && member_find(w, nick)) { member_rename(w, nick, arg[0]); - append_nickf(w, "* ", nick, " is now known as %s", arg[0]); + append_nickf(w, "* ", nick, " ", "is now known as %s", arg[0]); shown = 1; } else if (w->type == W_QUERY && strcasecmp(w->name, nick) == 0) { snprintf(w->name, sizeof w->name, "%s", arg[0]); @@ -1275,7 +1284,7 @@ static void handle_line(Server *s, char *line) Window *w = window_get(s, arg[0], W_CHANNEL); char why[MAXLINE]; if (narg > 2) snprintf(why, sizeof why, " (%s)", arg[2]); else why[0] = 0; - append_nickf(w, "* ", arg[1], " was kicked from %s by %s%s", + append_nickf(w, "* ", arg[1], " ", "was kicked from %s by %s%s", arg[0], *nick ? nick : "server", why); member_remove(w, arg[1]); return; @@ -1293,7 +1302,7 @@ static void handle_line(Server *s, char *line) if (w->type == W_CHANNEL) member_modes(w, arg[1], arg + 2, narg - 2); else if (strcasecmp(arg[0], s->nick) == 0) snprintf(s->umode, sizeof s->umode, "%s", arg[1]); - append_nickf(w, "* ", *nick ? nick : "server", " sets mode %s on %s", + append_nickf(w, "* ", *nick ? nick : "server", " ", "sets mode %s on %s", params, arg[0]); return; } @@ -1301,7 +1310,7 @@ static void handle_line(Server *s, char *line) if (strcasecmp(cmd, "TOPIC") == 0 && narg >= 2) { Window *w = window_get(s, arg[0], W_CHANNEL); set_channel_topic(w, arg[1]); - append_nickf(w, "* ", *nick ? nick : "server", " changed topic of %s: %s", + append_nickf(w, "* ", *nick ? nick : "server", " ", "changed topic of %s: %s", arg[0], arg[1]); return; } @@ -1838,7 +1847,7 @@ static void do_command(char *line) sendf(curwin->srv, "PRIVMSG %s :%s", target, q); Window *w = window_get(curwin->srv, target, is_channel(target) ? W_CHANNEL : W_QUERY); - append_nickf(w, "<", curwin->srv->nick, "> %s", q); + append_nickf(w, "<", curwin->srv->nick, "> ", "%s", q); } else if (strcasecmp(cmd, "whois") == 0) { char *nick = next_word(&q); if (!curwin) return; @@ -1850,7 +1859,7 @@ static void do_command(char *line) Window *t = input_target(); if (!t || !*q) return; sendf(t->srv, "PRIVMSG %s :\001ACTION %s\001", t->name, q); - append_nickf(t, "* ", t->srv->nick, " %s", q); + append_nickf(t, "* ", t->srv->nick, " ", "%s", q); } else if (strcasecmp(cmd, "away") == 0) { if (!curwin) return; sendf(curwin->srv, "AWAY :%s", *q ? q : ""); @@ -1912,7 +1921,7 @@ static void do_command(char *line) sendf(curwin->srv, "NOTICE %s :%s", target, q); Window *w = window_get(curwin->srv, target, is_channel(target) ? W_CHANNEL : W_QUERY); - append_nickf(w, "-", curwin->srv->nick, "- %s", q); + append_nickf(w, "-", curwin->srv->nick, "- ", "%s", q); } else if (strcasecmp(cmd, "query") == 0) { char *nick = next_word(&q); if (!curwin) return; @@ -1977,7 +1986,7 @@ static void do_command(char *line) appendf(t, " /voice /devoice /ban /unban /raw /close /wc /clear"); appendf(t, " /reconnect /quit /help"); appendf(t, "keys: ctrl-n / ctrl-p switch windows, pgup/pgdn scroll, ctrl-l clear screen"); - appendf(t, " ctrl-s save scrollback"); + appendf(t, " ctrl-s save scrollback, ctrl-t toggle copy view"); } else { if (curwin) appendf(curwin, "* unknown command: /%s", cmd); } @@ -1997,7 +2006,7 @@ static void submit_input(void) if (curwin) appendf(curwin, "* no target here; use /join or /msg"); } else { sendf(t->srv, "PRIVMSG %s :%s", t->name, line); - append_nickf(t, "<", t->srv->nick, "> %s", line); + append_nickf(t, "<", t->srv->nick, "> ", "%s", line); } } @@ -2017,7 +2026,7 @@ static void save_scrollback(Window *w, const char *path) int error = 0; for (int i = 0; i < w->nlines && !error; i++) { - char *line = strip_ircfmt(w->lines[i]); + char *line = strip_ircfmt(w->lines[i].text); for (char *p = line; *p; p++) if (((unsigned char)*p < 32 && *p != '\t') || *p == 127) *p = '?'; if (fprintf(file, "%s\n", line) < 0) error = errno; @@ -2055,6 +2064,7 @@ static void edit_line(int c, char text[MAXINPUT], int *len, int *pos) } static int layout_topic(int draw); +static void layout(void); static void handle_key(int c) { @@ -2080,6 +2090,12 @@ static void handle_key(int c) if (c == '\t') { complete_input(); return; } if (c == 0x0e) { switch_window(1); return; } /* ctrl-n */ if (c == 0x10) { switch_window(-1); return; } /* ctrl-p */ + if (c == 0x14) { /* ctrl-t */ + copy_view = !copy_view; + layout(); + dirty = 1; + return; + } if (c == 0x0c) { /* ctrl-l */ if (curwin) { curwin->scroll = -(mainh - layout_topic(0)); dirty = 1; } return; @@ -2121,7 +2137,8 @@ static void handle_key(int c) static void layout(void) { treew = cols >= 60 ? TREEW : (cols >= 30 ? 16 : 8); - if (treew > cols / 2) treew = cols / 2; + if (copy_view) treew = 0; + else if (treew > cols / 2) treew = cols / 2; mainh = lines > 4 ? lines - 4 : 0; } @@ -2431,7 +2448,7 @@ static void draw_title(void) /* Use the same topic height for drawing and paging the chat pane. */ static int layout_topic(int draw) { - int width = cols - treew - 2; + int width = cols - treew - (copy_view ? 0 : 2); if (!curwin || curwin->type != W_CHANNEL || mainh < 1 || width < 1) return 0; @@ -2446,7 +2463,7 @@ static int layout_topic(int draw) int height = count < limit ? count : limit; for (int row = 0; row < count; row++) { char *text = rows[count - 1 - row]; - if (draw && row < height) draw_irc_text(row + 1, treew + 1, text, width); + if (draw && row < height) draw_irc_text(row + 1, treew + (copy_view ? 0 : 1), text, width); free(text); } return height; @@ -2454,10 +2471,10 @@ static int layout_topic(int draw) static void draw_main(void) { - int width = cols - treew; + int ww = cols - treew - (copy_view ? 0 : 2); + int left = treew + (copy_view ? 0 : 1); - if (curwin && mainh > 0 && width >= 3) { - int ww = width - 2; + if (curwin && mainh > 0 && ww > 0) { int header = layout_topic(1); int h = mainh - header; if (h == 0) return; @@ -2469,8 +2486,11 @@ static void draw_main(void) char **rows = xalloc_array(cap, sizeof *rows); int nr = 0; int maxrows = need > INT_MAX ? INT_MAX : (int)need; - for (int i = curwin->nlines - 1; i >= 0 && nr < maxrows; i--) - push_wrapped(rows, &nr, maxrows, curwin->lines[i], ww); + for (int i = curwin->nlines - 1; i >= 0 && nr < maxrows; i--) { + const ScrollLine *line = &curwin->lines[i]; + const char *text = line->text + (copy_view ? line->body_offset : 0); + push_wrapped(rows, &nr, maxrows, text, ww); + } if ((size_t)nr < need) { int maxscroll = nr - h; if (maxscroll < 0) maxscroll = 0; @@ -2479,7 +2499,7 @@ static void draw_main(void) for (int r = 0; r < h; r++) { int idx = curwin->scroll + (h - 1 - r); if (idx >= 0 && idx < nr && rows[idx]) - draw_irc_text(r + 1 + header, treew + 1, rows[idx], ww); + draw_irc_text(r + 1 + header, left, rows[idx], ww); } for (int i = 0; i < nr; i++) free(rows[i]); free(rows); @@ -2816,7 +2836,7 @@ int main(int argc, char **argv) Server *sn = s->next; for (Window *w = s->windows; w; ) { Window *wn = w->next; - for (int i = 0; i < w->nlines; i++) free(w->lines[i]); + for (int i = 0; i < w->nlines; i++) free(w->lines[i].text); free(w->lines); for (Member *m = w->members; m; ) { Member *mn = m->next; free(m); m = mn;