reduce code duplication in app-update.sml by using higher order function
This commit is contained in:
@@ -19,13 +19,15 @@ struct
|
|||||||
(newApp, drawMsg)
|
(newApp, drawMsg)
|
||||||
end
|
end
|
||||||
|
|
||||||
fun moveRight (app: app_type) =
|
fun moveBackward (app: app_type, fMove) =
|
||||||
let
|
let
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
||||||
|
|
||||||
(* move LineGap to cursorIdx, which is necessary for finding newCursorIdx *)
|
(* move LineGap to cursorIdx, which is necessary for finding newCursorIdx *)
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||||
val cursorIdx = Cursor.viL (buffer, cursorIdx)
|
val cursorIdx = fMove (buffer, cursorIdx)
|
||||||
|
|
||||||
|
(* todo: get new startLine if cursor has moved out of screen *)
|
||||||
|
|
||||||
(* move LineGap to first line displayed on screen, and build new text *)
|
(* move LineGap to first line displayed on screen, and build new text *)
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
val buffer = LineGap.goToLine (startLine, buffer)
|
||||||
@@ -37,13 +39,17 @@ struct
|
|||||||
(newApp, drawMsg)
|
(newApp, drawMsg)
|
||||||
end
|
end
|
||||||
|
|
||||||
fun moveLeft (app: app_type) =
|
fun moveFowrards (app: app_type, fMove) =
|
||||||
let
|
let
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
||||||
|
|
||||||
|
(* move LineGap to cursorIdx, which is necessary for finding newCursorIdx *)
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||||
val cursorIdx = Cursor.viH (buffer, cursorIdx)
|
val cursorIdx = fMove (buffer, cursorIdx)
|
||||||
|
|
||||||
|
(* todo: get new startLine if cursor has moved out of screen *)
|
||||||
|
|
||||||
|
(* move LineGap to first line displayed on screen, and build new text *)
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
val buffer = LineGap.goToLine (startLine, buffer)
|
||||||
val drawMsg = TextBuilder.build
|
val drawMsg = TextBuilder.build
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
||||||
@@ -53,181 +59,20 @@ struct
|
|||||||
(newApp, drawMsg)
|
(newApp, drawMsg)
|
||||||
end
|
end
|
||||||
|
|
||||||
fun moveDown (app: app_type) =
|
|
||||||
let
|
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
|
||||||
val cursorIdx = Cursor.viJ (buffer, cursorIdx)
|
|
||||||
|
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
|
||||||
val drawMsg = TextBuilder.build
|
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
|
||||||
|
|
||||||
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
|
||||||
in
|
|
||||||
(newApp, drawMsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun moveUp (app: app_type) =
|
|
||||||
let
|
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
|
||||||
val cursorIdx = Cursor.viK (buffer, cursorIdx)
|
|
||||||
|
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
|
||||||
val drawMsg = TextBuilder.build
|
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
|
||||||
|
|
||||||
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
|
||||||
in
|
|
||||||
(newApp, drawMsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun moveToLineStart (app: app_type) =
|
|
||||||
let
|
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
|
||||||
val cursorIdx = Cursor.vi0 (buffer, cursorIdx)
|
|
||||||
|
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
|
||||||
val drawMsg = TextBuilder.build
|
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
|
||||||
|
|
||||||
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
|
||||||
in
|
|
||||||
(newApp, drawMsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun moveToLineEnd (app: app_type) =
|
|
||||||
let
|
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
|
||||||
val cursorIdx = Cursor.viDlr (buffer, cursorIdx)
|
|
||||||
|
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
|
||||||
val drawMsg = TextBuilder.build
|
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
|
||||||
|
|
||||||
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
|
||||||
in
|
|
||||||
(newApp, drawMsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun nextWord (app: app_type) =
|
|
||||||
let
|
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
|
||||||
val cursorIdx = Cursor.nextWord (buffer, cursorIdx)
|
|
||||||
|
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
|
||||||
val drawMsg = TextBuilder.build
|
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
|
||||||
|
|
||||||
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
|
||||||
in
|
|
||||||
(newApp, drawMsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun nextWORD (app: app_type) =
|
|
||||||
let
|
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
|
||||||
val cursorIdx = Cursor.nextWORD (buffer, cursorIdx)
|
|
||||||
|
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
|
||||||
val drawMsg = TextBuilder.build
|
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
|
||||||
|
|
||||||
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
|
||||||
in
|
|
||||||
(newApp, drawMsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun prevWord (app: app_type) =
|
|
||||||
let
|
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
|
||||||
val cursorIdx = Cursor.prevWord (buffer, cursorIdx)
|
|
||||||
|
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
|
||||||
val drawMsg = TextBuilder.build
|
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
|
||||||
|
|
||||||
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
|
||||||
in
|
|
||||||
(newApp, drawMsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun prevWORD (app: app_type) =
|
|
||||||
let
|
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
|
||||||
val cursorIdx = Cursor.prevWORD (buffer, cursorIdx)
|
|
||||||
|
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
|
||||||
val drawMsg = TextBuilder.build
|
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
|
||||||
|
|
||||||
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
|
||||||
in
|
|
||||||
(newApp, drawMsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun endOfWord (app: app_type) =
|
|
||||||
let
|
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
|
||||||
val cursorIdx = Cursor.endOfWord (buffer, cursorIdx)
|
|
||||||
|
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
|
||||||
val drawMsg = TextBuilder.build
|
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
|
||||||
|
|
||||||
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
|
||||||
in
|
|
||||||
(newApp, drawMsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun endOfWORD (app: app_type) =
|
|
||||||
let
|
|
||||||
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
|
||||||
val cursorIdx = Cursor.endOfWORD (buffer, cursorIdx)
|
|
||||||
|
|
||||||
val buffer = LineGap.goToLine (startLine, buffer)
|
|
||||||
val drawMsg = TextBuilder.build
|
|
||||||
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
|
||||||
|
|
||||||
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
|
||||||
in
|
|
||||||
(newApp, drawMsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
fun handleChr (app: app_type, chr) =
|
fun handleChr (app: app_type, chr) =
|
||||||
case chr of
|
case chr of
|
||||||
#"h" => moveLeft app
|
#"h" => moveBackward (app, Cursor.viH)
|
||||||
| #"j" => moveDown app
|
| #"j" => moveFowrards (app, Cursor.viJ)
|
||||||
| #"k" => moveUp app
|
| #"k" => moveBackward (app, Cursor.viK)
|
||||||
| #"l" => moveRight app
|
| #"l" => moveFowrards (app, Cursor.viL)
|
||||||
| #"0" => moveToLineStart app
|
| #"0" => moveBackward (app, Cursor.vi0)
|
||||||
| #"$" => moveToLineEnd app
|
| #"$" => moveFowrards (app, Cursor.viDlr)
|
||||||
| #"w" => nextWord app
|
| #"w" => moveFowrards (app, Cursor.nextWord)
|
||||||
| #"W" => nextWORD app
|
| #"W" => moveFowrards (app, Cursor.nextWORD)
|
||||||
| #"b" => prevWord app
|
| #"b" => moveBackward (app, Cursor.prevWord)
|
||||||
| #"B" => prevWORD app
|
| #"B" => moveBackward (app, Cursor.prevWORD)
|
||||||
| #"e" => endOfWord app
|
| #"e" => moveFowrards (app, Cursor.endOfWord)
|
||||||
| #"E" => endOfWORD app
|
| #"E" => moveFowrards (app, Cursor.endOfWORD)
|
||||||
| _ => (app, [])
|
| _ => (app, [])
|
||||||
|
|
||||||
fun update (app, msg) =
|
fun update (app, msg) =
|
||||||
|
|||||||
Reference in New Issue
Block a user