From 090e7ccea2ebed86983974dec93c9aa5077c7483 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Fri, 19 Sep 2025 23:17:28 +0100 Subject: [PATCH] pass 'maxLength' value to TextScroll function so that we can prevent scrolling past the bottom of the file (unless we want to od so by centtering the screen) --- fcore/normal-mode/normal-finish.sml | 3 ++- fcore/normal-mode/normal-move.sml | 22 +++++++++++++++++----- fcore/normal-mode/normal-search-finish.sml | 6 ++++-- fcore/text-scroll.sml | 7 +++++-- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/fcore/normal-mode/normal-finish.sml b/fcore/normal-mode/normal-finish.sml index c5b0fe6..10859f8 100644 --- a/fcore/normal-mode/normal-finish.sml +++ b/fcore/normal-mode/normal-finish.sml @@ -29,7 +29,8 @@ struct val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer) val startLine = - TextScroll.getStartLine (prevLineNumber, cursorLine, windowHeight) + TextScroll.getStartLine + (prevLineNumber, cursorLine, windowHeight, #lineLength buffer) (* move buffer to new startLine as required by TextBuilder.build *) val buffer = LineGap.goToLine (startLine, buffer) diff --git a/fcore/normal-mode/normal-move.sml b/fcore/normal-mode/normal-move.sml index f422d06..f0e0e44 100644 --- a/fcore/normal-mode/normal-move.sml +++ b/fcore/normal-mode/normal-move.sml @@ -74,7 +74,8 @@ struct (buffer, bufferIdx, windowWidth, prevScrollColumn) val bufferLine = - TextScroll.getStartLine (prevLineNumber, bufferLine, windowHeight) + TextScroll.getStartLine + (prevLineNumber, bufferLine, windowHeight, #lineLength buffer) val buffer = LineGap.goToLine (bufferLine, buffer) val drawMsg = NormalModeTextBuilder.build @@ -129,6 +130,11 @@ struct val buffer = LineGap.goToIdx (lineIdx, buffer) val endOfLineIdx = Cursor.viDlr (buffer, lineIdx, 1) + val endOfLineIdx = + if endOfLineIdx >= #textLength buffer - 2 then + Int.max (0, #textLength buffer - 2) + else + endOfLineIdx val cursorIdx = Int.min (endOfLineIdx, lineIdx + column) @@ -141,7 +147,11 @@ struct val startLine = TextScroll.getStartLine - (prevLineNumber, newCursorLineNumber, windowHeight) + ( prevLineNumber + , newCursorLineNumber + , windowHeight + , #lineLength buffer + ) val buffer = LineGap.goToLine (startLine, buffer) @@ -223,7 +233,7 @@ struct val newCursorLineNumber = cursorLineNumber + count val newCursorLineNumber = if cursorLineNumber >= #lineLength buffer - 2 then - Int.max (0, #lineLength buffer - 2) + Int.max (0, #lineLength buffer - 1) else newCursorLineNumber in @@ -270,7 +280,8 @@ struct val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer) val startLine = - TextScroll.getStartLine (prevLineNumber, cursorLine, windowHeight) + TextScroll.getStartLine + (prevLineNumber, cursorLine, windowHeight, #lineLength buffer) val buffer = LineGap.goToLine (startLine, buffer) @@ -331,7 +342,8 @@ struct val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer) val startLine = - TextScroll.getStartLine (prevLineNumber, cursorLine, windowHeight) + TextScroll.getStartLine + (prevLineNumber, cursorLine, windowHeight, #lineLength buffer) val buffer = LineGap.goToLine (startLine, buffer) diff --git a/fcore/normal-mode/normal-search-finish.sml b/fcore/normal-mode/normal-search-finish.sml index 96ab5e0..0624863 100644 --- a/fcore/normal-mode/normal-search-finish.sml +++ b/fcore/normal-mode/normal-search-finish.sml @@ -52,7 +52,8 @@ struct val buffer = LineGap.goToIdx (cursorIdx, buffer) val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer) val startLine = - TextScroll.getStartLine (prevLineNumber, cursorLine, windowHeight) + TextScroll.getStartLine + (prevLineNumber, cursorLine, windowHeight, #lineLength buffer) val buffer = LineGap.goToLine (startLine, buffer) val remainingWindowHeight = windowHeight - (TextConstants.ySpace * 2) @@ -122,7 +123,8 @@ struct val buffer = LineGap.goToIdx (cursorIdx, buffer) val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer) val startLine = - TextScroll.getStartLine (prevLineNumber, cursorLine, newWindowHeight) + TextScroll.getStartLine + (prevLineNumber, cursorLine, newWindowHeight, #lineLength buffer) val buffer = LineGap.goToLine (startLine, buffer) val remainingWindowHeight = newWindowHeight - (TextConstants.ySpace * 2) diff --git a/fcore/text-scroll.sml b/fcore/text-scroll.sml index 7e7a324..11f2978 100644 --- a/fcore/text-scroll.sml +++ b/fcore/text-scroll.sml @@ -37,7 +37,7 @@ struct fun getScrollColumnFromString (cursorIdx, windowWidth, prevScrollColumn) = calculateScrollColumn (0, cursorIdx, windowWidth, prevScrollColumn) - fun getStartLine (prevLineNumber, cursorLine, windowHeight) = + fun getStartLine (prevLineNumber, cursorLine, windowHeight, totalLines) = if cursorLine <= (prevLineNumber + 3) then (* cursorLine is prior to or same as prevLineNumber, * so use cursorLine to calculate the start line we want. *) @@ -50,7 +50,10 @@ struct if cursorLine > prevLineNumber + (howManyLinesWeCanFit - 3) then (* cursorLine is after the visible part of the screen * so return the mimimum line where cursorLine is visible *) - cursorLine - howManyLinesWeCanFit + 3 + if cursorLine >= totalLines - 3 then + Int.max (totalLines - howManyLinesWeCanFit, 0) + else + cursorLine - howManyLinesWeCanFit + 3 else prevLineNumber end