Enter copies indent from the current line

Shift+Enter (GUI) and ESC-Enter (terminal) insert a raw newline.
Paste and prompts keep using Newline.
This commit is contained in:
2026-11-01 19:44:49 -08:00
parent a629a7fb0e
commit e701b416da
6 changed files with 42 additions and 12 deletions
+1 -1
View File
@@ -37,7 +37,7 @@ enum class CommandId {
// Editing // Editing
InsertText, // arg: text to insert at cursor (UTF-8, no newlines) InsertText, // arg: text to insert at cursor (UTF-8, no newlines)
Newline, // insert a newline at cursor Newline, // insert a newline at cursor
SmartNewline, // insert a newline with auto-indent (Shift-Enter) SmartNewline, // insert a newline with auto-indent (Enter)
Backspace, // delete char before cursor (may join lines) Backspace, // delete char before cursor (may join lines)
DeleteChar, // delete char at cursor (may join lines) DeleteChar, // delete char at cursor (may join lines)
KillToEOL, // delete from cursor to end of line; if at EOL, delete newline KillToEOL, // delete from cursor to end of line; if at EOL, delete newline
+1 -5
View File
@@ -139,11 +139,7 @@ map_key(const SDL_Keycode key,
case SDLK_KP_ENTER: case SDLK_KP_ENTER:
k_prefix = false; k_prefix = false;
k_ctrl_pending = false; k_ctrl_pending = false;
if (mod & KMOD_SHIFT) { out = {true, KEnterCommand((mod & KMOD_SHIFT) != 0), "", 0};
out = {true, CommandId::SmartNewline, "", 0};
} else {
out = {true, CommandId::Newline, "", 0};
}
return true; return true;
case SDLK_ESCAPE: case SDLK_ESCAPE:
k_prefix = false; k_prefix = false;
+9 -2
View File
@@ -235,10 +235,17 @@ KLookupEscCommand(const int ascii_key, CommandId &out) -> bool
return true; return true;
case '\n': case '\n':
case '\r': case '\r':
out = CommandId::SmartNewline; // Shift+Enter (some terminals send this as Alt+Enter sequences) out = KEnterCommand(true); // ESC-Enter: raw newline (terminals rarely send Shift+Enter)
return true; return true;
default: default:
break; break;
} }
return false; return false;
} }
auto
KEnterCommand(const bool raw) -> CommandId
{
return raw ? CommandId::Newline : CommandId::SmartNewline;
}
+4
View File
@@ -20,6 +20,10 @@ bool KLookupCtrlCommand(int ascii_key, CommandId &out);
// ascii_key should be the lowercase ASCII of the letter. // ascii_key should be the lowercase ASCII of the letter.
bool KLookupEscCommand(int ascii_key, CommandId &out); bool KLookupEscCommand(int ascii_key, CommandId &out);
// Enter: copy indent from the current line. Shift+Enter (GUI) and ESC-Enter
// (terminal) insert a raw newline at column 0.
CommandId KEnterCommand(bool raw);
// Utility: normalize an int keycode to lowercased ASCII if it's in printable range. // Utility: normalize an int keycode to lowercased ASCII if it's in printable range.
inline int inline int
KLowerAscii(const int key) KLowerAscii(const int key)
+5 -4
View File
@@ -57,7 +57,8 @@ map_key_to_command(const int ch,
// ESC marks the next key as Meta. Consume that flag on every other key up // ESC marks the next key as Meta. Consume that flag on every other key up
// front: keys handled before the Meta lookup below (arrows, mouse, Enter, // front: keys handled before the Meta lookup below (arrows, mouse, Enter,
// control chords) used to leave it set, turning a later plain key into a // control chords) used to leave it set, turning a later plain key into a
// Meta command (ESC, Left, d => delete word). // Meta command (ESC, Left, d => delete word). KEY_ENTER still sees `meta`
// so ESC-Enter can insert a raw newline.
const bool meta = esc_meta && ch != 27; const bool meta = esc_meta && ch != 27;
if (ch != 27) if (ch != 27)
esc_meta = false; esc_meta = false;
@@ -67,7 +68,7 @@ map_key_to_command(const int ch,
// Some terminals send KEY_ENTER distinct from '\n'/'\r' // Some terminals send KEY_ENTER distinct from '\n'/'\r'
k_prefix = false; k_prefix = false;
k_ctrl_pending = false; k_ctrl_pending = false;
out = {true, CommandId::Newline, "", 0}; out = {true, KEnterCommand(meta), "", 0};
return true; return true;
case KEY_MOUSE: { case KEY_MOUSE: {
k_prefix = false; k_prefix = false;
@@ -195,7 +196,7 @@ map_key_to_command(const int ch,
// Meta (ESC-prefixed) keys: backspace, Enter and printable keys use the // Meta (ESC-prefixed) keys: backspace, Enter and printable keys use the
// ESC keymap. Checked before control chords so ESC ^H (meta-backspace) is // ESC keymap. Checked before control chords so ESC ^H (meta-backspace) is
// not taken as C-h, and before Enter so ESC Enter reaches SmartNewline. // not taken as C-h, and before Enter so ESC Enter reaches a raw newline.
// Other control chords after ESC act as themselves. // Other control chords after ESC act as themselves.
const bool is_backspace = (is_keycode && ch == KEY_BACKSPACE) || ch == 127 || ch == CTRL('H'); const bool is_backspace = (is_keycode && ch == KEY_BACKSPACE) || ch == 127 || ch == CTRL('H');
const bool meta_key = meta && (is_backspace || (!is_keycode && (ch == '\n' || ch == '\r' || ch >= 0x20))); const bool meta_key = meta && (is_backspace || (!is_keycode && (ch == '\n' || ch == '\r' || ch >= 0x20)));
@@ -315,7 +316,7 @@ map_key_to_command(const int ch,
if (ch == '\n' || ch == '\r') { if (ch == '\n' || ch == '\r') {
k_prefix = false; k_prefix = false;
k_ctrl_pending = false; k_ctrl_pending = false;
out = {true, CommandId::Newline, "", 0}; out = {true, KEnterCommand(false), "", 0};
return true; return true;
} }
// Backspace in ncurses can be KEY_BACKSPACE or 127 // Backspace in ncurses can be KEY_BACKSPACE or 127
+22
View File
@@ -97,3 +97,25 @@ TEST(KKeymap_EscChords_CanonicalChords)
ASSERT_EQ(KLookupEscCommand('z', id), false); ASSERT_EQ(KLookupEscCommand('z', id), false);
} }
TEST(KKeymap_Enter_IsSmartNewline)
{
ASSERT_EQ(KEnterCommand(false), CommandId::SmartNewline);
}
TEST(KKeymap_ShiftEnter_IsNewline)
{
ASSERT_EQ(KEnterCommand(true), CommandId::Newline);
}
TEST(KKeymap_EscEnter_IsNewline)
{
CommandId id{};
ASSERT_TRUE(KLookupEscCommand('\n', id));
ASSERT_EQ(id, CommandId::Newline);
ASSERT_TRUE(KLookupEscCommand('\r', id));
ASSERT_EQ(id, CommandId::Newline);
}