szn

Bug 517 — list-keys output is not round-trippable

517. list-keys prints enum tags; mapCommandToAction only accepts command names

Severity: MEDIUM Status: ✅ FIXED Files: src/cmd/cmd.zig:726, src/cmd/cmd.zig:735, src/key_binding.zig:474-526

Problem

list-keys renders the action’s Zig enum tag as if it were a command:

// src/cmd/cmd.zig:726
const line = std.fmt.bufPrint(&line_buf, "bind-key -T prefix {s} {s}\n", .{ key_str, @tagName(b.action) }) catch return .err;

Output is therefore of the form:

bind-key -T prefix C-b new_window
bind-key -T prefix % split_horizontal

But the reverse mapping matches hyphenated tmux names only:

// src/key_binding.zig:479
if (std.mem.eql(u8, name, "new-window") or std.mem.eql(u8, name, "neww")) return .new_window;

mapCommandToAction("new_window") returns null, so re-sourcing the output fails with bind-key: unknown command. tmux guarantees list-keys output is valid configuration; this breaks that contract and makes the command useless for its main purpose (inspecting and editing the key table).

The gap is wider than the naming mismatch. mapCommandToAction has no mapping for send-prefix, rotate-window, reflow-pane, resize-pane, or swap-pane, so the default bindings for send_prefix, rotate_window, reflow_pane and the resize_* / swap_pane_* variants cannot be expressed in a config file at all — only through the built-in defaults.

Proposed Fix

Add an actionToCommand(action) []const u8 reverse table next to mapCommandToAction, and have list-keys print that. Extend mapCommandToAction with the five missing commands so the round trip closes in both directions. A test that renders list-keys, feeds each line back through mapCommandToAction, and asserts a non-null result would lock this down.

Fix

A canonical reverse mapping was added next to the forward one:

// src/key_binding.zig
pub fn actionToCommand(action: Action) []const u8 {
    return switch (action) {
        .new_window => "new-window",
        .split_horizontal => "split-window -h",
        ...
        .resize_left => "resize-pane -L",
        .rotate_window => "rotate-window",
        .send_prefix => "send-prefix",
        .reflow_pane => "reflow-pane",
    };
}

It is an exhaustive switch, so adding an Action variant without giving it a command string is a compile error. The ten select_window_* variants index a comptime array of "select-window -t N" strings.

list-keys (src/cmd/cmd.zig) now emits actionToCommand(b.action) for both the prefix and root tables instead of @tagName(b.action).

mapCommandToAction gained the five commands it was missing — rotate-window, send-prefix, reflow-pane, resize-pane -L/-R/-U/-D, and swap-pane -U/-D — so the round trip closes in both directions and those actions are expressible in a config file for the first time.

Interactive bind-key and unbind-key (src/cmd/cmd.zig) were also updated to accept -T prefix and -T root flags (matching list-keys output) and to join multi-token command arguments (e.g. split-window -v, select-pane -L), preventing interactive re-sourcing from dropping arguments or silently flipping split_vertical to split_horizontal.

Regression tests

Verified with the full suite on Zig 0.16.0 and 0.17.0: 1057/1057 passing on both.

Bugs #47, #48 and #326 were earlier defects in mapCommandToAction’s parsing; this is the inverse direction of the same table.