From 298cec88b3b0ff0ce825dc5540fe2a7c364c71ae Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Tue, 2 Sep 2025 02:55:19 +0100 Subject: [PATCH] create higher order functions to remove boilerplate code when implementing different yank cases --- fcore/normal-mode/normal-mode.sml | 46 ++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/fcore/normal-mode/normal-mode.sml b/fcore/normal-mode/normal-mode.sml index 90f1a48..40a7d35 100644 --- a/fcore/normal-mode/normal-mode.sml +++ b/fcore/normal-mode/normal-mode.sml @@ -200,6 +200,46 @@ struct | #"a" => parseDeleteAround (app, chrCmd, time) | _ => NormalFinish.clearMode app + + fun yankWhenMovingBack (app: app_type, fMove, count) = + let + open DrawMsg + open MailboxType + + val {buffer, cursorIdx, ...} = app + + val buffer = LineGap.goToIdx (cursorIdx, buffer) + val low = fMove (buffer, cursorIdx, count) + + val length = cursorIdx - low + val str = LineGap.substring (low, length, buffer) + + val msg = YANK str + val mode = NORMAL_MODE "" + in + NormalModeWith.mode (app, mode, [DRAW msg]) + end + + fun yankWhenMovingForward (app: app_type, fMove, count) = + let + open DrawMsg + open MailboxType + + val {buffer, cursorIdx, ...} = app + + val buffer = LineGap.goToIdx (cursorIdx, buffer) + val high = fMove (buffer, cursorIdx, count) + + val buffer = LineGap.goToIdx (high, buffer) + val length = high - cursorIdx + val str = LineGap.substring (cursorIdx, length, buffer) + + val msg = YANK str + val mode = NORMAL_MODE "" + in + NormalModeWith.mode (app, mode, [DRAW msg]) + end + fun parseYankTerminal (str, count, app, chrCmd, time) = case chrCmd of #"y" => @@ -218,12 +258,16 @@ struct val buffer = LineGap.goToIdx (high, buffer) val length = high - low val str = LineGap.substring (low, length, buffer) - val msg = YANK str + val msg = YANK str val mode = NORMAL_MODE "" in NormalModeWith.mode (app, mode, [DRAW msg]) end + | #"0" => + let val f = fn (buffer, cursorIdx, _) => Cursor.vi0 (buffer, cursorIdx) + in yankWhenMovingBack (app, f, 1) + end | _ => app fun parseYank (strPos, str, count, app, chrCmd, time) =