diff --git a/APKBUILD b/APKBUILD index 1ba4f3b..4d3ea24 100644 --- a/APKBUILD +++ b/APKBUILD @@ -26,10 +26,10 @@ package() { } sha512sums=" -a2ce74a762c5d4a29ed2a0c1db930c4d6c5a02ff1b75c87c1a713ed3db01f26356d4a9bc73b43da7a94e05e90421353c7db3e5765a219b05adfda5690917c683 murx.c +95b73a9ce945da4071f60007a5c57f288b68e0677b05f8a4ffc8c0a10940e5ed57e9df1840e6ce2741d471e15375cfb5a4a09edade51c6b4866160a3c507cd73 murx.c ca73b902d64fc9dc52d8d14b6c8ca6b494f4bea3f3602c29fd2909252668e110edff075c881e014e689d7c7d990dee2a4a96208276bae50243ca382adc5ead80 Makefile -e084b9d2eaf2d93080898439a98e9a0a9acf2e076a53849f89ea0fff9a24d277cb348736cd3607bf18c9b94bd72af1c60d37617af9347d0452a04aff7160bedb murx.1 -af70fd707b1bcaaac68fffe8090783e06c9d494cc2ad11c0bfcc8e963bd51a658a02b17e296ed4017b87ffd04a412389c04d65a3192f78908deb7d70645162c1 README.md +db9fe919bac2b4976efa6d760b1f917ec6fee88a4f2811ea4ac69334f6f24d522569c2cd3c3d087eb43cace659bf4c00f3c4097cc901d769b4e4000342d65c45 murx.1 +153e4c4d23e64b74119714790c3701cc3cb2883494237fb44974efb9fc95a9adbc9a3425b1ed93d6e14014d8b3861e08ce2ef658093cd3ea151d9cf9fdada07f README.md 2fe33ba22c5f68e124904be594208f050be20f33d115f9a56f60cac93304102da32e6a7e78154e831d3704da7ca8bfefd7025e7b8901e65babcd83e7342f6382 LICENSE 4d32dba475e5f9818fcd008068b011cbae9fc4b8e29897c933775d6660f44a7992d08a1550320d83233af729a42b241ef1d182bf5983ef2f64deab76cab2260d AUTHORS " diff --git a/PKGBUILD b/PKGBUILD index 527a363..8dbf3f5 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -8,10 +8,10 @@ url='https://codeberg.org/armin/murx' license=('Apache-2.0') depends=('glibc') source=('murx.c' 'Makefile' 'murx.1' 'README.md' 'LICENSE' 'AUTHORS') -sha256sums=('0e30a88aac206f531087115521b3233acd35c011d625c44ff0c7e6809600d92f' +sha256sums=('ef7a6d215ec78876a7eb850eae5bb554011c4e12b4f0607f57d9195d9e2ba821' 'c683ed74c664ebb2a72603297367ed102afdf27e4d6cee47920db8632a8efa91' - '22bcaf7178867f0978616634951c6b89f9812698f9d21541cd742049dd6069e5' - '1155ea87e045f7cb90608c278abadf085c51ebf304d075eebf4f83d44fe30dc4' + 'd2219b9be02a6d588c593eb001c2c7f1ada845c4c72fa5837937d08192572678' + '7b47bf380ba9c9c5defede500514fba6527c8202e64d8f9f622c09de7c5610e8' '31c5556be4cdd9bf52bb55f18ead0c68a4f0e793c2c2dab0a2a2c6d92407d0d4' 'dbc8a4fc569d4cd7bcd45516ccc98c1396fce0682c262103069c8f5150538cdb') diff --git a/README.md b/README.md index f2a8525..a545100 100644 --- a/README.md +++ b/README.md @@ -299,12 +299,16 @@ Two bindings worth noting: `Ctrl-A Ctrl-H`/`Ctrl-A Ctrl-L` resize the pane Press `Ctrl-A`, then `Page Up` to scroll back in the active pane. Use `Page Up` and `Page Down` without the prefix to move through history. `Ctrl-A [` opens scrollback at the current screen. Press `q`, Escape, -Enter or Ctrl-C to return to live output. +Enter or Ctrl-C to return to live output. While browsing, use the usual +window shortcuts to switch directly, including `Ctrl-A n`, `Ctrl-A p`, +`Ctrl-A 0` through `Ctrl-A 9`, a double prefix, or `Ctrl-A w` for the window +chooser. Use `Ctrl-A c` to create a window while browsing. Returning to a +window restores its scrollback view and position. Each pane keeps up to 2,000 lines of normal screen history. The view is frozen while browsing; programs keep running and new output continues to be saved. -Scrollback is private to each client. Changing the active pane or window closes -it. Alternate-screen programs such as editors do not add history; opening +Scrollback views are private to each client and saved per pane while attached. +Alternate-screen programs such as editors do not add history; opening scrollback there shows only their current screen. History is kept in memory, retains colors, and is clipped or padded to the pane width without reflow. Restart an existing session server after upgrading to enable this feature. @@ -313,7 +317,8 @@ Restart an existing session server after upgrading to enable this feature. Hard-coded: -- Status on top by default (`-s bottom` moves it), default background (overridable with `-c`), foreground `colour12`. The palette mirrors +- Status on top by default (`-s bottom` moves it), default background (overridable with `-c`), + foreground `colour12`. The palette mirrors `~/.tmux.conf`. - With `--cwd`, `[/path/to/directory]` on the left (directory in `colour14`, brackets `colour12`), refreshed every second and when switching panes. On Linux this diff --git a/murx.1 b/murx.1 index 35e01cf..7972858 100644 --- a/murx.1 +++ b/murx.1 @@ -204,11 +204,15 @@ Ctrl-A Page Up freezes the active pane's history and moves up one page. Ctrl-A [ opens the view at the current screen. Use Page Up and Page Down without a prefix to browse; q, Escape, Enter or Ctrl-C returns to live output. +The usual window shortcuts work in scrollback, including Ctrl-A n, Ctrl-A p, +Ctrl-A 0 through Ctrl-A 9, a double prefix, and Ctrl-A w for the window chooser. +Use Ctrl-A c to create a window while browsing. +Returning to a window restores its scrollback view and position. .PP Each pane retains up to 2,000 normal-screen history lines in memory. Programs keep running while the view is frozen. -Scrollback is private to each client and closes when its active pane or -window changes. Alternate-screen programs such as editors do not add history; +Scrollback views are private to each client and saved per pane while attached. +Alternate-screen programs such as editors do not add history; opening scrollback there shows only their current screen. Colors are retained; lines are clipped or padded to the pane width without reflow. Text selection and copying are not provided. diff --git a/murx.c b/murx.c index 9a9bcf0..3523ed8 100644 --- a/murx.c +++ b/murx.c @@ -1207,6 +1207,13 @@ typedef struct Window { typedef struct Client Client; typedef struct Session Session; +typedef struct Scrollback { + Pane *pane; + Grid grid; + int offset; + struct Scrollback *next; +} Scrollback; + struct Client { int fd; int cols, rows; @@ -1226,15 +1233,13 @@ struct Client { int prefix_pending; unsigned char prefix; - int mode; /* 0 none, 1 prompt, 2 menu, 3 confirm, 4 scrollback */ + int mode; /* 0 none, 1 prompt, 2 menu, 3 confirm */ int prompt_kind; /* 1 rename window, 2 choose window */ char prompt[160]; int prompt_len; int prompt_label_len; - Grid backscroll; /* private frozen history and screen snapshot */ - Pane *backscroll_pane; - int backscroll_offset; + Scrollback *scrollbacks; int menu_sel; int confirm_action; /* 1 kill pane, 2 kill server */ @@ -2014,6 +2019,25 @@ static int status_y(Session *s) { return s->status_bottom ? s->h - 1 : 0; } +static Scrollback **scrollback_slot(Client *c, Pane *pane) { + Scrollback **slot = &c->scrollbacks; + while (*slot && (*slot)->pane != pane) slot = &(*slot)->next; + return slot; +} + +static Scrollback *scrollback_current(Client *c, Session *s) { + if (c->mode || !s->cur || !s->cur->active) return NULL; + return *scrollback_slot(c, s->cur->active); +} + +static void scrollback_remove(Scrollback **slot) { + Scrollback *view = *slot; + if (!view) return; + *slot = view->next; + grid_free(&view->grid); + free(view); +} + static void status_draw(Grid *g, Session *s, Client *c) { if (s->h < 1) return; int y = status_y(s); @@ -2083,9 +2107,10 @@ static void status_draw(Grid *g, Session *s, Client *c) { char scroll_msg[128]; const char *overlay = NULL; - if (c->mode == 4) { + Scrollback *view = scrollback_current(c, s); + if (view) { snprintf(scroll_msg, sizeof scroll_msg, "Scrollback [%d] PgUp/PgDn, q/Esc exits", - c->backscroll_offset); + view->offset); overlay = scroll_msg; } if (c->mode == 1 || c->mode == 3) overlay = c->prompt; @@ -2162,10 +2187,6 @@ static void menu_draw(Grid *g, Session *s, Client *c) { } static void compose(Session *s, Client *c, Grid *out) { - if (c->mode == 4 && (!s->cur || s->cur->active != c->backscroll_pane)) { - c->mode = 0; - grid_free(&c->backscroll); - } grid_init(out, s->w, s->h); grid_clear(out, DEFAULT_COLOR); if (s->cur && s->cur->root) { @@ -2176,12 +2197,13 @@ static void compose(Session *s, Client *c, Grid *out) { border_render(out, mask); free(mask); } - if (c->mode == 4 && s->cur && s->cur->active) { + Scrollback *view = scrollback_current(c, s); + if (view) { Pane *p = s->cur->active; - int top = c->backscroll.h - p->h - c->backscroll_offset; + int top = view->grid.h - p->h - view->offset; for (int y = 0; y < p->h; y++) { for (int x = 0; x < p->w; x++) { - Cell *src = grid_at(&c->backscroll, x, top + y); + Cell *src = grid_at(&view->grid, x, top + y); Cell *dst = grid_at(out, p->x + x, p->y + y); if (dst) { *dst = src ? *src : blank_cell(); @@ -2207,7 +2229,7 @@ static void cursor_for(Session *s, Client *c, int *x, int *y, int *visible) { *visible = 1; return; } - if (c->mode != 0) return; + if (c->mode != 0 || scrollback_current(c, s)) return; if (!s->cur || !s->cur->active) return; Pane *p = s->cur->active; if (!p->cursor_visible) return; @@ -2429,7 +2451,7 @@ static void draw_frame(Session *s, Client *c, Grid *scr) { the outer terminal continues sending ESC [ A/B/C/D. Overlays use normal cursor keys; switching panes restores that pane's mode. */ int app_cursor = !c->locked && c->mode == 0 && !c->prefix_pending && - s->cur && s->cur->active && s->cur->active->app_cursor; + !scrollback_current(c, s) && s->cur && s->cur->active && s->cur->active->app_cursor; buf_puts(&b, app_cursor ? "\x1b[?1h" : "\x1b[?1l"); buf_puts(&b, "\x1b[0m"); if (vis && (cx >= vw || cy >= vh)) vis = 0; @@ -2520,13 +2542,19 @@ static void session_recalc_size(Session *s) { invalidate_clients(s); } -static void window_free(Window *w) { +static void session_pane_free(Session *s, Pane *p) { + for (Client *c = s->clients; c; c = c->next) + scrollback_remove(scrollback_slot(c, p)); + pane_free(p); +} + +static void window_free(Session *s, Window *w) { if (!w) return; if (w->root) { Pane *list[128]; int n = 0; layout_collect(w->root, list, &n); - for (int i = 0; i < n; i++) pane_free(list[i]); + for (int i = 0; i < n; i++) session_pane_free(s, list[i]); layout_free(w->root); } free(w); @@ -2646,7 +2674,7 @@ static void kill_active_pane(Session *s) { s->nwin--; if (s->cur == w) s->cur = next ? next : prev; if (s->last == w) s->last = NULL; - window_free(w); + window_free(s, w); if (!s->windows) s->running = 0; } else { Layout *par = leaf->parent; @@ -2660,7 +2688,7 @@ static void kill_active_pane(Session *s) { sib->parent = par->parent; free(leaf); free(par); - pane_free(p); + session_pane_free(s, p); w->active = layout_first_pane(w->root); } window_renumber(s->windows); @@ -2684,7 +2712,7 @@ static void kill_active_window(Session *s) { s->nwin--; if (s->cur == w) s->cur = next ? next : prev; if (s->last == w) s->last = NULL; - window_free(w); + window_free(s, w); if (!s->windows) s->running = 0; window_renumber(s->windows); if (s->cur) { @@ -2954,35 +2982,33 @@ static void backscroll_start(Client *c, Session *s, int page_up) { Pane *p = s->cur ? s->cur->active : NULL; if (!p) return; int count = p->on_alt ? 0 : p->history_count; - grid_free(&c->backscroll); - grid_init(&c->backscroll, p->grid->w, count + p->grid->h); + Scrollback **slot = scrollback_slot(c, p); + if (!*slot) { + *slot = xcalloc(1, sizeof **slot); + (*slot)->pane = p; + } + Scrollback *view = *slot; + grid_free(&view->grid); + grid_init(&view->grid, p->grid->w, count + p->grid->h); for (int y = 0; y < count + p->grid->h; y++) { Grid *row = y < count ? &p->history[(p->history_start + y) % HISTORY_LINES] : p->grid; int sy = y < count ? 0 : y - count; - int width = row->w < c->backscroll.w ? row->w : c->backscroll.w; - memcpy(&c->backscroll.c[y * c->backscroll.w], &row->c[sy * row->w], + int width = row->w < view->grid.w ? row->w : view->grid.w; + memcpy(&view->grid.c[y * view->grid.w], &row->c[sy * row->w], (size_t)width * sizeof(Cell)); - Cell *last = grid_at(&c->backscroll, c->backscroll.w - 1, y); + Cell *last = grid_at(&view->grid, view->grid.w - 1, y); if (cp_width(last->ch) == 2) *last = blank_cell(); } - c->backscroll_pane = p; - c->backscroll_offset = page_up ? p->h : 0; - if (c->backscroll_offset > count) c->backscroll_offset = count; - c->mode = 4; + view->offset = page_up ? p->h : 0; + if (view->offset > count) view->offset = count; } /* Return zero for an incomplete page-key sequence. */ -static size_t backscroll_key(Client *c, Session *s, const unsigned char *buf, size_t n) { - if (!s->cur || s->cur->active != c->backscroll_pane) { - c->mode = 0; - grid_free(&c->backscroll); - return 1; - } +static size_t backscroll_key(Client *c, Scrollback *view, const unsigned char *buf, size_t n) { if (buf[0] == 27 && n == 1) return 0; if (buf[0] == 'q' || buf[0] == 3 || buf[0] == '\r' || buf[0] == '\n' || (buf[0] == 27 && (n == 1 || buf[1] != '['))) { - c->mode = 0; - grid_free(&c->backscroll); + scrollback_remove(scrollback_slot(c, view->pane)); return 1; } if (buf[0] == 27 && n >= 2 && buf[1] == '[') { @@ -2991,12 +3017,12 @@ static size_t backscroll_key(Client *c, Session *s, const unsigned char *buf, si if (used == n) return 0; used++; if (used == 4 && buf[3] == '~' && (buf[2] == '5' || buf[2] == '6')) { - int height = s->cur->active->h; - int max = c->backscroll.h - height; + int height = view->pane->h; + int max = view->grid.h - height; if (max < 0) max = 0; - c->backscroll_offset += buf[2] == '5' ? height : -height; - if (c->backscroll_offset < 0) c->backscroll_offset = 0; - if (c->backscroll_offset > max) c->backscroll_offset = max; + view->offset += buf[2] == '5' ? height : -height; + if (view->offset < 0) view->offset = 0; + if (view->offset > max) view->offset = max; } return used; } @@ -3004,6 +3030,17 @@ static size_t backscroll_key(Client *c, Session *s, const unsigned char *buf, si } static void prefix_command(Client *c, Session *s, int key) { + if (scrollback_current(c, s) && !(key >= '0' && key <= '9')) { + switch (key) { + case 0x03: case 'c': + case 0x0e: case ' ': case 'n': + case 0x10: case 'p': case 0x7f: + case 0x17: case 'w': case '"': + break; + default: + return; + } + } switch (key) { case '[': case K_PGUP: @@ -3278,10 +3315,8 @@ static void client_feed(Client *c, Session *s, const unsigned char *buf, size_t if (n > sizeof c->in - c->inlen) n = sizeof c->in - c->inlen; if (n == 0 && c->inlen == 1 && c->in[0] == 27 && c->escape_since && now_ms() - c->escape_since >= 100) { - if (c->mode == 4) { - c->mode = 0; - grid_free(&c->backscroll); - } + Scrollback *view = scrollback_current(c, s); + if (view) scrollback_remove(scrollback_slot(c, view->pane)); c->prefix_pending = 0; c->inlen = 0; c->escape_since = 0; @@ -3301,8 +3336,20 @@ static void client_feed(Client *c, Session *s, const unsigned char *buf, size_t continue; } if (c->dead) break; - if (c->mode == 4) { - size_t used = backscroll_key(c, s, p, left); + Scrollback *view = scrollback_current(c, s); + if (view) { + if (c->prefix_pending) { + int used = handle_prefix_key(c, s, p, left); + if (used == 0) break; + i += (size_t)used; + continue; + } + if (p[0] == c->prefix) { + c->prefix_pending = 1; + i++; + continue; + } + size_t used = backscroll_key(c, view, p, left); if (used == 0) break; i += used; continue; @@ -3346,7 +3393,7 @@ static void client_feed(Client *c, Session *s, const unsigned char *buf, size_t memmove(c->in, c->in + i, c->inlen - i); c->inlen -= i; } - if (c->inlen == 1 && c->in[0] == 27 && (c->mode == 4 || c->prefix_pending)) { + if (c->inlen == 1 && c->in[0] == 27 && (scrollback_current(c, s) || c->prefix_pending)) { if (!c->escape_since) c->escape_since = now_ms(); } else c->escape_since = 0; } @@ -3390,7 +3437,7 @@ static void unlink_window(Session *s, Window *w) { s->nwin--; if (s->cur == w) s->cur = next ? next : prev; if (s->last == w) s->last = NULL; - window_free(w); + window_free(s, w); window_renumber(s->windows); } @@ -3412,7 +3459,7 @@ static void remove_pane_ptr(Session *s, Pane *p) { sib->parent = par->parent; free(leaf); free(par); - pane_free(p); + session_pane_free(s, p); w->active = layout_first_pane(w->root); window_renumber(s->windows); } @@ -3433,7 +3480,7 @@ static void client_remove(Session *s, Client *c) { else s->clients = c->next; if (c->fd >= 0) close(c->fd); if (c->prev_valid) grid_free(&c->prev); - grid_free(&c->backscroll); + while (c->scrollbacks) scrollback_remove(&c->scrollbacks); free(c); session_recalc_size(s); invalidate_clients(s); @@ -3720,7 +3767,7 @@ static void server_cleanup(Session *s) { Client *next = c->next; if (c->fd >= 0) close(c->fd); if (c->prev_valid) grid_free(&c->prev); - grid_free(&c->backscroll); + while (c->scrollbacks) scrollback_remove(&c->scrollbacks); free(c); c = next; } @@ -3728,7 +3775,7 @@ static void server_cleanup(Session *s) { Window *w = s->windows; while (w) { Window *next = w->next; - window_free(w); + window_free(s, w); w = next; } s->windows = NULL; @@ -4347,6 +4394,7 @@ static void print_help(FILE *out) { " Ctrl-A Ctrl-L resize pane right\n" " Ctrl-A K kill pane (confirm)\n" " Ctrl-A PageUp, [ scrollback (PgUp/PgDn; q/Escape exits)\n" + " window creation and switching shortcuts work in scrollback\n" "\n" "Client and server\n" " Ctrl-A a send a literal Ctrl-A to the pane\n"