2025-09-11 16:17:56 +01:00
|
|
|
structure TextScroll =
|
|
|
|
|
struct
|
|
|
|
|
structure TC = TextConstants
|
|
|
|
|
|
|
|
|
|
(* Preqreuisite: move buffer to cursorIdx *)
|
2025-09-13 01:43:31 +01:00
|
|
|
fun getScrollColumn (buffer, cursorIdx, windowWidth, prevScrollColumn) =
|
2025-09-11 16:17:56 +01:00
|
|
|
let
|
|
|
|
|
val startOfLine = Cursor.vi0 (buffer, cursorIdx)
|
2025-09-13 01:43:31 +01:00
|
|
|
val newColumn = cursorIdx - startOfLine
|
|
|
|
|
val howManyColumnsCanWeFit =
|
|
|
|
|
if windowWidth >= TC.textLineWidth then TC.textLineCount
|
|
|
|
|
else windowWidth div TC.xSpace
|
|
|
|
|
val howManyColumnsCanWeFit = howManyColumnsCanWeFit - 1
|
2025-09-11 16:17:56 +01:00
|
|
|
in
|
2025-09-13 01:43:31 +01:00
|
|
|
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
|
2025-09-11 16:17:56 +01:00
|
|
|
else
|
2025-09-13 01:43:31 +01:00
|
|
|
(* we can display the current column without moving the scroll column
|
|
|
|
|
* so we do that *)
|
|
|
|
|
prevScrollColumn
|
2025-09-11 16:17:56 +01:00
|
|
|
end
|
2025-09-11 17:30:49 +01:00
|
|
|
|
|
|
|
|
fun getStartLine (prevLineNumber, cursorLine, windowHeight) =
|
|
|
|
|
if cursorLine <= prevLineNumber then
|
|
|
|
|
(* if cursorLine is prior or same as prevLineNumber,
|
|
|
|
|
* then use cursorLine as scroll-line-start *)
|
|
|
|
|
cursorLine
|
|
|
|
|
else
|
|
|
|
|
(* cursorLine > prevLineNumber *)
|
|
|
|
|
let
|
|
|
|
|
val howManyLinesWeCanFit = windowHeight div TC.ySpace
|
2025-09-13 05:12:17 +01:00
|
|
|
val howManyLinesWeCanFit = howManyLinesWeCanFit - 2
|
2025-09-11 17:30:49 +01:00
|
|
|
in
|
2025-09-13 05:12:17 +01:00
|
|
|
if cursorLine > prevLineNumber + howManyLinesWeCanFit then
|
|
|
|
|
cursorLine - howManyLinesWeCanFit
|
2025-09-11 17:30:49 +01:00
|
|
|
else
|
2025-09-13 05:12:17 +01:00
|
|
|
prevLineNumber
|
2025-09-11 17:30:49 +01:00
|
|
|
end
|
2025-09-11 16:17:56 +01:00
|
|
|
end
|