3 Commits

Author SHA1 Message Date
a8dcfbec58 Fix C-k c handling. 2025-12-08 15:28:45 -08:00
65705e3354 bump version 2025-12-07 15:25:50 -08:00
e1f9a9eb6a Preserve cursor position on buffer reload.
- Remember and restore the cursor's position after reloading a buffer, clamping if necessary.
- Improve user experience by maintaining editing context.
2025-12-07 15:25:40 -08:00
4 changed files with 63 additions and 45 deletions

View File

@@ -4,7 +4,7 @@ project(kte)
include(GNUInstallDirs) include(GNUInstallDirs)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(KTE_VERSION "1.5.2") set(KTE_VERSION "1.5.3")
# Default to terminal-only build to avoid SDL/OpenGL dependency by default. # Default to terminal-only build to avoid SDL/OpenGL dependency by default.
# Enable with -DBUILD_GUI=ON when SDL2/OpenGL/Freetype are available. # Enable with -DBUILD_GUI=ON when SDL2/OpenGL/Freetype are available.

View File

@@ -4224,22 +4224,38 @@ cmd_reflow_paragraph(CommandContext &ctx)
static bool static bool
cmd_reload_buffer(CommandContext &ctx) cmd_reload_buffer(CommandContext &ctx)
{ {
Buffer *buf = ctx.editor.CurrentBuffer(); Buffer *buf = ctx.editor.CurrentBuffer();
if (!buf) if (!buf)
return false; return false;
const std::string &filename = buf->Filename(); // Remember the current cursor position so we can attempt to restore it
if (filename.empty()) { const std::size_t old_x = buf->Curx();
ctx.editor.SetStatus("Cannot reload unnamed buffer"); const std::size_t old_y = buf->Cury();
return false; const std::string &filename = buf->Filename();
} if (filename.empty()) {
std::string err; ctx.editor.SetStatus("Cannot reload unnamed buffer");
if (!buf->OpenFromFile(filename, err)) { return false;
ctx.editor.SetStatus(std::string("Reload failed: ") + err); }
return false; std::string err;
} if (!buf->OpenFromFile(filename, err)) {
ctx.editor.SetStatus(std::string("Reloaded ") + filename); ctx.editor.SetStatus(std::string("Reload failed: ") + err);
ensure_cursor_visible(ctx.editor, *buf); return false;
return true; }
// Try to restore the cursor to its previous position if still valid; otherwise clamp
{
auto &rows = buf->Rows();
const std::size_t nrows = rows.size();
if (nrows == 0) {
buf->SetCursor(0, 0);
} else {
const std::size_t new_y = old_y < nrows ? old_y : (nrows - 1);
const std::size_t line_len = rows[new_y].size();
const std::size_t new_x = old_x < line_len ? old_x : line_len;
buf->SetCursor(new_x, new_y);
}
}
ctx.editor.SetStatus(std::string("Reloaded ") + filename);
ensure_cursor_visible(ctx.editor, *buf);
return true;
} }

View File

@@ -158,16 +158,17 @@ map_key(const SDL_Keycode key,
ascii_key = static_cast<int>(key); ascii_key = static_cast<int>(key);
} }
bool ctrl2 = (mod & KMOD_CTRL) != 0; bool ctrl2 = (mod & KMOD_CTRL) != 0;
// If user typed a literal 'C' (or '^') as a control qualifier, keep k-prefix active // If user typed a literal 'C' (uppercase) or '^' as a control qualifier, keep k-prefix active
if (ascii_key == 'C' || ascii_key == 'c' || ascii_key == '^') { // Do NOT treat lowercase 'c' as a qualifier; 'c' is a valid k-command (BufferClose).
k_ctrl_pending = true; if (ascii_key == 'C' || ascii_key == '^') {
// Keep waiting for the next suffix; show status and suppress ensuing TEXTINPUT k_ctrl_pending = true;
if (ed) // Keep waiting for the next suffix; show status and suppress ensuing TEXTINPUT
ed->SetStatus("C-k C _"); if (ed)
suppress_textinput_once = true; ed->SetStatus("C-k C _");
out.hasCommand = false; suppress_textinput_once = true;
return true; out.hasCommand = false;
} return true;
}
// Otherwise, consume the k-prefix now for the actual suffix // Otherwise, consume the k-prefix now for the actual suffix
k_prefix = false; k_prefix = false;
if (ascii_key != 0) { if (ascii_key != 0) {
@@ -472,16 +473,16 @@ ImGuiInputHandler::ProcessSDLEvent(const SDL_Event &e)
ascii_key = static_cast<int>(c0); ascii_key = static_cast<int>(c0);
} }
if (ascii_key != 0) { if (ascii_key != 0) {
// Qualifier via TEXTINPUT: 'C' or '^' // Qualifier via TEXTINPUT: uppercase 'C' or '^' only
if (ascii_key == 'C' || ascii_key == 'c' || ascii_key == '^') { if (ascii_key == 'C' || ascii_key == '^') {
k_ctrl_pending_ = true; k_ctrl_pending_ = true;
if (ed_) if (ed_)
ed_->SetStatus("C-k C _"); ed_->SetStatus("C-k C _");
// Keep k-prefix active; do not emit a command // Keep k-prefix active; do not emit a command
k_prefix_ = true; k_prefix_ = true;
produced = true; produced = true;
break; break;
} }
// Map via k-prefix table; do not pass Ctrl for TEXTINPUT case // Map via k-prefix table; do not pass Ctrl for TEXTINPUT case
CommandId id; CommandId id;
bool pass_ctrl = k_ctrl_pending_; bool pass_ctrl = k_ctrl_pending_;

View File

@@ -178,14 +178,15 @@ map_key_to_command(const int ch,
ctrl = true; ctrl = true;
ascii_key = 'a' + (ch - 1); ascii_key = 'a' + (ch - 1);
} }
// If user typed literal 'C'/'c' or '^' as a qualifier, keep k-prefix and set pending // If user typed literal 'C' or '^' as a qualifier, keep k-prefix and set pending
if (ascii_key == 'C' || ascii_key == 'c' || ascii_key == '^') { // Note: Do NOT treat lowercase 'c' as a qualifier, since 'c' is a valid C-k command (BufferClose).
k_ctrl_pending = true; if (ascii_key == 'C' || ascii_key == '^') {
if (ed) k_ctrl_pending = true;
ed->SetStatus("C-k C _"); if (ed)
out.hasCommand = false; ed->SetStatus("C-k C _");
return true; out.hasCommand = false;
} return true;
}
// For actual suffix, consume the k-prefix // For actual suffix, consume the k-prefix
k_prefix = false; k_prefix = false;
// Do NOT lowercase here; KLookupKCommand handles case-sensitive bindings // Do NOT lowercase here; KLookupKCommand handles case-sensitive bindings