From 9174e4923c2a22a35b108bab634c509ff97deeef Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Tue, 23 Sep 2025 07:04:20 +0100 Subject: [PATCH] don't deleteByDfa when buffer's length is 1, because that would mean that we would be deleting last newline in file --- fcore/normal-mode/make-normal-delete.sml | 40 +++++++++++++----------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/fcore/normal-mode/make-normal-delete.sml b/fcore/normal-mode/make-normal-delete.sml index 3e93e64..0f2e33c 100644 --- a/fcore/normal-mode/make-normal-delete.sml +++ b/fcore/normal-mode/make-normal-delete.sml @@ -227,26 +227,30 @@ struct finishAfterDeletingBuffer (app, low, buffer, time, initialMsg) end - fun deleteByDfa (app: app_type, count, fMove, time) = - let - val {buffer, cursorIdx, ...} = app + fun deleteByDfa (app as {buffer, ...}: app_type, count, fMove, time) = + if #textLength buffer = 1 then + NormalFinish.clearMode app + else + let + val {buffer, cursorIdx, ...} = app - val buffer = LineGap.goToIdx (cursorIdx, buffer) - val otherIdx = fMove (buffer, cursorIdx, count) - in - if otherIdx > cursorIdx then - (* prevent us from deleting last newline - * to help us preserve unix-style line endings *) - if otherIdx > #textLength buffer then - finishDeleteByDfa - (app, cursorIdx, #textLength buffer - 1, buffer, time) + val buffer = LineGap.goToIdx (cursorIdx, buffer) + val otherIdx = fMove (buffer, cursorIdx, count) + in + if otherIdx > cursorIdx then + if otherIdx >= #textLength buffer then + (* prevent us from deleting last newline + * to help us preserve unix-style line endings *) + let val high = #textLength buffer - 1 + in finishDeleteByDfa (app, cursorIdx, high, buffer, time) + end + else + finishDeleteByDfa (app, cursorIdx, otherIdx, buffer, time) + else if otherIdx < cursorIdx then + finishDeleteByDfa (app, otherIdx, cursorIdx, buffer, time) else - finishDeleteByDfa (app, cursorIdx, otherIdx, buffer, time) - else if otherIdx < cursorIdx then - finishDeleteByDfa (app, otherIdx, cursorIdx, buffer, time) - else - NormalFinish.clearMode app - end + NormalFinish.clearMode app + end fun deleteCharsLeft (app: app_type, count, time) = let