Files
sml-projects/fcore/normal-mode/normal-finish.sml

230 lines
6.2 KiB
Standard ML
Raw Normal View History

2025-08-31 06:28:05 +01:00
structure NormalFinish =
struct
open AppType
open MailboxType
open DrawMsg
open InputMsg
fun clearMode app =
2025-08-31 06:28:05 +01:00
NormalModeWith.mode (app, NORMAL_MODE "", [])
2025-08-20 12:51:31 +01:00
fun buildTextAndClear
(app: app_type, buffer, cursorIdx, searchList, msgs, bufferModifyTime) =
let
val {windowWidth, windowHeight, startLine, searchString, ...} = app
(* calculate scroll column *)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val visualScrollColumn =
TextScroll.getScrollColumn (buffer, cursorIdx, windowWidth)
(* move LineGap to first line displayed on screen *)
val buffer = LineGap.goToLine (startLine, buffer)
(* get new startLine which may move screen depending on cursor movements *)
val startLine = TextWindow.getStartLine
(buffer, startLine, cursorIdx, windowWidth, windowHeight)
(* move buffer to new startLine as required by TextBuilder.build *)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, searchString
, visualScrollColumn
)
val drawMsg = Vector.concat drawMsg
2025-09-12 13:47:14 +01:00
val drawMsg = DRAW_TEXT drawMsg
val msgs = DRAW drawMsg :: msgs
val mode = NORMAL_MODE ""
in
2025-08-31 06:28:05 +01:00
NormalModeWith.bufferAndCursorIdx
2025-08-20 12:51:31 +01:00
( app
, buffer
, cursorIdx
, mode
, startLine
, searchList
, msgs
, bufferModifyTime
, visualScrollColumn
2025-08-20 12:51:31 +01:00
)
end
fun withSearchList (app: app_type, searchList, searchTime) =
let
open Time
in
if searchTime >= #bufferModifyTime app then
let
val {buffer, searchString, cursorIdx, bufferModifyTime, ...} = app
val app = NormalModeWith.searchList
(app, searchList, buffer, searchString, bufferModifyTime)
in
buildTextAndClear
(app, buffer, cursorIdx, searchList, [], bufferModifyTime)
end
else
app
end
fun resizeText (app: app_type, newWidth, newHeight) =
let
val
{ buffer
, windowWidth
, windowHeight
, startLine
, cursorIdx
, searchList
, searchString
, bufferModifyTime
, ...
} = app
val newBuffer = LineGap.goToIdx (cursorIdx, buffer)
val visualScrollColumn =
TextScroll.getScrollColumn (buffer, cursorIdx, newWidth)
val newBuffer = LineGap.goToLine (startLine, newBuffer)
val lineIdx = TextBuilderUtils.getLineAbsIdxFromBuffer (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, newWidth
, newHeight
, searchList
, searchString
, visualScrollColumn
)
val drawMsg = Vector.concat drawMsg
2025-09-12 13:47:14 +01:00
val drawMsg = DRAW_TEXT drawMsg
val msgs = [DRAW drawMsg]
in
2025-08-31 06:28:05 +01:00
NormalModeWith.bufferAndSize
2025-08-20 12:51:31 +01:00
( app
, newBuffer
, newWidth
, newHeight
, searchList
, msgs
2025-08-20 12:51:31 +01:00
, bufferModifyTime
, visualScrollColumn
2025-08-20 12:51:31 +01:00
)
end
(* Difference between this and buildTextAndClear is that
* this is meant to be called after a chr movement,
* where the cursor may possibly jump off window by a wide marigin.
* Since the cursor may move away a lot, it is best to recenter.
* *)
fun buildTextAndClearAfterChr
2025-09-12 13:47:14 +01:00
(app: app_type, buffer, cursorIdx, searchList, initialMsg, bufferModifyTime) :
AppType.app_type =
let
val {windowWidth, windowHeight, startLine, searchString, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val visualScrollColumn =
TextScroll.getScrollColumn (buffer, cursorIdx, windowWidth)
(* move LineGap to first line displayed on screen *)
val buffer = LineGap.goToLine (startLine, buffer)
(* get new startLine which may move screen depending on cursor movements *)
val startLine = TextWindow.getStartLine
(buffer, startLine, cursorIdx, windowWidth, windowHeight)
(* move buffer to new startLine as required by TextBuilder.build
* and move searchList to idx where line starts as well *)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, searchString
, visualScrollColumn
)
val drawMsg = Vector.concat drawMsg
2025-09-12 13:47:14 +01:00
val drawMsg = DRAW_TEXT drawMsg
val msgs = DRAW drawMsg :: initialMsg
val mode = NORMAL_MODE ""
in
2025-08-31 06:28:05 +01:00
NormalModeWith.bufferAndCursorIdx
2025-08-20 12:51:31 +01:00
( app
, buffer
, cursorIdx
, mode
, startLine
, searchList
, msgs
2025-08-20 12:51:31 +01:00
, bufferModifyTime
, visualScrollColumn
2025-08-20 12:51:31 +01:00
)
end
fun centreToCursor (app: app_type) =
let
val
{ buffer
, windowWidth
, windowHeight
, startLine = origLine
, cursorIdx
, searchList
, searchString
, bufferModifyTime
, visualScrollColumn
, ...
} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val startLine = TextWindow.getStartLineWithCursorCentered
(buffer, cursorIdx, origLine, windowWidth, windowHeight div 2)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, searchString
, visualScrollColumn
)
val drawMsg = Vector.concat drawMsg
2025-09-12 13:47:14 +01:00
val drawMsg = DRAW_TEXT drawMsg
val drawMsg = [DRAW drawMsg]
in
let
val _ = raise Fail "centering to line is unimplemented\n"
in
NormalModeWith.bufferAndCursorIdx
( app
, buffer
, cursorIdx
, NORMAL_MODE ""
, startLine
, searchList
, drawMsg
, bufferModifyTime
, #visualScrollColumn app
)
end
end
end