1
0
Fork 0
forked from murx/murx

Made Ctrl-c configurable to any prefix.

This commit is contained in:
Johannes Findeisen 2026-11-15 06:28:29 +01:00
commit 1c4e34e0e3
2 changed files with 73 additions and 30 deletions

View file

@ -7,14 +7,27 @@ dependencies.
make -B CC=clang make -B CC=clang
./moerx # session "0" ./moerx # session "0"
./moerx work # separate named session ./moerx work # separate named session
./moerx -p C-x work # use Ctrl-X as this client's prefix
./moerx --help # all shortcuts ./moerx --help # all shortcuts
``` ```
Detach with `Ctrl-A d`. Run `moerx` again to reattach. Detach with `Ctrl-A d`. Run `moerx` again to reattach.
Every shortcut is a chain that starts with the `Ctrl-A` prefix: press `Ctrl-A`, Every shortcut is a chain that starts with the prefix (by default `Ctrl-A`):
release it, then press the rest of the chain. `Ctrl-B` is not a prefix; it is press it, release it, then press the rest of the chain.
sent straight to the pane.
Use `-p C-x` or `--prefix C-x` to choose Ctrl-X instead. Control letters
`C-a` through `C-z` are supported, case-insensitively. The choice applies to
this attached client; use the option again when reattaching, or set a shell
alias such as `alias moerx='moerx -p C-x'`. Clients attached to the same session
can choose different prefixes.
With `-p C-x`, detach with `Ctrl-X d`, send a literal Ctrl-X with `Ctrl-X a`,
and switch to the last window with `Ctrl-X Ctrl-X`. Doubling the prefix takes
priority over any other control-key binding; `Ctrl-X x` still locks the client.
Ctrl-A is sent straight to the pane. All tables below show the default prefix;
replace it with your chosen key. When upgrading from a version without this
option, restart the session server once so it understands the prefix setting.
## What it does ## What it does

70
moerx.c
View file

