with cursor movements, instead of passing in functions as parameters (callbacks/higher order functions), functorise the cursor movement functions instead so we can take advantage of defunctorisation and avoid the runtime cost of closures/higher order functions/function pointers
This commit is contained in:
@@ -40,39 +40,6 @@ struct
|
||||
(app, newBuffer, newWidth, newHeight, searchList, drawMsg)
|
||||
end
|
||||
|
||||
fun buildTextAndClear (app: app_type, buffer, cursorIdx, searchList) =
|
||||
let
|
||||
val {windowWidth, windowHeight, startLine, searchString, ...} = app
|
||||
|
||||
(* move LineGap to first line displayed on screen
|
||||
* and move searchList to line's start idx as well *)
|
||||
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 lineIdx = TextBuilder.getLineAbsIdx (startLine, buffer)
|
||||
val searchList = SearchList.goToNum (lineIdx, searchList)
|
||||
|
||||
val drawMsg = TextBuilder.build
|
||||
( startLine
|
||||
, cursorIdx
|
||||
, buffer
|
||||
, windowWidth
|
||||
, windowHeight
|
||||
, searchList
|
||||
, searchString
|
||||
)
|
||||
|
||||
val mode = NORMAL_MODE ""
|
||||
in
|
||||
AppWith.bufferAndCursorIdx
|
||||
(app, buffer, cursorIdx, mode, startLine, searchList, drawMsg)
|
||||
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.
|
||||
@@ -258,32 +225,6 @@ struct
|
||||
(app, buffer, cursorIdx, mode, startLine, searchList, drawMsg)
|
||||
end
|
||||
|
||||
fun helpMove (app: app_type, buffer, cursorIdx, count, fMove) =
|
||||
if count = 0 then
|
||||
buildTextAndClear (app, buffer, cursorIdx, #searchList app)
|
||||
else
|
||||
(* move LineGap to cursorIdx, which is necessary for finding newCursorIdx *)
|
||||
let
|
||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||
val newCursorIdx = fMove (buffer, cursorIdx)
|
||||
val newCursorIdx = Cursor.clipIdx (buffer, newCursorIdx)
|
||||
val newCount =
|
||||
(* it's possible to loop a very high number like 5432131
|
||||
* which will take a long time because of the high number of loops
|
||||
* regardless of the data structure used.
|
||||
* If this happens, and the newCursorIdx is the same as the old one,
|
||||
* then skip to end of loop by going to base case. *)
|
||||
if cursorIdx = newCursorIdx then 0
|
||||
else count - 1
|
||||
in
|
||||
helpMove (app, buffer, newCursorIdx, newCount, fMove)
|
||||
end
|
||||
|
||||
fun move (app: app_type, count, fMove) =
|
||||
let val {cursorIdx, buffer, ...} = app
|
||||
in helpMove (app, buffer, cursorIdx, count, fMove)
|
||||
end
|
||||
|
||||
fun moveToMatchingPair (app: app_type) =
|
||||
let
|
||||
val
|
||||
@@ -376,7 +317,7 @@ struct
|
||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||
val cursorIdx = Cursor.firstNonSpaceChr (buffer, cursorIdx)
|
||||
in
|
||||
buildTextAndClear (app, buffer, cursorIdx, #searchList app)
|
||||
Finish.buildTextAndClear (app, buffer, cursorIdx, #searchList app)
|
||||
end
|
||||
|
||||
fun helpMoveToChr (app: app_type, buffer, cursorIdx, count, fMove, chr) =
|
||||
@@ -407,8 +348,8 @@ struct
|
||||
case newCmd of
|
||||
CHAR_EVENT chr =>
|
||||
(case chr of
|
||||
#"e" => move (app, count, Cursor.endOfPrevWord)
|
||||
| #"E" => move (app, count, Cursor.endOfPrevWORD)
|
||||
#"e" => MoveToEndOfPrevWord.move (app, count)
|
||||
| #"E" => MoveToEndOfPrevWORD.move (app, count)
|
||||
| #"g" => moveToStart app
|
||||
| _ => clearMode app)
|
||||
| KEY_ESC => clearMode app
|
||||
@@ -429,7 +370,7 @@ struct
|
||||
|
||||
fun helpRemoveChr (app: app_type, buffer, searchList, cursorIdx, count) =
|
||||
if count = 0 then
|
||||
buildTextAndClear (app, buffer, cursorIdx, searchList)
|
||||
Finish.buildTextAndClear (app, buffer, cursorIdx, searchList)
|
||||
else
|
||||
let
|
||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||
@@ -516,7 +457,7 @@ struct
|
||||
val buffer = LineGap.goToIdx (low, buffer)
|
||||
val cursorIdx = Cursor.clipIdx (buffer, low)
|
||||
in
|
||||
buildTextAndClear (app, buffer, cursorIdx, searchList)
|
||||
Finish.buildTextAndClear (app, buffer, cursorIdx, searchList)
|
||||
end
|
||||
else
|
||||
let
|
||||
@@ -574,7 +515,7 @@ struct
|
||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||
val cursorIdx = Cursor.clipIdx (buffer, cursorIdx)
|
||||
in
|
||||
buildTextAndClear (app, buffer, cursorIdx, searchList)
|
||||
Finish.buildTextAndClear (app, buffer, cursorIdx, searchList)
|
||||
end
|
||||
else
|
||||
let
|
||||
@@ -608,7 +549,7 @@ struct
|
||||
|
||||
val buffer = LineGap.goToIdx (low, buffer)
|
||||
in
|
||||
buildTextAndClear (app, buffer, low, searchList)
|
||||
Finish.buildTextAndClear (app, buffer, low, searchList)
|
||||
end
|
||||
else
|
||||
let
|
||||
@@ -659,7 +600,7 @@ struct
|
||||
val (buffer, searchList) = deleteSearchList
|
||||
(low, length, searchString, searchList, buffer)
|
||||
in
|
||||
buildTextAndClear (app, buffer, low, searchList)
|
||||
Finish.buildTextAndClear (app, buffer, low, searchList)
|
||||
end
|
||||
|
||||
fun helpDeleteToChr
|
||||
@@ -757,16 +698,16 @@ struct
|
||||
|
||||
fun handleChr (app: app_type, count, chr, str) =
|
||||
case chr of
|
||||
#"h" => move (app, count, Cursor.viH)
|
||||
| #"j" => move (app, count, Cursor.viJ)
|
||||
| #"k" => move (app, count, Cursor.viK)
|
||||
| #"l" => move (app, count, Cursor.viL)
|
||||
| #"w" => move (app, count, Cursor.nextWord)
|
||||
| #"W" => move (app, count, Cursor.nextWORD)
|
||||
| #"b" => move (app, count, Cursor.prevWord)
|
||||
| #"B" => move (app, count, Cursor.prevWORD)
|
||||
| #"e" => move (app, count, Cursor.endOfWord)
|
||||
| #"E" => move (app, count, Cursor.endOfWORD)
|
||||
#"h" => MoveViH.move (app, count)
|
||||
| #"j" => MoveViJ.move (app, count)
|
||||
| #"k" => MoveViK.move (app, count)
|
||||
| #"l" => MoveViL.move (app, count)
|
||||
| #"w" => MoveToNextWord.move (app, count)
|
||||
| #"W" => MoveToNextWORD.move (app, count)
|
||||
| #"b" => MoveToPrevWord.move (app, count)
|
||||
| #"B" => MoveToPrevWORD.move (app, count)
|
||||
| #"e" => MoveToEndOfWord.move (app, count)
|
||||
| #"E" => MoveToEndOfWORD.move (app, count)
|
||||
| #"z" => centreToCursor app
|
||||
(* can only move to start or end of line once
|
||||
* so hardcode count as 1 *)
|
||||
@@ -790,11 +731,11 @@ struct
|
||||
AppWith.mode (app, mode, [])
|
||||
end
|
||||
else
|
||||
move (app, 1, Cursor.vi0)
|
||||
MoveToStartOfLine.move (app, 1)
|
||||
end
|
||||
else
|
||||
move (app, 1, Cursor.vi0)
|
||||
| #"$" => move (app, 1, Cursor.viDlr)
|
||||
MoveToStartOfLine.move (app, 1)
|
||||
| #"$" => MoveToEndOfLine.move (app, 1)
|
||||
| #"^" => firstNonSpaceChr app
|
||||
| #"G" =>
|
||||
(* if str has a size larger than 0,
|
||||
|
||||
37
fcore/finish.sml
Normal file
37
fcore/finish.sml
Normal file
@@ -0,0 +1,37 @@
|
||||
structure Finish =
|
||||
struct
|
||||
open AppType
|
||||
|
||||
fun buildTextAndClear (app: app_type, buffer, cursorIdx, searchList) =
|
||||
let
|
||||
val {windowWidth, windowHeight, startLine, searchString, ...} = app
|
||||
|
||||
(* move LineGap to first line displayed on screen
|
||||
* and move searchList to line's start idx as well *)
|
||||
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 lineIdx = TextBuilder.getLineAbsIdx (startLine, buffer)
|
||||
val searchList = SearchList.goToNum (lineIdx, searchList)
|
||||
|
||||
val drawMsg = TextBuilder.build
|
||||
( startLine
|
||||
, cursorIdx
|
||||
, buffer
|
||||
, windowWidth
|
||||
, windowHeight
|
||||
, searchList
|
||||
, searchString
|
||||
)
|
||||
|
||||
val mode = NORMAL_MODE ""
|
||||
in
|
||||
AppWith.bufferAndCursorIdx
|
||||
(app, buffer, cursorIdx, mode, startLine, searchList, drawMsg)
|
||||
end
|
||||
end
|
||||
60
fcore/move.sml
Normal file
60
fcore/move.sml
Normal file
@@ -0,0 +1,60 @@
|
||||
signature MOVE =
|
||||
sig
|
||||
val fMove: LineGap.t * int -> int
|
||||
end
|
||||
|
||||
signature MAKE_MOVE =
|
||||
sig
|
||||
val move: AppType.app_type * int -> AppType.app_type
|
||||
end
|
||||
|
||||
functor MakeMove(Fn: MOVE): MAKE_MOVE =
|
||||
struct
|
||||
fun helpMove (app: AppType.app_type, buffer, cursorIdx, count) =
|
||||
if count = 0 then
|
||||
Finish.buildTextAndClear (app, buffer, cursorIdx, #searchList app)
|
||||
else
|
||||
(* move LineGap to cursorIdx, which is necessary for finding newCursorIdx *)
|
||||
let
|
||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||
val newCursorIdx = Fn.fMove (buffer, cursorIdx)
|
||||
val newCursorIdx = Cursor.clipIdx (buffer, newCursorIdx)
|
||||
val newCount =
|
||||
(* it's possible to loop a very high number like 5432131
|
||||
* which will take a long time because of the high number of loops
|
||||
* regardless of the data structure used.
|
||||
* If this happens, and the newCursorIdx is the same as the old one,
|
||||
* then skip to end of loop by going to base case. *)
|
||||
if cursorIdx = newCursorIdx then 0
|
||||
else count - 1
|
||||
in
|
||||
helpMove (app, buffer, newCursorIdx, newCount)
|
||||
end
|
||||
|
||||
fun move (app: AppType.app_type, count) =
|
||||
let val {cursorIdx, buffer, ...} = app
|
||||
in helpMove (app, buffer, cursorIdx, count)
|
||||
end
|
||||
end
|
||||
|
||||
structure MoveViH = MakeMove (struct val fMove = Cursor.viH end)
|
||||
structure MoveViJ = MakeMove (struct val fMove = Cursor.viJ end)
|
||||
structure MoveViK = MakeMove (struct val fMove = Cursor.viK end)
|
||||
structure MoveViL = MakeMove (struct val fMove = Cursor.viL end)
|
||||
|
||||
structure MoveToNextWord = MakeMove (struct val fMove = Cursor.nextWord end)
|
||||
structure MoveToNextWORD = MakeMove (struct val fMove = Cursor.nextWORD end)
|
||||
|
||||
structure MoveToEndOfWord = MakeMove (struct val fMove = Cursor.endOfWord end)
|
||||
structure MoveToEndOfWORD = MakeMove (struct val fMove = Cursor.endOfWORD end)
|
||||
|
||||
structure MoveToPrevWord = MakeMove (struct val fMove = Cursor.prevWord end)
|
||||
structure MoveToPrevWORD = MakeMove (struct val fMove = Cursor.prevWORD end)
|
||||
|
||||
structure MoveToEndOfPrevWord =
|
||||
MakeMove (struct val fMove = Cursor.endOfPrevWord end)
|
||||
structure MoveToEndOfPrevWORD =
|
||||
MakeMove (struct val fMove = Cursor.endOfPrevWORD end)
|
||||
|
||||
structure MoveToStartOfLine = MakeMove (struct val fMove = Cursor.vi0 end)
|
||||
structure MoveToEndOfLine = MakeMove (struct val fMove = Cursor.viDlr end)
|
||||
@@ -1,10 +1,10 @@
|
||||
$(SML_LIB)/basis/basis.mlb
|
||||
|
||||
(* FUNCTIONAL CORE *)
|
||||
|
||||
(* LIBRARIEES *)
|
||||
lib/brolib-sml/src/line_gap.sml
|
||||
lib/cozette-sml/fonts/cozette-ascii.mlb
|
||||
|
||||
(* FUNCTIONAL CORE *)
|
||||
message-types/input-msg.sml
|
||||
message-types/draw-msg.sml
|
||||
message-types/mailbox-type.sml
|
||||
@@ -26,8 +26,9 @@ end
|
||||
fcore/cursor.sml
|
||||
fcore/text-window.sml
|
||||
|
||||
fcore/finish.sml
|
||||
fcore/move.sml
|
||||
fcore/app-update.sml
|
||||
|
||||
(* TEST FILES *)
|
||||
|
||||
test/Railroad/src/railroad.mlb
|
||||
|
||||
Reference in New Issue
Block a user