From 4574a23dc8f582ddb5230bec4c2eca7bde424056 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Fri, 8 Nov 2024 18:27:55 +0000 Subject: [PATCH] extract reusable function 'buildTextAndClear' (but need to be careful since some functions have specialised build functionality and shouldn't use this function) --- fcore/app-update.sml | 74 +++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/fcore/app-update.sml b/fcore/app-update.sml index 7c7f085..4c6344f 100644 --- a/fcore/app-update.sml +++ b/fcore/app-update.sml @@ -27,6 +27,31 @@ struct (newApp, drawMsg) end + fun buildTextAndClear (app: app_type, buffer, cursorIdx) = + let + val {windowWidth, windowHeight, startLine, ...} = app + + (* 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 = + TextBuilder.build + (startLine, cursorIdx, buffer, windowWidth, windowHeight) + + val mode = NORMAL_MODE "" + val newApp = AppWith.bufferAndCursorIdx + (app, buffer, cursorIdx, mode, startLine) + in + (newApp, drawMsg) + end + fun centreToCursor (app: app_type) = let val {buffer, windowWidth, windowHeight, startLine = origLine, cursorIdx, ...} = app @@ -126,29 +151,7 @@ struct fun helpMove (app: app_type, buffer, cursorIdx, count, fMove) = if count = 0 then - let - val {windowWidth, windowHeight, startLine, ...} = app - - (* 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 = - TextBuilder.build - (startLine, cursorIdx, buffer, windowWidth, windowHeight) - - val mode = NORMAL_MODE "" - val newApp = AppWith.bufferAndCursorIdx - (app, buffer, cursorIdx, mode, startLine) - in - (newApp, drawMsg) - end + buildTextAndClear (app, buffer, cursorIdx) else (* move LineGap to cursorIdx, which is necessary for finding newCursorIdx *) let @@ -219,35 +222,14 @@ struct (* move cursorIdx to first character on line *) val buffer = LineGap.goToIdx (cursorIdx, buffer) val cursorIdx = Cursor.firstNonSpaceChr (buffer, cursorIdx) - - (* move LineGap to first line displayed on screen, and build new text *) - val buffer = LineGap.goToLine (startLine, buffer) - val drawMsg = TextBuilder.build - (startLine, cursorIdx, buffer, windowWidth, windowHeight) - - val mode = NORMAL_MODE "" - val newApp = AppWith.bufferAndCursorIdx - (app, buffer, cursorIdx, mode, startLine) in - (newApp, drawMsg) + buildTextAndClear (app, buffer, cursorIdx) end (* equivalent of vi's 'x' command *) fun helpRemoveChr (app: app_type, buffer, cursorIdx, count) = if count = 0 then - let - val {startLine, windowWidth, windowHeight, ...} = app - - val buffer = LineGap.goToLine (startLine, buffer) - val newApp = AppWith.bufferAndCursorIdx - (app, buffer, cursorIdx, NORMAL_MODE "", startLine) - - val drawMsg = - TextBuilder.build - (startLine, cursorIdx, buffer, windowWidth, windowHeight) - in - (newApp, drawMsg) - end + buildTextAndClear (app, buffer, cursorIdx) else let val buffer = LineGap.goToIdx (cursorIdx, buffer)