From a99b6c8df81e60e725d6c8aa46e2fe31ea013165 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sat, 13 Sep 2025 01:43:31 +0100 Subject: [PATCH] implement smoother horizontal scrolling. If the new column is already visible without changing the previous scroll column, then don't change. If the new scroll column is prior to the old, then scroll backwards; else, scroll forwards. --- fcore/normal-mode/normal-finish.sml | 13 ++++++++----- fcore/normal-mode/normal-move.sml | 10 ++++++---- fcore/text-scroll.sml | 29 ++++++++++++++++------------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/fcore/normal-mode/normal-finish.sml b/fcore/normal-mode/normal-finish.sml index 3cd8677..7b77dda 100644 --- a/fcore/normal-mode/normal-finish.sml +++ b/fcore/normal-mode/normal-finish.sml @@ -12,12 +12,13 @@ struct fun buildTextAndClear (app: app_type, buffer, cursorIdx, searchList, msgs, bufferModifyTime) = let - val {windowWidth, windowHeight, startLine, searchString, ...} = app + val {windowWidth, windowHeight, startLine, searchString, + visualScrollColumn = prevScrollColumn, ...} = app (* calculate scroll column *) val buffer = LineGap.goToIdx (cursorIdx, buffer) val visualScrollColumn = - TextScroll.getScrollColumn (buffer, cursorIdx, windowWidth) + TextScroll.getScrollColumn (buffer, cursorIdx, windowWidth, prevScrollColumn) (* move LineGap to first line displayed on screen *) val buffer = LineGap.goToLine (startLine, buffer) @@ -86,12 +87,13 @@ struct , searchList , searchString , bufferModifyTime + , visualScrollColumn = prevScrollColumn , ... } = app val newBuffer = LineGap.goToIdx (cursorIdx, buffer) val visualScrollColumn = - TextScroll.getScrollColumn (buffer, cursorIdx, newWidth) + TextScroll.getScrollColumn (buffer, cursorIdx, newWidth, prevScrollColumn) val newBuffer = LineGap.goToLine (startLine, newBuffer) val lineIdx = TextBuilderUtils.getLineAbsIdxFromBuffer (startLine, buffer) @@ -130,11 +132,12 @@ struct (app: app_type, buffer, cursorIdx, searchList, initialMsg, bufferModifyTime) : AppType.app_type = let - val {windowWidth, windowHeight, startLine, searchString, ...} = app + val {windowWidth, windowHeight, startLine, searchString, visualScrollColumn + = prevScrollColumn, ...} = app val buffer = LineGap.goToIdx (cursorIdx, buffer) val visualScrollColumn = - TextScroll.getScrollColumn (buffer, cursorIdx, windowWidth) + TextScroll.getScrollColumn (buffer, cursorIdx, windowWidth, prevScrollColumn) (* move LineGap to first line displayed on screen *) val buffer = LineGap.goToLine (startLine, buffer) diff --git a/fcore/normal-mode/normal-move.sml b/fcore/normal-mode/normal-move.sml index f725a0f..147069b 100644 --- a/fcore/normal-mode/normal-move.sml +++ b/fcore/normal-mode/normal-move.sml @@ -57,6 +57,7 @@ struct , searchList , searchString , bufferModifyTime + , visualScrollColumn = prevScrollColumn , ... } = app @@ -69,7 +70,7 @@ struct val buffer = LineGap.goToIdx (bufferIdx, buffer) val visualScrollColumn = - TextScroll.getScrollColumn (buffer, bufferIdx, windowWidth) + TextScroll.getScrollColumn (buffer, bufferIdx, windowWidth, prevScrollColumn) val bufferLine = let val maxHeight = windowHeight - TextConstants.ySpace @@ -124,6 +125,7 @@ struct , searchList , searchString , bufferModifyTime + , visualScrollColumn = prevScrollColumn , ... } = app val buffer = LineGap.goToLine (reqLine, buffer) @@ -133,7 +135,7 @@ struct val buffer = LineGap.goToIdx (cursorIdx, buffer) val visualScrollColumn = - TextScroll.getScrollColumn (buffer, cursorIdx, windowWidth) + TextScroll.getScrollColumn (buffer, cursorIdx, windowWidth, prevScrollColumn) val startLine = TextWindow.getStartLineWithCursorCentered (buffer, cursorIdx, origLine, windowWidth, windowHeight div 2) @@ -179,7 +181,7 @@ struct , searchList , searchString , bufferModifyTime - , visualScrollColumn + , visualScrollColumn = prevScrollColumn , ... } = app @@ -193,7 +195,7 @@ struct let val buffer = LineGap.goToIdx (cursorIdx, buffer) val visualScrollColumn = - TextScroll.getScrollColumn (buffer, cursorIdx, windowWidth) + TextScroll.getScrollColumn (buffer, cursorIdx, windowWidth, prevScrollColumn) val startLine = TextWindow.getStartLineWithCursorCentered (buffer, cursorIdx, startLine, windowWidth, windowHeight div 2) diff --git a/fcore/text-scroll.sml b/fcore/text-scroll.sml index 3807921..cc46c7c 100644 --- a/fcore/text-scroll.sml +++ b/fcore/text-scroll.sml @@ -3,23 +3,26 @@ struct structure TC = TextConstants (* Preqreuisite: move buffer to cursorIdx *) - fun getScrollColumn (buffer, cursorIdx, windowWidth) = + fun getScrollColumn (buffer, cursorIdx, windowWidth, prevScrollColumn) = let val startOfLine = Cursor.vi0 (buffer, cursorIdx) - val columnDifference = cursorIdx - startOfLine + val newColumn = cursorIdx - startOfLine + val howManyColumnsCanWeFit = + if windowWidth >= TC.textLineWidth then TC.textLineCount + else windowWidth div TC.xSpace + val howManyColumnsCanWeFit = howManyColumnsCanWeFit - 1 in - if columnDifference = 0 then - 0 + if newColumn < prevScrollColumn then + (* we are moving the cursor backwards + * so make sure that newColumn is on the left side *) + newColumn + else if newColumn > prevScrollColumn + howManyColumnsCanWeFit then + (* we are scrolling forwards *) + newColumn - howManyColumnsCanWeFit else - let - val howManyColumnsCanWeFit = - if windowWidth >= TC.textLineWidth then TC.textLineCount - else windowWidth div TC.xSpace - val howManyColumnsCanWeFit = howManyColumnsCanWeFit - 1 - in - if columnDifference < howManyColumnsCanWeFit then 0 - else columnDifference - howManyColumnsCanWeFit - end + (* we can display the current column without moving the scroll column + * so we do that *) + prevScrollColumn end fun getStartLine (prevLineNumber, cursorLine, windowHeight) =