From 5d20b81bcccdccc2526015c8733daf9224884c67 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Tue, 23 Sep 2025 11:47:25 +0100 Subject: [PATCH] adjust functor to move by dfa so that we cannot go to the last char of the file if (last char is a newline and is preceded by a non-newline), but otherwise we can go to the last char of the file. --- fcore/cursor.sml | 5 +++++ fcore/move.sml | 24 +++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/fcore/cursor.sml b/fcore/cursor.sml index 48f4358..9e0dc1f 100644 --- a/fcore/cursor.sml +++ b/fcore/cursor.sml @@ -737,4 +737,9 @@ struct end | [] => true end + + (* Prerequisite: lineGap is moved to cursorIdx *) + fun isOnNewlineAfterChr (buffer, cursorIdx) = + cursorIdx > 0 andalso not (isPrevChrStartOfLine (buffer, cursorIdx)) + andalso isCursorAtStartOfLine (buffer, cursorIdx) end diff --git a/fcore/move.sml b/fcore/move.sml index 98407b7..6c1c427 100644 --- a/fcore/move.sml +++ b/fcore/move.sml @@ -63,12 +63,26 @@ struct val cursorIdx = Fn.fMove (buffer, cursorIdx, count) val textLength = #textLength buffer - val cursorIdx = - if cursorIdx >= textLength - 1 then Int.max (textLength - 1, 0) - else cursorIdx in - NormalFinish.buildTextAndClear - (app, buffer, cursorIdx, searchList, [], bufferModifyTime) + if cursorIdx >= textLength - 1 then + let + val cursorIdx = Int.max (textLength - 1, 0) + val buffer = LineGap.goToIdx (cursorIdx, buffer) + in + if Cursor.isOnNewlineAfterChr (buffer, cursorIdx) then + let + val cursorIdx = cursorIdx - 1 + in + NormalFinish.buildTextAndClear + (app, buffer, cursorIdx, searchList, [], bufferModifyTime) + end + else + NormalFinish.buildTextAndClear + (app, buffer, cursorIdx, searchList, [], bufferModifyTime) + end + else + NormalFinish.buildTextAndClear + (app, buffer, cursorIdx, searchList, [], bufferModifyTime) end end