Fix status message when there's nothing to show.

I was using "refresh requested" for debugging, but this is better.
This commit is contained in:
2025-11-29 21:00:03 -08:00
parent f9606626b4
commit 3de5ec68f8
2 changed files with 183 additions and 171 deletions

10
.idea/workspace.xml generated
View File

@@ -36,14 +36,6 @@
<list default="true" id="e1fe3ab0-3650-4fca-8664-a247d5dfa457" name="Changes" comment=""> <list default="true" id="e1fe3ab0-3650-4fca-8664-a247d5dfa457" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Command.cc" beforeDir="false" afterPath="$PROJECT_DIR$/Command.cc" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Command.cc" beforeDir="false" afterPath="$PROJECT_DIR$/Command.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Command.h" beforeDir="false" afterPath="$PROJECT_DIR$/Command.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/GUIFrontend.cc" beforeDir="false" afterPath="$PROJECT_DIR$/GUIFrontend.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/GUIInputHandler.cc" beforeDir="false" afterPath="$PROJECT_DIR$/GUIInputHandler.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/GUIRenderer.cc" beforeDir="false" afterPath="$PROJECT_DIR$/GUIRenderer.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/TerminalFrontend.cc" beforeDir="false" afterPath="$PROJECT_DIR$/TerminalFrontend.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/TerminalInputHandler.cc" beforeDir="false" afterPath="$PROJECT_DIR$/TerminalInputHandler.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/TerminalRenderer.cc" beforeDir="false" afterPath="$PROJECT_DIR$/TerminalRenderer.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.cc" beforeDir="false" afterPath="$PROJECT_DIR$/main.cc" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -148,7 +140,7 @@
<option name="number" value="Default" /> <option name="number" value="Default" />
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1764457173148</updated> <updated>1764457173148</updated>
<workItem from="1764457174208" duration="19741000" /> <workItem from="1764457174208" duration="20181000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@@ -85,29 +85,32 @@ ensure_at_least_one_line(Buffer &buf)
static std::size_t static std::size_t
inverse_render_to_source_col(const std::string &line, std::size_t rx_target, std::size_t tabw) inverse_render_to_source_col(const std::string &line, std::size_t rx_target, std::size_t tabw)
{ {
// Find source column that best matches given rendered x (with tabs expanded) // Find source column that best matches given rendered x (with tabs expanded)
std::size_t rx = 0; std::size_t rx = 0;
if (rx_target == 0) return 0; if (rx_target == 0)
std::size_t best_col = 0; return 0;
std::size_t best_dist = rx_target; // max std::size_t best_col = 0;
for (std::size_t i = 0; i <= line.size(); ++i) { std::size_t best_dist = rx_target; // max
std::size_t dist = (rx > rx_target) ? (rx - rx_target) : (rx_target - rx); for (std::size_t i = 0; i <= line.size(); ++i) {
if (dist <= best_dist) { std::size_t dist = (rx > rx_target) ? (rx - rx_target) : (rx_target - rx);
best_dist = dist; if (dist <= best_dist) {
best_col = i; best_dist = dist;
} best_col = i;
if (i == line.size()) break; }
if (line[i] == '\t') { if (i == line.size())
rx += tabw - (rx % tabw); break;
} else { if (line[i] == '\t') {
rx += 1; rx += tabw - (rx % tabw);
} } else {
if (rx >= rx_target && i + 1 <= line.size()) { rx += 1;
// next iteration will evaluate i+1 with updated rx }
} if (rx >= rx_target && i + 1 <= line.size()) {
} // next iteration will evaluate i+1 with updated rx
if (best_col > line.size()) best_col = line.size(); }
return best_col; }
if (best_col > line.size())
best_col = line.size();
return best_col;
} }
@@ -115,61 +118,72 @@ inverse_render_to_source_col(const std::string &line, std::size_t rx_target, std
static bool static bool
cmd_move_cursor_to(CommandContext &ctx) cmd_move_cursor_to(CommandContext &ctx)
{ {
Buffer *buf = ctx.editor.CurrentBuffer(); Buffer *buf = ctx.editor.CurrentBuffer();
if (!buf) if (!buf)
return false; return false;
ensure_at_least_one_line(*buf); ensure_at_least_one_line(*buf);
// Accept either: // Accept either:
// - "row:col" (buffer coordinates) // - "row:col" (buffer coordinates)
// - "@row:col" (screen coordinates within viewport; translated using offsets) // - "@row:col" (screen coordinates within viewport; translated using offsets)
std::size_t row = buf->Cury(); std::size_t row = buf->Cury();
std::size_t col = buf->Curx(); std::size_t col = buf->Curx();
const std::string &a = ctx.arg; const std::string &a = ctx.arg;
if (!a.empty()) { if (!a.empty()) {
bool screen = false; bool screen = false;
std::size_t start = 0; std::size_t start = 0;
if (!a.empty() && a[0] == '@') { screen = true; start = 1; } if (!a.empty() && a[0] == '@') {
std::size_t p = a.find(':', start); screen = true;
if (p != std::string::npos) { start = 1;
try { }
long ay = std::stol(a.substr(start, p - start)); std::size_t p = a.find(':', start);
long ax = std::stol(a.substr(p + 1)); if (p != std::string::npos) {
if (ay < 0) ay = 0; if (ax < 0) ax = 0; try {
if (screen) { long ay = std::stol(a.substr(start, p - start));
// Translate screen to buffer coordinates using offsets and tab expansion for x long ax = std::stol(a.substr(p + 1));
std::size_t vy = static_cast<std::size_t>(ay); if (ay < 0)
std::size_t vx = static_cast<std::size_t>(ax); ay = 0;
std::size_t bro = buf->Rowoffs(); if (ax < 0)
std::size_t bco = buf->Coloffs(); ax = 0;
std::size_t by = bro + vy; if (screen) {
// Clamp by to existing lines later // Translate screen to buffer coordinates using offsets and tab expansion for x
auto &lines2 = buf->Rows(); std::size_t vy = static_cast<std::size_t>(ay);
if (lines2.empty()) { lines2.emplace_back(""); } std::size_t vx = static_cast<std::size_t>(ax);
if (by >= lines2.size()) by = lines2.size() - 1; std::size_t bro = buf->Rowoffs();
const std::string &line2 = lines2[by]; std::size_t bco = buf->Coloffs();
std::size_t rx_target = bco + vx; std::size_t by = bro + vy;
std::size_t sx = inverse_render_to_source_col(line2, rx_target, 8); // Clamp by to existing lines later
row = by; auto &lines2 = buf->Rows();
col = sx; if (lines2.empty()) {
} else { lines2.emplace_back("");
row = static_cast<std::size_t>(ay); }
col = static_cast<std::size_t>(ax); if (by >= lines2.size())
} by = lines2.size() - 1;
} catch (...) { const std::string &line2 = lines2[by];
// ignore parse errors std::size_t rx_target = bco + vx;
} std::size_t sx = inverse_render_to_source_col(line2, rx_target, 8);
} row = by;
} col = sx;
auto &lines = buf->Rows(); } else {
if (lines.empty()) { row = static_cast<std::size_t>(ay);
lines.emplace_back(""); col = static_cast<std::size_t>(ax);
} }
if (row >= lines.size()) row = lines.size() - 1; } catch (...) {
const std::string &line = lines[row]; // ignore parse errors
if (col > line.size()) col = line.size(); }
buf->SetCursor(col, row); }
ensure_cursor_visible(ctx.editor, *buf); }
return true; auto &lines = buf->Rows();
if (lines.empty()) {
lines.emplace_back("");
}
if (row >= lines.size())
row = lines.size() - 1;
const std::string &line = lines[row];
if (col > line.size())
col = line.size();
buf->SetCursor(col, row);
ensure_cursor_visible(ctx.editor, *buf);
return true;
} }
@@ -357,7 +371,7 @@ cmd_refresh(CommandContext &ctx)
return true; return true;
} }
// Otherwise just a hint; renderer will redraw // Otherwise just a hint; renderer will redraw
ctx.editor.SetStatus("Refresh requested"); ctx.editor.SetStatus("");
return true; return true;
} }
@@ -365,23 +379,23 @@ cmd_refresh(CommandContext &ctx)
static bool static bool
cmd_kprefix(CommandContext &ctx) cmd_kprefix(CommandContext &ctx)
{ {
// Show k-command mode hint in status // Show k-command mode hint in status
ctx.editor.SetStatus("C-k _"); ctx.editor.SetStatus("C-k _");
return true; return true;
} }
static bool static bool
cmd_unknown_kcommand(CommandContext &ctx) cmd_unknown_kcommand(CommandContext &ctx)
{ {
char ch = '?'; char ch = '?';
if (!ctx.arg.empty()) { if (!ctx.arg.empty()) {
ch = ctx.arg[0]; ch = ctx.arg[0];
} }
char buf[64]; char buf[64];
std::snprintf(buf, sizeof(buf), "unknown k-command %c", ch); std::snprintf(buf, sizeof(buf), "unknown k-command %c", ch);
ctx.editor.SetStatus(buf); ctx.editor.SetStatus(buf);
return true; return true;
} }
@@ -901,75 +915,77 @@ cmd_move_end(CommandContext &ctx)
static bool static bool
cmd_page_up(CommandContext &ctx) cmd_page_up(CommandContext &ctx)
{ {
Buffer *buf = ctx.editor.CurrentBuffer(); Buffer *buf = ctx.editor.CurrentBuffer();
if (!buf) if (!buf)
return false; return false;
ensure_at_least_one_line(*buf); ensure_at_least_one_line(*buf);
auto &rows = buf->Rows(); auto &rows = buf->Rows();
int repeat = ctx.count > 0 ? ctx.count : 1; int repeat = ctx.count > 0 ? ctx.count : 1;
std::size_t content_rows = ctx.editor.Rows() > 0 ? ctx.editor.Rows() - 1 : 0; std::size_t content_rows = ctx.editor.Rows() > 0 ? ctx.editor.Rows() - 1 : 0;
if (content_rows == 0) if (content_rows == 0)
content_rows = 1; content_rows = 1;
// Base on current top-of-screen (row offset) // Base on current top-of-screen (row offset)
std::size_t rowoffs = buf->Rowoffs(); std::size_t rowoffs = buf->Rowoffs();
while (repeat-- > 0) { while (repeat-- > 0) {
if (rowoffs >= content_rows) if (rowoffs >= content_rows)
rowoffs -= content_rows; rowoffs -= content_rows;
else else
rowoffs = 0; rowoffs = 0;
} }
// Clamp to valid range // Clamp to valid range
if (rows.size() > content_rows) { if (rows.size() > content_rows) {
std::size_t max_top = rows.size() - content_rows; std::size_t max_top = rows.size() - content_rows;
if (rowoffs > max_top) rowoffs = max_top; if (rowoffs > max_top)
} else { rowoffs = max_top;
rowoffs = 0; } else {
} rowoffs = 0;
// Move cursor to first visible line, column 0 }
std::size_t y = rowoffs; // Move cursor to first visible line, column 0
if (y >= rows.size()) y = rows.empty() ? 0 : rows.size() - 1; std::size_t y = rowoffs;
buf->SetOffsets(rowoffs, 0); if (y >= rows.size())
buf->SetCursor(0, y); y = rows.empty() ? 0 : rows.size() - 1;
ensure_cursor_visible(ctx.editor, *buf); buf->SetOffsets(rowoffs, 0);
return true; buf->SetCursor(0, y);
ensure_cursor_visible(ctx.editor, *buf);
return true;
} }
static bool static bool
cmd_page_down(CommandContext &ctx) cmd_page_down(CommandContext &ctx)
{ {
Buffer *buf = ctx.editor.CurrentBuffer(); Buffer *buf = ctx.editor.CurrentBuffer();
if (!buf) if (!buf)
return false; return false;
ensure_at_least_one_line(*buf); ensure_at_least_one_line(*buf);
auto &rows = buf->Rows(); auto &rows = buf->Rows();
int repeat = ctx.count > 0 ? ctx.count : 1; int repeat = ctx.count > 0 ? ctx.count : 1;
std::size_t content_rows = ctx.editor.Rows() > 0 ? ctx.editor.Rows() - 1 : 0; std::size_t content_rows = ctx.editor.Rows() > 0 ? ctx.editor.Rows() - 1 : 0;
if (content_rows == 0) if (content_rows == 0)
content_rows = 1; content_rows = 1;
std::size_t rowoffs = buf->Rowoffs(); std::size_t rowoffs = buf->Rowoffs();
// Compute maximum top offset // Compute maximum top offset
std::size_t max_top = 0; std::size_t max_top = 0;
if (!rows.empty()) { if (!rows.empty()) {
if (rows.size() > content_rows) if (rows.size() > content_rows)
max_top = rows.size() - content_rows; max_top = rows.size() - content_rows;
else else
max_top = 0; max_top = 0;
} }
while (repeat-- > 0) { while (repeat-- > 0) {
if (rowoffs + content_rows <= max_top) if (rowoffs + content_rows <= max_top)
rowoffs += content_rows; rowoffs += content_rows;
else else
rowoffs = max_top; rowoffs = max_top;
} }
// Move cursor to first visible line, column 0 // Move cursor to first visible line, column 0
std::size_t y = std::min<std::size_t>(rowoffs, rows.empty() ? 0 : rows.size() - 1); std::size_t y = std::min<std::size_t>(rowoffs, rows.empty() ? 0 : rows.size() - 1);
buf->SetOffsets(rowoffs, 0); buf->SetOffsets(rowoffs, 0);
buf->SetCursor(0, y); buf->SetCursor(0, y);
ensure_cursor_visible(ctx.editor, *buf); ensure_cursor_visible(ctx.editor, *buf);
return true; return true;
} }
@@ -1147,13 +1163,15 @@ InstallDefaultCommands()
CommandRegistry::Register({CommandId::Save, "save", "Save current buffer", cmd_save}); CommandRegistry::Register({CommandId::Save, "save", "Save current buffer", cmd_save});
CommandRegistry::Register({CommandId::SaveAs, "save-as", "Save current buffer as...", cmd_save_as}); CommandRegistry::Register({CommandId::SaveAs, "save-as", "Save current buffer as...", cmd_save_as});
CommandRegistry::Register({CommandId::Quit, "quit", "Quit editor (request)", cmd_quit}); CommandRegistry::Register({CommandId::Quit, "quit", "Quit editor (request)", cmd_quit});
CommandRegistry::Register({CommandId::SaveAndQuit, "save-quit", "Save and quit (request)", cmd_save_and_quit}); CommandRegistry::Register({CommandId::SaveAndQuit, "save-quit", "Save and quit (request)", cmd_save_and_quit});
CommandRegistry::Register({CommandId::Refresh, "refresh", "Force redraw", cmd_refresh}); CommandRegistry::Register({CommandId::Refresh, "refresh", "Force redraw", cmd_refresh});
CommandRegistry::Register( CommandRegistry::Register(
{CommandId::KPrefix, "k-prefix", "Entering k-command prefix (show hint)", cmd_kprefix}); {CommandId::KPrefix, "k-prefix", "Entering k-command prefix (show hint)", cmd_kprefix});
CommandRegistry::Register({CommandId::UnknownKCommand, "unknown-k", "Unknown k-command (status)", CommandRegistry::Register({
cmd_unknown_kcommand}); CommandId::UnknownKCommand, "unknown-k", "Unknown k-command (status)",
CommandRegistry::Register({CommandId::FindStart, "find-start", "Begin incremental search", cmd_find_start}); cmd_unknown_kcommand
});
CommandRegistry::Register({CommandId::FindStart, "find-start", "Begin incremental search", cmd_find_start});
CommandRegistry::Register({ CommandRegistry::Register({
CommandId::OpenFileStart, "open-file-start", "Begin open-file prompt", cmd_open_file_start CommandId::OpenFileStart, "open-file-start", "Begin open-file prompt", cmd_open_file_start
}); });
@@ -1172,10 +1190,12 @@ InstallDefaultCommands()
CommandRegistry::Register({CommandId::MoveHome, "home", "Move to beginning of line", cmd_move_home}); CommandRegistry::Register({CommandId::MoveHome, "home", "Move to beginning of line", cmd_move_home});
CommandRegistry::Register({CommandId::MoveEnd, "end", "Move to end of line", cmd_move_end}); CommandRegistry::Register({CommandId::MoveEnd, "end", "Move to end of line", cmd_move_end});
CommandRegistry::Register({CommandId::PageUp, "page-up", "Page up", cmd_page_up}); CommandRegistry::Register({CommandId::PageUp, "page-up", "Page up", cmd_page_up});
CommandRegistry::Register({CommandId::PageDown, "page-down", "Page down", cmd_page_down}); CommandRegistry::Register({CommandId::PageDown, "page-down", "Page down", cmd_page_down});
CommandRegistry::Register({CommandId::WordPrev, "word-prev", "Move to previous word", cmd_word_prev}); CommandRegistry::Register({CommandId::WordPrev, "word-prev", "Move to previous word", cmd_word_prev});
CommandRegistry::Register({CommandId::WordNext, "word-next", "Move to next word", cmd_word_next}); CommandRegistry::Register({CommandId::WordNext, "word-next", "Move to next word", cmd_word_next});
CommandRegistry::Register({CommandId::MoveCursorTo, "move-cursor-to", "Move cursor to y:x", cmd_move_cursor_to}); CommandRegistry::Register({
CommandId::MoveCursorTo, "move-cursor-to", "Move cursor to y:x", cmd_move_cursor_to
});
} }