functorise some additional functions to reduce boilerplate
This commit is contained in:
140
fcore/cursor.sml
140
fcore/cursor.sml
@@ -153,6 +153,9 @@ struct
|
||||
cursorIdx
|
||||
end
|
||||
|
||||
structure ViH =
|
||||
MakeIfCharFolderPrev
|
||||
(struct
|
||||
fun helpViH (strIdx, hd, cursorIdx, leftStrings) =
|
||||
if strIdx > 0 then
|
||||
(* bounds check: can access prev char in hd *)
|
||||
@@ -213,31 +216,11 @@ struct
|
||||
cursorIdx - 1
|
||||
| [] => 0)
|
||||
|
||||
(* Prerequisite: lineGap is moved to requested idx first. *)
|
||||
fun viH (lineGap: LineGap.t, cursorIdx) =
|
||||
let
|
||||
val {rightStrings, leftStrings, idx = bufferIdx, ...} = lineGap
|
||||
in
|
||||
case rightStrings of
|
||||
hd :: tl =>
|
||||
let
|
||||
(* convert absolute cursorIdx to idx relative to hd string *)
|
||||
val strIdx = cursorIdx - bufferIdx
|
||||
in
|
||||
if strIdx < String.size hd then
|
||||
(* strIdx in hd *)
|
||||
fun fStart (strIdx, hd, _, cursorIdx, leftStrings, _) =
|
||||
helpViH (strIdx, hd, cursorIdx, leftStrings)
|
||||
else
|
||||
(* strIdx in tl *)
|
||||
(case tl of
|
||||
tlhd :: tltl =>
|
||||
let val strIdx = strIdx - String.size hd
|
||||
in helpViH (strIdx, tlhd, cursorIdx, hd :: leftStrings)
|
||||
end
|
||||
| [] => cursorIdx)
|
||||
end
|
||||
| [] => cursorIdx
|
||||
end
|
||||
end)
|
||||
|
||||
val viH = ViH.foldPrev
|
||||
|
||||
fun helpGetCursorColumn (distanceFromLine, strList, lineList) =
|
||||
case (strList, lineList) of
|
||||
@@ -452,6 +435,9 @@ struct
|
||||
| (_, _) => (* nowhere to go rightward, so return cursorIdx *) cursorIdx
|
||||
end
|
||||
|
||||
structure ViK =
|
||||
MakeIfCharFolderPrev
|
||||
(struct
|
||||
fun helpViK
|
||||
( strPos
|
||||
, str
|
||||
@@ -477,7 +463,8 @@ struct
|
||||
, lhd
|
||||
, ltl
|
||||
)
|
||||
| (_, _) => (* empty, so return start of previous string *) absIdx + 1
|
||||
| (_, _) => (* empty, so return start of previous string *)
|
||||
absIdx + 1
|
||||
else
|
||||
case String.sub (str, strPos) of
|
||||
#"\n" =>
|
||||
@@ -530,7 +517,7 @@ struct
|
||||
, lineTl
|
||||
)
|
||||
|
||||
fun startViK (strIdx, shd, cursorIdx, leftStrings, lhd, leftLines) =
|
||||
fun fStart (strIdx, shd, lhd, cursorIdx, leftStrings, leftLines) =
|
||||
if String.sub (shd, strIdx) = #"\n" then
|
||||
(* ? -> ? -> \n *)
|
||||
if strIdx > 0 then
|
||||
@@ -548,7 +535,13 @@ struct
|
||||
* so go to beginning of line,
|
||||
* starting from graphical-chr *)
|
||||
startVi0
|
||||
(strIdx - 2, shd, lhd, cursorIdx - 2, leftStrings, leftLines)
|
||||
( strIdx - 2
|
||||
, shd
|
||||
, lhd
|
||||
, cursorIdx - 2
|
||||
, leftStrings
|
||||
, leftLines
|
||||
)
|
||||
else
|
||||
(* strIdx - 2 is in leftStrings *)
|
||||
case (leftStrings, leftLines) of
|
||||
@@ -576,7 +569,8 @@ struct
|
||||
(* ? -> graphical-chr -> \n
|
||||
* Don't expect this case to happen
|
||||
* but if it does, go to start of line. *)
|
||||
startVi0 (strIdx - 1, shd, lhd, cursorIdx - 1, leftStrings, leftLines)
|
||||
startVi0
|
||||
(strIdx - 1, shd, lhd, cursorIdx - 1, leftStrings, leftLines)
|
||||
else
|
||||
(* strIdx - 1 is in leftStrings *)
|
||||
case (leftStrings, leftLines) of
|
||||
@@ -647,35 +641,9 @@ struct
|
||||
)
|
||||
end
|
||||
|
||||
fun viK (lineGap: LineGap.t, cursorIdx) =
|
||||
let
|
||||
val
|
||||
{rightStrings, idx = bufferIdx, rightLines, leftStrings, leftLines, ...} =
|
||||
lineGap
|
||||
in
|
||||
case (rightStrings, rightLines) of
|
||||
(shd :: stl, lhd :: ltl) =>
|
||||
let
|
||||
(* convert absolute cursorIdx to idx relative to hd string *)
|
||||
val strIdx = cursorIdx - bufferIdx
|
||||
in
|
||||
if strIdx < String.size shd then
|
||||
startViK (strIdx, shd, cursorIdx, leftStrings, lhd, leftLines)
|
||||
else
|
||||
case (stl, ltl) of
|
||||
(stlhd :: stltl, ltlhd :: ltltl) =>
|
||||
let
|
||||
val strIdx = strIdx - String.size shd
|
||||
val leftStrings = shd :: leftStrings
|
||||
val leftLines = lhd :: leftLines
|
||||
in
|
||||
startViK
|
||||
(strIdx, stlhd, cursorIdx, leftStrings, ltlhd, leftLines)
|
||||
end
|
||||
| (_, _) => cursorIdx
|
||||
end
|
||||
| (_, _) => (* nowhere to go rightward, so return cursorIdx *) cursorIdx
|
||||
end
|
||||
end)
|
||||
|
||||
val viK = ViK.foldPrev
|
||||
|
||||
(* equivalent of vi's 'w' command *)
|
||||
val nextWord = ViWordDfa.startOfNextWord
|
||||
@@ -703,60 +671,36 @@ struct
|
||||
val endOfWORD = ViWORDDfa.endOfCurrentWORD
|
||||
val endOfWORDForDelete = ViWORDDfa.endOfCurrentWORDForDelete
|
||||
|
||||
fun helpFirstNonSpaceChr (strPos, str, absIdx, stl, ltl) =
|
||||
(* Prerequisite:
|
||||
* LineGap has been moved to start of line (provided with vi0). *)
|
||||
structure FirstNonSpaceChr =
|
||||
MakeIfCharFolderPrev
|
||||
(struct
|
||||
fun helpFirstNonSpaceChr (strPos, str, absIdx, stl) =
|
||||
if strPos = String.size str then
|
||||
case (stl, ltl) of
|
||||
(shd :: stl, lhd :: ltl) =>
|
||||
helpFirstNonSpaceChr (0, shd, absIdx, stl, ltl)
|
||||
| (_, _) => absIdx - 1
|
||||
case stl of
|
||||
shd :: stl => helpFirstNonSpaceChr (0, shd, absIdx, stl)
|
||||
| [] => absIdx - 1
|
||||
else
|
||||
let
|
||||
val chr = String.sub (str, strPos)
|
||||
in
|
||||
if chr = #" " then
|
||||
helpFirstNonSpaceChr (strPos + 1, str, absIdx + 1, stl, ltl)
|
||||
helpFirstNonSpaceChr (strPos + 1, str, absIdx + 1, stl)
|
||||
else
|
||||
absIdx
|
||||
end
|
||||
|
||||
fun startFirstNonSpaceChr (shd, strIdx, absIdx, stl, ltl) =
|
||||
fun fStart (strIdx, shd, _, absIdx, stl, _) =
|
||||
if strIdx < String.size shd then
|
||||
helpFirstNonSpaceChr (strIdx, shd, absIdx, stl, ltl)
|
||||
helpFirstNonSpaceChr (strIdx, shd, absIdx, stl)
|
||||
else
|
||||
case (stl, ltl) of
|
||||
(stlhd :: stltl, ltlhd :: ltltl) =>
|
||||
helpFirstNonSpaceChr (0, stlhd, absIdx, stltl, ltltl)
|
||||
| (_, _) => (* tl is empty; just return absIdx *) absIdx
|
||||
case stl of
|
||||
stlhd :: stltl => helpFirstNonSpaceChr (0, stlhd, absIdx, stltl)
|
||||
| [] => (* tl is empty; just return absIdx *) absIdx
|
||||
end)
|
||||
|
||||
(* Prerequisite:
|
||||
* LineGap has been moved to start of line (provided with vi0). *)
|
||||
fun firstNonSpaceChr (lineGap: LineGap.t, cursorIdx) =
|
||||
let
|
||||
val {rightStrings, rightLines, idx = bufferIdx, ...} = lineGap
|
||||
in
|
||||
case (rightStrings, rightLines) of
|
||||
(shd :: stl, lhd :: ltl) =>
|
||||
let
|
||||
(* convert absolute cursorIdx to idx relative to hd string *)
|
||||
val strIdx = cursorIdx - bufferIdx
|
||||
in
|
||||
if strIdx < String.size shd then
|
||||
(* strIdx is in this string *)
|
||||
startFirstNonSpaceChr (shd, strIdx, cursorIdx, stl, ltl)
|
||||
else
|
||||
(* strIdx is in tl *)
|
||||
(case (stl, ltl) of
|
||||
(stlhd :: stltl, ltlhd :: ltltl) =>
|
||||
let
|
||||
val strIdx = strIdx - String.size shd
|
||||
in
|
||||
startFirstNonSpaceChr
|
||||
(stlhd, strIdx, cursorIdx, stltl, ltltl)
|
||||
end
|
||||
| (_, _) => cursorIdx)
|
||||
end
|
||||
| (_, _) => cursorIdx
|
||||
end
|
||||
val firstNonSpaceChr = FirstNonSpaceChr.foldPrev
|
||||
|
||||
fun helpToNextChr (strPos, str, absIdx, stl, ltl, origIdx, findChr) =
|
||||
if strPos = String.size str then
|
||||
|
||||
Reference in New Issue
Block a user