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:
@@ -37,7 +37,7 @@ enum class CommandId {
|
||||
// Editing
|
||||
InsertText, // arg: text to insert at cursor (UTF-8, no newlines)
|
||||
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)
|
||||
DeleteChar, // delete char at cursor (may join lines)
|
||||
KillToEOL, // delete from cursor to end of line; if at EOL, delete newline
|
||||
|
||||
@@ -139,11 +139,7 @@ map_key(const SDL_Keycode key,
|
||||
case SDLK_KP_ENTER:
|
||||
k_prefix = false;
|
||||
k_ctrl_pending = false;
|
||||
if (mod & KMOD_SHIFT) {
|
||||
out = {true, CommandId::SmartNewline, "", 0};
|
||||
} else {
|
||||
out = {true, CommandId::Newline, "", 0};
|
||||
}
|
||||
out = {true, KEnterCommand((mod & KMOD_SHIFT) != 0), "", 0};
|
||||
return true;
|
||||
case SDLK_ESCAPE:
|
||||
k_prefix = false;
|
||||
|
||||
+8
-1
@@ -235,10 +235,17 @@ KLookupEscCommand(const int ascii_key, CommandId &out) -> bool
|
||||
return true;
|
||||
case '\n':
|
||||
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;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
auto
|
||||
KEnterCommand(const bool raw) -> CommandId
|
||||
{
|
||||
return raw ? CommandId::Newline : CommandId::SmartNewline;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@ bool KLookupCtrlCommand(int ascii_key, CommandId &out);
|
||||
// ascii_key should be the lowercase ASCII of the letter.
|
||||
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.
|
||||
inline int
|
||||
KLowerAscii(const int key)
|
||||
|
||||
@@ -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
|
||||
// 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
|
||||
// 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;
|
||||
if (ch != 27)
|
||||
esc_meta = false;
|
||||
@@ -67,7 +68,7 @@ map_key_to_command(const int ch,
|
||||
// Some terminals send KEY_ENTER distinct from '\n'/'\r'
|
||||
k_prefix = false;
|
||||
k_ctrl_pending = false;
|
||||
out = {true, CommandId::Newline, "", 0};
|
||||
out = {true, KEnterCommand(meta), "", 0};
|
||||
return true;
|
||||
case KEY_MOUSE: {
|
||||
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
|
||||
// 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.
|
||||
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)));
|
||||
@@ -315,7 +316,7 @@ map_key_to_command(const int ch,
|
||||
if (ch == '\n' || ch == '\r') {
|
||||
k_prefix = false;
|
||||
k_ctrl_pending = false;
|
||||
out = {true, CommandId::Newline, "", 0};
|
||||
out = {true, KEnterCommand(false), "", 0};
|
||||
return true;
|
||||
}
|
||||
// Backspace in ncurses can be KEY_BACKSPACE or 127
|
||||
|
||||
@@ -97,3 +97,25 @@ TEST(KKeymap_EscChords_CanonicalChords)
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user