@ -54,7 +54,7 @@
#include <wchar.h> #include <wchar.h>
#include <wctype.h> #include <wctype.h>
#define PREFIX 0x01 #define DEFAULT_PREFIX 0x01
/* client -> server */ /* client -> server */
enum { C_ATTACH = 1, C_INPUT = 2, C_RESIZE = 3 }; enum { C_ATTACH = 1, C_INPUT = 2, C_RESIZE = 3 };
@ -1037,6 +1037,7 @@ struct Client {
int locked; int locked;
int prefix_pending; int prefix_pending;
unsigned char prefix;
int mode; /* 0 none, 1 prompt, 2 menu, 3 confirm */ int mode; /* 0 none, 1 prompt, 2 menu, 3 confirm */
int prompt_kind; /* 1 rename window, 2 choose window */ int prompt_kind; /* 1 rename window, 2 choose window */
@ -2576,7 +2577,7 @@ static void prefix_command(Client *c, Session *s, int key) {
switch (key) { switch (key) {
case 'a': case 'a':
if (s->cur && s->cur->active) { if (s->cur && s->cur->active) {
unsigned char b = PREFIX; unsigned char b = c->prefix;
pane_write(s->cur->active, &b, 1); pane_write(s->cur->active, &b, 1);
} }
break; break;
@ -2610,9 +2611,6 @@ static void prefix_command(Client *c, Session *s, int key) {
case 'A': case 'A':
start_rename_prompt(c, s); start_rename_prompt(c, s);
break; break;
case 0x01:
switch_last_window(s);
break;
case 0x10: case 0x10:
case 'p': case 'p':
case 0x7f: case 0x7f:
@ -2705,6 +2703,11 @@ static void prefix_command(Client *c, Session *s, int key) {
static int handle_prefix_key(Client *c, Session *s, const unsigned char *buf, size_t n) { static int handle_prefix_key(Client *c, Session *s, const unsigned char *buf, size_t n) {
if (n == 0) return 0; if (n == 0) return 0;
unsigned char b = buf[0]; unsigned char b = buf[0];
if (b == c->prefix) {
c->prefix_pending = 0;
switch_last_window(s);
return 1;
}
int key = b; int key = b;
int used = 1; int used = 1;
if (b == 0x1b) { if (b == 0x1b) {
@ -2875,13 +2878,13 @@ static void client_feed(Client *c, Session *s, const unsigned char *buf, size_t
i += (size_t)used; i += (size_t)used;
continue; continue;
} }
if (p[0] == PREFIX) { if (p[0] == c->prefix) {
c->prefix_pending = 1; c->prefix_pending = 1;
i++; i++;
continue; continue;
} }
size_t j = i; size_t j = i;
while (j < c->inlen && c->in[j] != PREFIX) j++; while (j < c->inlen && c->in[j] != c->prefix) j++;
if (s->cur && s->cur->active) pane_write(s->cur->active, c->in + i, j - i); if (s->cur && s->cur->active) pane_write(s->cur->active, c->in + i, j - i);
i = j; i = j;
} }
@ -2982,6 +2985,7 @@ static void client_remove(Session *s, Client *c) {
static Client *client_new(int fd) { static Client *client_new(int fd) {
Client *c = xcalloc(1, sizeof *c); Client *c = xcalloc(1, sizeof *c);
c->fd = fd; c->fd = fd;
c->prefix = DEFAULT_PREFIX;
return c; return c;
} }
@ -2992,6 +2996,10 @@ static void handle_message(Client *c, Session *s, uint32_t type, const unsigned
memcpy(&cols, data, 4); memcpy(&cols, data, 4);
memcpy(&rows, data + 4, 4); memcpy(&rows, data + 4, 4);
if (len >= 12) memcpy(&flags, data + 8, 4); if (len >= 12) memcpy(&flags, data + 8, 4);
uint32_t prefix = DEFAULT_PREFIX;
if (len >= 16) memcpy(&prefix, data + 12, 4);
if (prefix < 1 || prefix > 26) { c->dead = 1; return; }
c->prefix = (unsigned char)prefix;
if (cols < 1) cols = 1; if (cols < 1) cols = 1;
if (rows < 1) rows = 1; if (rows < 1) rows = 1;
if (cols > 1000) cols = 1000; if (cols > 1000) cols = 1000;
@ -3522,7 +3530,7 @@ static int terminal_truecolor(void) {
return 1; return 1;
} }
static int run_client(int fd, const char *name) { static int run_client(int fd, const char *name, unsigned char prefix) {
struct termios saved; struct termios saved;
if (tty_raw(&saved) < 0) { if (tty_raw(&saved) < 0) {
fprintf(stderr, "moerx: not a terminal\n"); fprintf(stderr, "moerx: not a terminal\n");
@ -3557,8 +3565,8 @@ static int run_client(int fd, const char *name) {
int cols, rows; int cols, rows;
tty_size(&cols, &rows); tty_size(&cols, &rows);
uint32_t attach[3] = { (uint32_t)cols, (uint32_t)rows, uint32_t attach[4] = { (uint32_t)cols, (uint32_t)rows,
(uint32_t)terminal_truecolor() }; (uint32_t)terminal_truecolor(), prefix };
if (send_msg(fd, C_ATTACH, attach, sizeof attach) < 0) { if (send_msg(fd, C_ATTACH, attach, sizeof attach) < 0) {
/* errno is the only clue: the server accepted the connection but the /* errno is the only clue: the server accepted the connection but the
attach write failed, usually because it closed the socket while attach write failed, usually because it closed the socket while
@ -3688,14 +3696,17 @@ static int run_client(int fd, const char *name) {
static void print_help(FILE *out) { static void print_help(FILE *out) {
fprintf(out, fprintf(out,
"usage: moerx [session]\n" "usage: moerx [-p C-key] [session]\n"
" moerx -h | --help\n" " moerx -h | --help\n"
"\n" "\n"
"Attach to a running session, or start it if it is not running.\n" "Attach to a running session, or start it if it is not running.\n"
"The session name defaults to \"0\".\n" "The session name defaults to \"0\".\n"
"\n" "\n"
"Every shortcut starts with the Ctrl-A prefix: press Ctrl-A, release it,\n" "Use -p C-x (or --prefix C-x) to choose Ctrl-X as this client's prefix.\n"
"then press the key shown. Ctrl-B is not a prefix and is sent to the pane.\n" "Keys C-a through C-z are supported; the default is C-a.\n"
"The shortcuts below use the default Ctrl-A prefix. Replace it with\n"
"your chosen prefix, including both keys in the last-window shortcut.\n"
"Prefix then a sends the chosen prefix literally to the pane.\n"
"\n" "\n"
"Windows\n" "Windows\n"
" Ctrl-A c new window\n" " Ctrl-A c new window\n"
@ -3748,21 +3759,40 @@ static void print_help(FILE *out) {
"Inside prompts use Enter to accept, Escape to cancel, Ctrl-U to clear.\n"); "Inside prompts use Enter to accept, Escape to cancel, Ctrl-U to clear.\n");
} }
static int parse_prefix(const char *text, unsigned char *prefix) {
if (!text || strlen(text) != 3 ||
(text[0] != 'C' && text[0] != 'c') || text[1] != '-') return 0;
unsigned char letter = (unsigned char)text[2];
if (letter >= 'A' && letter <= 'Z') letter += 'a' - 'A';
if (letter < 'a' || letter > 'z') return 0;
*prefix = letter - 'a' + 1;
return 1;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
int argi = 1; int argi = 1;
if (argc > 1 && strcmp(argv[1], "--") == 0) { unsigned char prefix = DEFAULT_PREFIX;
argi = 2; while (argi < argc && argv[argi][0] == '-' && argv[argi][1]) {
} else if (argc > 1 && (strcmp(argv[1], "-h") == 0 || const char *option = argv[argi++];
strcmp(argv[1], "--help") == 0)) { if (strcmp(option, "--") == 0) break;
if (strcmp(option, "-h") == 0 || strcmp(option, "--help") == 0) {
print_help(stdout); print_help(stdout);
return 0; return 0;
} else if (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') { }
fprintf(stderr, "moerx: unknown option '%s'\n", argv[1]); if (strcmp(option, "-p") == 0 || strcmp(option, "--prefix") == 0) {
if (argi >= argc || !parse_prefix(argv[argi], &prefix)) {
fprintf(stderr, "moerx: %s requires a key from C-a through C-z\n", option);
return 2;
}
argi++;
} else {
fprintf(stderr, "moerx: unknown option '%s'\n", option);
fprintf(stderr, "Try 'moerx --help' for more information.\n"); fprintf(stderr, "Try 'moerx --help' for more information.\n");
return 2; return 2;
} }
}
if (argc - argi > 1) { if (argc - argi > 1) {
fprintf(stderr, "moerx: too many arguments\n"); fprintf(stderr, "moerx: too many arguments\n");
@ -3786,6 +3816,6 @@ int main(int argc, char **argv) {
name, strerror(errno)); name, strerror(errno));
return 1; return 1;
} }
return run_client(fd, name); return run_client(fd, name, prefix);
} }