Add non-linear undo/redo design documentation and improve UndoSystem with backspace batching and GUI integration fixes.

This commit is contained in:
2025-11-30 01:49:07 -08:00
parent 35ffe6d11c
commit b77ebdfd07
17 changed files with 891 additions and 147 deletions

View File

@@ -18,7 +18,17 @@ TerminalInputHandler::~TerminalInputHandler() = default;
static bool
map_key_to_command(const int ch, bool &k_prefix, bool &esc_meta, MappedInput &out)
map_key_to_command(const int ch,
bool &k_prefix,
bool &esc_meta,
// universal-argument state (by ref)
bool &uarg_active,
bool &uarg_collecting,
bool &uarg_negative,
bool &uarg_had_digits,
int &uarg_value,
std::string &uarg_text,
MappedInput &out)
{
// Handle special keys from ncurses
switch (ch) {
@@ -75,8 +85,15 @@ map_key_to_command(const int ch, bool &k_prefix, bool &esc_meta, MappedInput &ou
// ESC as cancel of prefix; many terminals send meta sequences as ESC+...
if (ch == 27) {
// ESC
k_prefix = false;
esc_meta = true; // next key will be considered meta-modified
k_prefix = false;
esc_meta = true; // next key will be considered meta-modified
// Cancel any universal argument collection
uarg_active = false;
uarg_collecting = false;
uarg_negative = false;
uarg_had_digits = false;
uarg_value = 0;
uarg_text.clear();
out.hasCommand = false; // no command yet
return true;
}
@@ -92,7 +109,46 @@ map_key_to_command(const int ch, bool &k_prefix, bool &esc_meta, MappedInput &ou
// cancel
k_prefix = false;
esc_meta = false;
out = {true, CommandId::Refresh, "", 0};
// cancel universal argument as well
uarg_active = false;
uarg_collecting = false;
uarg_negative = false;
uarg_had_digits = false;
uarg_value = 0;
uarg_text.clear();
out = {true, CommandId::Refresh, "", 0};
return true;
}
// Universal argument: C-u
if (ch == CTRL('U')) {
// Start or extend universal argument
if (!uarg_active) {
uarg_active = true;
uarg_collecting = true;
uarg_negative = false;
uarg_had_digits = false;
uarg_value = 4; // default
// Reset collected text and emit status update
uarg_text.clear();
out = {true, CommandId::UArgStatus, uarg_text, 0};
return true;
} else if (uarg_collecting && !uarg_had_digits && !uarg_negative) {
// Bare repeated C-u multiplies by 4
if (uarg_value <= 0)
uarg_value = 4;
else
uarg_value *= 4;
// Keep showing status (no digits yet)
out = {true, CommandId::UArgStatus, uarg_text, 0};
return true;
} else {
// If digits or '-' have been entered, C-u ends the argument (ready for next command)
uarg_collecting = false;
if (!uarg_had_digits && !uarg_negative && uarg_value <= 0)
uarg_value = 4;
}
// No command produced by C-u itself
out.hasCommand = false;
return true;
}
// Tab (note: terminals encode Tab and C-i as the same code 9)
@@ -115,12 +171,13 @@ map_key_to_command(const int ch, bool &k_prefix, bool &esc_meta, MappedInput &ou
ctrl = true;
ascii_key = 'a' + (ch - 1);
}
ascii_key = KLowerAscii(ascii_key);
// Do NOT lowercase here; KLookupKCommand handles case-sensitive bindings
CommandId id;
if (KLookupKCommand(ascii_key, ctrl, id)) {
out = {true, id, "", 0};
} else {
char c = (ascii_key >= 0x20 && ascii_key <= 0x7e) ? static_cast<char>(ascii_key) : '?';
int shown = KLowerAscii(ascii_key);
char c = (shown >= 0x20 && shown <= 0x7e) ? static_cast<char>(shown) : '?';
std::string arg(1, c);
out = {true, CommandId::UnknownKCommand, arg, 0};
}
@@ -167,6 +224,36 @@ map_key_to_command(const int ch, bool &k_prefix, bool &esc_meta, MappedInput &ou
// k_prefix handled earlier
// If collecting universal arg, handle digits and optional leading '-'
if (uarg_active && uarg_collecting) {
if (ch >= '0' && ch <= '9') {
int d = ch - '0';
if (!uarg_had_digits) {
// First digit overrides any 4^n default
uarg_value = 0;
uarg_had_digits = true;
}
if (uarg_value < 100000000) {
// avoid overflow
uarg_value = uarg_value * 10 + d;
}
// Update raw text and status to reflect collected digits
uarg_text.push_back(static_cast<char>(ch));
out = {true, CommandId::UArgStatus, uarg_text, 0};
return true;
}
if (ch == '-' && !uarg_had_digits && !uarg_negative) {
uarg_negative = true;
// Show leading minus in status
uarg_text = "-";
out = {true, CommandId::UArgStatus, uarg_text, 0};
return true;
}
// Any other key will be processed as a command; fall through to mapping below
// but mark collection finished so we apply the argument to that command
uarg_collecting = false;
}
// Printable ASCII
if (ch >= 0x20 && ch <= 0x7E) {
out.hasCommand = true;
@@ -188,7 +275,33 @@ TerminalInputHandler::decode_(MappedInput &out)
if (ch == ERR) {
return false; // no input
}
return map_key_to_command(ch, k_prefix_, esc_meta_, out);
bool consumed = map_key_to_command(
ch,
k_prefix_, esc_meta_,
uarg_active_, uarg_collecting_, uarg_negative_, uarg_had_digits_, uarg_value_, uarg_text_,
out);
if (!consumed)
return false;
// If a command was produced and a universal argument is active, attach it and clear state
if (out.hasCommand && uarg_active_ && out.id != CommandId::UArgStatus) {
int count = 0;
if (!uarg_had_digits_ && !uarg_negative_) {
// No explicit digits: use current value (default 4 or 4^n)
count = (uarg_value_ > 0) ? uarg_value_ : 4;
} else {
count = uarg_value_;
if (uarg_negative_)
count = -count;
}
out.count = count;
// Clear state
uarg_active_ = false;
uarg_collecting_ = false;
uarg_negative_ = false;
uarg_had_digits_ = false;
uarg_value_ = 0;
}
return true;
}